|
1 | 1 | package redis.clients.jedis.tests.commands; |
2 | 2 |
|
| 3 | +import static org.junit.Assert.assertArrayEquals; |
3 | 4 | import static org.junit.Assert.assertEquals; |
4 | 5 | import static org.junit.Assert.assertTrue; |
5 | 6 | import static org.junit.Assert.fail; |
6 | 7 |
|
7 | 8 | import java.io.IOException; |
8 | 9 | import java.net.UnknownHostException; |
| 10 | +import java.util.ArrayList; |
9 | 11 | import java.util.Arrays; |
| 12 | +import java.util.Collections; |
10 | 13 | import java.util.HashMap; |
11 | 14 | import java.util.List; |
12 | 15 | import java.util.Map; |
@@ -112,6 +115,41 @@ public void onUnsubscribe(String channel, int subscribedChannels) { |
112 | 115 | assertEquals(0L, latchUnsubscribed.getCount()); |
113 | 116 | } |
114 | 117 |
|
| 118 | + @Test |
| 119 | + public void pubSubChannelWithPingPongWithArgument() throws InterruptedException { |
| 120 | + final CountDownLatch latchUnsubscribed = new CountDownLatch(1); |
| 121 | + final CountDownLatch latchReceivedPong = new CountDownLatch(1); |
| 122 | + final List<String> pongPatterns = new ArrayList<>(); |
| 123 | + jedis.subscribe(new JedisPubSub() { |
| 124 | + |
| 125 | + @Override |
| 126 | + public void onSubscribe(String channel, int subscribedChannels) { |
| 127 | + publishOne("testchan1", "hello"); |
| 128 | + } |
| 129 | + |
| 130 | + @Override |
| 131 | + public void onMessage(String channel, String message) { |
| 132 | + this.ping("hi!"); |
| 133 | + } |
| 134 | + |
| 135 | + @Override |
| 136 | + public void onPong(String pattern) { |
| 137 | + pongPatterns.add(pattern); |
| 138 | + latchReceivedPong.countDown(); |
| 139 | + unsubscribe(); |
| 140 | + } |
| 141 | + |
| 142 | + @Override |
| 143 | + public void onUnsubscribe(String channel, int subscribedChannels) { |
| 144 | + latchUnsubscribed.countDown(); |
| 145 | + } |
| 146 | + }, "testchan1"); |
| 147 | + |
| 148 | + assertEquals(0L, latchReceivedPong.getCount()); |
| 149 | + assertEquals(0L, latchUnsubscribed.getCount()); |
| 150 | + assertEquals(Collections.singletonList("hi!"), pongPatterns); |
| 151 | + } |
| 152 | + |
115 | 153 | @Test |
116 | 154 | public void pubSubNumPat() { |
117 | 155 | jedis.psubscribe(new JedisPubSub() { |
@@ -301,6 +339,75 @@ public void onPMessage(byte[] pattern, byte[] channel, byte[] message) { |
301 | 339 | }, SafeEncoder.encode("foo.*"), SafeEncoder.encode("bar.*")); |
302 | 340 | } |
303 | 341 |
|
| 342 | + @Test |
| 343 | + public void binaryPubSubChannelWithPingPong() throws InterruptedException { |
| 344 | + final CountDownLatch latchUnsubscribed = new CountDownLatch(1); |
| 345 | + final CountDownLatch latchReceivedPong = new CountDownLatch(1); |
| 346 | + |
| 347 | + jedis.subscribe(new BinaryJedisPubSub() { |
| 348 | + |
| 349 | + @Override |
| 350 | + public void onSubscribe(byte[] channel, int subscribedChannels) { |
| 351 | + publishOne("testchan1", "hello"); |
| 352 | + } |
| 353 | + |
| 354 | + @Override |
| 355 | + public void onMessage(byte[] channel, byte[] message) { |
| 356 | + this.ping(); |
| 357 | + } |
| 358 | + |
| 359 | + @Override |
| 360 | + public void onPong(byte[] pattern) { |
| 361 | + latchReceivedPong.countDown(); |
| 362 | + unsubscribe(); |
| 363 | + } |
| 364 | + |
| 365 | + @Override |
| 366 | + public void onUnsubscribe(byte[] channel, int subscribedChannels) { |
| 367 | + latchUnsubscribed.countDown(); |
| 368 | + } |
| 369 | + }, SafeEncoder.encode("testchan1")); |
| 370 | + assertEquals(0L, latchReceivedPong.getCount()); |
| 371 | + assertEquals(0L, latchUnsubscribed.getCount()); |
| 372 | + } |
| 373 | + |
| 374 | + @Test |
| 375 | + public void binaryPubSubChannelWithPingPongWithArgument() throws InterruptedException { |
| 376 | + final CountDownLatch latchUnsubscribed = new CountDownLatch(1); |
| 377 | + final CountDownLatch latchReceivedPong = new CountDownLatch(1); |
| 378 | + final List<byte[]> pongPatterns = new ArrayList<>(); |
| 379 | + final byte[] pingMessage = SafeEncoder.encode("hi!"); |
| 380 | + |
| 381 | + jedis.subscribe(new BinaryJedisPubSub() { |
| 382 | + |
| 383 | + @Override |
| 384 | + public void onSubscribe(byte[] channel, int subscribedChannels) { |
| 385 | + publishOne("testchan1", "hello"); |
| 386 | + } |
| 387 | + |
| 388 | + @Override |
| 389 | + public void onMessage(byte[] channel, byte[] message) { |
| 390 | + this.ping(pingMessage); |
| 391 | + } |
| 392 | + |
| 393 | + @Override |
| 394 | + public void onPong(byte[] pattern) { |
| 395 | + pongPatterns.add(pattern); |
| 396 | + latchReceivedPong.countDown(); |
| 397 | + unsubscribe(); |
| 398 | + } |
| 399 | + |
| 400 | + @Override |
| 401 | + public void onUnsubscribe(byte[] channel, int subscribedChannels) { |
| 402 | + latchUnsubscribed.countDown(); |
| 403 | + } |
| 404 | + }, SafeEncoder.encode("testchan1")); |
| 405 | + |
| 406 | + assertEquals(0L, latchReceivedPong.getCount()); |
| 407 | + assertEquals(0L, latchUnsubscribed.getCount()); |
| 408 | + assertArrayEquals(pingMessage, pongPatterns.get(0)); |
| 409 | + } |
| 410 | + |
304 | 411 | @Test |
305 | 412 | public void binarySubscribeLazily() throws UnknownHostException, IOException, |
306 | 413 | InterruptedException { |
|
0 commit comments