From 4c728b0ba906799afd30f0a337b540cf167a0cfd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Magdalena=20Grodzi=C5=84ska?= Date: Mon, 23 Dec 2019 19:50:46 +0100 Subject: Make timer inherit from module, move to modules package --- .../edu/mimuw/cloudatlas/agent/EventBusTest.java | 1 + .../edu/mimuw/cloudatlas/agent/ExecutorTest.java | 1 + .../edu/mimuw/cloudatlas/agent/SchedulerTest.java | 48 ++++++++++++++++++++++ 3 files changed, 50 insertions(+) create mode 100644 src/test/java/pl/edu/mimuw/cloudatlas/agent/SchedulerTest.java (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 fd7c244..178fb38 100644 --- a/src/test/java/pl/edu/mimuw/cloudatlas/agent/EventBusTest.java +++ b/src/test/java/pl/edu/mimuw/cloudatlas/agent/EventBusTest.java @@ -2,6 +2,7 @@ package pl.edu.mimuw.cloudatlas.agent; import org.junit.Test; import pl.edu.mimuw.cloudatlas.agent.message.AgentMessage; +import pl.edu.mimuw.cloudatlas.agent.modules.Module; import java.util.ArrayList; import java.util.HashMap; 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 2a6ef49..2b998f9 100644 --- a/src/test/java/pl/edu/mimuw/cloudatlas/agent/ExecutorTest.java +++ b/src/test/java/pl/edu/mimuw/cloudatlas/agent/ExecutorTest.java @@ -5,6 +5,7 @@ import static org.junit.Assert.*; import pl.edu.mimuw.cloudatlas.agent.message.AgentMessage; import pl.edu.mimuw.cloudatlas.agent.message.AgentMessage.AgentModule; +import pl.edu.mimuw.cloudatlas.agent.modules.Module; public class ExecutorTest { public class MessageCounterModule extends Module { diff --git a/src/test/java/pl/edu/mimuw/cloudatlas/agent/SchedulerTest.java b/src/test/java/pl/edu/mimuw/cloudatlas/agent/SchedulerTest.java new file mode 100644 index 0000000..45017c8 --- /dev/null +++ b/src/test/java/pl/edu/mimuw/cloudatlas/agent/SchedulerTest.java @@ -0,0 +1,48 @@ +package pl.edu.mimuw.cloudatlas.agent; + +import org.junit.Test; +import pl.edu.mimuw.cloudatlas.agent.message.AgentMessage; +import pl.edu.mimuw.cloudatlas.agent.modules.Module; +import pl.edu.mimuw.cloudatlas.agent.modules.TimerScheduler; + +import java.util.ArrayList; +import java.util.HashMap; + +import static org.junit.Assert.assertEquals; + +public class SchedulerTest { + + public HashMap initializeModule() { + HashMap modules = new HashMap(); + + modules.put(AgentMessage.AgentModule.TIMER_SCHEDULER, new TimerScheduler(AgentMessage.AgentModule.RMI)); + + return modules; + } + + @Test + void scheduleTask() { + + } + + @Test + void scheduleTwoTasks() { + HashMap modules = ini(); + 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(1000); + + eventBusThread.interrupt(); + Agent.closeExecutors(executorThreads); + assertEquals(1, ((EventBusTest.MessageCounterModule) modules.get(AgentMessage.AgentModule.RMI)).counter); + } +} -- cgit v1.2.3 From 219431065b335fdf250394ba66da8220d9bb868f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Magdalena=20Grodzi=C5=84ska?= Date: Mon, 23 Dec 2019 19:54:24 +0100 Subject: Rename messages package --- src/test/java/pl/edu/mimuw/cloudatlas/agent/EventBusTest.java | 2 +- src/test/java/pl/edu/mimuw/cloudatlas/agent/ExecutorTest.java | 4 ++-- src/test/java/pl/edu/mimuw/cloudatlas/agent/SchedulerTest.java | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) (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 178fb38..e43e7ea 100644 --- a/src/test/java/pl/edu/mimuw/cloudatlas/agent/EventBusTest.java +++ b/src/test/java/pl/edu/mimuw/cloudatlas/agent/EventBusTest.java @@ -1,7 +1,7 @@ package pl.edu.mimuw.cloudatlas.agent; import org.junit.Test; -import pl.edu.mimuw.cloudatlas.agent.message.AgentMessage; +import pl.edu.mimuw.cloudatlas.agent.messages.AgentMessage; import pl.edu.mimuw.cloudatlas.agent.modules.Module; import java.util.ArrayList; 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 2b998f9..1d3af92 100644 --- a/src/test/java/pl/edu/mimuw/cloudatlas/agent/ExecutorTest.java +++ b/src/test/java/pl/edu/mimuw/cloudatlas/agent/ExecutorTest.java @@ -3,8 +3,8 @@ package pl.edu.mimuw.cloudatlas.agent; import org.junit.Test; import static org.junit.Assert.*; -import pl.edu.mimuw.cloudatlas.agent.message.AgentMessage; -import pl.edu.mimuw.cloudatlas.agent.message.AgentMessage.AgentModule; +import pl.edu.mimuw.cloudatlas.agent.messages.AgentMessage; +import pl.edu.mimuw.cloudatlas.agent.messages.AgentMessage.AgentModule; import pl.edu.mimuw.cloudatlas.agent.modules.Module; public class ExecutorTest { diff --git a/src/test/java/pl/edu/mimuw/cloudatlas/agent/SchedulerTest.java b/src/test/java/pl/edu/mimuw/cloudatlas/agent/SchedulerTest.java index 45017c8..3787355 100644 --- a/src/test/java/pl/edu/mimuw/cloudatlas/agent/SchedulerTest.java +++ b/src/test/java/pl/edu/mimuw/cloudatlas/agent/SchedulerTest.java @@ -1,7 +1,7 @@ package pl.edu.mimuw.cloudatlas.agent; import org.junit.Test; -import pl.edu.mimuw.cloudatlas.agent.message.AgentMessage; +import pl.edu.mimuw.cloudatlas.agent.messages.AgentMessage; import pl.edu.mimuw.cloudatlas.agent.modules.Module; import pl.edu.mimuw.cloudatlas.agent.modules.TimerScheduler; -- cgit v1.2.3 From 6bd93c4fc80624eb83e9a1eb9b93e5781a796505 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Magdalena=20Grodzi=C5=84ska?= Date: Mon, 23 Dec 2019 23:27:01 +0100 Subject: Fix agent message type checks --- .../edu/mimuw/cloudatlas/agent/EventBusTest.java | 103 +++++++++++++++++-- .../edu/mimuw/cloudatlas/agent/ExecutorTest.java | 30 +++++- .../edu/mimuw/cloudatlas/agent/SchedulerTest.java | 109 +++++++++++++++++---- 3 files changed, 214 insertions(+), 28 deletions(-) (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 e43e7ea..f3df887 100644 --- a/src/test/java/pl/edu/mimuw/cloudatlas/agent/EventBusTest.java +++ b/src/test/java/pl/edu/mimuw/cloudatlas/agent/EventBusTest.java @@ -1,8 +1,13 @@ package pl.edu.mimuw.cloudatlas.agent; +import org.junit.Assert; +import org.junit.Ignore; import org.junit.Test; +import org.junit.runners.JUnit4; import pl.edu.mimuw.cloudatlas.agent.messages.AgentMessage; +import pl.edu.mimuw.cloudatlas.agent.messages.TimerSchedulerMessage; import pl.edu.mimuw.cloudatlas.agent.modules.Module; +import pl.edu.mimuw.cloudatlas.agent.modules.TimerScheduledTask; import java.util.ArrayList; import java.util.HashMap; @@ -24,7 +29,13 @@ public class EventBusTest { @Override public void handle(AgentMessage event) throws InterruptedException { System.out.println("Module 1 handle called"); - sendMessage(new AgentMessage("1", AgentMessage.AgentModule.UDP)); + // TODO correct message subclass + sendMessage(new AgentMessage("1", AgentMessage.AgentModule.UDP) { + @Override + public AgentModule getCorrectMessageType() { + return AgentModule.UDP; + } + }); counter ++; } }); @@ -55,6 +66,8 @@ public class EventBusTest { } @Test + @Ignore + // TODO correct message subclass public void messageModule() throws InterruptedException { HashMap modules = initializeModule(); HashMap executors = Agent.initializeExecutors(modules); @@ -63,7 +76,12 @@ public class EventBusTest { Thread eventBusThread = new Thread(eventBus); eventBusThread.start(); - eventBus.addMessage(new AgentMessage("0", AgentMessage.AgentModule.RMI)); + eventBus.addMessage(new AgentMessage("0", AgentMessage.AgentModule.RMI) { + @Override + public AgentModule getCorrectMessageType() { + return AgentModule.RMI; + } + }); Thread.sleep(1000); eventBusThread.interrupt(); Agent.closeExecutors(executorThreads); @@ -71,6 +89,8 @@ public class EventBusTest { } @Test + @Ignore + // TODO correct message subclass public void messagingBetweenModules() throws InterruptedException { HashMap modules = initializeTwoModules(); HashMap executors = Agent.initializeExecutors(modules); @@ -79,10 +99,12 @@ public class EventBusTest { Thread eventBusThread = new Thread(eventBus); eventBusThread.start(); - eventBus.addMessage(new AgentMessage( - "0", - AgentMessage.AgentModule.RMI, - System.currentTimeMillis() / 1000L)); + eventBus.addMessage(new AgentMessage("0", AgentMessage.AgentModule.RMI) { + @Override + public AgentModule getCorrectMessageType() { + return AgentModule.RMI; + } + }); Thread.sleep(1000); @@ -91,4 +113,73 @@ public class EventBusTest { assertEquals(1, ((MessageCounterModule) modules.get(AgentMessage.AgentModule.RMI)).counter); assertEquals(1, ((MessageCounterModule) modules.get(AgentMessage.AgentModule.UDP)).counter); } + + @Test + public void sendWrongMessageType1() 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); + Boolean routingErrorCaught = false; + eventBusThread.start(); + + try { + eventBus.addMessage(new TimerSchedulerMessage( + "0", + AgentMessage.AgentModule.RMI, + System.currentTimeMillis() / 1000L, + "1", + 10, + System.currentTimeMillis() / 1000L, + new TimerScheduledTask() { + @Override + public void run() { + System.out.println("Task executed"); + } + })); + Thread.sleep(1000); + } catch (AssertionError e) { + System.out.println("Wrong timer-scheduler message type error caught"); + routingErrorCaught = true; + } finally { + eventBusThread.interrupt(); + Agent.closeExecutors(executorThreads); + } + + if (!routingErrorCaught) { + Assert.fail("Routing not detected as faulty"); + } + } + + @Test + public void sendWrongMessageType2() 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); + Boolean routingErrorCaught = false; + eventBusThread.start(); + + try { + eventBus.addMessage(new AgentMessage("0", AgentMessage.AgentModule.RMI) { + @Override + public AgentModule getCorrectMessageType() { + return AgentModule.QUERY; + } + }); + Thread.sleep(1000); + } catch (AssertionError e) { + System.out.println("Wrong timer-scheduler message type error caught"); + routingErrorCaught = true; + } finally { + eventBusThread.interrupt(); + Agent.closeExecutors(executorThreads); + } + + if (!routingErrorCaught) { + Assert.fail("Routing not detected as faulty"); + } + } } 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 1d3af92..8653258 100644 --- a/src/test/java/pl/edu/mimuw/cloudatlas/agent/ExecutorTest.java +++ b/src/test/java/pl/edu/mimuw/cloudatlas/agent/ExecutorTest.java @@ -7,6 +7,8 @@ import pl.edu.mimuw.cloudatlas.agent.messages.AgentMessage; import pl.edu.mimuw.cloudatlas.agent.messages.AgentMessage.AgentModule; import pl.edu.mimuw.cloudatlas.agent.modules.Module; +// TODO make agent messages specific subclass + public class ExecutorTest { public class MessageCounterModule extends Module { public int counter = 0; @@ -35,7 +37,12 @@ public class ExecutorTest { public void testExecutesHandlerOnce() throws Exception { 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) { + @Override + public AgentModule getCorrectMessageType() { + return AgentModule.UDP; + } + }); Thread thread = new Thread(executor); thread.start(); Thread.sleep(100); @@ -47,12 +54,27 @@ public class ExecutorTest { public void testExecutesHandlerMultipleTimes() throws Exception { 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) {}); + executor.addMessage(new AgentMessage("", AgentModule.UDP, 0) { + @Override + public AgentModule getCorrectMessageType() { + return AgentModule.UDP; + } + }); + executor.addMessage(new AgentMessage("", AgentModule.UDP, 0) { + @Override + public AgentModule getCorrectMessageType() { + return AgentModule.UDP; + } + }); Thread thread = new Thread(executor); thread.start(); Thread.sleep(100); - executor.addMessage(new AgentMessage("", AgentModule.UDP, 0) {}); + executor.addMessage(new AgentMessage("", AgentModule.UDP, 0) { + @Override + public AgentModule getCorrectMessageType() { + return AgentModule.UDP; + } + }); Thread.sleep(100); thread.interrupt(); assertEquals(3, module.counter); diff --git a/src/test/java/pl/edu/mimuw/cloudatlas/agent/SchedulerTest.java b/src/test/java/pl/edu/mimuw/cloudatlas/agent/SchedulerTest.java index 3787355..5f0d1a7 100644 --- a/src/test/java/pl/edu/mimuw/cloudatlas/agent/SchedulerTest.java +++ b/src/test/java/pl/edu/mimuw/cloudatlas/agent/SchedulerTest.java @@ -2,47 +2,120 @@ package pl.edu.mimuw.cloudatlas.agent; import org.junit.Test; import pl.edu.mimuw.cloudatlas.agent.messages.AgentMessage; +import pl.edu.mimuw.cloudatlas.agent.messages.TimerSchedulerMessage; import pl.edu.mimuw.cloudatlas.agent.modules.Module; +import pl.edu.mimuw.cloudatlas.agent.modules.TimerScheduledTask; import pl.edu.mimuw.cloudatlas.agent.modules.TimerScheduler; import java.util.ArrayList; import java.util.HashMap; -import static org.junit.Assert.assertEquals; +// TODO better task tests after enabling messaging from tasks +// TODO add wrong message test with switched types public class SchedulerTest { + private HashMap modules; + private HashMap executors; + private ArrayList executorThreads; + private EventBus eventBus; + private Thread eventBusThread; + + public SchedulerTest() { + this.modules = initializeModule(); + this.executors = Agent.initializeExecutors(modules); + this.executorThreads = Agent.initializeExecutorThreads(executors); + this.eventBus = new EventBus(executors); + this.eventBusThread = new Thread(eventBus); + eventBusThread.start(); + } public HashMap initializeModule() { HashMap modules = new HashMap(); + modules.put(AgentMessage.AgentModule.TIMER_SCHEDULER, new TimerScheduler(AgentMessage.AgentModule.TIMER_SCHEDULER)); + return modules; + } - modules.put(AgentMessage.AgentModule.TIMER_SCHEDULER, new TimerScheduler(AgentMessage.AgentModule.RMI)); + @Test + public void initializeWrongModuleType() { + try { + Module timer = new TimerScheduler(AgentMessage.AgentModule.RMI); + } catch (AssertionError e) { + System.out.println("Wrong timer type during init error caught"); + } + } - return modules; + @Test + public void sendWrongMessageType() throws InterruptedException { + try { + this.eventBus.addMessage(new TimerSchedulerMessage( + "0", + AgentMessage.AgentModule.UDP, + System.currentTimeMillis() / 1000L, + "1", + 10, + System.currentTimeMillis() / 1000L, + new TimerScheduledTask() { + @Override + public void run() { + System.out.println("Task executed"); + } + })); + Thread.sleep(1000); + } catch (AssertionError e) { + System.out.println("Wrong timer-scheduler message type error caught"); + } } @Test - void scheduleTask() { + public void scheduleTask() throws InterruptedException { + this.eventBus.addMessage(new TimerSchedulerMessage( + "0", + AgentMessage.AgentModule.TIMER_SCHEDULER, + System.currentTimeMillis() / 1000L, + "1", + 10, + System.currentTimeMillis() / 1000L, + new TimerScheduledTask() { + @Override + public void run() { + System.out.println("Task executed"); + } + })); + Thread.sleep(1000); } @Test - void scheduleTwoTasks() { - HashMap modules = ini(); - HashMap executors = Agent.initializeExecutors(modules); - ArrayList executorThreads = Agent.initializeExecutorThreads(executors); - EventBus eventBus = new EventBus(executors); - Thread eventBusThread = new Thread(eventBus); - eventBusThread.start(); + public void scheduleTwoTasks() throws InterruptedException { + this.eventBus.addMessage(new TimerSchedulerMessage( + "0", + AgentMessage.AgentModule.TIMER_SCHEDULER, + System.currentTimeMillis() / 1000L, + "1", + 10, + System.currentTimeMillis() / 1000L, + new TimerScheduledTask() { + @Override + public void run() { + System.out.println("Task 1 executed"); + } + })); - eventBus.addMessage(new AgentMessage( + this.eventBus.addMessage(new TimerSchedulerMessage( "0", - AgentMessage.AgentModule.RMI, - System.currentTimeMillis() / 1000L)); + AgentMessage.AgentModule.TIMER_SCHEDULER, + System.currentTimeMillis() / 1000L, + "1", + 20, + System.currentTimeMillis() / 1000L, + new TimerScheduledTask() { + @Override + public void run() { + System.out.println("Task 2 executed"); + } + })); - Thread.sleep(1000); - eventBusThread.interrupt(); - Agent.closeExecutors(executorThreads); - assertEquals(1, ((EventBusTest.MessageCounterModule) modules.get(AgentMessage.AgentModule.RMI)).counter); + Thread.sleep(1000); } } -- cgit v1.2.3 From 28795098ebebd6a244b249355808a37912656f9d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Magdalena=20Grodzi=C5=84ska?= Date: Wed, 25 Dec 2019 12:52:48 +0100 Subject: Add timer scheduler tests with messages --- .../edu/mimuw/cloudatlas/agent/SchedulerTest.java | 41 +++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) (limited to 'src/test/java') diff --git a/src/test/java/pl/edu/mimuw/cloudatlas/agent/SchedulerTest.java b/src/test/java/pl/edu/mimuw/cloudatlas/agent/SchedulerTest.java index 5f0d1a7..133b3b9 100644 --- a/src/test/java/pl/edu/mimuw/cloudatlas/agent/SchedulerTest.java +++ b/src/test/java/pl/edu/mimuw/cloudatlas/agent/SchedulerTest.java @@ -10,7 +10,7 @@ import pl.edu.mimuw.cloudatlas.agent.modules.TimerScheduler; import java.util.ArrayList; import java.util.HashMap; -// TODO better task tests after enabling messaging from tasks +// TODO better task tests with counters after enabling messaging from tasks // TODO add wrong message test with switched types public class SchedulerTest { @@ -118,4 +118,43 @@ public class SchedulerTest { Thread.sleep(1000); } + + @Test + public void scheduleTwoMessagingTasks() throws InterruptedException { + TimerSchedulerMessage messageToSend = new TimerSchedulerMessage( + "0", + AgentMessage.AgentModule.TIMER_SCHEDULER, + System.currentTimeMillis() / 1000L, + "1", + 20, + System.currentTimeMillis() / 1000L, + new TimerScheduledTask() { + @Override + public void run() { + System.out.println("Task 2 executed"); + } + }); + + this.eventBus.addMessage(new TimerSchedulerMessage( + "0", + AgentMessage.AgentModule.TIMER_SCHEDULER, + System.currentTimeMillis() / 1000L, + "1", + 10, + System.currentTimeMillis() / 1000L, + new TimerScheduledTask() { + @Override + public void run() { + try { + this.sendMessage(messageToSend); + } catch (InterruptedException e) { + System.out.println("Task 1 message interrupted"); + e.printStackTrace(); + } + System.out.println("Task 1 executed"); + } + })); + + Thread.sleep(1000); + } } -- cgit v1.2.3