test: make spawn helper test independent of leaked tasks
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
2f4c8529e9
commit
3317bfd063
@ -8,6 +8,7 @@ from core import tasks
|
|||||||
|
|
||||||
class SpawnTests(unittest.IsolatedAsyncioTestCase):
|
class SpawnTests(unittest.IsolatedAsyncioTestCase):
|
||||||
async def test_survives_gc_and_set_emptied(self):
|
async def test_survives_gc_and_set_emptied(self):
|
||||||
|
base = len(tasks._BACKGROUND) # other tests may leave tasks from closed loops
|
||||||
done = asyncio.Event()
|
done = asyncio.Event()
|
||||||
|
|
||||||
async def work():
|
async def work():
|
||||||
@ -16,12 +17,14 @@ class SpawnTests(unittest.IsolatedAsyncioTestCase):
|
|||||||
|
|
||||||
tasks.spawn(work())
|
tasks.spawn(work())
|
||||||
gc.collect()
|
gc.collect()
|
||||||
self.assertEqual(len(tasks._BACKGROUND), 1)
|
self.assertEqual(len(tasks._BACKGROUND), base + 1)
|
||||||
await asyncio.wait_for(done.wait(), 1)
|
await asyncio.wait_for(done.wait(), 1)
|
||||||
await asyncio.sleep(0)
|
await asyncio.sleep(0)
|
||||||
self.assertEqual(len(tasks._BACKGROUND), 0)
|
self.assertEqual(len(tasks._BACKGROUND), base)
|
||||||
|
|
||||||
async def test_exception_logged_without_message(self):
|
async def test_exception_logged_without_message(self):
|
||||||
|
base = len(tasks._BACKGROUND)
|
||||||
|
|
||||||
async def boom():
|
async def boom():
|
||||||
raise ValueError("secret-token")
|
raise ValueError("secret-token")
|
||||||
|
|
||||||
@ -30,7 +33,7 @@ class SpawnTests(unittest.IsolatedAsyncioTestCase):
|
|||||||
await asyncio.sleep(0.02)
|
await asyncio.sleep(0.02)
|
||||||
self.assertIn("ValueError", cm.output[0])
|
self.assertIn("ValueError", cm.output[0])
|
||||||
self.assertNotIn("secret-token", 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__":
|
if __name__ == "__main__":
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user