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 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
|
import asyncio
import pytest
import aioredis
async def _reader(channel, output, waiter, conn):
await conn.execute('subscribe', channel)
ch = conn.pubsub_channels[channel]
waiter.set_result(conn)
while await ch.wait_message():
msg = await ch.get()
await output.put(msg)
@pytest.mark.run_loop
async def test_publish(create_connection, redis, server, loop):
out = asyncio.Queue(loop=loop)
fut = loop.create_future()
conn = await create_connection(
server.tcp_address, loop=loop)
sub = asyncio.ensure_future(_reader('chan:1', out, fut, conn), loop=loop)
await fut
await redis.publish('chan:1', 'Hello')
msg = await out.get()
assert msg == b'Hello'
sub.cancel()
@pytest.mark.run_loop
async def test_publish_json(create_connection, redis, server, loop):
out = asyncio.Queue(loop=loop)
fut = loop.create_future()
conn = await create_connection(
server.tcp_address, loop=loop)
sub = asyncio.ensure_future(_reader('chan:1', out, fut, conn), loop=loop)
await fut
res = await redis.publish_json('chan:1', {"Hello": "world"})
assert res == 1 # recievers
msg = await out.get()
assert msg == b'{"Hello": "world"}'
sub.cancel()
@pytest.mark.run_loop
async def test_subscribe(redis):
res = await redis.subscribe('chan:1', 'chan:2')
assert redis.in_pubsub == 2
ch1 = redis.channels['chan:1']
ch2 = redis.channels['chan:2']
assert res == [ch1, ch2]
assert ch1.is_pattern is False
assert ch2.is_pattern is False
res = await redis.unsubscribe('chan:1', 'chan:2')
assert res == [[b'unsubscribe', b'chan:1', 1],
[b'unsubscribe', b'chan:2', 0]]
@pytest.mark.parametrize('create_redis', [
pytest.param(aioredis.create_redis_pool, id='pool'),
])
@pytest.mark.run_loop
async def test_subscribe_empty_pool(create_redis, server, loop, _closable):
redis = await create_redis(server.tcp_address, loop=loop)
_closable(redis)
await redis.connection.clear()
res = await redis.subscribe('chan:1', 'chan:2')
assert redis.in_pubsub == 2
ch1 = redis.channels['chan:1']
ch2 = redis.channels['chan:2']
assert res == [ch1, ch2]
assert ch1.is_pattern is False
assert ch2.is_pattern is False
res = await redis.unsubscribe('chan:1', 'chan:2')
assert res == [[b'unsubscribe', b'chan:1', 1],
[b'unsubscribe', b'chan:2', 0]]
@pytest.mark.run_loop
async def test_psubscribe(redis, create_redis, server, loop):
sub = redis
res = await sub.psubscribe('patt:*', 'chan:*')
assert sub.in_pubsub == 2
pat1 = sub.patterns['patt:*']
pat2 = sub.patterns['chan:*']
assert res == [pat1, pat2]
pub = await create_redis(
server.tcp_address, loop=loop)
await pub.publish_json('chan:123', {"Hello": "World"})
res = await pat2.get_json()
assert res == (b'chan:123', {"Hello": "World"})
res = await sub.punsubscribe('patt:*', 'patt:*', 'chan:*')
assert res == [[b'punsubscribe', b'patt:*', 1],
[b'punsubscribe', b'patt:*', 1],
[b'punsubscribe', b'chan:*', 0],
]
@pytest.mark.parametrize('create_redis', [
pytest.param(aioredis.create_redis_pool, id='pool'),
])
@pytest.mark.run_loop
async def test_psubscribe_empty_pool(create_redis, server, loop, _closable):
sub = await create_redis(server.tcp_address, loop=loop)
pub = await create_redis(server.tcp_address, loop=loop)
_closable(sub)
_closable(pub)
await sub.connection.clear()
res = await sub.psubscribe('patt:*', 'chan:*')
assert sub.in_pubsub == 2
pat1 = sub.patterns['patt:*']
pat2 = sub.patterns['chan:*']
assert res == [pat1, pat2]
await pub.publish_json('chan:123', {"Hello": "World"})
res = await pat2.get_json()
assert res == (b'chan:123', {"Hello": "World"})
res = await sub.punsubscribe('patt:*', 'patt:*', 'chan:*')
assert res == [[b'punsubscribe', b'patt:*', 1],
[b'punsubscribe', b'patt:*', 1],
[b'punsubscribe', b'chan:*', 0],
]
@pytest.redis_version(
2, 8, 0, reason='PUBSUB CHANNELS is available since redis>=2.8.0')
@pytest.mark.run_loop
async def test_pubsub_channels(create_redis, server, loop):
redis = await create_redis(
server.tcp_address, loop=loop)
res = await redis.pubsub_channels()
assert res == []
res = await redis.pubsub_channels('chan:*')
assert res == []
sub = await create_redis(
server.tcp_address, loop=loop)
await sub.subscribe('chan:1')
res = await redis.pubsub_channels()
assert res == [b'chan:1']
res = await redis.pubsub_channels('ch*')
assert res == [b'chan:1']
await sub.unsubscribe('chan:1')
await sub.psubscribe('chan:*')
res = await redis.pubsub_channels()
assert res == []
@pytest.redis_version(
2, 8, 0, reason='PUBSUB NUMSUB is available since redis>=2.8.0')
@pytest.mark.run_loop
async def test_pubsub_numsub(create_redis, server, loop):
redis = await create_redis(
server.tcp_address, loop=loop)
res = await redis.pubsub_numsub()
assert res == {}
res = await redis.pubsub_numsub('chan:1')
assert res == {b'chan:1': 0}
sub = await create_redis(
server.tcp_address, loop=loop)
await sub.subscribe('chan:1')
res = await redis.pubsub_numsub()
assert res == {}
res = await redis.pubsub_numsub('chan:1')
assert res == {b'chan:1': 1}
res = await redis.pubsub_numsub('chan:2')
assert res == {b'chan:2': 0}
res = await redis.pubsub_numsub('chan:1', 'chan:2')
assert res == {b'chan:1': 1, b'chan:2': 0}
await sub.unsubscribe('chan:1')
await sub.psubscribe('chan:*')
res = await redis.pubsub_numsub()
assert res == {}
@pytest.redis_version(
2, 8, 0, reason='PUBSUB NUMPAT is available since redis>=2.8.0')
@pytest.mark.run_loop
async def test_pubsub_numpat(create_redis, server, loop, redis):
sub = await create_redis(
server.tcp_address, loop=loop)
res = await redis.pubsub_numpat()
assert res == 0
await sub.subscribe('chan:1')
res = await redis.pubsub_numpat()
assert res == 0
await sub.psubscribe('chan:*')
res = await redis.pubsub_numpat()
assert res == 1
@pytest.mark.run_loop
async def test_close_pubsub_channels(redis, loop):
ch, = await redis.subscribe('chan:1')
async def waiter(ch):
assert not await ch.wait_message()
tsk = asyncio.ensure_future(waiter(ch), loop=loop)
redis.close()
await redis.wait_closed()
await tsk
@pytest.mark.run_loop
async def test_close_pubsub_patterns(redis, loop):
ch, = await redis.psubscribe('chan:*')
async def waiter(ch):
assert not await ch.wait_message()
tsk = asyncio.ensure_future(waiter(ch), loop=loop)
redis.close()
await redis.wait_closed()
await tsk
@pytest.mark.run_loop
async def test_close_cancelled_pubsub_channel(redis, loop):
ch, = await redis.subscribe('chan:1')
async def waiter(ch):
with pytest.raises(asyncio.CancelledError):
await ch.wait_message()
tsk = asyncio.ensure_future(waiter(ch), loop=loop)
await asyncio.sleep(0, loop=loop)
tsk.cancel()
@pytest.mark.run_loop
async def test_channel_get_after_close(create_redis, loop, server):
sub = await create_redis(
server.tcp_address, loop=loop)
pub = await create_redis(
server.tcp_address, loop=loop)
ch, = await sub.subscribe('chan:1')
await pub.publish('chan:1', 'message')
assert await ch.get() == b'message'
loop.call_soon(sub.close)
assert await ch.get() is None
with pytest.raises(aioredis.ChannelClosedError):
assert await ch.get()
@pytest.mark.run_loop
async def test_subscribe_concurrency(create_redis, server, loop):
sub = await create_redis(
server.tcp_address, loop=loop)
pub = await create_redis(
server.tcp_address, loop=loop)
async def subscribe(*args):
return await sub.subscribe(*args)
async def publish(*args):
await asyncio.sleep(0, loop=loop)
return await pub.publish(*args)
res = await asyncio.gather(
subscribe('channel:0'),
publish('channel:0', 'Hello'),
subscribe('channel:1'),
loop=loop)
(ch1,), subs, (ch2,) = res
assert ch1.name == b'channel:0'
assert subs == 1
assert ch2.name == b'channel:1'
@pytest.redis_version(
3, 2, 0, reason='PUBSUB PING is available since redis>=3.2.0')
@pytest.mark.run_loop
async def test_pubsub_ping(redis):
await redis.subscribe('chan:1', 'chan:2')
res = await redis.ping()
assert res == b'PONG'
res = await redis.ping('Hello')
assert res == b'Hello'
res = await redis.ping('Hello', encoding='utf-8')
assert res == 'Hello'
await redis.unsubscribe('chan:1', 'chan:2')
@pytest.mark.run_loop
async def test_pubsub_channel_iter(create_redis, server, loop):
sub = await create_redis(server.tcp_address, loop=loop)
pub = await create_redis(server.tcp_address, loop=loop)
ch, = await sub.subscribe('chan:1')
async def coro(ch):
lst = []
async for msg in ch.iter():
lst.append(msg)
return lst
tsk = asyncio.ensure_future(coro(ch), loop=loop)
await pub.publish_json('chan:1', {'Hello': 'World'})
await pub.publish_json('chan:1', ['message'])
await asyncio.sleep(0, loop=loop)
ch.close()
assert await tsk == [b'{"Hello": "World"}', b'["message"]']
|