summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAngusP <a7cp3ar50n@gmail.com>2017-01-24 00:03:38 +0000
committerAngusP <a7cp3ar50n@gmail.com>2017-01-24 00:03:38 +0000
commitc7f75a8822e1eb859deb2f87f1186649099b954b (patch)
treecc365d31bee39a2c01d0ebe4715c207fcbc3f15b
parentd49aaacb7dd454781fd9cb2cd2cb92e9c07d7d31 (diff)
downloadredis-py-c7f75a8822e1eb859deb2f87f1186649099b954b.tar.gz
Implement PUBSUB * commands
PUBSUB CHANNELS, PUBSUB NUMSUB and PUBSUB NUMPAT are implemented as per https://redis.io/commands/pubsub and suggested in Issue #526. Implemented test class `TestPubSubPubSubSubcommands` to test added commands. Tested against python 3.4.2 and python 2.7.5
-rwxr-xr-xredis/client.py23
-rw-r--r--tests/test_pubsub.py28
2 files changed, 50 insertions, 1 deletions
diff --git a/redis/client.py b/redis/client.py
index b37e96d..b8dec5b 100755
--- a/redis/client.py
+++ b/redis/client.py
@@ -339,6 +339,10 @@ def parse_georadius_generic(response, **options):
]
+def parse_pubsub_numsub(response, **options):
+ return list(zip(response[0::2], response[1::2]))
+
+
class StrictRedis(object):
"""
Implementation of the Redis protocol.
@@ -447,6 +451,7 @@ class StrictRedis(object):
'GEOHASH': lambda r: list(map(nativestr, r)),
'GEORADIUS': parse_georadius_generic,
'GEORADIUSBYMEMBER': parse_georadius_generic,
+ 'PUBSUB NUMSUB': parse_pubsub_numsub,
}
)
@@ -2012,6 +2017,24 @@ class StrictRedis(object):
"""
return self.execute_command('PUBLISH', channel, message)
+ def pubsub_channels(self):
+ """
+ Return a list of channels
+ """
+ return self.execute_command('PUBSUB CHANNELS')
+
+ def pubsub_numpat(self):
+ """
+ Returns the number of subscriptions to patterns
+ """
+ return self.execute_command('PUBSUB NUMPAT')
+
+ def pubsub_numsub(self, *args):
+ """
+ Return a list of the number of subscribers for each channel given in ``*args``
+ """
+ return self.execute_command('PUBSUB NUMSUB', *args)
+
def cluster(self, cluster_arg, *args):
return self.execute_command('CLUSTER %s' % cluster_arg.upper(), *args)
diff --git a/tests/test_pubsub.py b/tests/test_pubsub.py
index dacac83..d0ed4d6 100644
--- a/tests/test_pubsub.py
+++ b/tests/test_pubsub.py
@@ -4,7 +4,7 @@ import time
import redis
from redis.exceptions import ConnectionError
-from redis._compat import basestring, u, unichr
+from redis._compat import basestring, u, unichr, b
from .conftest import r as _redis_client
@@ -398,3 +398,29 @@ class TestPubSubRedisDown(object):
p = r.pubsub()
with pytest.raises(ConnectionError):
p.subscribe('foo')
+
+
+class TestPubSubPubSubSubcommands(object):
+
+ def test_pubsub_channels(self, r):
+ p = r.pubsub(ignore_subscribe_messages=True)
+ p.subscribe('foo', 'bar', 'baz', 'quux')
+ channels = sorted(r.pubsub_channels())
+ assert channels == [b('bar'), b('baz'), b('foo'), b('quux')]
+
+ def test_pubsub_numsub(self, r):
+ p1 = r.pubsub(ignore_subscribe_messages=True)
+ p1.subscribe('foo', 'bar', 'baz')
+ p2 = r.pubsub(ignore_subscribe_messages=True)
+ p2.subscribe('bar', 'baz')
+ p3 = r.pubsub(ignore_subscribe_messages=True)
+ p3.subscribe('baz')
+
+ channels = [(b('foo'), 1), (b('bar'), 2), (b('baz'), 3)]
+ assert channels == r.pubsub_numsub('foo', 'bar', 'baz')
+
+ def test_pubsub_numpat(self, r):
+ p = r.pubsub(ignore_subscribe_messages=True)
+ p.psubscribe('*oo', '*ar', 'b*z')
+ assert r.pubsub_numpat() == 3
+