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
|
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;
// TODO better task tests with counters after enabling messaging from tasks
// TODO add wrong message test with switched types
public class SchedulerTest {
private HashMap<AgentMessage.AgentModule, Module> modules;
private HashMap<AgentMessage.AgentModule, Executor> executors;
private ArrayList<Thread> 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<AgentMessage.AgentModule, Module> initializeModule() {
HashMap<AgentMessage.AgentModule, Module> modules = new HashMap<AgentMessage.AgentModule, Module>();
modules.put(AgentMessage.AgentModule.TIMER_SCHEDULER, new TimerScheduler(AgentMessage.AgentModule.TIMER_SCHEDULER));
return modules;
}
@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");
}
}
@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
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
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");
}
}));
this.eventBus.addMessage(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");
}
}));
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);
}
}
|