From ed811661f709011c8b517376819c733a13adead4 Mon Sep 17 00:00:00 2001 From: "denis.bilenko" Date: Fri, 13 Nov 2026 16:14:50 +0000 Subject: [PATCH] Fix timing flakes in libs/apps/logstream tests TestBackoffStrategy_Wait asserted elapsed < 100ms around 50ms/20ms sleeps; CI runners observed 100-222ms. Keep the lower bound and use upper bounds that still distinguish "waited current" from "waited max" / "did not return on cancel". TestLogStreamerRetriesOnDialFailure gave the whole retry 300ms, of which 200ms is initialReconnectBackoff. Wait for msg1 with Eventually instead of a fixed deadline. TestLogStreamerRefreshesTokenAfterAuthClose read a bytes.Buffer from the test goroutine while the streamer wrote to it (go test -race); use syncBuffer like the other tests. Co-authored-by: Isaac --- libs/apps/logstream/backoff_test.go | 9 +++++---- libs/apps/logstream/streamer_test.go | 23 +++++++++++++++++------ 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/libs/apps/logstream/backoff_test.go b/libs/apps/logstream/backoff_test.go index ec55c5c3630..69b54293f3f 100644 --- a/libs/apps/logstream/backoff_test.go +++ b/libs/apps/logstream/backoff_test.go @@ -40,7 +40,8 @@ func TestBackoffStrategy_Reset(t *testing.T) { func TestBackoffStrategy_Wait(t *testing.T) { t.Run("blocks for duration", func(t *testing.T) { - b := newBackoffStrategy(50*time.Millisecond, 100*time.Millisecond) + // Upper bounds are loose: CI runners can stall a goroutine for 100ms+. + b := newBackoffStrategy(50*time.Millisecond, 10*time.Second) start := time.Now() err := b.Wait(t.Context()) @@ -48,11 +49,11 @@ func TestBackoffStrategy_Wait(t *testing.T) { require.NoError(t, err) assert.GreaterOrEqual(t, elapsed, 50*time.Millisecond) - assert.Less(t, elapsed, 100*time.Millisecond) + assert.Less(t, elapsed, 5*time.Second, "should wait the current duration, not the max") }) t.Run("returns early on cancel", func(t *testing.T) { - b := newBackoffStrategy(1*time.Second, 5*time.Second) + b := newBackoffStrategy(10*time.Second, 20*time.Second) ctx, cancel := context.WithCancel(t.Context()) go func() { @@ -65,6 +66,6 @@ func TestBackoffStrategy_Wait(t *testing.T) { elapsed := time.Since(start) assert.ErrorIs(t, err, context.Canceled) - assert.Less(t, elapsed, 100*time.Millisecond, "should return early on cancel") + assert.Less(t, elapsed, 5*time.Second, "should return early on cancel") }) } diff --git a/libs/apps/logstream/streamer_test.go b/libs/apps/logstream/streamer_test.go index b00eda9994a..eacc2f6f936 100644 --- a/libs/apps/logstream/streamer_test.go +++ b/libs/apps/logstream/streamer_test.go @@ -148,22 +148,33 @@ func TestLogStreamerRetriesOnDialFailure(t *testing.T) { }) defer server.Close() - buf := &bytes.Buffer{} + writer := &syncBuffer{} streamer := &logStreamer{ dialer: &flakyDialer{failures: 1, inner: &websocket.Dialer{}}, url: toWebSocketURL(server.URL), tail: 0, follow: true, prefetch: 0, - writer: buf, + writer: writer, formatter: newLogFormatter(false, flags.OutputText), } - ctx, cancel := context.WithTimeout(t.Context(), 300*time.Millisecond) + ctx, cancel := context.WithCancel(t.Context()) defer cancel() - require.ErrorIs(t, streamer.Run(ctx), context.DeadlineExceeded) - assert.Contains(t, buf.String(), "msg1") + done := make(chan error, 1) + go func() { + done <- streamer.Run(ctx) + }() + + // The retry happens only after initialReconnectBackoff, so a fixed short + // deadline leaves little time for the reconnect on a slow CI runner. + require.Eventually(t, func() bool { + return strings.Contains(writer.String(), "msg1") + }, 10*time.Second, 10*time.Millisecond, "expected a message after retrying the failed dial") + cancel() + + require.ErrorIs(t, <-done, context.Canceled) } func TestLogStreamerSendsSearchTerm(t *testing.T) { @@ -627,7 +638,7 @@ func TestLogStreamerRefreshesTokenAfterAuthClose(t *testing.T) { return "fresh", nil } - buf := &bytes.Buffer{} + buf := &syncBuffer{} streamer := &logStreamer{ dialer: &websocket.Dialer{}, url: toWebSocketURL(server.URL),