fix(test): 移除测试赛马模拟中的锁机制以避免死锁

重构测试模拟赛马流程,移除房间锁获取逻辑,改为直接清理现有房间状态。
引入沙盒环境构建提示信息,提升测试过程的可观测性。
This commit is contained in:
2026-04-04 21:41:42 +08:00
parent e5d0db268b
commit dc5ee6755b

View File

@@ -205,24 +205,17 @@ async def handle_test_simulate_race(bot: Bot, event: Event):
await test_simulate_race_cmd.send("收到:测试模拟赛马,开始执行完全模拟(无真实积分/数据库副作用)")
scope = get_scope(event)
lock = room_store.get_lock(scope)
try:
await asyncio.wait_for(lock.acquire(), timeout=3)
except TimeoutError:
await test_simulate_race_cmd.finish("完全模拟失败:房间锁被占用(可能有卡住的赛马/测试任务),请稍后重试或先重启 Bot")
return
try:
race_engine.stop_race(scope)
room_store.delete_room(scope)
finally:
lock.release()
except Exception:
pass
original_room_store = commands_mod.room_store
original_points_service = commands_mod.points_service
original_message_service = commands_mod.message_service
original_tick_interval = commands_mod.config.RACE_TICK_INTERVAL
fake_room_store = _InMemoryRoomStore()
fake_points_service = _InMemoryPointsService()
fake_message_service = _NoopMessageService()
@@ -232,6 +225,7 @@ async def handle_test_simulate_race(bot: Bot, event: Event):
room = None
try:
await test_simulate_race_cmd.send("阶段:构建沙盒环境与参赛数据")
commands_mod.room_store = fake_room_store
commands_mod.points_service = fake_points_service
commands_mod.message_service = fake_message_service
@@ -252,6 +246,7 @@ async def handle_test_simulate_race(bot: Bot, event: Event):
for horse in room.horses.values():
horse.state = HorseState.RACING
await test_simulate_race_cmd.send("阶段:执行赛程(后台任务)")
start_task = asyncio.create_task(commands_mod.run_race_with_settlement(fake_bot, room, scope))
commands_mod.race_engine.register_task(scope, start_task)
await asyncio.wait_for(start_task, timeout=15)