capture_replay_linux_test.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. //go:build linux
  2. package tests
  3. import (
  4. "context"
  5. "os"
  6. "path/filepath"
  7. "testing"
  8. "time"
  9. "hth/internal/app"
  10. "hth/internal/capture"
  11. hthv1 "hth/internal/gen/hth/api/v1"
  12. "hth/internal/grpcserver"
  13. "hth/internal/testutil"
  14. )
  15. func TestCaptureReplayCapabilityHashRoundTrip(t *testing.T) {
  16. a, obs, _ := startMockApp(t)
  17. out := t.TempDir()
  18. conn, err := grpcserver.DialUnix(obs, 2*time.Second)
  19. if err != nil {
  20. t.Fatal(err)
  21. }
  22. defer conn.Close()
  23. cli := hthv1.NewObserveServiceClient(conn)
  24. ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
  25. defer cancel()
  26. caps, err := cli.GetCapabilities(ctx, &hthv1.GetCapabilitiesRequest{DeviceId: "prod2u-0"})
  27. if err != nil {
  28. t.Fatal(err)
  29. }
  30. wantHash := caps.Capabilities.GetCapabilityHash()
  31. if wantHash == "" {
  32. t.Fatal("empty capability hash")
  33. }
  34. man := capture.Manifest{
  35. APIVersion: app.APIVersion, APISchemaVersion: app.APIVersion,
  36. ProfileHash: caps.Capabilities.GetProtocolContractHash(),
  37. ProfileContractHash: caps.Capabilities.GetProtocolContractHash(),
  38. ProfileID: caps.Capabilities.GetProfileId(), ProfileRevision: caps.Capabilities.GetProfileRevision(),
  39. CapabilityHash: wantHash, DeviceID: "prod2u-0",
  40. StartedAt: time.Now().UTC(), Status: capture.StatusIncomplete,
  41. }
  42. if err := capture.WriteManifest(out, man); err != nil {
  43. t.Fatal(err)
  44. }
  45. f, err := os.Create(capture.RecordsPath(out))
  46. if err != nil {
  47. t.Fatal(err)
  48. }
  49. ev, err := cli.SubscribeEvents(ctx, &hthv1.SubscribeEventsRequest{DeviceId: "prod2u-0"})
  50. if err != nil {
  51. t.Fatal(err)
  52. }
  53. for i := 0; i < 5; i++ {
  54. e, err := ev.Recv()
  55. if err != nil {
  56. break
  57. }
  58. rec, err := capture.EventRecord(e)
  59. if err != nil {
  60. t.Fatal(err)
  61. }
  62. if err := capture.AppendRecord(f, rec); err != nil {
  63. t.Fatal(err)
  64. }
  65. }
  66. f.Close()
  67. man.Status = capture.StatusComplete
  68. if err := capture.Seal(out, man); err != nil {
  69. t.Fatal(err)
  70. }
  71. _ = a
  72. dir := t.TempDir()
  73. robs := filepath.Join(dir, "o.sock")
  74. rctl := filepath.Join(dir, "c.sock")
  75. yaml := `
  76. api_version: hth.api.v1
  77. device_id: prod2u-0
  78. backend: replay
  79. profile: ` + testutil.ProfilePath(t) + `
  80. runtime_root: ` + dir + `
  81. replay:
  82. path: ` + out + `
  83. speed: 1000
  84. step: true
  85. sockets:
  86. observe: ` + robs + `
  87. control: ` + rctl + `
  88. poll:
  89. temperature_ms: 200
  90. motor_status_ms: 100
  91. watchdog:
  92. heartbeat_period_ms: 5000
  93. lease:
  94. min_ttl_ms: 50
  95. max_ttl_ms: 2000
  96. safety:
  97. fan: 10
  98. ptc: 0
  99. door_close_percent: 0
  100. ring_buffer_size: 32
  101. subscriber_buffer_size: 4
  102. grpc:
  103. reflection: false
  104. `
  105. cfgPath := filepath.Join(dir, "hthd.yaml")
  106. if err := os.WriteFile(cfgPath, []byte(yaml), 0o644); err != nil {
  107. t.Fatal(err)
  108. }
  109. ra, err := app.New(cfgPath, "replay", nil)
  110. if err != nil {
  111. t.Fatal(err)
  112. }
  113. rctx, rcancel := context.WithCancel(context.Background())
  114. errCh := make(chan error, 1)
  115. go func() { errCh <- ra.Run(rctx) }()
  116. t.Cleanup(func() { rcancel(); ra.Stop(); <-errCh })
  117. waitUnix(t, robs, 3*time.Second)
  118. rc, err := grpcserver.DialUnix(robs, 2*time.Second)
  119. if err != nil {
  120. t.Fatal(err)
  121. }
  122. defer rc.Close()
  123. rcli := hthv1.NewObserveServiceClient(rc)
  124. got, err := rcli.GetCapabilities(context.Background(), &hthv1.GetCapabilitiesRequest{DeviceId: "prod2u-0"})
  125. if err != nil {
  126. t.Fatal(err)
  127. }
  128. if got.Capabilities.GetCapabilityHash() != wantHash {
  129. t.Fatalf("capability hash mock %s replay %s", wantHash, got.Capabilities.GetCapabilityHash())
  130. }
  131. step, err := rcli.StepReplay(context.Background(), &hthv1.StepReplayRequest{DeviceId: "prod2u-0"})
  132. if err != nil {
  133. t.Fatal(err)
  134. }
  135. if !step.Advanced && step.Message == "" {
  136. t.Fatal("step")
  137. }
  138. snap, err := rcli.GetSnapshot(context.Background(), &hthv1.GetSnapshotRequest{DeviceId: "prod2u-0"})
  139. if err != nil {
  140. t.Fatal(err)
  141. }
  142. if snap.Snapshot == nil {
  143. t.Fatal("snapshot")
  144. }
  145. }
  146. func TestWatchdogStopsWhenActorFrozen(t *testing.T) {
  147. a, _, _ := startMockApp(t)
  148. if !a.WatchdogShouldPet() {
  149. t.Fatal("healthy mock should pet")
  150. }
  151. a.Dev.TestingSetProgress(time.Time{})
  152. if a.WatchdogShouldPet() {
  153. t.Fatal("frozen actor must stop watchdog pet")
  154. }
  155. }
  156. func TestWatchdogKeepsPettingDuringBoundedScan(t *testing.T) {
  157. a, _, _ := startMockApp(t)
  158. if !a.WatchdogShouldPet() {
  159. t.Fatal("healthy mock should pet")
  160. }
  161. a.Dev.TestingSetInflightSince(5 * time.Second)
  162. if !a.WatchdogShouldPet() {
  163. t.Fatal("5s temperature scan must not stop systemd watchdog pet")
  164. }
  165. }
  166. func TestWatchdogStopsWhenSerialWorkerExceeded(t *testing.T) {
  167. a, _, _ := startMockApp(t)
  168. a.Dev.TestingSetInflightSince(a.Dev.MaxSerialTxn() + time.Second)
  169. if a.WatchdogShouldPet() {
  170. t.Fatal("serial worker beyond MaxSerialTxn must stop watchdog pet")
  171. }
  172. }