package es.sidelab.webchat;

import static org.junit.Assert.assertTrue;

import java.util.ArrayList;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import org.junit.Test;
import es.codeurjc.webchat.Chat;
import es.codeurjc.webchat.ChatManager;
import es.codeurjc.webchat.User;

public class Mejora5_5 {
	//envio de un mensaje
	ExecutorService executor1 = Executors.newSingleThreadExecutor();
	
	ArrayList<String> arrayMensajes = new ArrayList<>();
	
	String message = "Hola,¿que tal?";
	Chat chat = null;
	ChatManager chatManager = new ChatManager(5);
	
	@Test
	public void newChat() throws Throwable {
		Callable<String> tarea1 = () -> {
			TestUser user = new TestUser("Jesus"){
				@Override
				public void newMessage(Chat chat, User user, String message) {
					arrayMensajes.add(message);
					System.out.println(message);
				}
			};
			chat = chatManager.newChat("Chat",5, TimeUnit.MILLISECONDS);
			chatManager.newUser(user);
			chat.addUser(user);
			chat.sendMessage(user, message);
			
			assertTrue(message == arrayMensajes.get(0));
				
				return null;
			};
			executor1.submit(tarea1);
			
		}
		
}


@@ -132,16 +132,99 @@
 llable;%0A
+import java.util.concurrent.CountDownLatch;%0Aimport java.util.concurrent.Exchanger;%0A
 import j
@@ -586,60 +586,283 @@
 );%0A%09
-%0A%09ArrayList%3CString%3E arrayMensajes = new ArrayList%3C%3E(
+ExecutorService executor2 = Executors.newSingleThreadExecutor();%0A%09%0A%09Exchanger%3CString%3E mensaje = new Exchanger%3CString%3E();%0A%09%0A%09ArrayList%3CString%3E arrayMensajes = new ArrayList%3C%3E();%0A%0A%09CountDownLatch barrera1 = new CountDownLatch(1);%0A%09CountDownLatch barrera2 = new CountDownLatch(1
 );%0A%09
@@ -1237,24 +1237,30 @@
 n(message);%0A
+%09%09%09%09%09%0A
 %09%09%09%09%7D%0A%09%09%09%7D;%0A
@@ -1368,32 +1368,78 @@
 .addUser(user);%0A
+%09%09%09barrera1.countDown();%0A%09%09%09barrera2.await();%0A
 %09%09%09chat.sendMess
@@ -1461,16 +1461,19 @@
 ge);%0A%09%09%09
+%09%09%09
 %0A%09%09%09asse
@@ -1479,23 +1479,36 @@
 ertTrue(
-message
+arrayMensajes.get(0)
  == arra
@@ -1521,18 +1521,51 @@
 jes.get(
-0)
+1) && arrayMensajes.get(0)==message
 );%0A%09%09%09%09%0A
@@ -1620,16 +1620,441 @@
 1);%0A%09%09%09%0A
+%09%09%09Callable%3CString%3E tarea2 = () -%3E %7B%0A%09%09%09%09barrera1.await();%0A%09%09%09%09TestUser user = new TestUser(%22C%C3%A9sar%22)%7B%0A%09%09%09%09%09@Override%0A%09%09%09%09%09public void newMessage(Chat chat, User user, String message) %7B%0A%09%09%09%09%09%09arrayMensajes.add(message);%0A%09%09%09%09%09%09System.out.println(message);%0A%09%09%09%09%09%09%0A%09%09%09%09%09%7D%0A%09%09%09%09%7D;%0A%09%09%09%09chatManager.newUser(user);%0A%09%09%09%09chat.addUser(user);%0A%09%09%09%09barrera2.countDown();%0A%09%09%09%09%0A%09%09%09%09%0A%09%09%09%09%09return null;%0A%09%09%09%09%7D;%0A%09%09%09%09executor2.submit(tarea2);%0A%09%09%09%0A
 %09%09%7D%0A%09%09%0A%7D

mejora 5