diff --git a/go/internal/api/api.go b/go/internal/api/api.go index 0c85bf2bd..733cdbc29 100644 --- a/go/internal/api/api.go +++ b/go/internal/api/api.go @@ -309,13 +309,6 @@ type Server struct { // nil in production; SendWithGeneration still binds every real dispatch to // the selected running generation. beforeDriverControlSend func() - // beforeDriverControlStateLock is a package-test seam for the narrower - // lookup-to-lock lifecycle race. It runs after the state map lookup while - // the map lock is still held, before the per-driver state lock is taken. - beforeDriverControlStateLock func() - // beforeDriverDefaultStateLock is a package-test seam for the default path's - // lookup-to-lock lifecycle race. It is nil in production. - beforeDriverDefaultStateLock func() versionUpdateMu sync.Mutex driverUpdateMu sync.Mutex diff --git a/go/internal/api/api_driver_control.go b/go/internal/api/api_driver_control.go index 82006752f..aa8016746 100644 --- a/go/internal/api/api_driver_control.go +++ b/go/internal/api/api_driver_control.go @@ -129,7 +129,7 @@ func (s *Server) handleDriverControl(w http.ResponseWriter, r *http.Request) { // command. The response is written inside done, still under that lock, so // the status read that explains an error is the one that caused it. s.controlHolds.Send(r.Context(), name, control.ID, applied, body, - time.Duration(seconds)*time.Second, s.beforeDriverControlStateLock, + time.Duration(seconds)*time.Second, func(hold drivercontrol.Hold, err error) { if err != nil { s.writeControlSendError(w, name, err) @@ -254,5 +254,5 @@ func (s *Server) controlRegistry() drivercontrol.Registry { // it clears any hold under the per-driver lock and sends the driver's own // default. See drivercontrol.Holds.SendDefault. func (s *Server) SendDriverDefault(ctx context.Context, name string) error { - return s.controlHolds.SendDefault(ctx, name, s.beforeDriverDefaultStateLock) + return s.controlHolds.SendDefault(ctx, name) } diff --git a/go/internal/api/api_driver_control_test.go b/go/internal/api/api_driver_control_test.go index e96d26547..4f6e26ab2 100644 --- a/go/internal/api/api_driver_control_test.go +++ b/go/internal/api/api_driver_control_test.go @@ -493,12 +493,12 @@ func TestDriverControlBindsHoldToRestartedGeneration(t *testing.T) { t.Fatalf("hold generation = %d, want replacement generation %d", hold.Generation, newStatus.Generation) } - state := srv.controlHolds.Peek("heat") + state := srv.controlHolds.Seams().Peek("heat") if state == nil { t.Fatal("control state disappeared before expiry") } - fired := state.CurrentHold() - if err := srv.controlHolds.Expire("heat", state, fired); err != nil { + fired := srv.controlHolds.Seams().CurrentHold(state) + if err := srv.controlHolds.Seams().Expire("heat", state, fired); err != nil { t.Fatalf("expiry after generation-bound command = %v", err) } waitMetric(t, tel, "heat", "defaulted", 1) @@ -545,12 +545,12 @@ func TestDriverControlLookupRaceCannotOrphanStateOrTimer(t *testing.T) { var resumeOnce sync.Once resumeRequest := func() { resumeOnce.Do(func() { close(resume) }) } defer resumeRequest() - srv.beforeDriverControlStateLock = func() { + srv.controlHolds.Seams().BeforeStateLock(func() { pauseOnce.Do(func() { close(lookedUp) <-resume }) - } + }) postDone := make(chan *httptest.ResponseRecorder, 1) go func() { @@ -566,7 +566,7 @@ func TestDriverControlLookupRaceCannotOrphanStateOrTimer(t *testing.T) { lifecycleDone := make(chan error, 1) go func() { lifecycleDone <- test.lifecycle(srv, cfg) }() mapProbeDone := make(chan bool, 1) - go func() { mapProbeDone <- srv.controlHolds.Peek("heat") == nil }() + go func() { mapProbeDone <- srv.controlHolds.Seams().Peek("heat") == nil }() select { case removed := <-mapProbeDone: if removed { @@ -635,16 +635,16 @@ func TestDriverControlExpiryDefaultFailureBlocksUntilRecovery(t *testing.T) { `{"control":"set_offset","value":2,"duration_s":600}`); rec.Code != http.StatusOK { t.Fatalf("POST = %d, body %s", rec.Code, rec.Body.String()) } - state := srv.controlHolds.Peek("heat") + state := srv.controlHolds.Seams().Peek("heat") if state == nil { t.Fatal("missing control state") } - hold := state.CurrentHold() + hold := srv.controlHolds.Seams().CurrentHold(state) if hold == nil { t.Fatal("missing control hold") } - if err := srv.controlHolds.Expire("heat", state, hold); err == nil { + if err := srv.controlHolds.Seams().Expire("heat", state, hold); err == nil { t.Fatal("expiry hid a failed default") } if got := srv.controlHolds.Active("heat"); got != nil { @@ -750,7 +750,7 @@ func TestDriverControlDefaultPathClearsHold(t *testing.T) { if hold := srv.controlHolds.Active("heat"); hold != nil { t.Fatalf("default path left hold active: %+v", hold) } - if state := srv.controlHolds.Peek("heat"); state == nil { + if state := srv.controlHolds.Seams().Peek("heat"); state == nil { t.Fatal("default path removed the existing control state") } waitMetric(t, tel, "heat", "defaulted", 1) @@ -853,27 +853,27 @@ func TestSendDriverDefaultClearsOnlyHoldsArmedBeforeIt(t *testing.T) { if !ok { t.Fatal("driver not running") } - state := srv.controlHolds.State("heat") - state.Lock() // an operator request in flight + state := srv.controlHolds.Seams().State("heat") + srv.controlHolds.Seams().Lock(state) // an operator request in flight if tc.armBefore { - srv.controlHolds.ArmLocked("heat", state, "set_offset", 1.0, status.Generation, time.Hour) + srv.controlHolds.Seams().ArmLocked("heat", state, "set_offset", 1.0, status.Generation, time.Hour) } done := make(chan error, 1) go func() { done <- srv.SendDriverDefault(context.Background(), "heat") }() select { case err := <-done: if err != nil { - state.Unlock() + srv.controlHolds.Seams().Unlock(state) t.Fatalf("SendDriverDefault = %v", err) } case <-time.After(time.Second): - state.Unlock() + srv.controlHolds.Seams().Unlock(state) t.Fatal("SendDriverDefault waited for the operator request") } if !tc.armBefore { - srv.controlHolds.ArmLocked("heat", state, "set_offset", 2.0, status.Generation, time.Hour) + srv.controlHolds.Seams().ArmLocked("heat", state, "set_offset", 2.0, status.Generation, time.Hour) } - state.Unlock() + srv.controlHolds.Seams().Unlock(state) if tc.wantCleared { deadline := time.Now().Add(time.Second) @@ -896,14 +896,14 @@ func TestSendDriverDefaultClearsOnlyHoldsArmedBeforeIt(t *testing.T) { func TestSendDriverDefaultDoesNotRecreateStateAfterRemove(t *testing.T) { srv, _ := controlServer(t) - srv.controlHolds.State("heat") + srv.controlHolds.Seams().State("heat") paused := make(chan struct{}) resume := make(chan struct{}) - srv.beforeDriverDefaultStateLock = func() { + srv.controlHolds.Seams().BeforeDefaultStateLock(func() { close(paused) <-resume - } + }) result := make(chan error, 1) go func() { @@ -926,7 +926,7 @@ func TestSendDriverDefaultDoesNotRecreateStateAfterRemove(t *testing.T) { close(resume) t.Fatal("RemoveProbe deadlocked behind SendDriverDefault") } - if state := srv.controlHolds.Peek("heat"); state != nil { + if state := srv.controlHolds.Seams().Peek("heat"); state != nil { t.Fatalf("removed driver still has control state before default resumes: %p", state) } close(resume) @@ -939,14 +939,14 @@ func TestSendDriverDefaultDoesNotRecreateStateAfterRemove(t *testing.T) { case <-time.After(time.Second): t.Fatal("SendDriverDefault did not finish after lifecycle removal") } - if state := srv.controlHolds.Peek("heat"); state != nil { + if state := srv.controlHolds.Seams().Peek("heat"); state != nil { t.Fatalf("default path recreated removed control state: %p", state) } } func TestSendDriverDefaultDoesNotAccumulateRemovedStates(t *testing.T) { srv, _ := controlServer(t) - srv.controlHolds.State("heat") + srv.controlHolds.Seams().State("heat") base := srv.deps.Cfg.Drivers[0] for i := 0; i < 64; i++ { @@ -956,14 +956,14 @@ func TestSendDriverDefaultDoesNotAccumulateRemovedStates(t *testing.T) { if err := srv.deps.Registry.Add(context.Background(), cfg); err != nil { t.Fatalf("add %s: %v", name, err) } - srv.controlHolds.State(name) + srv.controlHolds.Seams().State(name) paused := make(chan struct{}) resume := make(chan struct{}) - srv.beforeDriverDefaultStateLock = func() { + srv.controlHolds.Seams().BeforeDefaultStateLock(func() { close(paused) <-resume - } + }) result := make(chan error, 1) go func() { result <- srv.SendDriverDefault(context.Background(), name) @@ -984,13 +984,13 @@ func TestSendDriverDefaultDoesNotAccumulateRemovedStates(t *testing.T) { case <-time.After(time.Second): t.Fatalf("SendDriverDefault(%s) did not finish", name) } - srv.beforeDriverDefaultStateLock = nil - if state := srv.controlHolds.Peek(name); state != nil { + srv.controlHolds.Seams().BeforeDefaultStateLock(nil) + if state := srv.controlHolds.Seams().Peek(name); state != nil { t.Fatalf("removed driver %s left a control state", name) } } - if got := srv.controlHolds.Len(); got != 1 { + if got := srv.controlHolds.Seams().Len(); got != 1 { t.Fatalf("control state map has %d entries after removed names, want only heat", got) } } @@ -1005,15 +1005,15 @@ func TestDriverControlSerializesExpiryAndReplacement(t *testing.T) { `{"control":"set_offset","value":1,"duration_s":600}`); rec.Code != http.StatusOK { t.Fatalf("POST = %d, body %s", rec.Code, rec.Body.String()) } - state := srv.controlHolds.State("heat") - hold := state.CurrentHold() + state := srv.controlHolds.Seams().State("heat") + hold := srv.controlHolds.Seams().CurrentHold(state) if hold == nil { t.Fatal("missing hold") } done := make(chan struct{}) go func() { - srv.controlHolds.Expire("heat", state, hold) + srv.controlHolds.Seams().Expire("heat", state, hold) close(done) }() waitMetric(t, tel, "heat", "default_started", 1) @@ -1116,11 +1116,11 @@ func TestDriverControlClearsHoldAcrossLifecycle(t *testing.T) { t.Fatalf("initial POST = %d, body %s", rec.Code, rec.Body.String()) } waitMetric(t, tel, "heat", "applied", 2) - oldState := srv.controlHolds.Peek("heat") + oldState := srv.controlHolds.Seams().Peek("heat") if oldState == nil { t.Fatal("initial control state is missing") } - oldHold := oldState.CurrentHold() + oldHold := srv.controlHolds.Seams().CurrentHold(oldState) if oldHold == nil { t.Fatal("initial control hold is missing") } @@ -1131,7 +1131,7 @@ func TestDriverControlClearsHoldAcrossLifecycle(t *testing.T) { if got := srv.controlHolds.Active("heat"); got != nil { t.Fatalf("hold survived restart: %+v", got) } - if got := srv.controlHolds.Peek("heat"); got != nil { + if got := srv.controlHolds.Seams().Peek("heat"); got != nil { t.Fatal("control state map entry survived restart") } if got := driverDetail(t, srv, "heat"); got.Hold != nil { @@ -1146,7 +1146,7 @@ func TestDriverControlClearsHoldAcrossLifecycle(t *testing.T) { waitMetric(t, tel, "heat", "applied", -2) // Simulate a timer callback that was already in flight when the old // generation was removed. It must not default the new instance. - srv.controlHolds.Expire("heat", oldState, oldHold) + srv.controlHolds.Seams().Expire("heat", oldState, oldHold) time.Sleep(250 * time.Millisecond) if got, _, ok := tel.LatestMetric("heat", "defaulted"); !ok || got != 0 { t.Fatalf("old timer changed the replacement: defaulted=%v/%v", got, ok) @@ -1158,7 +1158,7 @@ func TestDriverControlClearsHoldAcrossLifecycle(t *testing.T) { disabled := cfg disabled.Disabled = true srv.deps.Registry.Reload(context.Background(), []config.Driver{disabled}, false) - if got := srv.controlHolds.Peek("heat"); got != nil { + if got := srv.controlHolds.Seams().Peek("heat"); got != nil { t.Fatal("control state map entry survived disable") } if got := driverDetail(t, srv, "heat"); got.Hold != nil { @@ -1166,7 +1166,7 @@ func TestDriverControlClearsHoldAcrossLifecycle(t *testing.T) { } srv.deps.Registry.Reload(context.Background(), []config.Driver{cfg}, false) - if got := srv.controlHolds.Peek("heat"); got != nil { + if got := srv.controlHolds.Seams().Peek("heat"); got != nil { t.Fatal("control state map entry survived re-add") } if got := driverDetail(t, srv, "heat"); got.Hold != nil { @@ -1184,7 +1184,7 @@ func TestDriverDetailUnknownNamesDoNotCreateControlState(t *testing.T) { t.Fatalf("GET %s = %d, body %s", name, rec.Code, rec.Body.String()) } } - if got := srv.controlHolds.Len(); got != 0 { + if got := srv.controlHolds.Seams().Len(); got != 0 { t.Fatalf("unknown GETs created %d control states", got) } } diff --git a/go/internal/drivercontrol/holds.go b/go/internal/drivercontrol/holds.go index c4c572719..d9088f73b 100644 --- a/go/internal/drivercontrol/holds.go +++ b/go/internal/drivercontrol/holds.go @@ -46,33 +46,24 @@ type Hold struct { } // State serializes command dispatch, default dispatch and hold transitions -// for one driver. +// for one driver. It is opaque outside this package: callers go through +// Holds' methods, which take and release its lock themselves. type State struct { mu sync.Mutex hold *Hold } -// Lock takes the per-driver lock. Exported for tests that stand in for a -// command in flight; production callers use Holds' methods. -func (st *State) Lock() { st.mu.Lock() } - -// Unlock releases the per-driver lock. -func (st *State) Unlock() { st.mu.Unlock() } - -// CurrentHold returns the held setting itself, not a copy, or nil. Its -// identity is what Expire compares. -func (st *State) CurrentHold() *Hold { - st.mu.Lock() - defer st.mu.Unlock() - return st.hold -} - // Holds is every driver's State. type Holds struct { registry func() Registry mu sync.Mutex states map[string]*State + + // Test seams, nil in production and set only through Seams. Guarded by + // mu. + beforeStateLock func() + beforeDefaultStateLock func() } // New returns empty holds. registry is read at each use, so a caller whose @@ -97,10 +88,7 @@ func (h *Holds) reg() Registry { // done runs with the outcome while the lock is still held, so a caller that // reads registry status to explain an error sees it before any lifecycle // change waiting on this driver can proceed. -// -// beforeStateLock is a test seam, run after the state lookup and before the -// per-driver lock; nil in production. -func (h *Holds) Send(ctx context.Context, name, controlID string, value any, body []byte, d time.Duration, beforeStateLock func(), done func(Hold, error)) { +func (h *Holds) Send(ctx context.Context, name, controlID string, value any, body []byte, d time.Duration, done func(Hold, error)) { reg := h.reg() if reg == nil { done(Hold{}, errors.New("driver registry not available")) @@ -112,7 +100,7 @@ func (h *Holds) Send(ctx context.Context, name, controlID string, value any, bod // old status snapshot. The state lookup and lock are also one lifecycle // transaction: clearState takes the same locks in the same order, so it // cannot remove the map entry between these operations. - state := h.lock(name, beforeStateLock) + state := h.lock(name) defer state.mu.Unlock() sendCtx, cancel := context.WithTimeout(ctx, DefaultTimeout) defer cancel() @@ -122,19 +110,12 @@ func (h *Holds) Send(ctx context.Context, name, controlID string, value any, bod done(Hold{}, err) return } - hold := h.ArmLocked(name, state, controlID, value, generation, d) + hold := h.armLocked(name, state, controlID, value, generation, d) out := *hold out.timer = nil done(out, nil) } -// State returns the driver's state, creating it. -func (h *Holds) State(name string) *State { - h.mu.Lock() - defer h.mu.Unlock() - return h.stateLocked(name) -} - func (h *Holds) stateLocked(name string) *State { if h.states == nil { h.states = make(map[string]*State) @@ -152,11 +133,11 @@ func (h *Holds) stateLocked(name string) *State { // can either own the state before removal starts or see a removed state // after removal finishes; it cannot keep a pointer that was deleted in // between. -func (h *Holds) lock(name string, beforeStateLock func()) *State { +func (h *Holds) lock(name string) *State { h.mu.Lock() state := h.stateLocked(name) - if beforeStateLock != nil { - beforeStateLock() + if h.beforeStateLock != nil { + h.beforeStateLock() } state.mu.Lock() h.mu.Unlock() @@ -177,11 +158,11 @@ func (h *Holds) tryLockExisting(name string) (state *State, locked bool) { return state, state.mu.TryLock() } -// ArmLocked replaces any existing hold for the driver; the caller holds +// armLocked replaces any existing hold for the driver; the caller holds // state's lock. One driver holds one control at a time: two overlapping // holds on the same device would each expire into a default that undoes the // other. -func (h *Holds) ArmLocked(name string, state *State, control string, value any, generation uint64, d time.Duration) *Hold { +func (h *Holds) armLocked(name string, state *State, control string, value any, generation uint64, d time.Duration) *Hold { h.clearLocked(state) now := time.Now() hold := &Hold{ @@ -192,7 +173,7 @@ func (h *Holds) ArmLocked(name string, state *State, control string, value any, armedAt: now, } hold.timer = time.AfterFunc(d, func() { - if err := h.Expire(name, state, hold); err != nil { + if err := h.expire(name, state, hold); err != nil { slog.Error("control hold expiry default failed; registry recovery gate remains active", "driver", name, "err", err) } }) @@ -207,11 +188,11 @@ func (h *Holds) clearLocked(state *State) { state.hold = nil } -// Expire hands the device back to itself. It checks identity first: a hold +// expire hands the device back to itself. It checks identity first: a hold // that was replaced or released already had its timer stopped, but a timer // that had begun firing cannot be stopped, and defaulting a driver that an // operator has just set again is the one wrong answer here. -func (h *Holds) Expire(name string, state *State, fired *Hold) error { +func (h *Holds) expire(name string, state *State, fired *Hold) error { state.mu.Lock() defer state.mu.Unlock() if state.hold != fired { @@ -245,9 +226,7 @@ func (h *Holds) Expire(name string, state *State, fired *Hold) error { // cleared once the lock is free, but only a hold armed before the default // returned. A later one belongs to a command sent after the default and // keeps its own expiry. -// -// beforeDefaultStateLock is a test seam; nil in production. -func (h *Holds) SendDefault(ctx context.Context, name string, beforeDefaultStateLock func()) error { +func (h *Holds) SendDefault(ctx context.Context, name string) error { reg := h.reg() if reg == nil { return errors.New("driver registry not available") @@ -255,8 +234,11 @@ func (h *Holds) SendDefault(ctx context.Context, name string, beforeDefaultState if _, ok := reg.ControlStatus(name); !ok { return fmt.Errorf("driver %q not found", name) } - if beforeDefaultStateLock != nil { - beforeDefaultStateLock() + h.mu.Lock() + before := h.beforeDefaultStateLock + h.mu.Unlock() + if before != nil { + before() } state, locked := h.tryLockExisting(name) if locked { @@ -296,13 +278,36 @@ func (h *Holds) sendDefaultLocked(ctx context.Context, name string) error { // Active returns a copy of the driver's hold, or nil. A hold bound to a // generation that is no longer running is cleared, not returned. +// +// It takes the per-driver lock, so it waits for a command in flight, up to +// DefaultTimeout. That suits a request. The control tick must use TryActive. func (h *Holds) Active(name string) *Hold { - state := h.Peek(name) + state := h.peek(name) if state == nil { return nil } state.mu.Lock() defer state.mu.Unlock() + return h.activeLocked(name, state) +} + +// TryActive is Active for a caller that must not wait, as the control tick +// must not. known is false when a command or a default holds the driver's +// lock: the hold is then changing, and the caller should treat the driver as +// held and look again on its next tick rather than send anything. +func (h *Holds) TryActive(name string) (hold *Hold, known bool) { + state, locked := h.tryLockExisting(name) + if state == nil { + return nil, true + } + if !locked { + return nil, false + } + defer state.mu.Unlock() + return h.activeLocked(name, state), true +} + +func (h *Holds) activeLocked(name string, state *State) *Hold { if state.hold == nil { return nil } @@ -321,21 +326,14 @@ func (h *Holds) Active(name string) *Hold { return &hold } -// Peek returns the driver's state without creating one. -func (h *Holds) Peek(name string) *State { +// peek returns the driver's state without creating one. +func (h *Holds) peek(name string) *State { h.mu.Lock() state := h.states[name] h.mu.Unlock() return state } -// Len is how many drivers have state. For tests of lifecycle cleanup. -func (h *Holds) Len() int { - h.mu.Lock() - defer h.mu.Unlock() - return len(h.states) -} - // Clear is called by the registry before a driver generation is removed. It // stops the old timer and drops the map entry so a re-added driver cannot // inherit a hold from the previous instance. diff --git a/go/internal/drivercontrol/holds_test.go b/go/internal/drivercontrol/holds_test.go index 4c6654911..d01c0da36 100644 --- a/go/internal/drivercontrol/holds_test.go +++ b/go/internal/drivercontrol/holds_test.go @@ -56,7 +56,7 @@ func send(t *testing.T, h *Holds, value any, d time.Duration) (Hold, error) { t.Helper() var hold Hold var err error - h.Send(context.Background(), "heat", "set_offset", value, []byte(`{}`), d, nil, func(got Hold, e error) { + h.Send(context.Background(), "heat", "set_offset", value, []byte(`{}`), d, func(got Hold, e error) { hold, err = got, e }) return hold, err @@ -122,12 +122,12 @@ func TestExpiryOfAnOldGenerationSendsNoDefault(t *testing.T) { if _, err := send(t, h, 1.0, time.Hour); err != nil { t.Fatal(err) } - state := h.Peek("heat") - fired := state.CurrentHold() + state := h.peek("heat") + fired := h.Seams().CurrentHold(state) reg.mu.Lock() reg.generation = 2 reg.mu.Unlock() - if err := h.Expire("heat", state, fired); err != nil { + if err := h.expire("heat", state, fired); err != nil { t.Fatal(err) } if _, d := reg.counts(); d != 0 { @@ -142,12 +142,12 @@ func TestExpiryOfAReplacedHoldIsANoOp(t *testing.T) { if _, err := send(t, h, 1.0, time.Hour); err != nil { t.Fatal(err) } - state := h.Peek("heat") - old := state.CurrentHold() + state := h.peek("heat") + old := h.Seams().CurrentHold(state) if _, err := send(t, h, 2.0, time.Hour); err != nil { t.Fatal(err) } - if err := h.Expire("heat", state, old); err != nil { + if err := h.expire("heat", state, old); err != nil { t.Fatal(err) } if _, d := reg.counts(); d != 0 { @@ -164,7 +164,7 @@ func TestSendDefaultClearsTheHold(t *testing.T) { if _, err := send(t, h, 1.0, time.Hour); err != nil { t.Fatal(err) } - if err := h.SendDefault(context.Background(), "heat", nil); err != nil { + if err := h.SendDefault(context.Background(), "heat"); err != nil { t.Fatal(err) } if _, d := reg.counts(); d != 1 { @@ -177,7 +177,7 @@ func TestSendDefaultClearsTheHold(t *testing.T) { func TestSendDefaultForAnUnknownDriverFails(t *testing.T) { h := newHolds(&fakeRegistry{}) - if err := h.SendDefault(context.Background(), "heat", nil); err == nil { + if err := h.SendDefault(context.Background(), "heat"); err == nil { t.Fatal("default to a driver that is not running succeeded") } } @@ -189,8 +189,8 @@ func TestClearDropsStateAndTimer(t *testing.T) { t.Fatal(err) } h.Clear("heat") - if h.Len() != 0 || h.Peek("heat") != nil { - t.Fatalf("Clear left state: len=%d", h.Len()) + if len(h.states) != 0 || h.peek("heat") != nil { + t.Fatalf("Clear left state: len=%d", len(h.states)) } time.Sleep(60 * time.Millisecond) if _, d := reg.counts(); d != 0 { @@ -204,10 +204,45 @@ func TestNoRegistry(t *testing.T) { if _, err := send(t, h, 1.0, time.Hour); err == nil { t.Fatal("send without a registry succeeded") } - if err := h.SendDefault(context.Background(), "heat", nil); err == nil { + if err := h.SendDefault(context.Background(), "heat"); err == nil { t.Fatal("default without a registry succeeded") } if h.Active("heat") != nil { t.Fatal("hold without a registry") } } + +// The control tick reads a hold without waiting. While a command or a +// default holds the driver's lock it is told so, not kept waiting and not +// told there is no hold. +func TestTryActiveDoesNotWaitForACommandInFlight(t *testing.T) { + reg := &fakeRegistry{generation: 1, running: true} + h := newHolds(reg) + if hold, known := h.TryActive("heat"); hold != nil || !known { + t.Fatalf("TryActive without state = %+v, known %v; want nil, known", hold, known) + } + if _, err := send(t, h, 1.0, time.Hour); err != nil { + t.Fatal(err) + } + if hold, known := h.TryActive("heat"); !known || hold == nil || hold.Value != 1.0 { + t.Fatalf("TryActive = %+v, known %v; want the hold", hold, known) + } + + seams := h.Seams() + state := seams.Peek("heat") + seams.Lock(state) // a command in flight + done := make(chan bool, 1) + go func() { + hold, known := h.TryActive("heat") + done <- hold == nil && !known + }() + select { + case unknown := <-done: + if !unknown { + t.Error("TryActive behind a command in flight claimed to know the hold") + } + case <-time.After(time.Second): + t.Error("TryActive waited for the command in flight") + } + seams.Unlock(state) +} diff --git a/go/internal/drivercontrol/seams.go b/go/internal/drivercontrol/seams.go new file mode 100644 index 000000000..5d57329fb --- /dev/null +++ b/go/internal/drivercontrol/seams.go @@ -0,0 +1,84 @@ +package drivercontrol + +import ( + "testing" + "time" +) + +// Seams is what tests need to stand in for a request in flight, a timer +// that fired, or a lifecycle change between two steps. It reaches under the +// per-driver lock, which no production caller may do: a planned-value +// caller that held that lock itself could keep an operator's command, or +// the tick's safety default, waiting on it. +// +// The API package's race tests need it as well as this package's own, so it +// cannot live in a _test file. Seams panics outside a test binary instead. +type Seams struct{ h *Holds } + +// Seams returns the test access. It panics outside a test binary. +func (h *Holds) Seams() Seams { + if !testing.Testing() { + panic("drivercontrol: Seams is for tests only") + } + return Seams{h} +} + +// State returns the driver's state, creating it. +func (s Seams) State(name string) *State { + s.h.mu.Lock() + defer s.h.mu.Unlock() + return s.h.stateLocked(name) +} + +// Peek returns the driver's state without creating one. +func (s Seams) Peek(name string) *State { return s.h.peek(name) } + +// Len is how many drivers have state. +func (s Seams) Len() int { + s.h.mu.Lock() + defer s.h.mu.Unlock() + return len(s.h.states) +} + +// Lock takes the per-driver lock, as a command in flight does. +func (s Seams) Lock(st *State) { st.mu.Lock() } + +// Unlock releases the per-driver lock. +func (s Seams) Unlock(st *State) { st.mu.Unlock() } + +// CurrentHold returns the held setting itself, not a copy, or nil. Its +// identity is what Expire compares. +func (s Seams) CurrentHold(st *State) *Hold { + st.mu.Lock() + defer st.mu.Unlock() + return st.hold +} + +// ArmLocked arms a hold as Send does after its command; the caller holds +// st's lock. +func (s Seams) ArmLocked(name string, st *State, control string, value any, generation uint64, d time.Duration) *Hold { + return s.h.armLocked(name, st, control, value, generation, d) +} + +// Expire runs what a hold's timer runs. +func (s Seams) Expire(name string, st *State, fired *Hold) error { + return s.h.expire(name, st, fired) +} + +// BeforeStateLock sets a function Send runs after the state lookup, while +// the map lock is held and before the per-driver lock is taken. nil clears +// it. +func (s Seams) BeforeStateLock(fn func()) { + s.h.mu.Lock() + defer s.h.mu.Unlock() + s.h.beforeStateLock = fn +} + +// BeforeDefaultStateLock sets a function SendDefault runs after it has +// found the driver and before it looks for the driver's state. nil clears +// it. +func (s Seams) BeforeDefaultStateLock(fn func()) { + s.h.mu.Lock() + defer s.h.mu.Unlock() + s.h.beforeDefaultStateLock = fn +}