tts-voice-creator-clone-and.../tests/test_tasks.py
mARTin-B78 4f30ee5c0d fix: keep references to background asyncio tasks
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-04 18:57:20 +02:00

38 lines
1006 B
Python

import asyncio
import gc
import logging
import unittest
from core import tasks
class SpawnTests(unittest.IsolatedAsyncioTestCase):
async def test_survives_gc_and_set_emptied(self):
done = asyncio.Event()
async def work():
await asyncio.sleep(0.01)
done.set()
tasks.spawn(work())
gc.collect()
self.assertEqual(len(tasks._BACKGROUND), 1)
await asyncio.wait_for(done.wait(), 1)
await asyncio.sleep(0)
self.assertEqual(len(tasks._BACKGROUND), 0)
async def test_exception_logged_without_message(self):
async def boom():
raise ValueError("secret-token")
with self.assertLogs("core.tasks", "ERROR") as cm:
tasks.spawn(boom())
await asyncio.sleep(0.02)
self.assertIn("ValueError", cm.output[0])
self.assertNotIn("secret-token", cm.output[0])
self.assertEqual(len(tasks._BACKGROUND), 0)
if __name__ == "__main__":
unittest.main()