Jelajahi Sumber

test(d2-02): 会话超时自动回收 + 心跳续约(安全地基)

huangjie 2 hari lalu
induk
melakukan
b7e8e403c8

+ 22 - 0
ivf_tl_operate_2.0/control/IvfTl.ControlHost.Tests/DebugSessionManagerTests.cs

@@ -46,5 +46,27 @@ namespace IvfTl.ControlHost.Tests
             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);
+        }
     }
 }

+ 4 - 3
ivf_tl_operate_2.0/control/IvfTl.ControlHost.Tests/Fakes/FakeHardware.cs

@@ -59,12 +59,13 @@ namespace IvfTl.ControlHost.Tests.Fakes
 
     public class FakeLease : IHardwareLease
     {
+        private readonly FakeGate _gate;
         public HardwareUser Owner { get; }
         public ISerialChannel Serial { get; }
         public ICamera Camera => null;
         public bool Disposed;
-        public FakeLease(ISerialChannel s, HardwareUser u) { Serial = s; Owner = u; }
-        public void Dispose() => Disposed = true;
+        public FakeLease(FakeGate gate, ISerialChannel s, HardwareUser u) { _gate = gate; Serial = s; Owner = u; }
+        public void Dispose() { Disposed = true; _gate.ResumeCapture(); }
     }
 
     public class FakeGate : IHouseGate
@@ -83,7 +84,7 @@ namespace IvfTl.ControlHost.Tests.Fakes
         {
             if (!CanAcquire) return null;
             IsCapturePaused = true;
-            LastLease = new FakeLease(_serial, u);
+            LastLease = new FakeLease(this, _serial, u);
             return LastLease;
         }
         public bool TryAcquire(HardwareUser u, out IHardwareLease l) { l = Acquire(u); return l != null; }