1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
|
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.ModuleType;
import pl.edu.mimuw.cloudatlas.agent.modules.TimerScheduledTask;
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(ModuleType moduleType) {
super(moduleType);
}
}
/*
public HashMap<ModuleType, Module> initializeTwoModules() {
HashMap<ModuleType, Module> modules = new HashMap<ModuleType, Module>();
modules.put(ModuleType.RMI, new MessageCounterModule(ModuleType.RMI) {
@Override
public void handle(AgentMessage event) throws InterruptedException {
System.out.println("Module 1 handle called");
// TODO correct message subclass
sendMessage(new AgentMessage("1", ModuleType.UDP) {});
counter ++;
}
});
modules.put(ModuleType.UDP, new MessageCounterModule(ModuleType.UDP) {
@Override
public void handle(AgentMessage event) {
System.out.println("Module 2 handle called");
counter++;
}
});
return modules;
}
public HashMap<ModuleType, Module> initializeModule() {
HashMap<ModuleType, Module> modules = new HashMap<ModuleType, Module>();
modules.put(ModuleType.RMI, new MessageCounterModule(ModuleType.RMI) {
@Override
public void handle(AgentMessage event) {
System.out.println("Module 1 handle called");
counter++;
}
});
return modules;
}
@Test
@Ignore
// TODO correct message subclass
public void messageModule() throws InterruptedException {
HashMap<ModuleType, Module> modules = initializeModule();
HashMap<ModuleType, 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", ModuleType.RMI) {});
Thread.sleep(1000);
eventBusThread.interrupt();
Agent.closeExecutors(executorThreads);
assertEquals(1, ((MessageCounterModule) modules.get(ModuleType.RMI)).counter);
}
@Test
@Ignore
// TODO correct message subclass
public void messagingBetweenModules() throws InterruptedException {
HashMap<ModuleType, Module> modules = initializeTwoModules();
HashMap<ModuleType, 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", ModuleType.RMI) {});
Thread.sleep(1000);
eventBusThread.interrupt();
Agent.closeExecutors(executorThreads);
assertEquals(1, ((MessageCounterModule) modules.get(ModuleType.RMI)).counter);
assertEquals(1, ((MessageCounterModule) modules.get(ModuleType.UDP)).counter);
}
@Test
public void sendWrongMessageType1() throws InterruptedException {
HashMap<ModuleType, Module> modules = initializeModule();
HashMap<ModuleType, Executor> executors = Agent.initializeExecutors(modules);
ArrayList<Thread> 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",
ModuleType.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<ModuleType, Module> modules = initializeModule();
HashMap<ModuleType, Executor> executors = Agent.initializeExecutors(modules);
ArrayList<Thread> 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", ModuleType.RMI) {});
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");
}
}
*/
}
|