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

@@ -7,39 +7,40 @@ namespace SVSim.UnitTests.BattleNode.Sessions;
[TestFixture]
public class InMemoryBattleSessionStoreTests
{
private InMemoryBattleSessionStore _store = null!;
[SetUp] public void Setup() => _store = new InMemoryBattleSessionStore();
[Test]
public void RegisterThenGet_ReturnsRegisteredBattle()
public void TryRegisterThenGet_ReturnsRegisteredBattle()
{
var store = new InMemoryBattleSessionStore();
var battle = new PendingBattle("bid-1", BattleType.Bot, new BattlePlayer(906243102, FixtureCtx()), null);
_store.RegisterPending(battle);
Assert.That(store.TryRegisterPending(battle), Is.True);
Assert.That(_store.TryGetPending("bid-1"), Is.EqualTo(battle));
Assert.That(store.TryGetPending("bid-1"), Is.EqualTo(battle));
}
[Test]
public void Get_UnknownBattleId_ReturnsNull()
{
Assert.That(_store.TryGetPending("nope"), Is.Null);
var store = new InMemoryBattleSessionStore();
Assert.That(store.TryGetPending("nope"), Is.Null);
}
[Test]
public void Remove_ReturnsTrueWhenPresent_FalseWhenAbsent()
{
_store.RegisterPending(new PendingBattle("bid", BattleType.Bot, new BattlePlayer(1, FixtureCtx()), null));
Assert.That(_store.RemovePending("bid"), Is.True);
Assert.That(_store.RemovePending("bid"), Is.False);
var store = new InMemoryBattleSessionStore();
store.TryRegisterPending(new PendingBattle("bid", BattleType.Bot, new BattlePlayer(1, FixtureCtx()), null));
Assert.That(store.RemovePending("bid"), Is.True);
Assert.That(store.RemovePending("bid"), Is.False);
}
[Test]
public void Register_DuplicateBattleId_OverwritesPrior()
public void TryRegister_DuplicateBattleId_ReturnsFalseAndPreservesOriginal()
{
_store.RegisterPending(new PendingBattle("bid", BattleType.Bot, new BattlePlayer(1, FixtureCtx()), null));
_store.RegisterPending(new PendingBattle("bid", BattleType.Bot, new BattlePlayer(2, FixtureCtx()), null));
Assert.That(_store.TryGetPending("bid")!.P1.ViewerId, Is.EqualTo(2));
var store = new InMemoryBattleSessionStore();
store.TryRegisterPending(new PendingBattle("bid", BattleType.Bot, new BattlePlayer(1, FixtureCtx()), null));
var second = store.TryRegisterPending(new PendingBattle("bid", BattleType.Bot, new BattlePlayer(2, FixtureCtx()), null));
Assert.That(second, Is.False);
Assert.That(store.TryGetPending("bid")!.P1.ViewerId, Is.EqualTo(1));
}
private static MatchContext FixtureCtx() => new(