Skip to content

Commit 96683b9

Browse files
committed
add different combinations of unsubscribe for channels and channel groups and tests for them
1 parent 49fe8be commit 96683b9

4 files changed

Lines changed: 210 additions & 43 deletions

File tree

java/srcPubnubApi/com/pubnub/api/PubnubCore.java

Lines changed: 76 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1723,34 +1723,39 @@ private boolean inputsValid(Hashtable args) throws PubnubException {
17231723
}
17241724

17251725
private void leave(final String channel) {
1726+
_leave(PubnubUtil.urlEncode(channel), new Hashtable<String, String>());
1727+
}
17261728

1727-
String[] urlargs = { getPubnubUrl(), "v2/presence/sub_key",
1728-
this.SUBSCRIBE_KEY, "channel", PubnubUtil.urlEncode(channel),
1729-
"leave"
1730-
};
1731-
Hashtable params = new Hashtable();
1732-
params.put("uuid", UUID);
1729+
private void leaveGroup(String group) {
1730+
Hashtable<String, String> params = new Hashtable<String, String>();
1731+
params.put("channel-group", group);
17331732

1734-
HttpRequest hreq = new HttpRequest(urlargs, params,
1735-
new ResponseHandler() {
1733+
_leave(",", params);
1734+
}
17361735

1737-
public void handleResponse(HttpRequest hreq, String response) {
1736+
private void _leave(String channel, Hashtable<String, String> params) {
1737+
String[] urlArgs = {getPubnubUrl(), "v2/presence/sub_key",
1738+
this.SUBSCRIBE_KEY, "channel", channel, "leave"
1739+
};
17381740

1739-
}
1741+
params.put("uuid", UUID);
17401742

1741-
public void handleError(HttpRequest hreq, PubnubError error) {
1743+
HttpRequest hreq = new HttpRequest(urlArgs, params,
1744+
new ResponseHandler() {
1745+
public void handleResponse(HttpRequest hreq, String response) {
1746+
}
17421747

1743-
}
1748+
public void handleError(HttpRequest hreq, PubnubError error) {
1749+
}
1750+
});
17441751

1745-
});
17461752
_request(hreq, nonSubscribeManager);
17471753
}
17481754

17491755
/**
17501756
* Unsubscribe from channels.
17511757
*
1752-
* @param channels
1753-
* String array containing channel names
1758+
* @param channels String array containing channel names
17541759
*/
17551760
public void unsubscribe(String[] channels) {
17561761
for (String channel : channels) {
@@ -1763,18 +1768,26 @@ public void unsubscribe(String[] channels) {
17631768
}
17641769

17651770
/**
1766-
* Unsubscribe from all channel.
1771+
* Unsubscribe/Disconnect from channel.
17671772
*
1773+
* @param channel channel name as String.
17681774
*/
1769-
public void unsubscribeAll() {
1770-
String[] channels = channelSubscriptions.getItemNames();
1775+
public void unsubscribe(String channel) {
1776+
unsubscribe(new String[]{channel});
1777+
}
17711778

1772-
for (String channel : channels) {
1773-
channelSubscriptions.removeItem(channel);
1774-
leave(channel);
1779+
/**
1780+
* Unsubscribe/Disconnect from channel.
1781+
*
1782+
* @param args Hashtable containing channel name.
1783+
*/
1784+
protected void unsubscribe(Hashtable args) {
1785+
String[] channelList = (String[]) args.get("channels");
1786+
if (channelList == null) {
1787+
channelList = new String[]{(String) args.get("channel")};
17751788
}
17761789

1777-
disconnectAndResubscribe();
1790+
unsubscribe(channelList);
17781791
}
17791792

17801793
/**
@@ -1794,6 +1807,7 @@ public void unsubscribeGroup(String group) {
17941807
public void unsubscribeGroup(String[] groups) {
17951808
for (String group : groups) {
17961809
channelGroupSubscriptions.removeItem(group);
1810+
leaveGroup(group);
17971811
}
17981812

17991813
resubscribe();
@@ -1802,35 +1816,58 @@ public void unsubscribeGroup(String[] groups) {
18021816
/**
18031817
* Unsubscribe from presence channel.
18041818
*
1805-
* @param channel
1806-
* channel name as String.
1819+
* @param channel channel name as String.
18071820
*/
18081821
public void unsubscribePresence(String channel) {
18091822
unsubscribe(new String[]{channel + PRESENCE_SUFFIX});
18101823
}
18111824

18121825
/**
1813-
* Unsubscribe/Disconnect from channel.
1814-
*
1815-
* @param channel
1816-
* channel name as String.
1826+
* Unsubscribe from all channel and channel groups.
18171827
*/
1818-
public void unsubscribe(String channel) {
1819-
unsubscribe(new String[]{channel});
1828+
public void unsubscribeAll() {
1829+
String[] channels = channelSubscriptions.getItemNames();
1830+
String[] groups = channelGroupSubscriptions.getItemNames();
1831+
1832+
for (String channel : channels) {
1833+
channelSubscriptions.removeItem(channel);
1834+
leave(channel);
1835+
}
1836+
1837+
for (String group : groups) {
1838+
channelGroupSubscriptions.removeItem(group);
1839+
leaveGroup(group);
1840+
}
1841+
1842+
disconnectAndResubscribe();
18201843
}
18211844

18221845
/**
1823-
* Unsubscribe/Disconnect from channel.
1824-
*
1825-
* @param args
1826-
* Hashtable containing channel name.
1846+
* Unsubscribe from all channel.
18271847
*/
1828-
protected void unsubscribe(Hashtable args) {
1829-
String[] channelList = (String[]) args.get("channels");
1830-
if (channelList == null) {
1831-
channelList = new String[] { (String) args.get("channel") };
1848+
public void unsubscribeAllChannels() {
1849+
String[] channels = channelSubscriptions.getItemNames();
1850+
1851+
for (String channel : channels) {
1852+
channelSubscriptions.removeItem(channel);
1853+
leave(channel);
18321854
}
1833-
unsubscribe(channelList);
1855+
1856+
disconnectAndResubscribe();
1857+
}
1858+
1859+
/**
1860+
* Unsubscribe from all channel groups.
1861+
*/
1862+
public void unsubscribeAllGroups() {
1863+
String[] groups = channelGroupSubscriptions.getItemNames();
1864+
1865+
for (String group : groups) {
1866+
channelGroupSubscriptions.removeItem(group);
1867+
leaveGroup(group);
1868+
}
1869+
1870+
disconnectAndResubscribe();
18341871
}
18351872

18361873
/**

java/srcTest/com/pubnub/api/TestHelper.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,18 @@ public void errorCallback(String channel, PubnubError error) {
4343
}
4444
}
4545

46+
static class SubscribeCallback extends SimpleCallback {
47+
public SubscribeCallback(CountDownLatch latch) {
48+
this.latch = latch;
49+
}
50+
51+
@Override
52+
public void connectCallback(String channel, Object message) {
53+
if (this.latch != null) {
54+
this.latch.countDown();
55+
}
56+
}
57+
}
4658

4759
static class PresenceCallback extends Callback {
4860

java/srcTest/com/pubnub/api/UnsubscribeTest.java

Lines changed: 121 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package com.pubnub.api;
22

3+
import org.json.JSONArray;
4+
import org.json.JSONException;
5+
import org.json.JSONObject;
36
import org.junit.Before;
47
import org.junit.Rule;
58
import org.junit.Test;
@@ -8,12 +11,16 @@
811
import java.util.concurrent.CountDownLatch;
912
import java.util.concurrent.TimeUnit;
1013

11-
import static org.junit.Assert.*;
14+
import static com.pubnub.api.matchers.JSONAssert.assertJSONArrayHas;
15+
import static org.junit.Assert.assertEquals;
1216

1317
public class UnsubscribeTest {
1418
Pubnub pubnub;
1519
Pubnub pubnub2;
1620

21+
String channel;
22+
String group;
23+
1724
double random;
1825

1926
@Rule
@@ -30,12 +37,13 @@ public void setUp() {
3037
pubnub2.setCacheBusting(false);
3138

3239
random = Math.random();
40+
channel = "ch6-" + random;
41+
group = "jtest-" + random;
3342
}
3443

3544
@Test
3645
public void testUnsubscribe()
3746
throws InterruptedException, PubnubException {
38-
String channel = "ch6";
3947
CountDownLatch latch = new CountDownLatch(1);
4048
CountDownLatch latch2 = new CountDownLatch(1);
4149

@@ -58,9 +66,119 @@ public void testUnsubscribe()
5866
assertEquals("leave", presenceCb.getAction());
5967
}
6068

69+
@Test
70+
public void testUnsubscribeAllForGroup()
71+
throws PubnubException, InterruptedException, JSONException {
72+
JSONObject result;
73+
74+
CountDownLatch latch1 = new CountDownLatch(1);
75+
TestHelper.SimpleCallback cb1 = new TestHelper.SimpleCallback(latch1);
76+
77+
// check online
78+
connectToGroup();
79+
80+
// check offline
81+
pubnub2.unsubscribeAll();
82+
Thread.sleep(1000);
83+
84+
pubnub.hereNowGroup(group, cb1);
85+
latch1.await(10, TimeUnit.SECONDS);
86+
87+
result = (JSONObject) cb1.getResponse();
88+
assertEquals(0, result.getInt("total_occupancy"));
89+
}
90+
91+
@Test
92+
public void testUnsubscribeAllGroupsForGroup() throws PubnubException, InterruptedException, JSONException {
93+
JSONObject result;
94+
95+
CountDownLatch latch1 = new CountDownLatch(1);
96+
TestHelper.SimpleCallback cb1 = new TestHelper.SimpleCallback(latch1);
97+
98+
// check online
99+
connectToGroup();
100+
101+
// check offline
102+
pubnub2.unsubscribeAllGroups();
103+
Thread.sleep(1000);
104+
105+
pubnub.hereNowGroup(group, cb1);
106+
latch1.await(10, TimeUnit.SECONDS);
107+
108+
result = (JSONObject) cb1.getResponse();
109+
assertEquals(0, result.getInt("total_occupancy"));
110+
}
111+
112+
@Test
113+
public void testUnsubscribeGroupForGroup()
114+
throws PubnubException, InterruptedException, JSONException {
115+
JSONObject result;
116+
117+
CountDownLatch latch1 = new CountDownLatch(1);
118+
TestHelper.SimpleCallback cb1 = new TestHelper.SimpleCallback(latch1);
119+
120+
// check online
121+
connectToGroup();
122+
123+
// check offline
124+
pubnub2.unsubscribeGroup(group);
125+
Thread.sleep(1000);
126+
127+
pubnub.hereNowGroup(group, cb1);
128+
latch1.await(10, TimeUnit.SECONDS);
129+
130+
result = (JSONObject) cb1.getResponse();
131+
assertEquals(0, result.getInt("total_occupancy"));
132+
}
61133

62134
@Test
63-
public void testUnsubscribeGroups() {
135+
public void testUnsubscribeGroupsForGroup()
136+
throws PubnubException, InterruptedException, JSONException {
137+
JSONObject result;
138+
139+
CountDownLatch latch1 = new CountDownLatch(1);
140+
TestHelper.SimpleCallback cb1 = new TestHelper.SimpleCallback(latch1);
141+
142+
// check online
143+
connectToGroup();
144+
145+
// check offline
146+
pubnub2.unsubscribeGroup(new String[]{group});
147+
Thread.sleep(1000);
148+
149+
pubnub.hereNowGroup(group, cb1);
150+
latch1.await(10, TimeUnit.SECONDS);
151+
152+
result = (JSONObject) cb1.getResponse();
153+
assertEquals(0, result.getInt("total_occupancy"));
154+
}
155+
156+
private void connectToGroup()
157+
throws InterruptedException, PubnubException, JSONException {
158+
CountDownLatch latch1 = new CountDownLatch(1);
159+
CountDownLatch latch2 = new CountDownLatch(1);
160+
CountDownLatch latch3 = new CountDownLatch(1);
161+
162+
TestHelper.SimpleCallback cb1 = new TestHelper.SimpleCallback(latch1);
163+
TestHelper.SubscribeCallback cb2 = new TestHelper.SubscribeCallback(latch2);
164+
TestHelper.SimpleCallback cb3 = new TestHelper.SimpleCallback(latch3);
165+
166+
pubnub.addChannelToGroup(group, channel, cb1);
167+
latch1.await(10, TimeUnit.SECONDS);
168+
169+
// check online
170+
pubnub2.subscribeGroup(group, cb2);
171+
latch2.await(10, TimeUnit.SECONDS);
172+
Thread.sleep(1000);
173+
174+
pubnub.hereNowGroup(group, cb3);
175+
latch3.await(10, TimeUnit.SECONDS);
176+
177+
JSONObject result = (JSONObject) cb3.getResponse();
178+
JSONArray uuids = result.getJSONObject("channels")
179+
.getJSONObject(channel)
180+
.getJSONArray("uuids");
64181

182+
assertJSONArrayHas(pubnub2.getUUID(), uuids);
65183
}
66184
}

java/srcTest/com/pubnub/api/matchers/JSONAssert.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
public class JSONAssert {
1111

1212
public static void assertJSONArrayHasNo(String item, JSONArray jArray) {
13-
assertFalse("JSONArray should contain item \"" + item + "\"", listData(jArray).contains(item));
13+
assertFalse("JSONArray should not contain item \"" + item + "\"", listData(jArray).contains(item));
1414
}
1515
public static void assertJSONArrayHas(String item, JSONArray jArray) {
1616
assertTrue("JSONArray should contain item \"" + item + "\"", listData(jArray).contains(item));

0 commit comments

Comments
 (0)