Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions libs/apps/logstream/backoff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,20 @@ 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())
elapsed := time.Since(start)

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() {
Expand All @@ -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")
})
}
23 changes: 17 additions & 6 deletions libs/apps/logstream/streamer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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),
Expand Down
Loading