From 8d73f705afb9a7e93059d19be27a70400400890f Mon Sep 17 00:00:00 2001 From: jochen Date: Sun, 11 Oct 2026 21:05:04 +0200 Subject: [PATCH] Wait for the build's log lines to be stored before counting them (hq issue 507) A line is said with a plain publish, so a live subscriber can hold both lines while the stream has stored one or none yet; the test read the stream once and failed under load. It now waits up to 5 s for both. --- internal/link/builds_nats_test.go | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/internal/link/builds_nats_test.go b/internal/link/builds_nats_test.go index b3661388..6b405d42 100644 --- a/internal/link/builds_nats_test.go +++ b/internal/link/builds_nats_test.go @@ -123,15 +123,24 @@ func TestNatsABuildIsTakenAndItsOutcomeReachesEverybody(t *testing.T) { t.Fatalf("line %d came back as %s (%v)", want, msg.Data, err) } } - // And it is in the stream for a reader who comes later. - info, err := js.Context().StreamInfo(broker.EventsStream, &nats.StreamInfoRequest{SubjectsFilter: BuildLog("b-1")}) - if err != nil { - t.Fatal(err) + // And it is in the stream for a reader who comes later. **Waited for, with a bound, rather + // than read once** (novox/hq issue 507): a line is said with a plain publish, so the server + // hands it to a live subscriber and stores it in the stream independently, and under load the + // subscriber above had both lines while the stream still held one, or none. + var held map[string]uint64 + for until := time.Now().Add(5 * time.Second); ; { + info, err := js.Context().StreamInfo(broker.EventsStream, &nats.StreamInfoRequest{SubjectsFilter: BuildLog("b-1")}) + if err != nil { + t.Fatal(err) + } + held = info.State.Subjects + if held[BuildLog("b-1")] >= 2 || time.Now().After(until) { + break + } + time.Sleep(10 * time.Millisecond) } - if info.State.Subjects[BuildLog("b-1")] != 2 { - t.Fatalf("the stream holds %v under the build's subject, want 2", info.State.Subjects) - } - if err == nil { + if held[BuildLog("b-1")] != 2 { + t.Fatalf("the stream holds %v under the build's subject, want 2", held) } if result.ID != "b-1" || result.Commit != "abc1234" { t.Fatalf("the asker got %+v", result)