DebugSessionManagerTests.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. using System;
  2. using IvfTl.ControlHost.Debug;
  3. using IvfTl.ControlHost.Tests.Fakes;
  4. using Xunit;
  5. namespace IvfTl.ControlHost.Tests
  6. {
  7. public class DebugSessionManagerTests
  8. {
  9. private DateTime _now = new DateTime(2026, 6, 23, 0, 0, 0);
  10. private (DebugSessionManager mgr, FakeGate gate) New(bool canAcquire = true)
  11. {
  12. var serial = new FakeSerial();
  13. var gate = new FakeGate(5, serial) { CanAcquire = canAcquire };
  14. var mgr = new DebugSessionManager(sn => gate, () => _now, ttlMs: 10000, log: _ => { });
  15. return (mgr, gate);
  16. }
  17. [Fact]
  18. public void Acquire_Returns_SessionId_And_Pauses()
  19. {
  20. var (mgr, gate) = New();
  21. var r = mgr.Acquire(5);
  22. Assert.True(r.Ok);
  23. Assert.False(string.IsNullOrEmpty((string)r.Result));
  24. Assert.True(gate.IsCapturePaused);
  25. }
  26. [Fact]
  27. public void Acquire_Busy_Returns_BUSY()
  28. {
  29. var (mgr, _) = New(canAcquire: false);
  30. var r = mgr.Acquire(5);
  31. Assert.False(r.Ok);
  32. Assert.Equal("BUSY", r.Code);
  33. }
  34. [Fact]
  35. public void Release_Disposes_Lease_And_Resumes()
  36. {
  37. var (mgr, gate) = New();
  38. string sid = (string)mgr.Acquire(5).Result;
  39. var r = mgr.Release(sid);
  40. Assert.True(r.Ok);
  41. Assert.True(gate.LastLease.Disposed);
  42. }
  43. [Fact]
  44. public void Release_Unknown_Session_Is_Idempotent_Ok()
  45. {
  46. var (mgr, _) = New();
  47. Assert.True(mgr.Release("not-a-session").Ok);
  48. }
  49. [Fact]
  50. public void Sweep_Reclaims_After_Ttl_And_Resumes()
  51. {
  52. var (mgr, gate) = New();
  53. string sid = (string)mgr.Acquire(5).Result;
  54. _now = _now.AddMilliseconds(11000);
  55. int reclaimed = mgr.SweepExpired();
  56. Assert.Equal(1, reclaimed);
  57. Assert.True(gate.LastLease.Disposed);
  58. Assert.False(gate.IsCapturePaused);
  59. Assert.Equal("SESSION_EXPIRED", mgr.Heartbeat(sid).Code);
  60. }
  61. [Fact]
  62. public void Heartbeat_Keeps_Session_Alive()
  63. {
  64. var (mgr, gate) = New();
  65. string sid = (string)mgr.Acquire(5).Result;
  66. _now = _now.AddMilliseconds(8000); mgr.Heartbeat(sid);
  67. _now = _now.AddMilliseconds(8000);
  68. Assert.Equal(0, mgr.SweepExpired());
  69. Assert.False(gate.LastLease.Disposed);
  70. }
  71. }
  72. }