fix(test): 移除测试赛马模拟中的锁机制以避免死锁
重构测试模拟赛马流程,移除房间锁获取逻辑,改为直接清理现有房间状态。 引入沙盒环境构建提示信息,提升测试过程的可观测性。
This commit is contained in:
@@ -205,24 +205,17 @@ async def handle_test_simulate_race(bot: Bot, event: Event):
|
|||||||
await test_simulate_race_cmd.send("收到:测试模拟赛马,开始执行完全模拟(无真实积分/数据库副作用)")
|
await test_simulate_race_cmd.send("收到:测试模拟赛马,开始执行完全模拟(无真实积分/数据库副作用)")
|
||||||
|
|
||||||
scope = get_scope(event)
|
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:
|
try:
|
||||||
race_engine.stop_race(scope)
|
race_engine.stop_race(scope)
|
||||||
room_store.delete_room(scope)
|
room_store.delete_room(scope)
|
||||||
finally:
|
except Exception:
|
||||||
lock.release()
|
pass
|
||||||
|
|
||||||
original_room_store = commands_mod.room_store
|
original_room_store = commands_mod.room_store
|
||||||
original_points_service = commands_mod.points_service
|
original_points_service = commands_mod.points_service
|
||||||
original_message_service = commands_mod.message_service
|
original_message_service = commands_mod.message_service
|
||||||
original_tick_interval = commands_mod.config.RACE_TICK_INTERVAL
|
original_tick_interval = commands_mod.config.RACE_TICK_INTERVAL
|
||||||
|
|
||||||
|
|
||||||
fake_room_store = _InMemoryRoomStore()
|
fake_room_store = _InMemoryRoomStore()
|
||||||
fake_points_service = _InMemoryPointsService()
|
fake_points_service = _InMemoryPointsService()
|
||||||
fake_message_service = _NoopMessageService()
|
fake_message_service = _NoopMessageService()
|
||||||
@@ -232,6 +225,7 @@ async def handle_test_simulate_race(bot: Bot, event: Event):
|
|||||||
room = None
|
room = None
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
await test_simulate_race_cmd.send("阶段:构建沙盒环境与参赛数据")
|
||||||
commands_mod.room_store = fake_room_store
|
commands_mod.room_store = fake_room_store
|
||||||
commands_mod.points_service = fake_points_service
|
commands_mod.points_service = fake_points_service
|
||||||
commands_mod.message_service = fake_message_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():
|
for horse in room.horses.values():
|
||||||
horse.state = HorseState.RACING
|
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))
|
start_task = asyncio.create_task(commands_mod.run_race_with_settlement(fake_bot, room, scope))
|
||||||
commands_mod.race_engine.register_task(scope, start_task)
|
commands_mod.race_engine.register_task(scope, start_task)
|
||||||
await asyncio.wait_for(start_task, timeout=15)
|
await asyncio.wait_for(start_task, timeout=15)
|
||||||
|
|||||||
Reference in New Issue
Block a user