| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235 |
- //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/serialtransport"
- "hth/internal/testutil"
- )
- func startSerialApp(t *testing.T, sim *devsim.Sim) (obs, ctl string) {
- t.Helper()
- serialtransport.SetTestWriteCap(0)
- serialtransport.SetTestForceWriteError(false)
- 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 + `
- 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())
- errCh := make(chan error, 1)
- go func() { errCh <- a.Run(ctx) }()
- t.Cleanup(func() {
- serialtransport.SetTestWriteCap(0)
- serialtransport.SetTestForceWriteError(false)
- cancel()
- a.Stop()
- <-errCh
- })
- waitUnix(t, obs, 4*time.Second)
- return obs, ctl
- }
- func waitReadySnap(t *testing.T, obs string) *hthv1.Snapshot {
- t.Helper()
- _ = waitUntilReady(t, obs, 20*time.Second)
- waitSlewBudget()
- return waitUntilReady(t, obs, 20*time.Second)
- }
- func applyDoors(t *testing.T, obs, ctl, cmd string, door float64) *hthv1.ActionResult {
- t.Helper()
- oc, err := grpcserver.DialUnix(obs, time.Second)
- if err != nil {
- t.Fatal(err)
- }
- defer oc.Close()
- cc, err := grpcserver.DialUnix(ctl, time.Second)
- if err != nil {
- t.Fatal(err)
- }
- defer cc.Close()
- ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
- defer cancel()
- obsCli := hthv1.NewObserveServiceClient(oc)
- ctrl := hthv1.NewControlServiceClient(cc)
- lease, err := ctrl.AcquireControlLease(ctx, &hthv1.AcquireControlLeaseRequest{
- DeviceId: "prod2u-0", Holder: "pty-fault", Ttl: durationpb.New(20 * time.Second),
- })
- if err != nil {
- t.Fatal(err)
- }
- snap, err := obsCli.GetSnapshot(ctx, &hthv1.GetSnapshotRequest{DeviceId: "prod2u-0"})
- if err != nil {
- t.Fatal(err)
- }
- res, err := ctrl.ApplyAction(ctx, &hthv1.ApplyActionRequest{Batch: &hthv1.ActionBatch{
- DeviceId: "prod2u-0", CommandId: cmd, 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.DoorX, Value: door}, {Id: ids.DoorY, Value: door}, {Id: ids.DoorZ, Value: door},
- },
- }})
- if err != nil {
- t.Fatal(err)
- }
- return res
- }
- func TestPTYShortWriteOneByteReconnects(t *testing.T) {
- sim, err := devsim.Start(devsim.Options{StartupDone: true})
- if err != nil {
- t.Fatal(err)
- }
- defer sim.Close()
- obs, ctl := startSerialApp(t, sim)
- waitReadySnap(t, obs)
- serialtransport.SetTestWriteCap(1)
- res := applyDoors(t, obs, ctl, "one-byte", 55)
- if res.Confirmation != hthv1.ConfirmationStatus_CONFIRMATION_STATUS_OUTCOME_UNKNOWN {
- t.Fatalf("short write: %+v", res)
- }
- serialtransport.SetTestWriteCap(0)
- waitReadySnap(t, obs)
- res2 := applyDoors(t, obs, ctl, "after-one-byte", 10)
- if !res2.Accepted && res2.Confirmation == hthv1.ConfirmationStatus_CONFIRMATION_STATUS_OUTCOME_UNKNOWN {
- t.Fatalf("reconnect apply failed: %+v", res2)
- }
- }
- func TestPTYHalfFrameReconnects(t *testing.T) {
- sim, err := devsim.Start(devsim.Options{StartupDone: true})
- if err != nil {
- t.Fatal(err)
- }
- defer sim.Close()
- obs, ctl := startSerialApp(t, sim)
- waitReadySnap(t, obs)
- serialtransport.SetTestWriteCap(8)
- res := applyDoors(t, obs, ctl, "half", 60)
- if res.Confirmation != hthv1.ConfirmationStatus_CONFIRMATION_STATUS_OUTCOME_UNKNOWN {
- t.Fatalf("half frame: %+v", res)
- }
- serialtransport.SetTestWriteCap(0)
- }
- func TestPTYFullWriteErrorReconnects(t *testing.T) {
- sim, err := devsim.Start(devsim.Options{StartupDone: true})
- if err != nil {
- t.Fatal(err)
- }
- defer sim.Close()
- obs, ctl := startSerialApp(t, sim)
- waitReadySnap(t, obs)
- serialtransport.SetTestForceWriteError(true)
- res := applyDoors(t, obs, ctl, "full-err", 65)
- if res.Confirmation != hthv1.ConfirmationStatus_CONFIRMATION_STATUS_OUTCOME_UNKNOWN {
- t.Fatalf("full write+err: %+v", res)
- }
- }
- func TestPTYDisconnectInvalidatesOldLease(t *testing.T) {
- sim, err := devsim.Start(devsim.Options{StartupDone: true})
- if err != nil {
- t.Fatal(err)
- }
- defer sim.Close()
- obs, ctl := startSerialApp(t, sim)
- waitReadySnap(t, obs)
- oc, err := grpcserver.DialUnix(obs, time.Second)
- if err != nil {
- t.Fatal(err)
- }
- defer oc.Close()
- cc, err := grpcserver.DialUnix(ctl, time.Second)
- if err != nil {
- t.Fatal(err)
- }
- defer cc.Close()
- ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
- defer cancel()
- ctrl := hthv1.NewControlServiceClient(cc)
- obsCli := hthv1.NewObserveServiceClient(oc)
- lease, err := ctrl.AcquireControlLease(ctx, &hthv1.AcquireControlLeaseRequest{
- DeviceId: "prod2u-0", Holder: "disc", Ttl: durationpb.New(5 * time.Second),
- })
- if err != nil {
- t.Fatal(err)
- }
- e0 := waitReadySnap(t, obs).DeviceEpoch
- sim.Disconnect()
- deadline := time.Now().Add(8 * time.Second)
- bumped := false
- for time.Now().Before(deadline) {
- s, err := obsCli.GetSnapshot(context.Background(), &hthv1.GetSnapshotRequest{DeviceId: "prod2u-0"})
- if err == nil && s.Snapshot.DeviceEpoch > e0 {
- bumped = true
- break
- }
- time.Sleep(30 * time.Millisecond)
- }
- if !bumped {
- t.Fatal("epoch did not bump after PTY disconnect")
- }
- actx, acancel := context.WithTimeout(context.Background(), 3*time.Second)
- defer acancel()
- res, err := ctrl.ApplyAction(actx, &hthv1.ApplyActionRequest{Batch: &hthv1.ActionBatch{
- DeviceId: "prod2u-0", CommandId: "after-disc", LeaseId: lease.Lease.LeaseId, LeaseEpoch: lease.Lease.LeaseEpoch,
- ValidUntil: timestamppb.New(time.Now().Add(20 * time.Second)),
- ContinuousTargets: []*hthv1.ContinuousTarget{{Id: ids.Fan1, Value: 10}},
- }})
- if err != nil {
- t.Fatal(err)
- }
- if res.Accepted {
- t.Fatalf("old lease must not work after disconnect: %+v", res)
- }
- }
|