summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndy McCurdy <andy@andymccurdy.com>2018-11-13 11:58:01 -0800
committerGitHub <noreply@github.com>2018-11-13 11:58:01 -0800
commite69ecbb16a207616e4b7cf5ebdd3075d4eb43e61 (patch)
tree1b61d72599723331f5c836b1c7208a6b82664593
parent3e3c96ffdbf44d6daba841b8b77f6dba4ec02e8a (diff)
parent4147b2088801040298cc5f595c9ede226e283634 (diff)
downloadredis-py-e69ecbb16a207616e4b7cf5ebdd3075d4eb43e61.tar.gz
Merge pull request #1019 from krishan-carbon/patch-1
Allow pings in PubSub
-rwxr-xr-xredis/client.py16
-rw-r--r--tests/test_pubsub.py23
2 files changed, 37 insertions, 2 deletions
diff --git a/redis/client.py b/redis/client.py
index c6a8e4b..b132336 100755
--- a/redis/client.py
+++ b/redis/client.py
@@ -3054,6 +3054,13 @@ class PubSub(object):
return self.handle_message(response, ignore_subscribe_messages)
return None
+ def ping(self, message=None):
+ """
+ Ping the Redis server
+ """
+ message = '' if message is None else message
+ return self.execute_command('PING', message)
+
def handle_message(self, response, ignore_subscribe_messages=False):
"""
Parses a pub/sub message. If the channel or pattern was subscribed to
@@ -3068,6 +3075,13 @@ class PubSub(object):
'channel': response[2],
'data': response[3]
}
+ elif message_type == 'pong':
+ message = {
+ 'type': message_type,
+ 'pattern': None,
+ 'channel': None,
+ 'data': response[1]
+ }
else:
message = {
'type': message_type,
@@ -3098,7 +3112,7 @@ class PubSub(object):
if handler:
handler(message)
return None
- else:
+ elif message_type != 'pong':
# this is a subscribe/unsubscribe message. ignore if we don't
# want them
if ignore_subscribe_messages or self.ignore_subscribe_messages:
diff --git a/tests/test_pubsub.py b/tests/test_pubsub.py
index 2e54384..91e9e48 100644
--- a/tests/test_pubsub.py
+++ b/tests/test_pubsub.py
@@ -27,7 +27,7 @@ def make_message(type, channel, data, pattern=None):
return {
'type': type,
'pattern': pattern and pattern.encode('utf-8') or None,
- 'channel': channel.encode('utf-8'),
+ 'channel': channel and channel.encode('utf-8') or None,
'data': data.encode('utf-8') if isinstance(data, basestring) else data
}
@@ -427,3 +427,24 @@ class TestPubSubPubSubSubcommands(object):
p = r.pubsub(ignore_subscribe_messages=True)
p.psubscribe('*oo', '*ar', 'b*z')
assert r.pubsub_numpat() == 3
+
+
+class TestPubSubPings(object):
+
+ @skip_if_server_version_lt('3.0.0')
+ def test_send_pubsub_ping(self, r):
+ p = r.pubsub(ignore_subscribe_messages=True)
+ p.subscribe('foo')
+ p.ping()
+ assert wait_for_message(p) == make_message(type='pong', channel=None,
+ data='',
+ pattern=None)
+
+ @skip_if_server_version_lt('3.0.0')
+ def test_send_pubsub_ping_message(self, r):
+ p = r.pubsub(ignore_subscribe_messages=True)
+ p.subscribe('foo')
+ p.ping(message='hello world')
+ assert wait_for_message(p) == make_message(type='pong', channel=None,
+ data='hello world',
+ pattern=None)