Skip to content

Commit ca55f0b

Browse files
authored
Fix async test (#886)
Test failed to wait for all futures to clean up after them but shut down the event loop while for instance sessions where still closing
1 parent 5c77ce6 commit ca55f0b

File tree

1 file changed

+18
-24
lines changed

1 file changed

+18
-24
lines changed

tests/integration/mixed/test_async_driver.py

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def test_can_create_async_driver_outside_of_loop(uri, auth):
4242
counter = 0
4343
was_full = False
4444

45-
async def return_1(tx):
45+
async def return_1(tx: neo4j.AsyncManagedTransaction) -> None:
4646
nonlocal counter, was_full
4747
res = await tx.run("RETURN 1")
4848

@@ -58,30 +58,24 @@ async def return_1(tx):
5858
await res.consume()
5959
counter -= 1
6060

61-
async def run(driver: neo4j.AsyncDriver):
62-
async with driver:
63-
sessions = []
64-
try:
65-
for i in range(pool_size * 4):
66-
sessions.append(driver.session())
67-
work_loads = (session.execute_read(return_1)
68-
for session in sessions)
69-
await asyncio.gather(*work_loads)
70-
finally:
71-
cancelled = None
72-
for session in sessions:
73-
if not cancelled:
74-
try:
75-
await session.close()
76-
except asyncio.CancelledError as e:
77-
session.cancel()
78-
cancelled = e
79-
else:
80-
session.cancel()
81-
await driver.close()
82-
if cancelled:
83-
raise cancelled
61+
async def session_handler(session: neo4j.AsyncSession) -> None:
62+
nonlocal was_full
63+
try:
64+
async with session:
65+
await session.execute_read(return_1)
66+
except BaseException:
67+
# if we failed, no need to make return_1 stall any longer
68+
was_full = True
69+
raise
8470

71+
async def run(driver_: neo4j.AsyncDriver):
72+
async with driver_:
73+
work_loads = (session_handler(driver_.session())
74+
for _ in range(pool_size * 4))
75+
res = await asyncio.gather(*work_loads, return_exceptions=True)
76+
for r in res:
77+
if isinstance(r, Exception):
78+
raise r
8579

8680
driver = neo4j.AsyncGraphDatabase.driver(
8781
uri, auth=auth, max_connection_pool_size=pool_size

0 commit comments

Comments
 (0)