11import asyncio
22import contextlib
33import dataclasses
4- from typing import Any , Awaitable , Callable , List
4+ import logging
5+ from typing import Awaitable , Callable
56
67import asyncpg
8+ import pytest
79import pytest_pg
810
911import asyncpg_listen
1416class Handler :
1517 def __init__ (self , delay : float = 0 ) -> None :
1618 self .delay = delay
17- self .notifications : List [asyncpg_listen .NotificationOrTimeout ] = []
19+ self .notifications : list [asyncpg_listen .NotificationOrTimeout ] = []
1820
1921 async def handle (self , notification : asyncpg_listen .NotificationOrTimeout ) -> None :
2022 await asyncio .sleep (self .delay )
@@ -27,10 +29,10 @@ async def cancel_and_wait(future: "asyncio.Future[None]") -> None:
2729 await future
2830
2931
30- async def test_two_inactive_channels (pg_11 : pytest_pg .PG ) -> None :
32+ async def test_two_inactive_channels (pg_14 : pytest_pg .PG ) -> None :
3133 handler_1 = Handler ()
3234 handler_2 = Handler ()
33- listener = asyncpg_listen .NotificationListener (asyncpg_listen .connect_func (** dataclasses .asdict (pg_11 )))
35+ listener = asyncpg_listen .NotificationListener (asyncpg_listen .connect_func (** dataclasses .asdict (pg_14 )))
3436 listener_task = asyncio .create_task (
3537 listener .run ({"inactive_1" : handler_1 .handle , "inactive_2" : handler_2 .handle }, notification_timeout = 1 )
3638 )
@@ -42,14 +44,14 @@ async def test_two_inactive_channels(pg_11: pytest_pg.PG) -> None:
4244 assert handler_2 .notifications == [asyncpg_listen .Timeout ("inactive_2" )]
4345
4446
45- async def test_one_active_channel_and_one_passive_channel (pg_11 : pytest_pg .PG ) -> None :
47+ async def test_one_active_channel_and_one_passive_channel (pg_14 : pytest_pg .PG ) -> None :
4648 active_handler = Handler ()
4749 inactive_handler = Handler ()
48- listener = asyncpg_listen .NotificationListener (asyncpg_listen .connect_func (** dataclasses .asdict (pg_11 )))
50+ listener = asyncpg_listen .NotificationListener (asyncpg_listen .connect_func (** dataclasses .asdict (pg_14 )))
4951 listener_task = asyncio .create_task (
5052 listener .run ({"active" : active_handler .handle , "inactive" : inactive_handler .handle }, notification_timeout = 1 )
5153 )
52- connection = await asyncpg .connect (** dataclasses .asdict (pg_11 ))
54+ connection = await asyncpg .connect (** dataclasses .asdict (pg_14 ))
5355 try :
5456 await asyncio .sleep (0.75 )
5557 await connection .execute ("NOTIFY active, '1'" )
@@ -69,16 +71,16 @@ async def test_one_active_channel_and_one_passive_channel(pg_11: pytest_pg.PG) -
6971 ]
7072
7173
72- async def test_two_active_channels (pg_11 : pytest_pg .PG ) -> None :
74+ async def test_two_active_channels (pg_14 : pytest_pg .PG ) -> None :
7375 handler_1 = Handler ()
7476 handler_2 = Handler ()
75- listener = asyncpg_listen .NotificationListener (asyncpg_listen .connect_func (** dataclasses .asdict (pg_11 )))
77+ listener = asyncpg_listen .NotificationListener (asyncpg_listen .connect_func (** dataclasses .asdict (pg_14 )))
7678 listener_task = asyncio .create_task (
7779 listener .run ({"active_1" : handler_1 .handle , "active_2" : handler_2 .handle }, notification_timeout = 1 )
7880 )
7981 await asyncio .sleep (0.1 )
8082
81- connection = await asyncpg .connect (** dataclasses .asdict (pg_11 ))
83+ connection = await asyncpg .connect (** dataclasses .asdict (pg_14 ))
8284 try :
8385 await connection .execute ("NOTIFY active_1, '1'" )
8486 await connection .execute ("NOTIFY active_2, '2'" )
@@ -100,15 +102,15 @@ async def test_two_active_channels(pg_11: pytest_pg.PG) -> None:
100102 ]
101103
102104
103- async def test_listen_policy_last (pg_11 : pytest_pg .PG ) -> None :
105+ async def test_listen_policy_last (pg_14 : pytest_pg .PG ) -> None :
104106 handler = Handler (delay = 0.1 )
105- listener = asyncpg_listen .NotificationListener (asyncpg_listen .connect_func (** dataclasses .asdict (pg_11 )))
107+ listener = asyncpg_listen .NotificationListener (asyncpg_listen .connect_func (** dataclasses .asdict (pg_14 )))
106108 listener_task = asyncio .create_task (
107109 listener .run ({"simple" : handler .handle }, policy = asyncpg_listen .ListenPolicy .LAST , notification_timeout = 1 )
108110 )
109111 await asyncio .sleep (0.1 )
110112
111- connection = await asyncpg .connect (** dataclasses .asdict (pg_11 ))
113+ connection = await asyncpg .connect (** dataclasses .asdict (pg_14 ))
112114 try :
113115 for i in range (10 ):
114116 await connection .execute (f"NOTIFY simple, '{ i } '" )
@@ -124,13 +126,13 @@ async def test_listen_policy_last(pg_11: pytest_pg.PG) -> None:
124126 ]
125127
126128
127- async def test_listen_policy_all (pg_11 : pytest_pg .PG ) -> None :
129+ async def test_listen_policy_all (pg_14 : pytest_pg .PG ) -> None :
128130 handler = Handler (delay = 0.05 )
129- listener = asyncpg_listen .NotificationListener (asyncpg_listen .connect_func (** dataclasses .asdict (pg_11 )))
131+ listener = asyncpg_listen .NotificationListener (asyncpg_listen .connect_func (** dataclasses .asdict (pg_14 )))
130132 listener_task = asyncio .create_task (listener .run ({"simple" : handler .handle }, notification_timeout = 1 ))
131133 await asyncio .sleep (0.1 )
132134
133- connection = await asyncpg .connect (** dataclasses .asdict (pg_11 ))
135+ connection = await asyncpg .connect (** dataclasses .asdict (pg_14 ))
134136 try :
135137 for i in range (10 ):
136138 await connection .execute (f"NOTIFY simple, '{ i } '" )
@@ -171,16 +173,16 @@ async def connect() -> asyncpg.Connection:
171173 assert handler .notifications == []
172174
173175
174- async def test_failing_handler (pg_11 : pytest_pg .PG ) -> None :
176+ async def test_failing_handler (pg_14 : pytest_pg .PG ) -> None :
175177 async def handle (_ : asyncpg_listen .NotificationOrTimeout ) -> None :
176178 raise RuntimeError ("Oops" )
177179
178- listener = asyncpg_listen .NotificationListener (asyncpg_listen .connect_func (** dataclasses .asdict (pg_11 )))
180+ listener = asyncpg_listen .NotificationListener (asyncpg_listen .connect_func (** dataclasses .asdict (pg_14 )))
179181 listener_task = asyncio .create_task (listener .run ({"simple" : handle }, notification_timeout = 1 ))
180182
181183 await asyncio .sleep (0.1 )
182184
183- connection = await asyncpg .connect (** dataclasses .asdict (pg_11 ))
185+ connection = await asyncpg .connect (** dataclasses .asdict (pg_14 ))
184186 try :
185187 await connection .execute ("NOTIFY simple" )
186188 await connection .execute ("NOTIFY simple" )
@@ -198,22 +200,23 @@ async def handle(_: asyncpg_listen.NotificationOrTimeout) -> None:
198200async def test_reconnect (
199201 tcp_proxy : Callable [[int , int ], Awaitable [TcpProxy ]],
200202 unused_port : Callable [[], int ],
201- pg_11 : pytest_pg .PG ,
202- caplog : Any ,
203+ pg_14 : pytest_pg .PG ,
204+ caplog : pytest . LogCaptureFixture ,
203205) -> None :
206+ caplog .set_level (logging .WARNING )
204207 proxy_port = unused_port ()
205208
206209 handler = Handler ()
207- proxy = await tcp_proxy (proxy_port , pg_11 .port )
210+ proxy = await tcp_proxy (proxy_port , pg_14 .port )
208211 listener = asyncpg_listen .NotificationListener (
209- asyncpg_listen .connect_func (** {** (dataclasses .asdict (pg_11 )), ** {"port" : proxy_port }})
212+ asyncpg_listen .connect_func (** {** (dataclasses .asdict (pg_14 )), ** {"port" : proxy_port }})
210213 )
211214
212215 listener_task = asyncio .create_task (listener .run ({"simple" : handler .handle }, notification_timeout = 1 ))
213216
214217 await asyncio .sleep (0.5 )
215218
216- connection = await asyncpg .connect (** dataclasses .asdict (pg_11 ))
219+ connection = await asyncpg .connect (** dataclasses .asdict (pg_14 ))
217220 try :
218221 await connection .execute ("NOTIFY simple, 'before'" )
219222 finally :
@@ -223,7 +226,7 @@ async def test_reconnect(
223226 await proxy .drop_connections ()
224227 await asyncio .sleep (2 )
225228
226- connection = await asyncpg .connect (** dataclasses .asdict (pg_11 ))
229+ connection = await asyncpg .connect (** dataclasses .asdict (pg_14 ))
227230 try :
228231 await connection .execute ("NOTIFY simple, 'after'" )
229232 finally :
@@ -235,4 +238,8 @@ async def test_reconnect(
235238 assert asyncpg_listen .Notification ("simple" , "before" ) in handler .notifications
236239 assert asyncpg_listen .Notification ("simple" , "after" ) in handler .notifications
237240
238- assert any (record for record in caplog .records if "Connection was lost or not established" in record .message )
241+ assert any (
242+ record
243+ for record in caplog .records
244+ if "Connection was lost or not established" in record .message or "Connection was lost" in record .message
245+ )
0 commit comments