test_conftest_query_cancellations.py 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. """The test harness waits for asyncpg's query cancellations before it closes
  2. a test's engines.
  3. A test that cancels a task mid-query leaves asyncpg's own cancel task behind;
  4. if the test ends first, it is still pending when the loop closes and shows up
  5. as an unraisable "Connection._cancel" warning in some later test on PostgreSQL.
  6. """
  7. import asyncio
  8. from backend.tests.conftest import _finish_query_cancellations
  9. class Connection:
  10. """Stands in for asyncpg's: the helper recognises the task by this name."""
  11. async def _cancel(self) -> None:
  12. await asyncio.sleep(0.05)
  13. async def test_a_pending_cancellation_is_finished_first():
  14. task = asyncio.get_running_loop().create_task(Connection()._cancel())
  15. await _finish_query_cancellations()
  16. assert task.done()
  17. async def test_other_tasks_are_left_alone():
  18. async def unrelated() -> None:
  19. await asyncio.sleep(10)
  20. task = asyncio.get_running_loop().create_task(unrelated())
  21. try:
  22. await asyncio.wait_for(_finish_query_cancellations(), timeout=1)
  23. assert not task.done()
  24. finally:
  25. task.cancel()