1 | package omq.test.workspace; |
---|
2 | |
---|
3 | import static org.junit.Assert.assertEquals; |
---|
4 | |
---|
5 | import java.util.Arrays; |
---|
6 | import java.util.Collection; |
---|
7 | import java.util.Properties; |
---|
8 | |
---|
9 | import omq.common.broker.Broker; |
---|
10 | import omq.common.util.ParameterQueue; |
---|
11 | import omq.common.util.Serializer; |
---|
12 | |
---|
13 | import org.junit.After; |
---|
14 | import org.junit.BeforeClass; |
---|
15 | import org.junit.Test; |
---|
16 | import org.junit.runner.RunWith; |
---|
17 | import org.junit.runners.Parameterized; |
---|
18 | import org.junit.runners.Parameterized.Parameters; |
---|
19 | |
---|
20 | @RunWith(value = Parameterized.class) |
---|
21 | public class WorkspaceTest { |
---|
22 | private String type; |
---|
23 | private static Broker serverBroker; |
---|
24 | private static Broker clientBroker1; |
---|
25 | private static Broker clientBroker2; |
---|
26 | private static RemoteWorkspaceImpl w1C1; |
---|
27 | private static RemoteWorkspaceImpl w3C1; |
---|
28 | private static RemoteWorkspaceImpl w1C2; |
---|
29 | private static RemoteWorkspaceImpl w2C2; |
---|
30 | private static String[] workspaces = { "w1", "w2", "w3" }; |
---|
31 | private static RemoteWorkspace[] remoteWorks = new RemoteWorkspace[3]; |
---|
32 | |
---|
33 | // In this case the Constructor acts as a server |
---|
34 | public WorkspaceTest(String type) throws Exception { |
---|
35 | this.type = type; |
---|
36 | Properties env = new Properties(); |
---|
37 | env.setProperty(ParameterQueue.USER_NAME, "guest"); |
---|
38 | env.setProperty(ParameterQueue.USER_PASS, "guest"); |
---|
39 | |
---|
40 | // Set host info of rabbimq (where it is) |
---|
41 | env.setProperty(ParameterQueue.RABBIT_HOST, "127.0.0.1"); |
---|
42 | env.setProperty(ParameterQueue.RABBIT_PORT, "5672"); |
---|
43 | env.setProperty(ParameterQueue.DURABLE_QUEUES, "false"); |
---|
44 | env.setProperty(ParameterQueue.PROXY_SERIALIZER, type); |
---|
45 | env.setProperty(ParameterQueue.ENABLE_COMPRESSION, "false"); |
---|
46 | |
---|
47 | // Set info about where the message will be sent |
---|
48 | env.setProperty(ParameterQueue.RPC_EXCHANGE, "rpc_exchange"); |
---|
49 | |
---|
50 | serverBroker = new Broker(env); |
---|
51 | int i = 0; |
---|
52 | for (String w : workspaces) { |
---|
53 | remoteWorks[i++] = serverBroker.lookupMulti(w, RemoteWorkspace.class); |
---|
54 | } |
---|
55 | |
---|
56 | } |
---|
57 | |
---|
58 | @Parameters |
---|
59 | public static Collection<Object[]> data() { |
---|
60 | Object[][] data = new Object[][] { { Serializer.JAVA }, { Serializer.GSON }, { Serializer.KRYO } }; |
---|
61 | return Arrays.asList(data); |
---|
62 | } |
---|
63 | |
---|
64 | @BeforeClass |
---|
65 | public static void client() throws Exception { |
---|
66 | Properties env = new Properties(); |
---|
67 | env.setProperty(ParameterQueue.USER_NAME, "guest"); |
---|
68 | env.setProperty(ParameterQueue.USER_PASS, "guest"); |
---|
69 | |
---|
70 | // Get host info of rabbimq (where it is) |
---|
71 | env.setProperty(ParameterQueue.RABBIT_HOST, "127.0.0.1"); |
---|
72 | env.setProperty(ParameterQueue.RABBIT_PORT, "5672"); |
---|
73 | env.setProperty(ParameterQueue.DURABLE_QUEUES, "false"); |
---|
74 | env.setProperty(ParameterQueue.ENABLE_COMPRESSION, "false"); |
---|
75 | |
---|
76 | // Set info about where the message will be sent |
---|
77 | env.setProperty(ParameterQueue.RPC_EXCHANGE, "rpc_exchange"); |
---|
78 | env.setProperty(ParameterQueue.RETRY_TIME_CONNECTION, "2000"); |
---|
79 | |
---|
80 | clientBroker1 = new Broker(env); |
---|
81 | clientBroker2 = new Broker(env); |
---|
82 | |
---|
83 | // Client 1 will subscribe to changes in the workspaces w1 and w3 |
---|
84 | w1C1 = new RemoteWorkspaceImpl(); |
---|
85 | w3C1 = new RemoteWorkspaceImpl(); |
---|
86 | |
---|
87 | clientBroker1.bind(workspaces[0], w1C1); |
---|
88 | clientBroker1.bind(workspaces[2], w3C1); |
---|
89 | |
---|
90 | // Client 2 will subscribe to changes in the workspaces w1 and w2 |
---|
91 | w1C2 = new RemoteWorkspaceImpl(); |
---|
92 | w2C2 = new RemoteWorkspaceImpl(); |
---|
93 | |
---|
94 | clientBroker2.bind(workspaces[0], w1C2); |
---|
95 | clientBroker2.bind(workspaces[1], w2C2); |
---|
96 | |
---|
97 | System.out.println("Client 1 & client2 started"); |
---|
98 | } |
---|
99 | |
---|
100 | @After |
---|
101 | public void stop() throws Exception { |
---|
102 | serverBroker.stopBroker(); |
---|
103 | } |
---|
104 | |
---|
105 | @Test |
---|
106 | public void test() throws Exception { |
---|
107 | System.out.println("Starting test: " + type); |
---|
108 | String expected = null; |
---|
109 | String actual = null; |
---|
110 | |
---|
111 | // The server will notify a change in the w2 |
---|
112 | expected = "w2 has changed"; |
---|
113 | Info info = new Info(expected); |
---|
114 | remoteWorks[1].update(info); |
---|
115 | Thread.sleep(200); |
---|
116 | |
---|
117 | // If everything has worked, the client2 will see a change in w2 |
---|
118 | actual = w2C2.getInfo().getId(); |
---|
119 | assertEquals(expected, actual); |
---|
120 | |
---|
121 | // The server will notify a change in the w1 |
---|
122 | expected = "w1 has changed"; |
---|
123 | info.setId(expected); |
---|
124 | remoteWorks[0].update(info); |
---|
125 | Thread.sleep(200); |
---|
126 | |
---|
127 | // If everything has worked, both clients will see a change in w1 |
---|
128 | actual = w1C1.getInfo().getId(); |
---|
129 | assertEquals(expected, actual); |
---|
130 | actual = w1C2.getInfo().getId(); |
---|
131 | assertEquals(expected, actual); |
---|
132 | } |
---|
133 | |
---|
134 | } |
---|