fix(battle-node): collision-safe battle-id registration + viewer eviction

RegisterPending → TryRegisterPending (TryAdd instead of indexer) so
battle-id collisions return false instead of silently evicting a live
battle. MatchingBridge retries with fresh IDs on collision (max 5).

Before registering, EvictStaleForViewer removes any stale pending
battle the viewer left behind, enforcing the one-pending-per-viewer
invariant that was previously comment-asserted.

Store tests switched to per-test local stores to fix a race under
the assembly-wide ParallelScope.All.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
gamer147
2026-06-04 22:13:20 -04:00
parent c6fb411861
commit 564b1d678f
5 changed files with 60 additions and 27 deletions

View File

@@ -76,6 +76,21 @@ public class MatchingBridgeTests
new BattlePlayer(1, FixtureCtx()), new BattlePlayer(2, FixtureCtx()), BattleType.Bot));
}
[Test]
public void RegisterBattle_evicts_stale_pending_for_same_viewer()
{
var store = new InMemoryBattleSessionStore();
var bridge = new MatchingBridge(store, new BattleNodeOptions());
var p1 = new BattlePlayer(42, FixtureCtx());
var first = bridge.RegisterBattle(p1, p2: null, BattleType.Bot);
Assert.That(store.TryGetPending(first.BattleId), Is.Not.Null);
var second = bridge.RegisterBattle(p1, p2: null, BattleType.Bot);
Assert.That(store.TryGetPending(first.BattleId), Is.Null, "stale entry must be evicted");
Assert.That(store.TryGetPending(second.BattleId), Is.Not.Null);
}
private static MatchContext FixtureCtx() => new(
SelfDeckCardIds: Enumerable.Range(1, 30).Select(i => 100_011_010L).ToList(),
ClassId: "1", CharaId: "1", CardMasterName: "card_master_node_10015",