| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270 |
- //go:build linux
- package tests
- import (
- "context"
- "os"
- "path/filepath"
- "testing"
- "time"
- "google.golang.org/protobuf/types/known/durationpb"
- "google.golang.org/protobuf/types/known/timestamppb"
- "hth/internal/app"
- "hth/internal/devsim"
- hthv1 "hth/internal/gen/hth/api/v1"
- "hth/internal/grpcserver"
- "hth/internal/ids"
- "hth/internal/protocol"
- "hth/internal/serialtransport"
- "hth/internal/testutil"
- )
- func TestPTYSerialPath(t *testing.T) {
- serialtransport.SetTestWriteCap(0)
- serialtransport.SetTestForceWriteError(false)
- sim, err := devsim.Start(devsim.Options{StartupDone: true})
- if err != nil {
- t.Fatal(err)
- }
- defer sim.Close()
- dir := t.TempDir()
- obs := filepath.Join(dir, "o.sock")
- ctl := filepath.Join(dir, "c.sock")
- yaml := `
- api_version: hth.api.v1
- device_id: prod2u-0
- backend: serial
- profile: ` + testutil.ProfilePath(t) + `
- runtime_root: ` + dir + `
- serial:
- path: ` + sim.Name + `
- sockets:
- observe: ` + obs + `
- control: ` + ctl + `
- observe_perm: "0660"
- control_perm: "0660"
- poll:
- temperature_ms: 60000
- motor_status_ms: 60000
- watchdog:
- heartbeat_period_ms: 5000
- lease:
- min_ttl_ms: 50
- max_ttl_ms: 2000
- safety:
- fan: 10
- ptc: 0
- door_close_percent: 0
- ring_buffer_size: 32
- subscriber_buffer_size: 4
- grpc:
- reflection: false
- `
- cfgPath := filepath.Join(dir, "hthd.yaml")
- if err := os.WriteFile(cfgPath, []byte(yaml), 0o644); err != nil {
- t.Fatal(err)
- }
- a, err := app.New(cfgPath, "serial", nil)
- if err != nil {
- t.Fatal(err)
- }
- ctx, cancel := context.WithCancel(context.Background())
- defer cancel()
- go a.Run(ctx)
- defer a.Stop()
- deadline := time.Now().Add(4 * time.Second)
- var cconn interface{}
- _ = cconn
- for time.Now().Before(deadline) {
- if _, err := os.Stat(obs); err == nil {
- if _, err := grpcserver.DialUnix(obs, 200*time.Millisecond); err == nil {
- break
- }
- }
- time.Sleep(30 * time.Millisecond)
- }
- oconn, err := grpcserver.DialUnix(obs, time.Second)
- if err != nil {
- t.Fatal(err)
- }
- defer oconn.Close()
- cc, err := grpcserver.DialUnix(ctl, time.Second)
- if err != nil {
- t.Fatal(err)
- }
- defer cc.Close()
- obsCli := hthv1.NewObserveServiceClient(oconn)
- ctrl := hthv1.NewControlServiceClient(cc)
- waitUntilReady(t, obs, 20*time.Second)
- waitSlewBudget()
- ctx2, cancel2 := context.WithTimeout(context.Background(), 30*time.Second)
- defer cancel2()
- lease, err := ctrl.AcquireControlLease(ctx2, &hthv1.AcquireControlLeaseRequest{DeviceId: "prod2u-0", Holder: "pty", Ttl: durationpb.New(20 * time.Second)})
- if err != nil {
- t.Fatal(err)
- }
- snap, err := obsCli.GetSnapshot(ctx2, &hthv1.GetSnapshotRequest{DeviceId: "prod2u-0"})
- if err != nil {
- t.Fatal(err)
- }
- b := &hthv1.ActionBatch{
- DeviceId: "prod2u-0", CommandId: "pty-1", LeaseId: lease.Lease.LeaseId, LeaseEpoch: lease.Lease.LeaseEpoch,
- BasedOnSnapshotSeq: snap.Snapshot.SnapshotSeq, ExpectedControlRevision: snap.Snapshot.ControlRevision, AllowClamp: true,
- ValidUntil: timestamppb.New(time.Now().Add(20 * time.Second)),
- ContinuousTargets: []*hthv1.ContinuousTarget{
- {Id: ids.Fan1, Value: 10}, {Id: ids.Fan2, Value: 10}, {Id: ids.Fan3, Value: 10}, {Id: ids.Fan4, Value: 10},
- {Id: ids.PTC1, Value: 0}, {Id: ids.PTC2, Value: 0}, {Id: ids.PTC3, Value: 0}, {Id: ids.PTC4, Value: 0},
- {Id: ids.DoorX, Value: 40}, {Id: ids.DoorY, Value: 40}, {Id: ids.DoorZ, Value: 40},
- },
- }
- res, err := ctrl.ApplyAction(ctx2, &hthv1.ApplyActionRequest{Batch: b})
- if err != nil {
- t.Fatal(err)
- }
- if res.Confirmation == hthv1.ConfirmationStatus_CONFIRMATION_STATUS_CONFIRMED {
- t.Fatal("no ACK")
- }
- if !res.Accepted {
- t.Fatal(res.Message, res.RejectReason)
- }
- res2, err := ctrl.ApplyAction(ctx2, &hthv1.ApplyActionRequest{Batch: b})
- if err != nil {
- t.Fatal(err)
- }
- if !res2.ReplayedFromCache {
- t.Fatal("duplicate command_id must replay cache")
- }
- time.Sleep(200 * time.Millisecond)
- if sim.FrameCountSETALL() < 1 {
- t.Fatal("expected SET_ALL on PTY")
- }
- if sim.FrameCountSETALL() < 1 {
- t.Fatal("expected SET_ALL")
- }
- sets := sim.SetAlls()
- firstUser := -1
- for i, s := range sets {
- if s.DoorX > 0 {
- firstUser = i
- break
- }
- }
- if firstUser < 0 {
- t.Fatalf("user door percent not seen: %+v", sets)
- }
- user := sets[firstUser]
- for _, s := range sets[firstUser:] {
- if s.DoorX != user.DoorX || s.DoorY != user.DoorY || s.DoorZ != user.DoorZ {
- t.Fatalf("heartbeat rewrote doors %+v after user frame idx=%d all=%+v", s, firstUser, sets)
- }
- }
- if sim.FrameCountSETALL() != len(sets) {
- t.Fatal("count")
- }
- dupWrites := 0
- for _, s := range sets {
- if s.DoorX == 40 {
- dupWrites++
- }
- }
- if dupWrites < 1 {
- t.Fatal("user frame missing")
- }
- }
- func TestPTYDelayedStartupSuppressesGetTemperature(t *testing.T) {
- serialtransport.SetTestWriteCap(0)
- serialtransport.SetTestForceWriteError(false)
- sim, err := devsim.Start(devsim.Options{StartupDone: false, DropGETTemp: true})
- if err != nil {
- t.Fatal(err)
- }
- defer sim.Close()
- dir := t.TempDir()
- obs := filepath.Join(dir, "observe", "o.sock")
- ctl := filepath.Join(dir, "control", "c.sock")
- yaml := `
- api_version: hth.api.v1
- device_id: prod2u-0
- backend: serial
- profile: ` + testutil.ProfilePath(t) + `
- runtime_root: ` + dir + `
- serial:
- path: ` + sim.Name + `
- get_temp_timeout_ms: 8000
- get_motor_timeout_ms: 8000
- probe_timeout_ms: 8000
- sockets:
- observe: ` + obs + `
- control: ` + ctl + `
- observe_perm: "0660"
- control_perm: "0660"
- poll:
- temperature_ms: 200
- motor_status_ms: 80
- watchdog:
- heartbeat_period_ms: 5000
- lease:
- min_ttl_ms: 50
- max_ttl_ms: 2000
- safety:
- fan: 10
- ptc: 0
- door_close_percent: 0
- ring_buffer_size: 32
- subscriber_buffer_size: 4
- grpc:
- reflection: false
- `
- cfgPath := filepath.Join(dir, "hthd.yaml")
- if err := os.WriteFile(cfgPath, []byte(yaml), 0o644); err != nil {
- t.Fatal(err)
- }
- a, err := app.New(cfgPath, "serial", nil)
- if err != nil {
- t.Fatal(err)
- }
- ctx, cancel := context.WithCancel(context.Background())
- defer cancel()
- go a.Run(ctx)
- defer a.Stop()
- deadline := time.Now().Add(4 * time.Second)
- for time.Now().Before(deadline) {
- if _, err := os.Stat(obs); err == nil {
- break
- }
- time.Sleep(20 * time.Millisecond)
- }
- hold := 10 * time.Second
- time.Sleep(hold)
- if n := sim.CountType(protocol.TypeGetTemperature); n != 0 {
- t.Fatalf("GET_TEMPERATURE during 10s PTY startup: %d", n)
- }
- if sim.CountType(protocol.TypeGetMotorStatus) == 0 {
- t.Fatal("expected GET_MOTOR_STATUS during delayed PTY startup")
- }
- oconn, err := grpcserver.DialUnix(obs, time.Second)
- if err != nil {
- t.Fatal(err)
- }
- defer oconn.Close()
- obsCli := hthv1.NewObserveServiceClient(oconn)
- snap, err := obsCli.GetSnapshot(context.Background(), &hthv1.GetSnapshotRequest{DeviceId: "prod2u-0"})
- if err != nil {
- t.Fatal(err)
- }
- if snap.Snapshot.GetDeviceEpoch() != 1 {
- t.Fatalf("epoch changed during delayed PTY startup: %d", snap.Snapshot.GetDeviceEpoch())
- }
- sim.SetOptions(devsim.Options{StartupDone: true, DropGETTemp: false})
- until := time.Now().Add(4 * time.Second)
- for time.Now().Before(until) {
- if sim.CountType(protocol.TypeGetTemperature) > 0 {
- return
- }
- time.Sleep(20 * time.Millisecond)
- }
- t.Fatal("expected GET_TEMPERATURE after PTY startup_complete")
- }
|