m-chrzan.xyz
aboutsummaryrefslogtreecommitdiff
path: root/src/test
diff options
context:
space:
mode:
authorMagdalena GrodziƄska <mag.grodzinska@gmail.com>2019-12-23 19:19:30 +0100
committerGitHub <noreply@github.com>2019-12-23 19:19:30 +0100
commit0949c9d56c595c0e253e6a35604fb2b9af46f82a (patch)
tree7f573d76b1c89c1165513d90ecd82694e849e4da /src/test
parent651c2b76ff3db39cefd1542d6f4c19d8686dfb85 (diff)
parent7a104ca7eeb0d312bc76d2393495387079032aed (diff)
Merge pull request #67 from m-chrzan/event_bus
Event bus
Diffstat (limited to 'src/test')
-rw-r--r--src/test/java/pl/edu/mimuw/cloudatlas/agent/EventBusTest.java93
-rw-r--r--src/test/java/pl/edu/mimuw/cloudatlas/agent/ExecutorTest.java11
2 files changed, 101 insertions, 3 deletions
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..fd7c244
--- /dev/null
+++ b/src/test/java/pl/edu/mimuw/cloudatlas/agent/EventBusTest.java
@@ -0,0 +1,93 @@
+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;
+
+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 HashMap<AgentMessage.AgentModule, Module> initializeTwoModules() {
+ HashMap<AgentMessage.AgentModule, Module> modules = new HashMap<AgentMessage.AgentModule, Module>();
+ 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 MessageCounterModule(AgentMessage.AgentModule.UDP) {
+ @Override
+ public void handle(AgentMessage event) {
+ System.out.println("Module 2 handle called");
+ counter++;
+ }
+ });
+
+ return modules;
+ }
+
+ public HashMap<AgentMessage.AgentModule, Module> initializeModule() {
+ HashMap<AgentMessage.AgentModule, Module> modules = new HashMap<AgentMessage.AgentModule, Module>();
+
+ modules.put(AgentMessage.AgentModule.RMI, new MessageCounterModule(AgentMessage.AgentModule.RMI) {
+ @Override
+ public void handle(AgentMessage event) {
+ System.out.println("Module 1 handle called");
+ counter++;
+ }
+ });
+
+ return modules;
+ }
+
+ @Test
+ public void messageModule() throws InterruptedException {
+ HashMap<AgentMessage.AgentModule, Module> modules = initializeModule();
+ HashMap<AgentMessage.AgentModule, Executor> executors = Agent.initializeExecutors(modules);
+ ArrayList<Thread> 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);
+ assertEquals(1, ((MessageCounterModule) modules.get(AgentMessage.AgentModule.RMI)).counter);
+ }
+
+ @Test
+ public void messagingBetweenModules() throws InterruptedException {
+ HashMap<AgentMessage.AgentModule, Module> modules = initializeTwoModules();
+ HashMap<AgentMessage.AgentModule, Executor> executors = Agent.initializeExecutors(modules);
+ ArrayList<Thread> 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(1000);
+
+ eventBusThread.interrupt();
+ Agent.closeExecutors(executorThreads);
+ assertEquals(1, ((MessageCounterModule) modules.get(AgentMessage.AgentModule.RMI)).counter);
+ assertEquals(1, ((MessageCounterModule) modules.get(AgentMessage.AgentModule.UDP)).counter);
+ }
+}
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) {});