Sitelet https://github.com/snehamendiratta/java/commit/a9d1bbafdc00246682431ba9922402ea33ce54d4
Skip to content

Commit a9d1bba

Browse files
committed
add channel wildcards support
1 parent 692d25c commit a9d1bba

2 files changed

Lines changed: 75 additions & 1 deletion

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2451,7 +2451,7 @@ public void handleResponse(HttpRequest hreq, String response) {
24512451
SubscriptionItem _group = channelGroupSubscriptions.getItem(_groups[i]);
24522452
SubscriptionItem _channel = channelSubscriptions.getItem(_groups[i]);
24532453

2454-
if (_groupName.equals(_channelName)
2454+
if ((_groupName.equals(_channelName) || _groupName.endsWith("*"))
24552455
&& _channel != null
24562456
&& !isWorkerDead(hreq)
24572457
) {
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package com.pubnub.api;
2+
3+
import org.junit.Before;
4+
import org.junit.Test;
5+
6+
import java.util.concurrent.CountDownLatch;
7+
import java.util.concurrent.TimeUnit;
8+
9+
import static org.junit.Assert.assertEquals;
10+
11+
public class WildcardSubscribeTest {
12+
Pubnub pubnub = new Pubnub("demo-36", "demo-36");
13+
14+
@Before
15+
public void setUp() throws PubnubException {
16+
pubnub.setCacheBusting(false);
17+
}
18+
19+
@Test
20+
public void testSubscribeToWildcardChannel() throws PubnubException, InterruptedException {
21+
final CountDownLatch latch1 = new CountDownLatch(1);
22+
final CountDownLatch latch2 = new CountDownLatch(1);
23+
24+
TestHelper.SimpleCallback cb = new TestHelper.SimpleCallback(latch2) {
25+
@Override
26+
public void connectCallback(String channel, Object message) {
27+
latch1.countDown();
28+
}
29+
};
30+
31+
String channel = "jtest-foo";
32+
33+
pubnub.subscribe(channel + ".*", cb);
34+
35+
latch1.await(3, TimeUnit.SECONDS);
36+
assertEquals(0, latch1.getCount());
37+
38+
pubnub.publish(channel + ".bar", "hi", new Callback() {
39+
});
40+
41+
latch2.await(3, TimeUnit.SECONDS);
42+
43+
assertEquals("hi", cb.getResponse());
44+
assertEquals(0, latch2.getCount());
45+
}
46+
47+
@Test
48+
public void testSubscribeToWildcardChannel2() throws PubnubException, InterruptedException {
49+
final CountDownLatch latch1 = new CountDownLatch(1);
50+
final CountDownLatch latch2 = new CountDownLatch(1);
51+
52+
TestHelper.SimpleCallback cb = new TestHelper.SimpleCallback(latch2) {
53+
@Override
54+
public void connectCallback(String channel, Object message) {
55+
latch1.countDown();
56+
}
57+
};
58+
59+
String channel = "jtest-foo";
60+
61+
pubnub.subscribe(channel + ".bar.*", cb);
62+
63+
latch1.await(3, TimeUnit.SECONDS);
64+
assertEquals(0, latch1.getCount());
65+
66+
pubnub.publish(channel + ".bar.barbaz", "hi", new Callback() {
67+
});
68+
69+
latch2.await(3, TimeUnit.SECONDS);
70+
71+
assertEquals("hi", cb.getResponse());
72+
assertEquals(0, latch2.getCount());
73+
}
74+
}

0 commit comments

Comments
 (0)