forked from ringcentral/pubnub-jtools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSubscribeGroupTest.java
More file actions
353 lines (283 loc) · 12.9 KB
/
SubscribeGroupTest.java
File metadata and controls
353 lines (283 loc) · 12.9 KB
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
package com.pubnub.api;
import org.junit.*;
import org.junit.rules.ExpectedException;
import java.util.Hashtable;
import java.util.UUID;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
public class SubscribeGroupTest {
Pubnub pubnub = new Pubnub("demo", "demo");
String random;
@Rule
public ExpectedException thrown = ExpectedException.none();
@Before
public void setUp() throws PubnubException {
pubnub.setCacheBusting(false);
pubnub.channelGroupRemoveGroup("jtest", new TestHelper.SimpleCallback());
pubnub.channelGroupRemoveGroup("jtest1", new TestHelper.SimpleCallback());
pubnub.channelGroupRemoveGroup("jtest2", new TestHelper.SimpleCallback());
pubnub.channelGroupRemoveGroup("jtest3", new TestHelper.SimpleCallback());
random = UUID.randomUUID().toString().substring(0, 8);
}
@Test
public void testSubscribeMultipleChannelsAndMultipleChannelGroups()
throws InterruptedException, PubnubException {
final String[] channels = new String[]{"ch1" + random, "ch2" + random, "ch3" + random};
final String[] groups = new String[]{"jtest1" + random, "jtest2" + random, "jtest3" + random};
final String testChannel = "ch4" + random;
final String sendMessage = "Test Message-" + Math.random();
final String okMessage = "ok-" + random;
final CountDownLatch latch1 = new CountDownLatch(1);
final CountDownLatch latch2 = new CountDownLatch(1);
final CountDownLatch latch3 = new CountDownLatch(2);
final TestHelper.SimpleCallback cb1 = new TestHelper.SimpleCallback(latch1);
final TestHelper.SimpleCallback cb2 = new TestHelper.SimpleCallback(latch2);
final TestHelper.SimpleCallback cb3 = new TestHelper.SimpleCallback(latch3) {
@Override
public void connectCallback(String item, Object message) {
if (item.equals(groups[1])) {
pubnub.publish(testChannel, sendMessage, cb2);
}
}
@Override
public void successCallback(String channel, Object message) {
if (message.equals(sendMessage)) {
pubnub.publish(channel, okMessage, cb2);
}
super.successCallback(channel, message);
}
};
pubnub.channelGroupAddChannel(groups[0], testChannel, cb1);
latch1.await(10, TimeUnit.SECONDS);
try {
pubnub.subscribe(channels, groups, cb3);
} catch (PubnubException e) {
fail(e.getMessage());
}
latch2.await(10, TimeUnit.SECONDS);
latch3.await(10, TimeUnit.SECONDS);
assertEquals(okMessage, cb3.getResponse());
}
@Test
public void testSubscribeMultipleChannelsAndOneChannelGroup()
throws InterruptedException, PubnubException {
final String[] channels = new String[]{"ch1", "ch2", "ch3"};
final String group = "jtest" + random;
final String ch4 = "ch4-" + random;
final String sendMessage = "Test Message " + Math.random();
final CountDownLatch latch1 = new CountDownLatch(1);
final CountDownLatch latch2 = new CountDownLatch(1);
final CountDownLatch latch3 = new CountDownLatch(1);
final CountDownLatch latch4 = new CountDownLatch(3);
final TestHelper.SimpleCallback cb1 = new TestHelper.SimpleCallback(latch1);
final TestHelper.SimpleCallback cb2 = new TestHelper.SimpleCallback(latch2);
final TestHelper.SimpleCallback cb3 = new TestHelper.SimpleCallback(latch3);
final TestHelper.SimpleCallback cb4 = new TestHelper.SimpleCallback(latch4) {
@Override
public void connectCallback(String item, Object message) {
if (item.equals(group)) {
pubnub.publish(ch4, sendMessage, cb2);
latch.countDown();
}
}
@Override
public void successCallback(String channel, Object message) {
if (channel.equals(ch4) && message.equals(sendMessage)) {
pubnub.publish(channels[1], "ok", cb3);
}
super.successCallback(channel, message);
}
};
pubnub.channelGroupAddChannel(group, ch4, cb1);
latch1.await(10, TimeUnit.SECONDS);
try {
pubnub.subscribe(channels, group, cb4);
} catch (PubnubException e) {
fail(e.getMessage());
}
latch2.await(10, TimeUnit.SECONDS);
latch3.await(10, TimeUnit.SECONDS);
latch4.await(10, TimeUnit.SECONDS);
assertEquals("ok", cb4.getResponse());
}
@Test
public void testSubscribeOneChannelAndMultipleChannelGroups()
throws InterruptedException, PubnubException {
final String channel = "ch1";
final String[] groups = new String[]{"jtest1" + random, "jtest2" + random, "jtest3" + random};
final String ch4 = "ch4-" + random;
final String sendMessage = "Test Message " + Math.random();
final CountDownLatch latch1 = new CountDownLatch(1);
final CountDownLatch latch2 = new CountDownLatch(1);
final CountDownLatch latch3 = new CountDownLatch(1);
final CountDownLatch latch4 = new CountDownLatch(3);
final TestHelper.SimpleCallback cb1 = new TestHelper.SimpleCallback(latch1);
final TestHelper.SimpleCallback cb2 = new TestHelper.SimpleCallback(latch2);
final TestHelper.SimpleCallback cb3 = new TestHelper.SimpleCallback(latch3);
final TestHelper.SimpleCallback cb4 = new TestHelper.SimpleCallback(latch4) {
@Override
public void connectCallback(String item, Object message) {
if (item.equals(groups[0])) {
pubnub.publish(ch4, sendMessage, cb2);
latch.countDown();
}
}
@Override
public void successCallback(String channel, Object message) {
pubnub.publish(channel, "ok", cb3);
super.successCallback(channel, message);
}
};
pubnub.channelGroupAddChannel(groups[0], ch4, cb1);
latch1.await(10, TimeUnit.SECONDS);
try {
pubnub.subscribe(channel, groups, cb4);
} catch (PubnubException e) {
fail(e.getMessage());
}
latch2.await(10, TimeUnit.SECONDS);
latch3.await(10, TimeUnit.SECONDS);
latch4.await(10, TimeUnit.SECONDS);
assertEquals("ok", cb4.getResponse());
}
@Test
public void testSubscribeOneChannelAndOneChannelGroup()
throws InterruptedException, PubnubException {
String channel = "ch1";
final String group = "jtest" + random;
final String ch4 = "ch4" + random;
final String sendMessage = "Test Message " + Math.random();
final CountDownLatch latch1 = new CountDownLatch(1);
final CountDownLatch latch2 = new CountDownLatch(1);
final CountDownLatch latch3 = new CountDownLatch(1);
final CountDownLatch latch4 = new CountDownLatch(3);
final TestHelper.SimpleCallback cb1 = new TestHelper.SimpleCallback(latch1);
final TestHelper.SimpleCallback cb2 = new TestHelper.SimpleCallback(latch2);
final TestHelper.SimpleCallback cb3 = new TestHelper.SimpleCallback(latch3);
final TestHelper.SimpleCallback cb4 = new TestHelper.SimpleCallback(latch4) {
@Override
public void connectCallback(String item, Object message) {
if (item.equals(group)) {
pubnub.publish(ch4, sendMessage, cb2);
latch.countDown();
}
}
@Override
public void successCallback(String channel, Object message) {
pubnub.publish(channel, "ok", cb3);
super.successCallback(channel, message);
}
};
pubnub.channelGroupAddChannel(group, ch4, cb1);
latch1.await(10, TimeUnit.SECONDS);
try {
pubnub.subscribe(channel, group, cb4);
} catch (PubnubException e) {
fail(e.getMessage());
}
latch2.await(10, TimeUnit.SECONDS);
latch3.await(10, TimeUnit.SECONDS);
latch4.await(10, TimeUnit.SECONDS);
assertEquals("ok", cb4.getResponse());
}
@Test
public void testSubscribeOneChannelAndNoChannelGroup()
throws InterruptedException {
final String channel = "ch1" + random;
final String sendMessage = "Test Message " + Math.random();
final CountDownLatch latch2 = new CountDownLatch(1);
final CountDownLatch latch3 = new CountDownLatch(1);
final CountDownLatch latch4 = new CountDownLatch(3);
final TestHelper.SimpleCallback cb2 = new TestHelper.SimpleCallback(latch2);
final TestHelper.SimpleCallback cb3 = new TestHelper.SimpleCallback(latch3);
final TestHelper.SimpleCallback cb4 = new TestHelper.SimpleCallback(latch4) {
@Override
public void connectCallback(String item, Object message) {
if (item.equals(channel)) {
pubnub.publish(channel, "test", cb2);
latch.countDown();
}
}
@Override
public void successCallback(String channel, Object message) {
pubnub.publish(channel, sendMessage, cb3);
super.successCallback(channel, message);
}
};
try {
pubnub.subscribe(channel, cb4);
} catch (PubnubException e) {
fail(e.getMessage());
}
latch2.await(10, TimeUnit.SECONDS);
latch3.await(10, TimeUnit.SECONDS);
latch4.await(10, TimeUnit.SECONDS);
assertEquals(sendMessage, cb4.getResponse());
}
@Test
public void testSubscribeNoChannelAndOneChannelGroup()
throws InterruptedException, PubnubException {
final String group = "jtest-" + random;
final String ch4 = "ch4-" + random;
final String sendMessage = "Test Message " + Math.random();
final String testMessage = "test-" + Math.random();
final CountDownLatch latch1 = new CountDownLatch(1);
final CountDownLatch latch2 = new CountDownLatch(1);
final CountDownLatch latch3 = new CountDownLatch(1);
final CountDownLatch latch4 = new CountDownLatch(3);
final TestHelper.SimpleCallback cb1 = new TestHelper.SimpleCallback(latch1);
final TestHelper.SimpleCallback cb2 = new TestHelper.SimpleCallback(latch2);
final TestHelper.SimpleCallback cb3 = new TestHelper.SimpleCallback(latch3);
final TestHelper.SimpleCallback cb4 = new TestHelper.SimpleCallback(latch4) {
@Override
public void connectCallback(String item, Object message) {
if (item.equals(group)) {
pubnub.publish(ch4, testMessage, cb2);
latch.countDown();
}
}
@Override
public void successCallback(String channel, Object message) {
pubnub.publish(ch4, sendMessage, cb3);
super.successCallback(channel, message);
}
};
pubnub.channelGroupAddChannel(group, ch4, cb1);
latch1.await(10, TimeUnit.SECONDS);
try {
pubnub.channelGroupSubscribe(group, cb4);
} catch (PubnubException e) {
fail(e.getMessage());
}
latch2.await(10, TimeUnit.SECONDS);
latch3.await(10, TimeUnit.SECONDS);
latch4.await(10, TimeUnit.SECONDS);
assertEquals(sendMessage, cb4.getResponse());
}
@Test
public void testSubscribeNoChannelAndNoChannelGroup()
throws InterruptedException, PubnubException {
final TestHelper.SimpleCallback callback = new TestHelper.SimpleCallback() {
@Override
public void connectCallback(String item, Object message) {
}
@Override
public void successCallback(String channel, Object message) {
super.successCallback(channel, message);
}
};
Hashtable<String, Object> args = new Hashtable<String, Object>();
args.put("callback", callback);
thrown.expect(PubnubException.class);
pubnub.subscribe(args);
}
@After
public void tearDown() throws PubnubException {
pubnub.channelGroupRemoveGroup("jtest" + random, new TestHelper.SimpleCallback());
pubnub.channelGroupRemoveGroup("jtest1" + random, new TestHelper.SimpleCallback());
pubnub.channelGroupRemoveGroup("jtest2" + random, new TestHelper.SimpleCallback());
pubnub.channelGroupRemoveGroup("jtest3" + random, new TestHelper.SimpleCallback());
}
}