| 57 | @pytest.mark.slow |
| 58 | @pytest.mark.timing |
| 59 | def 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 |