| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- using System;
- using IvfTl.ControlHost.Debug;
- using IvfTl.ControlHost.Tests.Fakes;
- using Xunit;
- namespace IvfTl.ControlHost.Tests
- {
- public class DebugSessionManagerTests
- {
- private DateTime _now = new DateTime(2026, 6, 23, 0, 0, 0);
- private (DebugSessionManager mgr, FakeGate gate) New(bool canAcquire = true)
- {
- var serial = new FakeSerial();
- var gate = new FakeGate(5, serial) { CanAcquire = canAcquire };
- var mgr = new DebugSessionManager(sn => gate, () => _now, ttlMs: 10000, log: _ => { });
- return (mgr, gate);
- }
- [Fact]
- public void Acquire_Returns_SessionId_And_Pauses()
- {
- var (mgr, gate) = New();
- var r = mgr.Acquire(5);
- Assert.True(r.Ok);
- Assert.False(string.IsNullOrEmpty((string)r.Result));
- Assert.True(gate.IsCapturePaused);
- }
- [Fact]
- public void Acquire_Busy_Returns_BUSY()
- {
- var (mgr, _) = New(canAcquire: false);
- var r = mgr.Acquire(5);
- Assert.False(r.Ok);
- Assert.Equal("BUSY", r.Code);
- }
- [Fact]
- public void Release_Disposes_Lease_And_Resumes()
- {
- var (mgr, gate) = New();
- string sid = (string)mgr.Acquire(5).Result;
- var r = mgr.Release(sid);
- Assert.True(r.Ok);
- Assert.True(gate.LastLease.Disposed);
- }
- [Fact]
- public void Release_Unknown_Session_Is_Idempotent_Ok()
- {
- var (mgr, _) = New();
- Assert.True(mgr.Release("not-a-session").Ok);
- }
- [Fact]
- public void Sweep_Reclaims_After_Ttl_And_Resumes()
- {
- var (mgr, gate) = New();
- string sid = (string)mgr.Acquire(5).Result;
- _now = _now.AddMilliseconds(11000);
- int reclaimed = mgr.SweepExpired();
- Assert.Equal(1, reclaimed);
- Assert.True(gate.LastLease.Disposed);
- Assert.False(gate.IsCapturePaused);
- Assert.Equal("SESSION_EXPIRED", mgr.Heartbeat(sid).Code);
- }
- [Fact]
- public void Heartbeat_Keeps_Session_Alive()
- {
- var (mgr, gate) = New();
- string sid = (string)mgr.Acquire(5).Result;
- _now = _now.AddMilliseconds(8000); mgr.Heartbeat(sid);
- _now = _now.AddMilliseconds(8000);
- Assert.Equal(0, mgr.SweepExpired());
- Assert.False(gate.LastLease.Disposed);
- }
- }
- }
|