Presenting: the held-stream test waits on the hold, not a clock
Review fix round 3. The regression test slept and hoped the stream was held; a late lease callback would let A then B through without the catch-up ever running. streamTrace, a test-only func var (nil in production), is told when a presentation stream enters a hold and what ended it. The test waits for the hold, sets the retry to an hour so only A's event can end it, insists A's event did, and asserts the stream never goes back in seq.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2 files changed,
+66
−11
internal/slides/presentation.go+30 −7internal/slides/presenting_position_test.go+36 −4
diff --git a/internal/slides/presentation.go b/internal/slides/presentation.go| index f0791fe..5ffe577 100644 |
| --- a/internal/slides/presentation.go |
| +++ b/internal/slides/presentation.go |
| @@ -1131,6 +1131,18 @@ const ( |
| streamTTL = 5 * time.Minute |
| ) |
| +// streamTrace, when a test sets it, is told when a presentation stream |
| +// enters a hold and what ended one ("caught up by an event", "… by a |
| +// ping", "… on retry"), so the test can order its steps against the |
| +// hold rather than against a clock. Nil in production. |
| +var streamTrace func(state string) |
| + |
| +func traceStream(state string) { |
| + if streamTrace != nil { |
| + streamTrace(state) |
| + } |
| +} |
| + |
| // presentStream is the session's SSE feed: `event: slide` carrying the |
| // whole state as JSON, one event on connect, and a comment ping in |
| // between. |
| @@ -1222,6 +1234,9 @@ func (a *app) streamPresentation(w http.ResponseWriter, r *http.Request, p Prese |
| case presenterGone: |
| return false |
| case presenterUnknown: |
| + if !held { |
| + traceStream("held") |
| + } |
| held, next = true, streamHoldRetry |
| default: |
| held = false |
| @@ -1290,8 +1305,9 @@ func (a *app) streamPresentation(w http.ResponseWriter, r *http.Request, p Prese |
| } |
| // open runs the checks before any write: false ends the stream. A |
| // stream that was held and stands again is caught up first, so |
| - // whatever the caller writes next is compared with the row. |
| - open := func() bool { |
| + // whatever the caller writes next is compared with the row. why |
| + // names the caller, for traceStream. |
| + open := func(why string) bool { |
| if alive != nil && !alive() { |
| return false |
| } |
| @@ -1299,16 +1315,20 @@ func (a *app) streamPresentation(w http.ResponseWriter, r *http.Request, p Prese |
| if !stands() { |
| return false |
| } |
| - return held || !wasHeld || catchUp() |
| + if held || !wasHeld { |
| + return true |
| + } |
| + traceStream("caught up by " + why) |
| + return catchUp() |
| } |
| // ping is the heartbeat, which a held stream still sends. |
| ping := func() bool { |
| - return open() && send(": ping\n\n") |
| + return open("a ping") && send(": ping\n\n") |
| } |
| // event is one of the hub's events. A held stream drops it; the |
| // catch-up writes the row when the hold ends. |
| event := func(e present.Event) bool { |
| - if !open() { |
| + if !open("an event") { |
| return false |
| } |
| return held || deliver(e) |
| @@ -1359,8 +1379,11 @@ func (a *app) streamPresentation(w http.ResponseWriter, r *http.Request, p Prese |
| if !stands() { |
| return |
| } |
| - if wasHeld && !held && !catchUp() { |
| - return |
| + if wasHeld && !held { |
| + traceStream("caught up on retry") |
| + if !catchUp() { |
| + return |
| + } |
| } |
| } |
| } |
diff --git a/internal/slides/presenting_position_test.go b/internal/slides/presenting_position_test.go| index 2c6c915..4971fe8 100644 |
| --- a/internal/slides/presenting_position_test.go |
| +++ b/internal/slides/presenting_position_test.go |
| @@ -13,6 +13,7 @@ import ( |
| "testing" |
| "time" |
| + "amadan.net/oficina/home/roster" |
| "amadan.net/oficina/slides/internal/slides/present" |
| ) |
| @@ -173,11 +174,38 @@ func readSlideEvent(t *testing.T, r *bufio.Reader) present.Event { |
| // unknown): A commits (seq 4), B commits (seq 5), and the hold ends on |
| // the arrival of A's late event. The catch-up writes the row, B, and A |
| // must not be written after it; nor may B's own event, arriving last, |
| -// be lost. The page ends at B. |
| +// be lost. The page never goes back, and ends at B. |
| // |
| -// The hold re-asks at the roster's own 5 s, not shortHold's 100 ms, so |
| -// it is A's event, not the retry timer, that ends the hold. |
| +// The test orders itself against the stream, not a clock: streamTrace |
| +// says when the stream enters its hold and what ended it. The hold's |
| +// retry is an hour, so only A's event can end it, and the test insists |
| +// that it did. |
| func TestAHeldStreamNeverGoesBackToAnOlderPress(t *testing.T) { |
| + trace := make(chan string, 16) |
| + streamTrace = func(state string) { |
| + select { |
| + case trace <- state: |
| + default: |
| + } |
| + } |
| + streamHoldRetry = time.Hour |
| + t.Cleanup(func() { streamTrace, streamHoldRetry = nil, roster.RetryAfter }) |
| + waitFor := func(want string) { |
| + t.Helper() |
| + deadline := time.After(10 * time.Second) |
| + for { |
| + select { |
| + case got := <-trace: |
| + if got == want { |
| + return |
| + } |
| + t.Fatalf("the stream said %q, waiting for %q", got, want) |
| + case <-deadline: |
| + t.Fatalf("the stream never said %q", want) |
| + } |
| + } |
| + } |
| + |
| f := newTeamFixture(t) |
| colleague := f.member(teamSubject('b'), "member") |
| plan := f.deck(colleague, "Team plan", rungTeam) |
| @@ -191,7 +219,7 @@ func TestAHeldStreamNeverGoesBackToAnOlderPress(t *testing.T) { |
| f.clock.Step(59 * time.Second) |
| feed := lines(f.openStream(t, "/p/"+session+"/stream", false)) |
| f.clock.Step(2 * time.Second) |
| - time.Sleep(1500 * time.Millisecond) // past the stream's lease timer: held |
| + waitFor("held") // the stream's lease ran out with the roster down |
| // Both committed, in order: the row holds B. |
| a2, b := p, p |
| @@ -206,6 +234,7 @@ func TestAHeldStreamNeverGoesBackToAnOlderPress(t *testing.T) { |
| f.dir.SetDown(false) |
| f.refreshed() |
| f.a.hub.Publish(session, eventFor(a2)) |
| + waitFor("caught up by an event") // A's arrival ended the hold |
| f.a.hub.Publish(session, eventFor(b)) |
| f.a.hub.Publish(session, present.Event{Ended: true}) |
| @@ -232,6 +261,9 @@ func TestAHeldStreamNeverGoesBackToAnOlderPress(t *testing.T) { |
| } |
| return |
| } |
| + if e.Seq < last.Seq { |
| + t.Errorf("the held stream went back from seq %d to %d (wrote seqs %v)", last.Seq, e.Seq, append(written, e.Seq)) |
| + } |
| written = append(written, e.Seq) |
| last = e |
| case <-deadline: |