diff options
Diffstat (limited to 'src/test/java')
-rw-r--r-- | src/test/java/pl/edu/mimuw/cloudatlas/agent/EventBusTest.java | 74 |
1 files changed, 74 insertions, 0 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 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<AgentMessage.AgentModule, Module> initializeTwoModules() { + HashMap<AgentMessage.AgentModule, Module> modules = new HashMap<AgentMessage.AgentModule, Module>(); + 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<AgentMessage.AgentModule, Module> initializeModule() { + HashMap<AgentMessage.AgentModule, Module> modules = new HashMap<AgentMessage.AgentModule, Module>(); + + 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<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); + } + + @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(10000); + + eventBusThread.interrupt(); + Agent.closeExecutors(executorThreads); + } } |