| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- //go:build linux
- package tests
- import (
- "context"
- "os"
- "path/filepath"
- "testing"
- "time"
- "hth/internal/app"
- "hth/internal/testutil"
- )
- func countFDs(t *testing.T) int {
- t.Helper()
- ents, err := os.ReadDir("/proc/self/fd")
- if err != nil {
- t.Fatal(err)
- }
- return len(ents)
- }
- func TestMockAppStartStopDoesNotLeakFDs(t *testing.T) {
- before := countFDs(t)
- for i := 0; i < 3; i++ {
- dir := t.TempDir()
- obs := filepath.Join(dir, "observe.sock")
- ctl := filepath.Join(dir, "control.sock")
- yaml := `
- api_version: hth.api.v1
- device_id: prod2u-0
- backend: mock
- profile: ` + testutil.ProfilePath(t) + `
- runtime_root: ` + dir + `
- sockets:
- observe: ` + obs + `
- control: ` + ctl + `
- observe_perm: "0660"
- control_perm: "0660"
- poll:
- temperature_ms: 80
- 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: 2
- grpc:
- reflection: false
- mock:
- seed: 1
- startup_complete: true
- `
- cfgPath := filepath.Join(dir, "hthd.yaml")
- if err := os.WriteFile(cfgPath, []byte(yaml), 0o644); err != nil {
- t.Fatal(err)
- }
- a, err := app.New(cfgPath, "", nil)
- if err != nil {
- t.Fatal(err)
- }
- ctx, cancel := context.WithCancel(context.Background())
- errCh := make(chan error, 1)
- go func() { errCh <- a.Run(ctx) }()
- deadline := time.Now().Add(3 * time.Second)
- for time.Now().Before(deadline) {
- if _, err := os.Stat(obs); err == nil {
- if _, err := os.Stat(ctl); err == nil {
- break
- }
- }
- time.Sleep(20 * time.Millisecond)
- }
- cancel()
- a.Stop()
- select {
- case <-errCh:
- case <-time.After(5 * time.Second):
- t.Fatal("app.Run did not return after Stop")
- }
- }
- after := countFDs(t)
- if after > before+12 {
- t.Fatalf("FD leak: before=%d after=%d", before, after)
- }
- }
|