forked from ringcentral/pubnub-jtools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnsubscribeTest.java
More file actions
183 lines (134 loc) · 5.44 KB
/
UnsubscribeTest.java
File metadata and controls
183 lines (134 loc) · 5.44 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
package com.pubnub.api;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
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 UnsubscribeTest {
Pubnub pubnub;
Pubnub pubnub2;
String channel;
String group;
String random;
@Rule
public ExpectedException thrown = ExpectedException.none();
@Before
public void setUp() {
pubnub = new Pubnub("demo", "demo");
pubnub.setCacheBusting(false);
pubnub2 = new Pubnub("demo", "demo");
pubnub2.setCacheBusting(false);
random = UUID.randomUUID().toString().substring(0, 8);
channel = "ch6-" + random;
group = "jtest-" + random;
}
@Test
public void testUnsubscribe()
throws InterruptedException, PubnubException {
CountDownLatch latch = new CountDownLatch(1);
CountDownLatch latch2 = new CountDownLatch(1);
TestHelper.PresenceCallback presenceCb = new TestHelper.PresenceCallback(latch);
pubnub.presence(channel, presenceCb);
pubnub2.subscribe(channel, new TestHelper.SimpleCallback());
latch.await(10, TimeUnit.SECONDS);
assertEquals(pubnub2.getUUID(), presenceCb.getUUID());
assertEquals("join", presenceCb.getAction());
presenceCb.setLatch(latch2);
pubnub2.unsubscribe(channel);
latch2.await(10, TimeUnit.SECONDS);
assertEquals(pubnub2.getUUID(), presenceCb.getUUID());
assertEquals("leave", presenceCb.getAction());
}
@Test
public void testUnsubscribeAllForGroup()
throws PubnubException, InterruptedException, JSONException {
JSONObject result;
CountDownLatch latch1 = new CountDownLatch(1);
TestHelper.SimpleCallback cb1 = new TestHelper.SimpleCallback(latch1);
// check online
connectToGroup();
// check offline
pubnub2.unsubscribeAll();
Thread.sleep(3000);
pubnub.channelGroupHereNow(group, cb1);
latch1.await(10, TimeUnit.SECONDS);
result = (JSONObject) cb1.getResponse();
assertEquals(0, result.getInt("total_occupancy"));
}
@Test
public void testUnsubscribeAllGroupsForGroup() throws PubnubException, InterruptedException, JSONException {
JSONObject result;
CountDownLatch latch1 = new CountDownLatch(1);
TestHelper.SimpleCallback cb1 = new TestHelper.SimpleCallback(latch1);
// check online
connectToGroup();
// check offline
pubnub2.channelGroupUnsubscribeAllGroups();
Thread.sleep(3000);
pubnub.channelGroupHereNow(group, cb1);
latch1.await(10, TimeUnit.SECONDS);
result = (JSONObject) cb1.getResponse();
assertEquals(0, result.getInt("total_occupancy"));
}
@Test
public void testUnsubscribeGroupForGroup()
throws PubnubException, InterruptedException, JSONException {
JSONObject result;
CountDownLatch latch1 = new CountDownLatch(1);
TestHelper.SimpleCallback cb1 = new TestHelper.SimpleCallback(latch1);
// check online
connectToGroup();
// check offline
pubnub2.channelGroupUnsubscribe(group);
Thread.sleep(3000);
pubnub.channelGroupHereNow(group, cb1);
latch1.await(10, TimeUnit.SECONDS);
result = (JSONObject) cb1.getResponse();
assertEquals(0, result.getInt("total_occupancy"));
}
@Test
public void testUnsubscribeGroupsForGroup()
throws PubnubException, InterruptedException, JSONException {
JSONObject result;
CountDownLatch latch1 = new CountDownLatch(1);
TestHelper.SimpleCallback cb1 = new TestHelper.SimpleCallback(latch1);
// check online
connectToGroup();
// check offline
pubnub2.channelGroupUnsubscribe(new String[]{group});
Thread.sleep(3000);
pubnub.channelGroupHereNow(group, cb1);
latch1.await(10, TimeUnit.SECONDS);
result = (JSONObject) cb1.getResponse();
assertEquals(0, result.getInt("total_occupancy"));
}
private void connectToGroup()
throws InterruptedException, PubnubException, JSONException {
CountDownLatch latch1 = new CountDownLatch(1);
CountDownLatch latch2 = new CountDownLatch(1);
CountDownLatch latch3 = new CountDownLatch(1);
TestHelper.SimpleCallback cb1 = new TestHelper.SimpleCallback(latch1);
TestHelper.SubscribeCallback cb2 = new TestHelper.SubscribeCallback(latch2);
TestHelper.SimpleCallback cb3 = new TestHelper.SimpleCallback(latch3);
pubnub.channelGroupAddChannel(group, channel, cb1);
latch1.await(10, TimeUnit.SECONDS);
// check online
pubnub2.channelGroupSubscribe(group, cb2);
latch2.await(10, TimeUnit.SECONDS);
Thread.sleep(1000);
pubnub.channelGroupHereNow(group, cb3);
latch3.await(10, TimeUnit.SECONDS);
JSONObject result = (JSONObject) cb3.getResponse();
JSONArray uuids = result.getJSONObject("channels")
.getJSONObject(channel)
.getJSONArray("uuids");
assertJSONArrayHas(pubnub2.getUUID(), uuids);
}
}