pty_serial_test.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. //go:build linux
  2. package tests
  3. import (
  4. "context"
  5. "os"
  6. "path/filepath"
  7. "testing"
  8. "time"
  9. "google.golang.org/protobuf/types/known/durationpb"
  10. "google.golang.org/protobuf/types/known/timestamppb"
  11. "hth/internal/app"
  12. "hth/internal/devsim"
  13. hthv1 "hth/internal/gen/hth/api/v1"
  14. "hth/internal/grpcserver"
  15. "hth/internal/ids"
  16. "hth/internal/protocol"
  17. "hth/internal/serialtransport"
  18. "hth/internal/testutil"
  19. )
  20. func TestPTYSerialPath(t *testing.T) {
  21. serialtransport.SetTestWriteCap(0)
  22. serialtransport.SetTestForceWriteError(false)
  23. sim, err := devsim.Start(devsim.Options{StartupDone: true})
  24. if err != nil {
  25. t.Fatal(err)
  26. }
  27. defer sim.Close()
  28. dir := t.TempDir()
  29. obs := filepath.Join(dir, "o.sock")
  30. ctl := filepath.Join(dir, "c.sock")
  31. yaml := `
  32. api_version: hth.api.v1
  33. device_id: prod2u-0
  34. backend: serial
  35. profile: ` + testutil.ProfilePath(t) + `
  36. runtime_root: ` + dir + `
  37. serial:
  38. path: ` + sim.Name + `
  39. sockets:
  40. observe: ` + obs + `
  41. control: ` + ctl + `
  42. observe_perm: "0660"
  43. control_perm: "0660"
  44. poll:
  45. temperature_ms: 60000
  46. motor_status_ms: 60000
  47. watchdog:
  48. heartbeat_period_ms: 5000
  49. lease:
  50. min_ttl_ms: 50
  51. max_ttl_ms: 2000
  52. safety:
  53. fan: 10
  54. ptc: 0
  55. door_close_percent: 0
  56. ring_buffer_size: 32
  57. subscriber_buffer_size: 4
  58. grpc:
  59. reflection: false
  60. `
  61. cfgPath := filepath.Join(dir, "hthd.yaml")
  62. if err := os.WriteFile(cfgPath, []byte(yaml), 0o644); err != nil {
  63. t.Fatal(err)
  64. }
  65. a, err := app.New(cfgPath, "serial", nil)
  66. if err != nil {
  67. t.Fatal(err)
  68. }
  69. ctx, cancel := context.WithCancel(context.Background())
  70. defer cancel()
  71. go a.Run(ctx)
  72. defer a.Stop()
  73. deadline := time.Now().Add(4 * time.Second)
  74. var cconn interface{}
  75. _ = cconn
  76. for time.Now().Before(deadline) {
  77. if _, err := os.Stat(obs); err == nil {
  78. if _, err := grpcserver.DialUnix(obs, 200*time.Millisecond); err == nil {
  79. break
  80. }
  81. }
  82. time.Sleep(30 * time.Millisecond)
  83. }
  84. oconn, err := grpcserver.DialUnix(obs, time.Second)
  85. if err != nil {
  86. t.Fatal(err)
  87. }
  88. defer oconn.Close()
  89. cc, err := grpcserver.DialUnix(ctl, time.Second)
  90. if err != nil {
  91. t.Fatal(err)
  92. }
  93. defer cc.Close()
  94. obsCli := hthv1.NewObserveServiceClient(oconn)
  95. ctrl := hthv1.NewControlServiceClient(cc)
  96. waitUntilReady(t, obs, 20*time.Second)
  97. waitSlewBudget()
  98. ctx2, cancel2 := context.WithTimeout(context.Background(), 30*time.Second)
  99. defer cancel2()
  100. lease, err := ctrl.AcquireControlLease(ctx2, &hthv1.AcquireControlLeaseRequest{DeviceId: "prod2u-0", Holder: "pty", Ttl: durationpb.New(20 * time.Second)})
  101. if err != nil {
  102. t.Fatal(err)
  103. }
  104. snap, err := obsCli.GetSnapshot(ctx2, &hthv1.GetSnapshotRequest{DeviceId: "prod2u-0"})
  105. if err != nil {
  106. t.Fatal(err)
  107. }
  108. b := &hthv1.ActionBatch{
  109. DeviceId: "prod2u-0", CommandId: "pty-1", LeaseId: lease.Lease.LeaseId, LeaseEpoch: lease.Lease.LeaseEpoch,
  110. BasedOnSnapshotSeq: snap.Snapshot.SnapshotSeq, ExpectedControlRevision: snap.Snapshot.ControlRevision, AllowClamp: true,
  111. ValidUntil: timestamppb.New(time.Now().Add(20 * time.Second)),
  112. ContinuousTargets: []*hthv1.ContinuousTarget{
  113. {Id: ids.Fan1, Value: 10}, {Id: ids.Fan2, Value: 10}, {Id: ids.Fan3, Value: 10}, {Id: ids.Fan4, Value: 10},
  114. {Id: ids.PTC1, Value: 0}, {Id: ids.PTC2, Value: 0}, {Id: ids.PTC3, Value: 0}, {Id: ids.PTC4, Value: 0},
  115. {Id: ids.DoorX, Value: 40}, {Id: ids.DoorY, Value: 40}, {Id: ids.DoorZ, Value: 40},
  116. },
  117. }
  118. res, err := ctrl.ApplyAction(ctx2, &hthv1.ApplyActionRequest{Batch: b})
  119. if err != nil {
  120. t.Fatal(err)
  121. }
  122. if res.Confirmation == hthv1.ConfirmationStatus_CONFIRMATION_STATUS_CONFIRMED {
  123. t.Fatal("no ACK")
  124. }
  125. if !res.Accepted {
  126. t.Fatal(res.Message, res.RejectReason)
  127. }
  128. res2, err := ctrl.ApplyAction(ctx2, &hthv1.ApplyActionRequest{Batch: b})
  129. if err != nil {
  130. t.Fatal(err)
  131. }
  132. if !res2.ReplayedFromCache {
  133. t.Fatal("duplicate command_id must replay cache")
  134. }
  135. time.Sleep(200 * time.Millisecond)
  136. if sim.FrameCountSETALL() < 1 {
  137. t.Fatal("expected SET_ALL on PTY")
  138. }
  139. if sim.FrameCountSETALL() < 1 {
  140. t.Fatal("expected SET_ALL")
  141. }
  142. sets := sim.SetAlls()
  143. firstUser := -1
  144. for i, s := range sets {
  145. if s.DoorX > 0 {
  146. firstUser = i
  147. break
  148. }
  149. }
  150. if firstUser < 0 {
  151. t.Fatalf("user door percent not seen: %+v", sets)
  152. }
  153. user := sets[firstUser]
  154. for _, s := range sets[firstUser:] {
  155. if s.DoorX != user.DoorX || s.DoorY != user.DoorY || s.DoorZ != user.DoorZ {
  156. t.Fatalf("heartbeat rewrote doors %+v after user frame idx=%d all=%+v", s, firstUser, sets)
  157. }
  158. }
  159. if sim.FrameCountSETALL() != len(sets) {
  160. t.Fatal("count")
  161. }
  162. dupWrites := 0
  163. for _, s := range sets {
  164. if s.DoorX == 40 {
  165. dupWrites++
  166. }
  167. }
  168. if dupWrites < 1 {
  169. t.Fatal("user frame missing")
  170. }
  171. }
  172. func TestPTYDelayedStartupSuppressesGetTemperature(t *testing.T) {
  173. serialtransport.SetTestWriteCap(0)
  174. serialtransport.SetTestForceWriteError(false)
  175. sim, err := devsim.Start(devsim.Options{StartupDone: false, DropGETTemp: true})
  176. if err != nil {
  177. t.Fatal(err)
  178. }
  179. defer sim.Close()
  180. dir := t.TempDir()
  181. obs := filepath.Join(dir, "observe", "o.sock")
  182. ctl := filepath.Join(dir, "control", "c.sock")
  183. yaml := `
  184. api_version: hth.api.v1
  185. device_id: prod2u-0
  186. backend: serial
  187. profile: ` + testutil.ProfilePath(t) + `
  188. runtime_root: ` + dir + `
  189. serial:
  190. path: ` + sim.Name + `
  191. get_temp_timeout_ms: 8000
  192. get_motor_timeout_ms: 8000
  193. probe_timeout_ms: 8000
  194. sockets:
  195. observe: ` + obs + `
  196. control: ` + ctl + `
  197. observe_perm: "0660"
  198. control_perm: "0660"
  199. poll:
  200. temperature_ms: 200
  201. motor_status_ms: 80
  202. watchdog:
  203. heartbeat_period_ms: 5000
  204. lease:
  205. min_ttl_ms: 50
  206. max_ttl_ms: 2000
  207. safety:
  208. fan: 10
  209. ptc: 0
  210. door_close_percent: 0
  211. ring_buffer_size: 32
  212. subscriber_buffer_size: 4
  213. grpc:
  214. reflection: false
  215. `
  216. cfgPath := filepath.Join(dir, "hthd.yaml")
  217. if err := os.WriteFile(cfgPath, []byte(yaml), 0o644); err != nil {
  218. t.Fatal(err)
  219. }
  220. a, err := app.New(cfgPath, "serial", nil)
  221. if err != nil {
  222. t.Fatal(err)
  223. }
  224. ctx, cancel := context.WithCancel(context.Background())
  225. defer cancel()
  226. go a.Run(ctx)
  227. defer a.Stop()
  228. deadline := time.Now().Add(4 * time.Second)
  229. for time.Now().Before(deadline) {
  230. if _, err := os.Stat(obs); err == nil {
  231. break
  232. }
  233. time.Sleep(20 * time.Millisecond)
  234. }
  235. hold := 10 * time.Second
  236. time.Sleep(hold)
  237. if n := sim.CountType(protocol.TypeGetTemperature); n != 0 {
  238. t.Fatalf("GET_TEMPERATURE during 10s PTY startup: %d", n)
  239. }
  240. if sim.CountType(protocol.TypeGetMotorStatus) == 0 {
  241. t.Fatal("expected GET_MOTOR_STATUS during delayed PTY startup")
  242. }
  243. oconn, err := grpcserver.DialUnix(obs, time.Second)
  244. if err != nil {
  245. t.Fatal(err)
  246. }
  247. defer oconn.Close()
  248. obsCli := hthv1.NewObserveServiceClient(oconn)
  249. snap, err := obsCli.GetSnapshot(context.Background(), &hthv1.GetSnapshotRequest{DeviceId: "prod2u-0"})
  250. if err != nil {
  251. t.Fatal(err)
  252. }
  253. if snap.Snapshot.GetDeviceEpoch() != 1 {
  254. t.Fatalf("epoch changed during delayed PTY startup: %d", snap.Snapshot.GetDeviceEpoch())
  255. }
  256. sim.SetOptions(devsim.Options{StartupDone: true, DropGETTemp: false})
  257. until := time.Now().Add(4 * time.Second)
  258. for time.Now().Before(until) {
  259. if sim.CountType(protocol.TypeGetTemperature) > 0 {
  260. return
  261. }
  262. time.Sleep(20 * time.Millisecond)
  263. }
  264. t.Fatal("expected GET_TEMPERATURE after PTY startup_complete")
  265. }