|
16 | 16 | # limitations under the License.
|
17 | 17 |
|
18 | 18 |
|
19 |
| -from unittest.mock import Mock |
20 |
| - |
21 | 19 | import pytest
|
22 | 20 |
|
23 | 21 | from neo4j import (
|
@@ -185,6 +183,37 @@ def break_connection():
|
185 | 183 | assert cx2 in pool.connections[cx2.addr]
|
186 | 184 |
|
187 | 185 |
|
| 186 | +@mark_async_test |
| 187 | +async def test_does_not_close_stale_connections_in_use(opener): |
| 188 | + pool = AsyncNeo4jPool( |
| 189 | + opener, PoolConfig(), WorkspaceConfig(), ROUTER_ADDRESS |
| 190 | + ) |
| 191 | + cx1 = await pool.acquire(READ_ACCESS, 30, "test_db", None) |
| 192 | + assert cx1 in pool.connections[cx1.addr] |
| 193 | + # simulate connection going stale (e.g. exceeding) while being in use |
| 194 | + cx1.stale.return_value = True |
| 195 | + cx2 = await pool.acquire(READ_ACCESS, 30, "test_db", None) |
| 196 | + await pool.release(cx2) |
| 197 | + cx1.close.assert_not_called() |
| 198 | + assert cx2 is not cx1 |
| 199 | + assert cx2.addr == cx1.addr |
| 200 | + assert cx1 in pool.connections[cx1.addr] |
| 201 | + assert cx2 in pool.connections[cx2.addr] |
| 202 | + |
| 203 | + await pool.release(cx1) |
| 204 | + # now that cx1 is back in the pool and still stale, |
| 205 | + # it should be closed when trying to acquire the next connection |
| 206 | + cx1.close.assert_not_called() |
| 207 | + |
| 208 | + cx3 = await pool.acquire(READ_ACCESS, 30, "test_db", None) |
| 209 | + await pool.release(cx3) |
| 210 | + cx1.close.assert_called_once() |
| 211 | + assert cx2 is cx3 |
| 212 | + assert cx3.addr == cx1.addr |
| 213 | + assert cx1 not in pool.connections[cx1.addr] |
| 214 | + assert cx3 in pool.connections[cx2.addr] |
| 215 | + |
| 216 | + |
188 | 217 | @mark_async_test
|
189 | 218 | async def test_release_resets_connections(opener):
|
190 | 219 | pool = AsyncNeo4jPool(
|
|
0 commit comments