m-chrzan.xyz
aboutsummaryrefslogtreecommitdiff
path: root/src/test/java/pl/edu/mimuw/cloudatlas/agent/UDUPTest.java
blob: e4f601a51c00b86b3535d5554e9efc5f4eb6df2c (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
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
169
170
171
172
173
174
175
176
package pl.edu.mimuw.cloudatlas.agent;

import org.junit.*;
import pl.edu.mimuw.cloudatlas.agent.messages.UDUPMessage;
import pl.edu.mimuw.cloudatlas.agent.modules.Module;
import pl.edu.mimuw.cloudatlas.agent.modules.ModuleType;
import pl.edu.mimuw.cloudatlas.agent.modules.UDUP;
import pl.edu.mimuw.cloudatlas.model.PathName;
import pl.edu.mimuw.cloudatlas.model.ValueContact;

import java.net.InetAddress;
import java.net.UnknownHostException;

public class UDUPTest {

    @Test
    public void messageBetweenUDUPs() {
        UDUP udp1 = null;
        UDUP udp2 = null;
        UDUPMessage msg1 = null;
        boolean testSuccess = true;

        try {
            System.out.println("Starting udp1");

            udp1 = new UDUP(
                    InetAddress.getByName("127.0.0.2"),
                    5999,
                    5000,
                    1000);

            System.out.println("Starting udp2");

            udp2 = new UDUP(
                    InetAddress.getByName("127.0.0.3"),
                    5999,
                    5000,
                    1000);

            UDUPMessage testContent = new UDUPMessage();
            testContent.setDestinationModule(ModuleType.TEST);

            msg1 = new UDUPMessage(
                    "udup1",
                    new ValueContact(new PathName("/udp2"), InetAddress.getByName("127.0.0.3")),
                    testContent
            );

        } catch (UnknownHostException e) {
            e.printStackTrace();
        }

        Thread udpThread1 = new Thread(udp1);
        udpThread1.start();
        Thread udpThread2 = new Thread(udp2);
        udpThread2.start();

        try {
            Thread.sleep(5000);
            System.out.println("Sending message");
            if (udp1 == null | udp2 == null) {
                Assert.fail("UDPs not initialized");
            } else {
                udp1.handle(msg1);
                Thread.sleep(10000);
            }
        } catch (InterruptedException | Module.InvalidMessageType e) {
            e.printStackTrace();
            testSuccess = false;
        }

        udpThread1.interrupt();
        udpThread2.interrupt();

        if (testSuccess) {
            Assert.assertTrue(true);
        } else {
            Assert.fail();
        }
    }
/*
    @Test
    public void bigMessageBetweenUDUPs() {
        UDUP udp1 = new UDUP(
                ModuleType.UDP,
                5999,
                1000,
                5000,
                256);

        UDUP udp2 = new UDUP(
                ModuleType.UDP,
                5998,
                1000,
                5000,
                256);

        AttributesMap bigAttrib = new AttributesMap();
        for (Long i = 1L; i < 20; i++) {
            bigAttrib.add(i.toString(), new ValueInt(i));
        }

        UDUPMessage msg1 = new UDUPMessage(
                "udp1",
                ModuleType.UDP,
                "localhost",
                5998,
                new UpdateAttributesMessage("updateattrib1", 0,"/", bigAttrib),
                0,
                "conv1");

        try {
            udp1.handle(msg1);
            Thread.sleep(1000);
            UDUPMessage conv = udp2.fetchConversation("conv1");
            Assert.assertEquals(conv.getConversationId(), "conv1");
        } catch (InterruptedException | Module.InvalidMessageType e) {
            e.printStackTrace();
        } catch (UDUP.InvalidConversation noConversationError) {
            Assert.fail();
        }
    }


    @Test
    public void sendMultipleMessages() {
        @Test
        public void messageBetweenUDUPs() {
            UDUP udp1 = new UDUP(
                    ModuleType.UDP,
                    5999,
                    1000,
                    5000,
                    3,
                    256);

            UDUP udp2 = new UDUP(
                    ModuleType.UDP,
                    5998,
                    1000,
                    5000,
                    3,
                    256);

            UDUPMessage msg1 = new UDUPMessage(
                    "1",
                    ModuleType.UDP,
                    "localhost",
                    5998,
                    null,
                    0,
                    "conv1");




            try {
                udp1.handle(msg1);
                udp1.handle(msg2);
                udp1.handle(msg3);
                Thread.sleep(1000);
                UDUPMessage conv1 = udp2.fetchConversation("conv1");
                Assert.assertEquals(conv1.getConversationId(), "conv1");
                UDUPMessage conv2 = udp2.fetchConversation("conv2");
                Assert.assertEquals(conv2.getConversationId(), "conv2");
                UDUPMessage conv3 = udp2.fetchConversation("conv3");
                Assert.assertEquals(conv3.getConversationId(), "conv3");
            } catch (InterruptedException | Module.InvalidMessageType e) {
                e.printStackTrace();
            } catch (UDUP.InvalidConversation invalidConversation) {
                Assert.fail();
            }
        }
    }
*/
}