From 8336a73e4d33448b133e9f805e84a5f9c98ff3b3 Mon Sep 17 00:00:00 2001 From: Julian Sommer <115516918+juliansommer@users.noreply.github.com> Date: Thu, 27 Aug 2026 16:12:22 +1000 Subject: [PATCH] fix: stop the leader-bucket test racing its own create advisory The server queues the API audit advisory after the reply the caller waits on, so subscribing once the bucket exists could still catch the create the first call made. Watch from the start and account for it. --- spinifex/handlers/ecs/store_test.go | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/spinifex/handlers/ecs/store_test.go b/spinifex/handlers/ecs/store_test.go index 1caceff64..a7fb22c42 100644 --- a/spinifex/handlers/ecs/store_test.go +++ b/spinifex/handlers/ecs/store_test.go @@ -72,12 +72,9 @@ func TestInitLeaderBucket_Idempotent(t *testing.T) { func TestInitLeaderBucket_AttachesWithoutCreating(t *testing.T) { _, nc, js := testutil.StartTestJetStream(t) - _, err := InitLeaderBucket(t.Context(), js) - require.NoError(t, err) - - // Subscribe only after the bucket exists, so the first, legitimate create is - // not counted. The server drops advisories when nothing is listening, so the - // subscription has to be flushed before the call under test. + // Watch from before the first call. The server queues the API advisory after + // the reply the caller waits on, so subscribing once the bucket exists can + // still catch the create that first call legitimately made. creates := make(chan string, 8) sub, err := nc.Subscribe("$JS.EVENT.ADVISORY.API", func(m *nats.Msg) { var adv struct { @@ -92,6 +89,17 @@ func TestInitLeaderBucket_AttachesWithoutCreating(t *testing.T) { defer func() { _ = sub.Unsubscribe() }() require.NoError(t, nc.Flush()) + _, err = InitLeaderBucket(t.Context(), js) + require.NoError(t, err) + + // Take the first, legitimate create off the channel before the call under + // test, so anything left there afterwards can only have come from it. + select { + case <-creates: + case <-time.After(5 * time.Second): + t.Fatal("the first InitLeaderBucket must create the bucket") + } + _, err = InitLeaderBucket(t.Context(), js) require.NoError(t, err) require.NoError(t, nc.Flush())