MCPcopy
hub / github.com/psycopg/psycopg / test_notify_handlers

Function test_notify_handlers

tests/test_notify_async.py:15–51  ·  view source on GitHub ↗
(aconn)

Source from the content-addressed store, hash-verified

13
14
15async def test_notify_handlers(aconn):
16 nots1 = []
17 nots2 = []
18
19 def cb1(n):
20 nots1.append(n)
21
22 aconn.add_notify_handler(cb1)
23 aconn.add_notify_handler(lambda n: nots2.append(n))
24
25 await aconn.set_autocommit(True)
26 await aconn.execute("listen foo")
27 await aconn.execute("notify foo, 'n1'")
28
29 assert len(nots1) == 1
30 n = nots1[0]
31 assert n.channel == "foo"
32 assert n.payload == "n1"
33 assert n.pid == aconn.pgconn.backend_pid
34
35 assert len(nots2) == 1
36 assert nots2[0] == nots1[0]
37
38 aconn.remove_notify_handler(cb1)
39 await aconn.execute("notify foo, 'n2'")
40
41 assert len(nots1) == 1
42 assert len(nots2) == 2
43 n = nots2[1]
44 assert isinstance(n, Notify)
45 assert n.channel == "foo"
46 assert n.payload == "n2"
47 assert n.pid == aconn.pgconn.backend_pid
48 assert hash(n)
49
50 with pytest.raises(ValueError):
51 aconn.remove_notify_handler(cb1)
52
53
54@pytest.mark.slow

Callers

nothing calls this directly

Calls 4

add_notify_handlerMethod · 0.80
remove_notify_handlerMethod · 0.80
set_autocommitMethod · 0.45
executeMethod · 0.45

Tested by

no test coverage detected