summaryrefslogtreecommitdiff
path: root/tests/test_pubsub.py
diff options
context:
space:
mode:
authorandy <andy@whiskeymedia.com>2013-06-06 11:46:15 -0700
committerandy <andy@whiskeymedia.com>2013-06-06 11:46:15 -0700
commit533bc4a0b37598b1e25a35892659e5f61ce54d29 (patch)
tree0dd0ab45bca898f5b82fed03cd496825f533fd7e /tests/test_pubsub.py
parent6a69aa50797f95b8fd426651ac0b27d7718036d9 (diff)
downloadredis-py-533bc4a0b37598b1e25a35892659e5f61ce54d29.tar.gz
pubsub tests
Diffstat (limited to 'tests/test_pubsub.py')
-rw-r--r--tests/test_pubsub.py95
1 files changed, 95 insertions, 0 deletions
diff --git a/tests/test_pubsub.py b/tests/test_pubsub.py
new file mode 100644
index 0000000..0756345
--- /dev/null
+++ b/tests/test_pubsub.py
@@ -0,0 +1,95 @@
+import pytest
+
+import redis
+from redis._compat import b, next
+from redis.exceptions import ConnectionError
+
+
+class TestPubSub(object):
+
+ def test_channel_subscribe(self, r):
+ p = r.pubsub()
+
+ # subscribe doesn't return anything
+ assert p.subscribe('foo') is None
+
+ # send a message
+ assert r.publish('foo', 'hello foo') == 1
+
+ # there should be now 2 messages in the buffer, a subscribe and the
+ # one we just published
+ assert next(p.listen()) == \
+ {
+ 'type': 'subscribe',
+ 'pattern': None,
+ 'channel': 'foo',
+ 'data': 1
+ }
+
+ assert next(p.listen()) == \
+ {
+ 'type': 'message',
+ 'pattern': None,
+ 'channel': 'foo',
+ 'data': b('hello foo')
+ }
+
+ # unsubscribe
+ assert p.unsubscribe('foo') is None
+
+ # unsubscribe message should be in the buffer
+ assert next(p.listen()) == \
+ {
+ 'type': 'unsubscribe',
+ 'pattern': None,
+ 'channel': 'foo',
+ 'data': 0
+ }
+
+ def test_pattern_subscribe(self, r):
+ p = r.pubsub()
+
+ # psubscribe doesn't return anything
+ assert p.psubscribe('f*') is None
+
+ # send a message
+ assert r.publish('foo', 'hello foo') == 1
+
+ # there should be now 2 messages in the buffer, a subscribe and the
+ # one we just published
+ assert next(p.listen()) == \
+ {
+ 'type': 'psubscribe',
+ 'pattern': None,
+ 'channel': 'f*',
+ 'data': 1
+ }
+
+ assert next(p.listen()) == \
+ {
+ 'type': 'pmessage',
+ 'pattern': 'f*',
+ 'channel': 'foo',
+ 'data': b('hello foo')
+ }
+
+ # unsubscribe
+ assert p.punsubscribe('f*') is None
+
+ # unsubscribe message should be in the buffer
+ assert next(p.listen()) == \
+ {
+ 'type': 'punsubscribe',
+ 'pattern': None,
+ 'channel': 'f*',
+ 'data': 0
+ }
+
+
+class TestPubSubRedisDown(object):
+
+ def test_channel_subscribe(self, r):
+ r = redis.Redis(host='localhost', port=6390)
+ p = r.pubsub()
+ with pytest.raises(ConnectionError):
+ p.subscribe('foo')