diff --git a/.changeset/plan-rev-once.md b/.changeset/plan-rev-once.md new file mode 100644 index 000000000..da70216a1 --- /dev/null +++ b/.changeset/plan-rev-once.md @@ -0,0 +1,5 @@ +--- +"ftw": patch +--- + +A replan now moves the plan number the app sees at most once, even with two phones or a phone and a watch connected. Before, two sessions reading the plan at the same moment could move it more than once for one replan. diff --git a/go/cmd/ftw/app_link.go b/go/cmd/ftw/app_link.go index 4ac24905e..89aeba800 100644 --- a/go/cmd/ftw/app_link.go +++ b/go/cmd/ftw/app_link.go @@ -552,14 +552,17 @@ func (a *appPlans) Latest() *mpc.Plan { // redelivery of the same one. Derived from the plan's own timestamp because // mpc has no revision of its own and inventing one there would be a field two // packages had to keep in step. +// +// The plan is read under revMu. Each session calls Rev, and a session that +// read the old plan outside the lock could count it again after another had +// counted the new one, moving the rev twice for one replan. func (a *appPlans) Rev() uint64 { + a.revMu.Lock() + defer a.revMu.Unlock() plan := a.Latest() if plan == nil { return 0 } - - a.revMu.Lock() - defer a.revMu.Unlock() if plan.GeneratedAtMs != a.lastGen { a.lastGen = plan.GeneratedAtMs a.rev++ diff --git a/go/cmd/ftw/app_link_test.go b/go/cmd/ftw/app_link_test.go index cbeae1bc2..ad9ef4391 100644 --- a/go/cmd/ftw/app_link_test.go +++ b/go/cmd/ftw/app_link_test.go @@ -11,6 +11,7 @@ import ( "github.com/srcfl/ftw/go/internal/config" "github.com/srcfl/ftw/go/internal/control" "github.com/srcfl/ftw/go/internal/loadpoint" + "github.com/srcfl/ftw/go/internal/mpc" "github.com/srcfl/ftw/go/internal/state" "github.com/srcfl/ftw/go/internal/telemetry" ) @@ -469,3 +470,33 @@ func TestAnOfflineSiteMeterSendsNoPhases(t *testing.T) { t.Fatalf("an offline meter sent phases %v", got) } } + +// One replan moves the plan's rev once, however many sessions read it. A +// session that read the old plan just before another session counted the new +// one must not count the old plan again when its turn comes. +func TestPlanRevMovesOncePerReplanAcrossSessions(t *testing.T) { + svc := mpc.New(nil, nil, "SE3", mpc.Params{}) + svc.InstallPlan(mpc.Plan{GeneratedAtMs: 1_760_000_000_000}, svc.Defaults, "") + plans := &appPlans{planner: svc} + if rev := plans.Rev(); rev != 1 { + t.Fatalf("first plan: rev %d, want 1", rev) + } + + // The test holds the counter. A session that reads the plan before it + // takes the counter still has the old plan when it gets it. + plans.revMu.Lock() + slow := make(chan uint64) + go func() { slow <- plans.Rev() }() + time.Sleep(50 * time.Millisecond) // let the slow session reach the counter + svc.InstallPlan(mpc.Plan{GeneratedAtMs: 1_760_000_060_000}, svc.Defaults, "") + // Another session counts the replan, as its own Rev does under the lock. + plans.lastGen, plans.rev = 1_760_000_060_000, 2 + plans.revMu.Unlock() + + if rev := <-slow; rev != 2 { + t.Errorf("the slow session got rev %d for the replan, want 2", rev) + } + if rev := plans.Rev(); rev != 2 { + t.Errorf("the next read got rev %d with no replan since, want 2", rev) + } +}