From 488c6fa6f6f850268196734bdd777e164c5bdc66 Mon Sep 17 00:00:00 2001 From: "Mr.Xia" <1424473282@qq.com> Date: Sat, 4 Apr 2026 21:50:53 +0800 Subject: [PATCH] =?UTF-8?q?test(=E8=B5=9B=E9=A9=AC=E6=8F=92=E4=BB=B6):=20?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=A8=A1=E6=8B=9F=E6=B5=8B=E8=AF=95=E4=B8=AD?= =?UTF-8?q?=E7=9A=84=E7=AB=9E=E6=80=81=E6=9D=A1=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 在测试模拟赛马时,添加对 CancelledError 的异常处理,防止任务取消导致测试挂起。同时修复 stop_race 方法的模拟,确保测试清理时能正确移除活动任务。 --- danding_bot/plugins/group_horse_racing/test_commands.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/danding_bot/plugins/group_horse_racing/test_commands.py b/danding_bot/plugins/group_horse_racing/test_commands.py index dea227a..efaddb3 100644 --- a/danding_bot/plugins/group_horse_racing/test_commands.py +++ b/danding_bot/plugins/group_horse_racing/test_commands.py @@ -214,6 +214,7 @@ async def handle_test_simulate_race(bot: Bot, event: Event): original_points_service = commands_mod.points_service original_message_service = commands_mod.message_service original_tick_interval = commands_mod.config.RACE_TICK_INTERVAL + original_stop_race = commands_mod.race_engine.stop_race fake_room_store = _InMemoryRoomStore() @@ -230,6 +231,7 @@ async def handle_test_simulate_race(bot: Bot, event: Event): commands_mod.points_service = fake_points_service commands_mod.message_service = fake_message_service commands_mod.config.RACE_TICK_INTERVAL = 0 + commands_mod.race_engine.stop_race = lambda _scope: commands_mod.race_engine.active_tasks.pop(_scope, None) room = fake_room_store.create_room(scope) horse_names = _generate_random_horse_names(8) @@ -304,6 +306,9 @@ async def handle_test_simulate_race(bot: Bot, event: Event): except asyncio.TimeoutError: ticks = room.tick_count if room else 0 await test_simulate_race_cmd.finish(f"完全模拟失败:超时未完成(当前回合:{ticks})") + except asyncio.CancelledError: + ticks = room.tick_count if room else 0 + await test_simulate_race_cmd.finish(f"完全模拟失败:任务被取消(当前回合:{ticks})") except Exception as e: tail = "\n".join(traceback.format_exc().splitlines()[-8:]) await test_simulate_race_cmd.finish(f"完全模拟异常:{type(e).__name__}: {e}\n{tail}") @@ -314,3 +319,4 @@ async def handle_test_simulate_race(bot: Bot, event: Event): commands_mod.points_service = original_points_service commands_mod.message_service = original_message_service commands_mod.config.RACE_TICK_INTERVAL = original_tick_interval + commands_mod.race_engine.stop_race = original_stop_race