From b873e051b8237b7db5f1cfcd556ff6e1d3e781ae Mon Sep 17 00:00:00 2001 From: Simon Koudijs Date: Tue, 3 Nov 2026 22:16:36 +0000 Subject: [PATCH] test(queue): stop the trim rate-limit test racing its own first trim The opening publish trims after its XADD. With a 1 ms TTL, a pause over 1 ms between the two removed the entry the test then counted, about once in 2,400 runs under load. Let an opening publish start the interval and assert on the two entries after it. Co-Authored-By: Claude Opus 5.5 --- internal/queue/fact_stream_redis_test.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/internal/queue/fact_stream_redis_test.go b/internal/queue/fact_stream_redis_test.go index 4960bb6ce..f81f53fad 100644 --- a/internal/queue/fact_stream_redis_test.go +++ b/internal/queue/fact_stream_redis_test.go @@ -33,12 +33,16 @@ func TestRedisFactStream_StreamKeyLayout(t *testing.T) { // TestRedisFactStream_TrimIsRateLimited pins the amortized trim: the first publish on a stream // trims it, and a publish inside the same interval does not. +// +// Nothing is asserted about the opening publish's own entry. Its trim runs after its XADD, so with +// a 1 ms TTL a slow moment between the two ages that entry out. func TestRedisFactStream_TrimIsRateLimited(t *testing.T) { store, mr := newTestRedisStoreWithRedis(t) stream := store.FactStream(RedisFactStreamConfig{TTL: time.Millisecond, TrimInterval: time.Hour}) key := factStreamTestKey("", "configmaps") ctx := t.Context() + require.NoError(t, stream.PublishFacts(ctx, key, authorFacts("opening"))) require.NoError(t, stream.PublishFacts(ctx, key, authorFacts("first"))) time.Sleep(10 * time.Millisecond) require.NoError(t, stream.PublishFacts(ctx, key, authorFacts("second"))) @@ -47,7 +51,10 @@ func TestRedisFactStream_TrimIsRateLimited(t *testing.T) { // the TTL horizon is enforced only when the next trim comes due. entries, err := mr.Stream(stream.streamKey(key)) require.NoError(t, err) - require.Len(t, entries, 2) + require.GreaterOrEqual(t, len(entries), 2) + recent := entries[len(entries)-2:] + require.Contains(t, recent[0].Values[1], `"author":"first"`) + require.Contains(t, recent[1].Values[1], `"author":"second"`) } // TestRedisFactStream_StreamExpiresWhenNobodyWritesToItAnyMore pins the counterpart to the