forked from ringcentral/pubnub-jtools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHereNowTest.java
More file actions
210 lines (160 loc) · 7.14 KB
/
HereNowTest.java
File metadata and controls
210 lines (160 loc) · 7.14 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
package com.pubnub.api;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.junit.Before;
import org.junit.Test;
import java.util.UUID;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import static com.pubnub.api.matchers.JSONAssert.assertJSONArrayHas;
import static org.junit.Assert.assertEquals;
public class HereNowTest {
Pubnub pubnub;
String group;
String random;
@Before
public void setUp() throws InterruptedException {
random = UUID.randomUUID().toString().substring(0, 8);
pubnub = new Pubnub("demo", "demo", "demo");
pubnub.setCacheBusting(false);
group = "jtest-" + random;
}
@Test
public void testHereNowForOneChannel()
throws InterruptedException, PubnubException, JSONException {
final String channel = "ch1" + random;
final CountDownLatch latch1 = new CountDownLatch(1);
final CountDownLatch latch2 = new CountDownLatch(1);
final TestHelper.SimpleCallback cb1 = new TestHelper.SimpleCallback(latch1) {
@Override
public void connectCallback(String item, Object message) {
if (item.equals(channel)) latch.countDown();
}
};
final TestHelper.SimpleCallback cb2 = new TestHelper.SimpleCallback(latch2);
pubnub.subscribe(channel, cb1);
latch1.await(10, TimeUnit.SECONDS);
Thread.sleep(1000);
pubnub.hereNow(channel, cb2);
latch2.await(10, TimeUnit.SECONDS);
JSONObject response = (JSONObject) cb2.getResponse();
JSONArray uuids = response.getJSONArray("uuids");
assertJSONArrayHas(pubnub.getUUID(), uuids);
}
@Test
public void testHereNowForOneChannelGroup()
throws InterruptedException, PubnubException, JSONException {
final String[] channels = new String[]{"ch1" + random, "ch2" + random};
final CountDownLatch latch1 = new CountDownLatch(1);
final CountDownLatch latch2 = new CountDownLatch(1);
final CountDownLatch latch3 = new CountDownLatch(1);
final TestHelper.SimpleCallback cb1 = new TestHelper.SimpleCallback(latch1);
final TestHelper.SimpleCallback cb2 = new TestHelper.SimpleCallback(latch2) {
@Override
public void connectCallback(String item, Object message) {
latch.countDown();
}
};
final TestHelper.SimpleCallback cb3 = new TestHelper.SimpleCallback(latch3);
pubnub.channelGroupAddChannel(group, channels, cb1);
latch1.await(10, TimeUnit.SECONDS);
pubnub.subscribe(channels[0], cb2);
latch2.await(10, TimeUnit.SECONDS);
Thread.sleep(1000);
pubnub.channelGroupHereNow(group, cb3);
latch3.await(10, TimeUnit.SECONDS);
JSONObject response = (JSONObject) cb3.getResponse();
JSONArray uuids = response
.getJSONObject("channels")
.getJSONObject(channels[0])
.getJSONArray("uuids");
assertEquals(1, response.getInt("total_occupancy"));
assertEquals(1, response.getInt("total_channels"));
assertJSONArrayHas(pubnub.getUUID(), uuids);
}
@Test
public void testHereNowForMultipleChannels() throws InterruptedException, PubnubException, JSONException {
final String[] channels = new String[]{"ch1" + random, "ch2" + random};
final String[] groups = new String[]{group, "jtest2-" + random};
final CountDownLatch latch1 = new CountDownLatch(1);
final CountDownLatch latch2 = new CountDownLatch(1);
final CountDownLatch latch3 = new CountDownLatch(1);
final CountDownLatch latch4 = new CountDownLatch(1);
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) {
latch.countDown();
}
};
final TestHelper.SimpleCallback cb4 = new TestHelper.SimpleCallback(latch4);
pubnub.channelGroupAddChannel(groups[0], channels, cb1);
pubnub.channelGroupAddChannel(groups[1], channels, cb2);
latch1.await(10, TimeUnit.SECONDS);
latch2.await(10, TimeUnit.SECONDS);
pubnub.subscribe(channels, cb3);
latch3.await(10, TimeUnit.SECONDS);
Thread.sleep(1000);
pubnub.channelGroupHereNow(group, cb4);
latch4.await(10, TimeUnit.SECONDS);
JSONObject response = (JSONObject) cb4.getResponse();
JSONArray uuids1 = response
.getJSONObject("channels")
.getJSONObject(channels[0])
.getJSONArray("uuids");
JSONArray uuids2 = response
.getJSONObject("channels")
.getJSONObject(channels[1])
.getJSONArray("uuids");
assertEquals(2, response.getInt("total_occupancy"));
assertEquals(2, response.getInt("total_channels"));
assertJSONArrayHas(pubnub.getUUID(), uuids1);
assertJSONArrayHas(pubnub.getUUID(), uuids2);
}
@Test
public void testHereNowGlobal() throws JSONException, InterruptedException, PubnubException {
final String[] channels = new String[]{"ch1-" + random, "ch2-" + random};
Pubnub pubnub2 = new Pubnub("demo", "demo");
pubnub2.setCacheBusting(false);
final CountDownLatch latch3 = new CountDownLatch(1);
final CountDownLatch latch4 = new CountDownLatch(1);
final CountDownLatch latch5 = new CountDownLatch(1);
final TestHelper.SimpleCallback cb3 = new TestHelper.SimpleCallback(latch3) {
@Override
public void connectCallback(String item, Object message) {
latch.countDown();
}
};
final TestHelper.SimpleCallback cb4 = new TestHelper.SimpleCallback(latch4) {
@Override
public void connectCallback(String item, Object message) {
latch.countDown();
}
};
final TestHelper.SimpleCallback cb5 = new TestHelper.SimpleCallback(latch5);
pubnub.subscribe(channels[0], cb3);
latch3.await(10, TimeUnit.SECONDS);
pubnub2.subscribe(channels[1], cb4);
latch4.await(10, TimeUnit.SECONDS);
Thread.sleep(1000);
pubnub.hereNow(true, true, cb5);
latch5.await(10, TimeUnit.SECONDS);
JSONObject response = (JSONObject) cb5.getResponse();
String uuid1 = response
.getJSONObject("channels")
.getJSONObject(channels[0])
.getJSONArray("uuids")
.getJSONObject(0)
.getString("uuid");
String uuid2 = response
.getJSONObject("channels")
.getJSONObject(channels[1])
.getJSONArray("uuids")
.getJSONObject(0)
.getString("uuid");
assertEquals(pubnub.getUUID(), uuid1);
assertEquals(pubnub2.getUUID(), uuid2);
}
}