m-chrzan.xyz
aboutsummaryrefslogtreecommitdiff
path: root/src/test/java/pl/edu/mimuw/cloudatlas/agent/ExecutorTest.java
blob: 8653258fa853cf374de9ee75d9363077e2112ae5 (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
package pl.edu.mimuw.cloudatlas.agent;

import org.junit.Test;
import static org.junit.Assert.*;

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;

        MessageCounterModule(AgentModule moduleType) {
            super(moduleType);
        }

        public void handle(AgentMessage m) {
            counter++;
        }
    }

    @Test
    public void testDoesntExecuteWhenNoMessages() throws Exception {
        MessageCounterModule module = new MessageCounterModule(AgentModule.UDP);
        Executor executor = new Executor(module);
        Thread thread = new Thread(executor);
        thread.start();
        Thread.sleep(100);
        thread.interrupt();
        assertEquals(0, module.counter);
    }

    @Test
    public void testExecutesHandlerOnce() throws Exception {
        MessageCounterModule module = new MessageCounterModule(AgentModule.UDP);
        Executor executor = new Executor(module);
        executor.addMessage(new AgentMessage("", AgentModule.UDP, 0) {
            @Override
            public AgentModule getCorrectMessageType() {
                return AgentModule.UDP;
            }
        });
        Thread thread = new Thread(executor);
        thread.start();
        Thread.sleep(100);
        thread.interrupt();
        assertEquals(1, module.counter);
    }

    @Test
    public void testExecutesHandlerMultipleTimes() throws Exception {
        MessageCounterModule module = new MessageCounterModule(AgentModule.UDP);
        Executor executor = new Executor(module);
        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) {
            @Override
            public AgentModule getCorrectMessageType() {
                return AgentModule.UDP;
            }
        });
        Thread.sleep(100);
        thread.interrupt();
        assertEquals(3, module.counter);
    }
}