From 3317bfd063803f260065d7c3a869ad46a3308275 Mon Sep 17 00:00:00 2001 From: mARTin-B78 Date: Sun, 4 Oct 2026 19:00:06 +0200 Subject: [PATCH] test: make spawn helper test independent of leaked tasks Co-Authored-By: Claude Sonnet 5.5 --- tests/test_tasks.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/tests/test_tasks.py b/tests/test_tasks.py index a6ac502..e325022 100644 --- a/tests/test_tasks.py +++ b/tests/test_tasks.py @@ -8,6 +8,7 @@ from core import tasks class SpawnTests(unittest.IsolatedAsyncioTestCase): async def test_survives_gc_and_set_emptied(self): + base = len(tasks._BACKGROUND) # other tests may leave tasks from closed loops done = asyncio.Event() async def work(): @@ -16,12 +17,14 @@ class SpawnTests(unittest.IsolatedAsyncioTestCase): tasks.spawn(work()) gc.collect() - self.assertEqual(len(tasks._BACKGROUND), 1) + self.assertEqual(len(tasks._BACKGROUND), base + 1) await asyncio.wait_for(done.wait(), 1) await asyncio.sleep(0) - self.assertEqual(len(tasks._BACKGROUND), 0) + self.assertEqual(len(tasks._BACKGROUND), base) async def test_exception_logged_without_message(self): + base = len(tasks._BACKGROUND) + async def boom(): raise ValueError("secret-token") @@ -30,7 +33,7 @@ class SpawnTests(unittest.IsolatedAsyncioTestCase): await asyncio.sleep(0.02) self.assertIn("ValueError", cm.output[0]) self.assertNotIn("secret-token", cm.output[0]) - self.assertEqual(len(tasks._BACKGROUND), 0) + self.assertEqual(len(tasks._BACKGROUND), base) if __name__ == "__main__":