m-chrzan.xyz
aboutsummaryrefslogtreecommitdiff
path: root/src/test/java/pl/edu/mimuw/cloudatlas/agent/SchedulerTest.java
blob: 99051fff539bd4518dc59212015b0161d89d0a20 (plain)
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
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.ModuleType;
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<ModuleType, Module> modules;
    private HashMap<ModuleType, Executor> executors;
    private ArrayList<Thread> executorThreads;
    private EventBus eventBus;
    private Thread eventBusThread;

    public SchedulerTest() {
        this.modules = initializeModule();
        this.executors = AgentConfig.initializeExecutors(modules);
        this.executorThreads = AgentConfig.initializeExecutorThreads(executors);
        this.eventBus = new EventBus(executors);
        this.eventBusThread = new Thread(eventBus);
        eventBusThread.start();
    }

    public HashMap<ModuleType, Module> initializeModule() {
        HashMap<ModuleType, Module> modules = new HashMap<ModuleType, Module>();
        modules.put(ModuleType.TIMER_SCHEDULER, new TimerScheduler(ModuleType.TIMER_SCHEDULER));
        return modules;
    }

    @Test
    public void initializeWrongModuleType() {
        try {
            Module timer = new TimerScheduler(ModuleType.RMI);
        } catch (AssertionError e) {
            System.out.println("Wrong timer type during init error caught");
        }
    }

    @Test
    public void scheduleTask() throws InterruptedException {
        this.eventBus.addMessage(new TimerSchedulerMessage(
                "0",
                System.currentTimeMillis() / 1000L,
                "1",
                10,
                System.currentTimeMillis() / 1000L,
                new TimerScheduledTask() {
                    @Override
                    public void run() {
                        System.out.println("Task executed");
                    }
                }));

        Thread.sleep(300);
    }

    @Test
    public void scheduleTwoTasks() throws InterruptedException {
        this.eventBus.addMessage(new TimerSchedulerMessage(
                "0",
                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",
                System.currentTimeMillis() / 1000L,
                "1",
                20,
                System.currentTimeMillis() / 1000L,
                new TimerScheduledTask() {
                    @Override
                    public void run() {
                        System.out.println("Task 2 executed");
                    }
                }));


        Thread.sleep(300);
    }

    @Test
    public void scheduleTwoMessagingTasks() throws InterruptedException {
        TimerSchedulerMessage messageToSend = new TimerSchedulerMessage(
                "0",
                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",
                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(300);
    }
}