test(赛马插件): 修复模拟测试中的竞态条件

在测试模拟赛马时,添加对 CancelledError 的异常处理,防止任务取消导致测试挂起。同时修复 stop_race 方法的模拟,确保测试清理时能正确移除活动任务。
This commit is contained in:
2026-04-04 21:50:53 +08:00
parent dc5ee6755b
commit 488c6fa6f6

View File

@@ -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