@@ -185,6 +185,37 @@ def break_connection():
185185 assert cx2 in pool .connections [cx2 .addr ]
186186
187187
188+ @mark_async_test
189+ async def test_does_not_close_stale_connections_in_use (opener ):
190+ pool = AsyncNeo4jPool (
191+ opener , PoolConfig (), WorkspaceConfig (), ROUTER_ADDRESS
192+ )
193+ cx1 = await pool .acquire (READ_ACCESS , 30 , "test_db" , None )
194+ assert cx1 in pool .connections [cx1 .addr ]
195+ # simulate connection going stale (e.g. exceeding) while being in use
196+ cx1 .stale .return_value = True
197+ cx2 = await pool .acquire (READ_ACCESS , 30 , "test_db" , None )
198+ await pool .release (cx2 )
199+ cx1 .close .assert_not_called ()
200+ assert cx2 is not cx1
201+ assert cx2 .addr == cx1 .addr
202+ assert cx1 in pool .connections [cx1 .addr ]
203+ assert cx2 in pool .connections [cx2 .addr ]
204+
205+ await pool .release (cx1 )
206+ # now that cx1 is back in the pool and still stale,
207+ # it should be closed when trying to acquire the next connection
208+ cx1 .close .assert_not_called ()
209+
210+ cx3 = await pool .acquire (READ_ACCESS , 30 , "test_db" , None )
211+ await pool .release (cx3 )
212+ cx1 .close .assert_called_once ()
213+ assert cx2 is cx3
214+ assert cx3 .addr == cx1 .addr
215+ assert cx1 not in pool .connections [cx1 .addr ]
216+ assert cx3 in pool .connections [cx2 .addr ]
217+
218+
188219@mark_async_test
189220async def test_release_resets_connections (opener ):
190221 pool = AsyncNeo4jPool (
0 commit comments