forked from MagicStack/asyncpg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_listeners.py
76 lines (54 loc) · 2.64 KB
/
test_listeners.py
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
# Copyright (C) 2016-present the ayncpg authors and contributors
# <see AUTHORS file>
#
# This module is part of asyncpg and is released under
# the Apache 2.0 License: http://www.apache.org/licenses/LICENSE-2.0
import asyncio
from asyncpg import _testbase as tb
class TestListeners(tb.ClusterTestCase):
async def test_listen_01(self):
async with self.create_pool(database='postgres') as pool:
async with pool.acquire() as con:
q1 = asyncio.Queue(loop=self.loop)
q2 = asyncio.Queue(loop=self.loop)
def listener1(*args):
q1.put_nowait(args)
def listener2(*args):
q2.put_nowait(args)
await con.add_listener('test', listener1)
await con.add_listener('test', listener2)
await con.execute("NOTIFY test, 'aaaa'")
self.assertEqual(
await q1.get(),
(con, con.get_server_pid(), 'test', 'aaaa'))
self.assertEqual(
await q2.get(),
(con, con.get_server_pid(), 'test', 'aaaa'))
await con.remove_listener('test', listener2)
await con.execute("NOTIFY test, 'aaaa'")
self.assertEqual(
await q1.get(),
(con, con.get_server_pid(), 'test', 'aaaa'))
with self.assertRaises(asyncio.TimeoutError):
await asyncio.wait_for(q2.get(),
timeout=0.05, loop=self.loop)
await con.reset()
await con.remove_listener('test', listener1)
await con.execute("NOTIFY test, 'aaaa'")
with self.assertRaises(asyncio.TimeoutError):
await asyncio.wait_for(q1.get(),
timeout=0.05, loop=self.loop)
with self.assertRaises(asyncio.TimeoutError):
await asyncio.wait_for(q2.get(),
timeout=0.05, loop=self.loop)
async def test_listen_02(self):
async with self.create_pool(database='postgres') as pool:
async with pool.acquire() as con1, pool.acquire() as con2:
q1 = asyncio.Queue(loop=self.loop)
def listener1(*args):
q1.put_nowait(args)
await con1.add_listener('ipc', listener1)
await con2.execute("NOTIFY ipc, 'hello'")
self.assertEqual(
await q1.get(),
(con1, con2.get_server_pid(), 'ipc', 'hello'))