1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
|
from __future__ import with_statement
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')
|