This commit is contained in:
Your Name
2026-07-30 10:06:53 +08:00
parent 8f68af1c2c
commit 3fc94c4a89
9 changed files with 275 additions and 10 deletions
+36
View File
@@ -87,6 +87,42 @@ class BatchStartQueueTests(unittest.IsolatedAsyncioTestCase):
self.assertEqual(maximum_active, 2)
self.assertEqual(active, 0)
async def test_default_concurrency_admits_more_than_two_accounts(self):
with patch.dict(os.environ, {}, clear=False):
os.environ.pop("KEFU_BATCH_START_CONCURRENCY", None)
queue = BatchStartQueue(lambda _account_id: None)
self.assertEqual(queue.concurrency, batch_start_module.DEFAULT_CONCURRENCY)
self.assertGreaterEqual(queue.concurrency, 4)
async def test_dead_worker_is_replaced_so_width_never_shrinks(self):
"""One crashed worker must not permanently narrow the queue.
Width used to be restored only when every worker had exited, so a
single unexpected worker death left later batches crawling through the
survivors until the process restarted.
"""
async def handler(account_id: int) -> dict:
return {"message": f"started-{account_id}"}
queue = self._make_queue(handler, concurrency=3)
first = await queue.submit([91])
await self._wait_for_complete(queue, first["batch_id"])
self.assertEqual(len(queue._workers), 3)
casualty = queue._workers[0]
casualty.cancel()
await asyncio.gather(casualty, return_exceptions=True)
second = await queue.submit([92])
await self._wait_for_complete(queue, second["batch_id"])
self.assertEqual(len(queue._workers), 3)
self.assertNotIn(casualty, queue._workers)
self.assertTrue(all(not task.done() for task in queue._workers))
names = [task.get_name() for task in queue._workers]
self.assertEqual(len(set(names)), 3)
async def test_submit_returns_while_handler_is_blocked(self):
handler_started = asyncio.Event()
release_handler = asyncio.Event()