|
| 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