From cb173ac60b8307247efd83aaed5a0bb44a107766 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Magdalena=20Grodzi=C5=84ska?= Date: Mon, 23 Dec 2019 19:06:41 +0100 Subject: Add sending messages from modules --- src/test/java/pl/edu/mimuw/cloudatlas/agent/EventBusTest.java | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 src/test/java/pl/edu/mimuw/cloudatlas/agent/EventBusTest.java (limited to 'src/test/java/pl/edu') diff --git a/src/test/java/pl/edu/mimuw/cloudatlas/agent/EventBusTest.java b/src/test/java/pl/edu/mimuw/cloudatlas/agent/EventBusTest.java new file mode 100644 index 0000000..7320e69 --- /dev/null +++ b/src/test/java/pl/edu/mimuw/cloudatlas/agent/EventBusTest.java @@ -0,0 +1,4 @@ +package pl.edu.mimuw.cloudatlas.agent; + +public class EventBusTest { +} -- cgit v1.2.3 From 4456b71ab46b375eb21861051423222e6e5bb01e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Magdalena=20Grodzi=C5=84ska?= Date: Mon, 23 Dec 2019 19:08:44 +0100 Subject: Adjust executor tests to module changes --- src/test/java/pl/edu/mimuw/cloudatlas/agent/ExecutorTest.java | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) (limited to 'src/test/java/pl/edu') diff --git a/src/test/java/pl/edu/mimuw/cloudatlas/agent/ExecutorTest.java b/src/test/java/pl/edu/mimuw/cloudatlas/agent/ExecutorTest.java index 445a5b4..2a6ef49 100644 --- a/src/test/java/pl/edu/mimuw/cloudatlas/agent/ExecutorTest.java +++ b/src/test/java/pl/edu/mimuw/cloudatlas/agent/ExecutorTest.java @@ -9,6 +9,11 @@ import pl.edu.mimuw.cloudatlas.agent.message.AgentMessage.AgentModule; public class ExecutorTest { public class MessageCounterModule extends Module { public int counter = 0; + + MessageCounterModule(AgentModule moduleType) { + super(moduleType); + } + public void handle(AgentMessage m) { counter++; } @@ -16,7 +21,7 @@ public class ExecutorTest { @Test public void testDoesntExecuteWhenNoMessages() throws Exception { - MessageCounterModule module = new MessageCounterModule(); + MessageCounterModule module = new MessageCounterModule(AgentModule.UDP); Executor executor = new Executor(module); Thread thread = new Thread(executor); thread.start(); @@ -27,7 +32,7 @@ public class ExecutorTest { @Test public void testExecutesHandlerOnce() throws Exception { - MessageCounterModule module = new MessageCounterModule(); + MessageCounterModule module = new MessageCounterModule(AgentModule.UDP); Executor executor = new Executor(module); executor.addMessage(new AgentMessage("", AgentModule.UDP, 0) {}); Thread thread = new Thread(executor); @@ -39,7 +44,7 @@ public class ExecutorTest { @Test public void testExecutesHandlerMultipleTimes() throws Exception { - MessageCounterModule module = new MessageCounterModule(); + MessageCounterModule module = new MessageCounterModule(AgentModule.UDP); Executor executor = new Executor(module); executor.addMessage(new AgentMessage("", AgentModule.UDP, 0) {}); executor.addMessage(new AgentMessage("", AgentModule.UDP, 0) {}); -- cgit v1.2.3 From 87577a88139354a9640a2afc1058dad4c2ef9d27 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Magdalena=20Grodzi=C5=84ska?= Date: Mon, 23 Dec 2019 19:09:03 +0100 Subject: Add event bus tests --- .../edu/mimuw/cloudatlas/agent/EventBusTest.java | 74 ++++++++++++++++++++++ 1 file changed, 74 insertions(+) (limited to 'src/test/java/pl/edu') diff --git a/src/test/java/pl/edu/mimuw/cloudatlas/agent/EventBusTest.java b/src/test/java/pl/edu/mimuw/cloudatlas/agent/EventBusTest.java index 7320e69..d9a30ed 100644 --- a/src/test/java/pl/edu/mimuw/cloudatlas/agent/EventBusTest.java +++ b/src/test/java/pl/edu/mimuw/cloudatlas/agent/EventBusTest.java @@ -1,4 +1,78 @@ package pl.edu.mimuw.cloudatlas.agent; +import org.junit.Test; +import pl.edu.mimuw.cloudatlas.agent.message.AgentMessage; + +import java.util.ArrayList; +import java.util.HashMap; + public class EventBusTest { + + public static HashMap initializeTwoModules() { + HashMap modules = new HashMap(); + modules.put(AgentMessage.AgentModule.RMI, new Module(AgentMessage.AgentModule.RMI) { + @Override + public void handle(AgentMessage event) throws InterruptedException { + System.out.println("Module 1 handle called"); + sendMessage(new AgentMessage("1", AgentMessage.AgentModule.UDP)); + } + }); + + modules.put(AgentMessage.AgentModule.UDP, new Module(AgentMessage.AgentModule.UDP) { + @Override + public void handle(AgentMessage event) { + System.out.println("Module 2 handle called"); + } + }); + + return modules; + } + + public static HashMap initializeModule() { + HashMap modules = new HashMap(); + + modules.put(AgentMessage.AgentModule.RMI, new Module(AgentMessage.AgentModule.RMI) { + @Override + public void handle(AgentMessage event) { + System.out.println("Module 1 handle called"); + } + }); + + return modules; + } + + @Test + public void messageModule() throws InterruptedException { + HashMap modules = initializeModule(); + HashMap executors = Agent.initializeExecutors(modules); + ArrayList executorThreads = Agent.initializeExecutorThreads(executors); + EventBus eventBus = new EventBus(executors); + Thread eventBusThread = new Thread(eventBus); + + eventBusThread.start(); + eventBus.addMessage(new AgentMessage("0", AgentMessage.AgentModule.RMI)); + Thread.sleep(1000); + eventBusThread.interrupt(); + Agent.closeExecutors(executorThreads); + } + + @Test + public void messagingBetweenModules() throws InterruptedException { + HashMap modules = initializeTwoModules(); + HashMap executors = Agent.initializeExecutors(modules); + ArrayList executorThreads = Agent.initializeExecutorThreads(executors); + EventBus eventBus = new EventBus(executors); + Thread eventBusThread = new Thread(eventBus); + eventBusThread.start(); + + eventBus.addMessage(new AgentMessage( + "0", + AgentMessage.AgentModule.RMI, + System.currentTimeMillis() / 1000L)); + + Thread.sleep(10000); + + eventBusThread.interrupt(); + Agent.closeExecutors(executorThreads); + } } -- cgit v1.2.3 From 7a104ca7eeb0d312bc76d2393495387079032aed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Magdalena=20Grodzi=C5=84ska?= Date: Mon, 23 Dec 2019 19:16:54 +0100 Subject: Improve event bus tests --- .../edu/mimuw/cloudatlas/agent/EventBusTest.java | 27 +++++++++++++++++----- 1 file changed, 21 insertions(+), 6 deletions(-) (limited to 'src/test/java/pl/edu') diff --git a/src/test/java/pl/edu/mimuw/cloudatlas/agent/EventBusTest.java b/src/test/java/pl/edu/mimuw/cloudatlas/agent/EventBusTest.java index d9a30ed..fd7c244 100644 --- a/src/test/java/pl/edu/mimuw/cloudatlas/agent/EventBusTest.java +++ b/src/test/java/pl/edu/mimuw/cloudatlas/agent/EventBusTest.java @@ -6,35 +6,47 @@ import pl.edu.mimuw.cloudatlas.agent.message.AgentMessage; import java.util.ArrayList; import java.util.HashMap; +import static org.junit.Assert.assertEquals; + public class EventBusTest { + public abstract class MessageCounterModule extends Module { + public int counter = 0; + + MessageCounterModule(AgentMessage.AgentModule moduleType) { + super(moduleType); + } + } - public static HashMap initializeTwoModules() { + public HashMap initializeTwoModules() { HashMap modules = new HashMap(); - modules.put(AgentMessage.AgentModule.RMI, new Module(AgentMessage.AgentModule.RMI) { + modules.put(AgentMessage.AgentModule.RMI, new MessageCounterModule(AgentMessage.AgentModule.RMI) { @Override public void handle(AgentMessage event) throws InterruptedException { System.out.println("Module 1 handle called"); sendMessage(new AgentMessage("1", AgentMessage.AgentModule.UDP)); + counter ++; } }); - modules.put(AgentMessage.AgentModule.UDP, new Module(AgentMessage.AgentModule.UDP) { + modules.put(AgentMessage.AgentModule.UDP, new MessageCounterModule(AgentMessage.AgentModule.UDP) { @Override public void handle(AgentMessage event) { System.out.println("Module 2 handle called"); + counter++; } }); return modules; } - public static HashMap initializeModule() { + public HashMap initializeModule() { HashMap modules = new HashMap(); - modules.put(AgentMessage.AgentModule.RMI, new Module(AgentMessage.AgentModule.RMI) { + modules.put(AgentMessage.AgentModule.RMI, new MessageCounterModule(AgentMessage.AgentModule.RMI) { @Override public void handle(AgentMessage event) { System.out.println("Module 1 handle called"); + counter++; } }); @@ -54,6 +66,7 @@ public class EventBusTest { Thread.sleep(1000); eventBusThread.interrupt(); Agent.closeExecutors(executorThreads); + assertEquals(1, ((MessageCounterModule) modules.get(AgentMessage.AgentModule.RMI)).counter); } @Test @@ -70,9 +83,11 @@ public class EventBusTest { AgentMessage.AgentModule.RMI, System.currentTimeMillis() / 1000L)); - Thread.sleep(10000); + Thread.sleep(1000); eventBusThread.interrupt(); Agent.closeExecutors(executorThreads); + assertEquals(1, ((MessageCounterModule) modules.get(AgentMessage.AgentModule.RMI)).counter); + assertEquals(1, ((MessageCounterModule) modules.get(AgentMessage.AgentModule.UDP)).counter); } } -- cgit v1.2.3