Skip to content

Commit

Permalink
add cancel cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
nerdai committed Jan 23, 2025
1 parent 3cdff5d commit ab03506
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions tests/smoke_tests/test_smoke_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@
pytestmark = pytest.mark.asyncio(loop_scope="module")


def cleanup_cancelled_tasks(event_loop: asyncio.AbstractEventLoop) -> None:
"""This helper function cleans up cancelled tasks."""
remaining_tasks = asyncio.all_tasks()
for t in remaining_tasks:
t.cancel()
if not event_loop.is_running():
event_loop.run_forever() # give time for cancelled tasks to clear


def assert_on_done_task(task: asyncio.Task, event_loop: asyncio.AbstractEventLoop) -> None:
"""This function takes a done task and makes assert if a result was returned.
Expand All @@ -36,8 +45,7 @@ def assert_on_done_task(task: asyncio.Task, event_loop: asyncio.AbstractEventLoo
assert len(server_errors) == 0, f"Server metrics check failed. Errors: {server_errors}"
assert len(client_errors) == 0, f"Client metrics check failed. Errors: {client_errors}"
except asyncio.exceptions.CancelledError:
if not event_loop.is_running():
event_loop.run_forever()
cleanup_cancelled_tasks(event_loop)


# @pytest.mark.smoketest
Expand Down Expand Up @@ -218,6 +226,7 @@ async def test_client_level_dp_breast_cancer(tolerance: float) -> None:
try:
await task
except asyncio.exceptions.TimeoutError:
cleanup_cancelled_tasks(event_loop)
pytest.fail("Smoke test failed due to Timeout Error.")
assert_on_done_task(task, event_loop)

Expand Down

0 comments on commit ab03506

Please sign in to comment.