Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JedisPubSub and BinaryJedisPubSub PING don't support optional argument #2254

Merged
merged 6 commits into from
Dec 6, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/main/java/redis/clients/jedis/BinaryJedisPubSub.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static redis.clients.jedis.Protocol.Keyword.MESSAGE;
import static redis.clients.jedis.Protocol.Keyword.PMESSAGE;
import static redis.clients.jedis.Protocol.Keyword.PONG;
import static redis.clients.jedis.Protocol.Keyword.PSUBSCRIBE;
import static redis.clients.jedis.Protocol.Keyword.PUNSUBSCRIBE;
import static redis.clients.jedis.Protocol.Keyword.SUBSCRIBE;
Expand All @@ -10,6 +11,7 @@
import java.util.Arrays;
import java.util.List;

import redis.clients.jedis.exceptions.JedisConnectionException;
sabbey37 marked this conversation as resolved.
Show resolved Hide resolved
import redis.clients.jedis.exceptions.JedisException;

public abstract class BinaryJedisPubSub {
Expand All @@ -34,6 +36,9 @@ public void onPUnsubscribe(byte[] pattern, int subscribedChannels) {
public void onPSubscribe(byte[] pattern, int subscribedChannels) {
}

public void onPong(byte[] pattern) {
}

public void unsubscribe() {
client.unsubscribe();
client.flush();
Expand Down Expand Up @@ -64,6 +69,16 @@ public void punsubscribe(byte[]... patterns) {
client.flush();
}

public void ping() {
client.ping();
client.flush();
}

public void ping(byte[] argument) {
client.ping(argument);
client.flush();
}

public boolean isSubscribed() {
return subscribedChannels > 0;
}
Expand Down Expand Up @@ -115,6 +130,9 @@ private void process(Client client) {
subscribedChannels = ((Long) reply.get(2)).intValue();
final byte[] bpattern = (byte[]) reply.get(1);
onPUnsubscribe(bpattern, subscribedChannels);
} else if (Arrays.equals(PONG.raw, resp)) {
final byte[] bpattern = (byte[]) reply.get(1);
sazzad16 marked this conversation as resolved.
Show resolved Hide resolved
onPong(bpattern);
} else {
throw new JedisException("Unknown message type: " + firstObj);
}
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/redis/clients/jedis/JedisPubSub.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,14 @@ public void ping() {
client.flush();
}

public void ping(String argument) {
if (client == null) {
throw new JedisConnectionException(JEDIS_SUBSCRIPTION_MESSAGE);
}
client.ping(argument);
client.flush();
}

public boolean isSubscribed() {
return subscribedChannels > 0;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package redis.clients.jedis.tests.commands;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.io.IOException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -112,6 +115,41 @@ public void onUnsubscribe(String channel, int subscribedChannels) {
assertEquals(0L, latchUnsubscribed.getCount());
}

@Test
public void pubSubChannelWithPingPongWithArgument() throws InterruptedException {
final CountDownLatch latchUnsubscribed = new CountDownLatch(1);
final CountDownLatch latchReceivedPong = new CountDownLatch(1);
final List<String> pongPatterns = new ArrayList<>();
jedis.subscribe(new JedisPubSub() {

@Override
public void onSubscribe(String channel, int subscribedChannels) {
publishOne("testchan1", "hello");
}

@Override
public void onMessage(String channel, String message) {
this.ping("hi!");
}

@Override
public void onPong(String pattern) {
pongPatterns.add(pattern);
latchReceivedPong.countDown();
unsubscribe();
}

@Override
public void onUnsubscribe(String channel, int subscribedChannels) {
latchUnsubscribed.countDown();
}
}, "testchan1");

assertEquals(0L, latchReceivedPong.getCount());
assertEquals(0L, latchUnsubscribed.getCount());
assertEquals(Collections.singletonList("hi!"), pongPatterns);
}

@Test
public void pubSubNumPat() {
jedis.psubscribe(new JedisPubSub() {
Expand Down Expand Up @@ -301,6 +339,75 @@ public void onPMessage(byte[] pattern, byte[] channel, byte[] message) {
}, SafeEncoder.encode("foo.*"), SafeEncoder.encode("bar.*"));
}

@Test
public void binaryPubSubChannelWithPingPong() throws InterruptedException {
final CountDownLatch latchUnsubscribed = new CountDownLatch(1);
final CountDownLatch latchReceivedPong = new CountDownLatch(1);

jedis.subscribe(new BinaryJedisPubSub() {

@Override
public void onSubscribe(byte[] channel, int subscribedChannels) {
publishOne("testchan1", "hello");
}

@Override
public void onMessage(byte[] channel, byte[] message) {
this.ping();
}

@Override
public void onPong(byte[] pattern) {
latchReceivedPong.countDown();
unsubscribe();
}

@Override
public void onUnsubscribe(byte[] channel, int subscribedChannels) {
latchUnsubscribed.countDown();
}
}, SafeEncoder.encode("testchan1"));
assertEquals(0L, latchReceivedPong.getCount());
assertEquals(0L, latchUnsubscribed.getCount());
}

@Test
public void binaryPubSubChannelWithPingPongWithArgument() throws InterruptedException {
final CountDownLatch latchUnsubscribed = new CountDownLatch(1);
final CountDownLatch latchReceivedPong = new CountDownLatch(1);
final List<byte[]> pongPatterns = new ArrayList<>();
final byte[] pingMessage = SafeEncoder.encode("hi!");

jedis.subscribe(new BinaryJedisPubSub() {

@Override
public void onSubscribe(byte[] channel, int subscribedChannels) {
publishOne("testchan1", "hello");
}

@Override
public void onMessage(byte[] channel, byte[] message) {
this.ping(pingMessage);
}

@Override
public void onPong(byte[] pattern) {
pongPatterns.add(pattern);
latchReceivedPong.countDown();
unsubscribe();
}

@Override
public void onUnsubscribe(byte[] channel, int subscribedChannels) {
latchUnsubscribed.countDown();
}
}, SafeEncoder.encode("testchan1"));

assertEquals(0L, latchReceivedPong.getCount());
assertEquals(0L, latchUnsubscribed.getCount());
assertArrayEquals(pingMessage, pongPatterns.get(0));
}

@Test
public void binarySubscribeLazily() throws UnknownHostException, IOException,
InterruptedException {
Expand Down