feat(battle-node): OutboundSequencer assigns playSeq + archives for Resume
This commit is contained in:
27
SVSim.BattleNode/Reliability/OutboundSequencer.cs
Normal file
27
SVSim.BattleNode/Reliability/OutboundSequencer.cs
Normal file
@@ -0,0 +1,27 @@
|
||||
using SVSim.BattleNode.Protocol;
|
||||
|
||||
namespace SVSim.BattleNode.Reliability;
|
||||
|
||||
/// <summary>
|
||||
/// Per-session outbound ledger. Assigns monotonic playSeq to ordered pushes and archives
|
||||
/// them for future Resume retransmit (v2). No-stock control pushes (BattleFinish/JudgeResult/Resume)
|
||||
/// are wrapped with no playSeq and skip the archive.
|
||||
/// </summary>
|
||||
public sealed class OutboundSequencer
|
||||
{
|
||||
private long _next = 1;
|
||||
private readonly Dictionary<long, MsgEnvelope> _archive = new();
|
||||
|
||||
public IReadOnlyDictionary<long, MsgEnvelope> Archive => _archive;
|
||||
|
||||
public MsgEnvelope AssignAndArchive(MsgEnvelope envelope)
|
||||
{
|
||||
var seq = _next++;
|
||||
var stamped = envelope with { PlaySeq = seq };
|
||||
_archive[seq] = stamped;
|
||||
return stamped;
|
||||
}
|
||||
|
||||
public MsgEnvelope WrapNoStock(MsgEnvelope envelope) =>
|
||||
envelope with { PlaySeq = null };
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
using NUnit.Framework;
|
||||
using SVSim.BattleNode.Protocol;
|
||||
using SVSim.BattleNode.Reliability;
|
||||
|
||||
namespace SVSim.UnitTests.BattleNode.Reliability;
|
||||
|
||||
[TestFixture]
|
||||
public class OutboundSequencerTests
|
||||
{
|
||||
private static MsgEnvelope MakeEnvelope(NetworkBattleUri uri) =>
|
||||
new(uri, ViewerId: 1, Uuid: "u", Bid: null, Try: 0, Cat: EmitCategory.Battle,
|
||||
PubSeq: null, PlaySeq: null, Body: new Dictionary<string, object?>());
|
||||
|
||||
[Test]
|
||||
public void AssignAndArchive_FirstCall_ReturnsEnvelopeWithPlaySeq1()
|
||||
{
|
||||
var seq = new OutboundSequencer();
|
||||
var assigned = seq.AssignAndArchive(MakeEnvelope(NetworkBattleUri.BattleStart));
|
||||
|
||||
Assert.That(assigned.PlaySeq, Is.EqualTo(1));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AssignAndArchive_SubsequentCalls_ReturnContiguousSequence()
|
||||
{
|
||||
var seq = new OutboundSequencer();
|
||||
Assert.That(seq.AssignAndArchive(MakeEnvelope(NetworkBattleUri.Matched)).PlaySeq, Is.EqualTo(1));
|
||||
Assert.That(seq.AssignAndArchive(MakeEnvelope(NetworkBattleUri.BattleStart)).PlaySeq, Is.EqualTo(2));
|
||||
Assert.That(seq.AssignAndArchive(MakeEnvelope(NetworkBattleUri.Deal)).PlaySeq, Is.EqualTo(3));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void NoStockControlPush_DoesNotAssignPlaySeqOrArchive()
|
||||
{
|
||||
var seq = new OutboundSequencer();
|
||||
var env = seq.WrapNoStock(MakeEnvelope(NetworkBattleUri.BattleFinish));
|
||||
|
||||
Assert.That(env.PlaySeq, Is.Null);
|
||||
Assert.That(seq.Archive, Is.Empty);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Archive_ContainsArchivedEnvelopesKeyedByPlaySeq()
|
||||
{
|
||||
var seq = new OutboundSequencer();
|
||||
seq.AssignAndArchive(MakeEnvelope(NetworkBattleUri.Matched));
|
||||
seq.AssignAndArchive(MakeEnvelope(NetworkBattleUri.BattleStart));
|
||||
|
||||
Assert.That(seq.Archive.Keys, Is.EquivalentTo(new[] { 1L, 2L }));
|
||||
Assert.That(seq.Archive[1L].Uri, Is.EqualTo(NetworkBattleUri.Matched));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user