source: trunk/src/test/java/omq/test/workspace/WorkspaceTest.java @ 83

Last change on this file since 83 was 83, checked in by stoda, 11 years ago

J

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