This commit is contained in:
Your Name
2026-07-28 09:00:19 +08:00
parent 8ba13a8ff9
commit 153db97dc7
14 changed files with 793 additions and 75 deletions
+60 -2
View File
@@ -17,8 +17,18 @@ from rpa_engine import batch_start as batch_start_module
class BatchStartQueueTests(unittest.IsolatedAsyncioTestCase):
def _make_queue(self, handler, *, concurrency: int = 2) -> BatchStartQueue:
queue = BatchStartQueue(handler, concurrency=concurrency)
def _make_queue(
self,
handler,
*,
concurrency: int = 2,
timeout_seconds: float | None = None,
) -> BatchStartQueue:
queue = BatchStartQueue(
handler,
concurrency=concurrency,
timeout_seconds=timeout_seconds,
)
self.addAsyncCleanup(queue.stop)
return queue
@@ -168,6 +178,54 @@ class BatchStartQueueTests(unittest.IsolatedAsyncioTestCase):
self.assertEqual(by_account[32]["status"], "submitted")
self.assertEqual(by_account[33]["status"], "submitted")
async def test_two_timeouts_release_both_workers_for_following_accounts(self):
never_release = asyncio.Event()
calls: list[int] = []
async def handler(account_id: int) -> dict:
calls.append(account_id)
if account_id in (71, 72):
await never_release.wait()
return {"message": f"started-{account_id}"}
queue = self._make_queue(
handler,
concurrency=2,
timeout_seconds=0.02,
)
with patch.object(batch_start_module.logger, "warning"):
submitted = await queue.submit([71, 72, 73, 74])
completed = await self._wait_for_complete(
queue,
submitted["batch_id"],
)
self.assertEqual(calls, [71, 72, 73, 74])
self.assertEqual(completed["failed_count"], 2)
self.assertEqual(completed["submitted_count"], 2)
by_account = {item["account_id"]: item for item in completed["items"]}
self.assertIn("已跳过并继续处理后续账号", by_account[71]["message"])
self.assertIn("已跳过并继续处理后续账号", by_account[72]["message"])
self.assertEqual(by_account[73]["status"], "submitted")
self.assertEqual(by_account[74]["status"], "submitted")
async def test_handler_timeout_error_keeps_its_original_detail(self):
async def handler(_account_id: int) -> dict:
raise asyncio.TimeoutError("upstream request timed out")
queue = self._make_queue(
handler,
concurrency=1,
timeout_seconds=10,
)
with patch.object(batch_start_module.logger, "exception"):
submitted = await queue.submit([75])
completed = await self._wait_for_complete(queue, submitted["batch_id"])
item = completed["items"][0]
self.assertEqual(item["status"], "failed")
self.assertEqual(item["message"], "upstream request timed out")
async def test_failed_account_can_be_submitted_again(self):
attempts = 0