MCPcopy
hub / github.com/psycopg/psycopg / test_notify

Function test_notify

tests/test_notify.py:59–98  ·  view source on GitHub ↗
(conn_cls, conn, dsn)

Source from the content-addressed store, hash-verified

57@pytest.mark.slow
58@pytest.mark.timing
59def test_notify(conn_cls, conn, dsn):
60 npid = None
61
62 def notifier():
63 with conn_cls.connect(dsn, autocommit=True) as nconn:
64 nonlocal npid
65 npid = nconn.pgconn.backend_pid
66
67 sleep(0.25)
68 nconn.execute("notify foo, '1'")
69 sleep(0.25)
70 nconn.execute("notify foo, '2'")
71
72 def receiver():
73 conn.set_autocommit(True)
74 cur = conn.cursor()
75 cur.execute("listen foo")
76 gen = conn.notifies()
77 for n in gen:
78 ns.append((n, time()))
79 if len(ns) >= 2:
80 gen.close()
81
82 ns: list[tuple[Notify, float]] = []
83 t0 = time()
84 workers = [spawn(notifier), spawn(receiver)]
85 gather(*workers)
86 assert len(ns) == 2
87
88 n, t1 = ns[0]
89 assert n.pid == npid
90 assert n.channel == "foo"
91 assert n.payload == "1"
92 assert t1 - t0 == pytest.approx(0.25, abs=0.05)
93
94 n, t1 = ns[1]
95 assert n.pid == npid
96 assert n.channel == "foo"
97 assert n.payload == "2"
98 assert t1 - t0 == pytest.approx(0.5, abs=0.05)
99
100
101@pytest.mark.slow

Callers

nothing calls this directly

Calls 2

spawnFunction · 0.70
gatherFunction · 0.70

Tested by

no test coverage detected