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') 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