DebugSessionClientTests.cs 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. using System;
  2. using System.Net;
  3. using System.Net.Http;
  4. using System.Threading;
  5. using System.Threading.Tasks;
  6. using ivf_tl_Operate.Debug;
  7. using Xunit;
  8. namespace IvfTl.ControlHost.Tests
  9. {
  10. /// <summary>
  11. /// operate 端 DebugSessionClient 纯逻辑单测:用 fake HttpMessageHandler 注入响应,
  12. /// 验证 acquire 解析(含培养态)/SESSION_EXPIRED 触发失效回调/release 幂等/心跳定时器。
  13. /// </summary>
  14. public class DebugSessionClientTests
  15. {
  16. private sealed class FakeHandler : HttpMessageHandler
  17. {
  18. public Func<HttpRequestMessage, (HttpStatusCode, string)> Responder;
  19. public int HeartbeatCount;
  20. protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage req, CancellationToken ct)
  21. {
  22. if (req.RequestUri.AbsolutePath.Contains("heartbeat")) Interlocked.Increment(ref HeartbeatCount);
  23. var (code, body) = Responder(req);
  24. return Task.FromResult(new HttpResponseMessage(code) { Content = new StringContent(body) });
  25. }
  26. }
  27. [Fact]
  28. public async Task Acquire_ParsesSessionIdAndCultivation()
  29. {
  30. var h = new FakeHandler { Responder = _ => (HttpStatusCode.OK, "{\"ok\":true,\"result\":\"sid123\",\"cultivating\":true,\"embryoCount\":3}") };
  31. var c = new DebugSessionClient("http://127.0.0.1:9/", new HttpClient(h));
  32. var r = await c.AcquireAsync(6);
  33. Assert.True(r.Ok);
  34. Assert.Equal("sid123", r.SessionId);
  35. Assert.True(r.Cultivating);
  36. Assert.Equal(3, r.EmbryoCount);
  37. await c.ReleaseAsync();
  38. }
  39. [Fact]
  40. public async Task Command_SessionExpired_FiresOnSessionExpired()
  41. {
  42. bool fired = false;
  43. var h = new FakeHandler
  44. {
  45. Responder = req => req.RequestUri.AbsolutePath.Contains("command")
  46. ? (HttpStatusCode.Gone, "{\"ok\":false,\"code\":\"SESSION_EXPIRED\",\"error\":\"过期\"}")
  47. : (HttpStatusCode.OK, "{\"ok\":true,\"result\":\"sid123\"}")
  48. };
  49. var c = new DebugSessionClient("http://127.0.0.1:9/", new HttpClient(h));
  50. c.OnSessionExpired = () => fired = true;
  51. await c.AcquireAsync(6);
  52. var r = await c.CommandAsync("ReadTemp", null);
  53. Assert.False(r.Ok);
  54. Assert.Equal("SESSION_EXPIRED", r.Code);
  55. Assert.True(fired);
  56. }
  57. [Fact]
  58. public async Task Release_IsIdempotent_NoThrowWhenCalledTwice()
  59. {
  60. var h = new FakeHandler { Responder = _ => (HttpStatusCode.OK, "{\"ok\":true,\"result\":\"sid123\"}") };
  61. var c = new DebugSessionClient("http://127.0.0.1:9/", new HttpClient(h));
  62. await c.AcquireAsync(6);
  63. await c.ReleaseAsync();
  64. await c.ReleaseAsync(); // 第二次不抛
  65. }
  66. [Fact]
  67. public async Task Heartbeat_TimerSendsAfterAcquire()
  68. {
  69. var h = new FakeHandler { Responder = _ => (HttpStatusCode.OK, "{\"ok\":true,\"result\":\"sid123\"}") };
  70. var c = new DebugSessionClient("http://127.0.0.1:9/", new HttpClient(h)) { HeartbeatIntervalMs = 50 };
  71. await c.AcquireAsync(6);
  72. await Task.Delay(300);
  73. await c.ReleaseAsync();
  74. Assert.True(h.HeartbeatCount >= 2); // 50ms 间隔 300ms 内至少 2 次(放宽窗口防 CI 慢机 flaky)
  75. }
  76. }
  77. }