diff --git a/internal/api/chief_host_test.go b/internal/api/chief_host_test.go index 2586ebea5..536682c49 100644 --- a/internal/api/chief_host_test.go +++ b/internal/api/chief_host_test.go @@ -208,9 +208,12 @@ func TestChoosingAMachineStartsTheChiefThere(t *testing.T) { // the same row. func TestChoosingTheMachineAgainNeverStartsASecondChief(t *testing.T) { r := newHostChiefRig(t) - clock := time.Now() - r.s.now = func() time.Time { return clock } - r.host.Now = func() time.Time { return clock } + // A locked clock: a choose's spawn reply is applied on its own goroutine, + // which reads s.now after the mirror has seen the spawn, while the test + // moves the clock on. + clock := &outcomeClock{t: time.Now()} + r.s.now = clock.Now + r.host.Now = clock.Now rec := r.started(t) spawns := func() int { n, _, _ := r.m.counts(); return n } again := func(want string) { @@ -237,9 +240,9 @@ func TestChoosingTheMachineAgainNeverStartsASecondChief(t *testing.T) { } // A spawn whose reply has not come: up inside the countdown, down past it. pending, _ := r.host.BeginChiefSpawn(chiefHostAs(chiefHostOwner), chiefHostOwner, hostOffHost) - clock = clock.Add(hostPendingCountdown - time.Second) + clock.Add(hostPendingCountdown - time.Second) again("chief=hostrunning") - clock = clock.Add(2 * time.Second) + clock.Add(2 * time.Second) again("chief=hostsaved") <-r.m.spawned // A refused spawn is not up. diff --git a/internal/api/chief_host_turns_test.go b/internal/api/chief_host_turns_test.go index d6b50f834..ed7fbccd8 100644 --- a/internal/api/chief_host_turns_test.go +++ b/internal/api/chief_host_turns_test.go @@ -10,6 +10,7 @@ import ( "strconv" "strings" "sync" + "sync/atomic" "testing" "time" @@ -467,13 +468,13 @@ func TestTheChiefsChatIsItsWindowsRenderedView(t *testing.T) { } // reconcileRig is the rig with its clock under the test's hand. -func reconcileRig(t *testing.T) (*hostChiefRig, *time.Time) { +func reconcileRig(t *testing.T) (*hostChiefRig, *outcomeClock) { t.Helper() r := newHostChiefRig(t) - clock := time.Date(2031, 4, 5, 6, 7, 8, 0, time.UTC) - r.s.now = func() time.Time { return clock } - r.host.Now = func() time.Time { return clock } - return r, &clock + clock := &outcomeClock{t: time.Date(2031, 4, 5, 6, 7, 8, 0, time.UTC)} + r.s.now = clock.Now + r.host.Now = clock.Now + return r, clock } // spawnsSent is how many chief spawns the fake host received. @@ -526,7 +527,7 @@ func TestTheReconcilerRestartsAGoneChiefWithoutALoop(t *testing.T) { gone() step := func(d time.Duration, want string) { t.Helper() - *clock = clock.Add(d) + clock.Add(d) if got := r.s.reconcileHostChief(context.Background(), chiefHostOwner); got != want { t.Fatalf("at +%s: %q, want %q", d, got, want) } @@ -572,7 +573,7 @@ func TestTheReconcilerRestartsAGoneChiefWithoutALoop(t *testing.T) { cur, _ = r.host.HostChief(chiefHostAs(chiefHostOwner), chiefHostOwner) gone() step(0, hostChiefWaiting) - *clock = clock.Add(time.Hour) + clock.Add(time.Hour) _ = r.prefs.SetEnabled(context.Background(), chiefHostOwner, false) if got := r.s.reconcileHostChief(context.Background(), chiefHostOwner); !strings.HasPrefix(got, hostChiefNotStarted) || !strings.Contains(got, "off") { t.Errorf("the chief off: %q", got) @@ -803,6 +804,16 @@ func TestARestartNeverLeavesASecondChiefWindow(t *testing.T) { // closes nothing, and the list forgets the id (it is bounded). func TestARestartPastTheCountdownNeedsNoForce(t *testing.T) { r := newHostChiefRig(t) + // The real time until the test sets one, installed BEFORE the choose: its + // spawn reply is applied on its own goroutine, which reads s.now. + var fixed atomic.Pointer[time.Time] + r.s.now = func() time.Time { + if at := fixed.Load(); at != nil { + return *at + } + return time.Now() + } + setClock := func(at time.Time) { fixed.Store(&at) } r.m.mu.Lock() r.m.spawnErr = mirror.ErrSpawnUnconfirmed r.m.mu.Unlock() @@ -814,14 +825,13 @@ func TestARestartPastTheCountdownNeedsNoForce(t *testing.T) { first, _ := r.host.HostChief(ctx, chiefHostOwner) r.m.list(hostOffHost, mirror.Window{ID: turnWinOther, Session: "work", Claude: true, ClaudeSession: chiefHostSess2}) mine := toolCaller{asker: attention.Asker{MirrorHost: hostOffHost, MirrorSession: chiefHostSess2}, mirrorWindow: turnWinOther} - clock := first.SpawnedAt.Add(hostPendingCountdown + time.Second) - r.s.now = func() time.Time { return clock } + setClock(first.SpawnedAt.Add(hostPendingCountdown + time.Second)) if out := r.s.toolChiefRestart(ctx, mine, `{}`); isToolErr(out) || !strings.Contains(out, `"restarted":true`) { t.Fatalf("a spawn awaited past the countdown, no force: %s, want restarted", out) } r.awaitSent(t, 2) // Past hostChiefSentKept since it was sent: its late reply closes nothing. - clock = first.SpawnedAt.Add(hostChiefSentKept + time.Second) + setClock(first.SpawnedAt.Add(hostChiefSentKept + time.Second)) late := mirror.Msg{Type: mirror.TypeAgentSpawned, Dispatch: first.SpawnID, Spawned: &mirror.AgentSpawned{Session: mirror.AgentSpawnSession, Window: "@83", AgentSession: turnSess3}} if r.s.applyChiefSpawnReply(context.Background(), chiefHostOwner, hostOffHost, late) { @@ -879,7 +889,7 @@ func TestTheSweepReconcilesEveryHostChief(t *testing.T) { r.started(t) r.m.list(hostOffHost) r.s.sweepHostChiefs(context.Background()) - *clock = clock.Add(hostChiefRestartBase) + clock.Add(hostChiefRestartBase) r.s.sweepHostChiefs(context.Background()) r.awaitSpawns(t, 2) r.s.ext.ChiefHost = nil @@ -962,7 +972,7 @@ func TestNothingOfTheHostChiefWithoutTheOptIn(t *testing.T) { t.Errorf("hostChiefTurn = %v, want not sent", err) } r.m.list(hostOffHost) - *clock = clock.Add(time.Hour) + clock.Add(time.Hour) if got := r.s.reconcileHostChief(context.Background(), chiefHostOwner); got != hostChiefOff { t.Errorf("reconcile = %q, want off", got) }