From 9fa1c0b8d6a5e605ee38c96754b9327b7e4d850d Mon Sep 17 00:00:00 2001 From: "denis.bilenko" Date: Wed, 11 Nov 2026 19:44:08 +0000 Subject: [PATCH 1/2] Fix flaky TestLogStreamerTailFlushesWithoutFollow The test gave the prefetch flush 150ms (measured from before the dial) to show up, which a loaded runner can miss. Make it deterministic instead: the server keeps the socket open until the test ends, so Run returning at all proves the prefetch timer flushed the tail, and the dial returns only after the server has written every entry, so the timer cannot fire on a partial tail. Co-authored-by: Isaac --- libs/apps/logstream/streamer_test.go | 96 +++++++++++++++------------- 1 file changed, 51 insertions(+), 45 deletions(-) diff --git a/libs/apps/logstream/streamer_test.go b/libs/apps/logstream/streamer_test.go index f264765342b..cda1c05c077 100644 --- a/libs/apps/logstream/streamer_test.go +++ b/libs/apps/logstream/streamer_test.go @@ -360,6 +360,24 @@ func (f *flakyDialer) DialContext(ctx context.Context, urlStr string, requestHea return f.inner.DialContext(ctx, urlStr, requestHeader) } +// waitDialer delays returning a dialed connection until wait is closed. +type waitDialer struct { + inner Dialer + wait <-chan struct{} +} + +func (d *waitDialer) DialContext(ctx context.Context, urlStr string, requestHeader http.Header) (*websocket.Conn, *http.Response, error) { + conn, resp, err := d.inner.DialContext(ctx, urlStr, requestHeader) + if err != nil { + return conn, resp, err + } + select { + case <-d.wait: + case <-ctx.Done(): + } + return conn, resp, nil +} + func newTestLogServer(t *testing.T, handler func(int, *websocket.Conn)) *httptest.Server { upgrader := websocket.Upgrader{} var connCount atomic.Int32 @@ -409,39 +427,47 @@ func sendEntry(conn *websocket.Conn, ts float64, message string) error { func TestLogStreamerTailFlushesWithoutFollow(t *testing.T) { t.Parallel() + sent := make(chan struct{}) + release := make(chan struct{}) + // Deferred calls run before t.Cleanup, which waits for the handler to return. + defer close(release) + server := newTestLogServer(t, func(id int, conn *websocket.Conn) { defer conn.Close() - _, _, _ = conn.ReadMessage() for i := 1; i <= 4; i++ { require.NoError(t, sendEntry(conn, float64(i), fmt.Sprintf("line%d", i))) } - time.Sleep(250 * time.Millisecond) + close(sent) + // Keep the socket open until the test is done: the only way for the + // streamer to emit the tail is the prefetch timer, not a server close. + <-release _ = conn.WriteControl(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""), time.Now().Add(time.Second)) }) defer server.Close() - writer := newNotifyBuffer() + writer := &syncBuffer{} streamer := &logStreamer{ - dialer: &websocket.Dialer{}, + // The prefetch timer starts once the connection is established. Waiting + // for the server to finish writing means all entries are already buffered + // on the client socket when it starts, so a slow runner cannot fire the + // timer before they are read and flush a partial tail. + dialer: &waitDialer{inner: &websocket.Dialer{}, wait: sent}, url: toWebSocketURL(server.URL), token: "token", tail: 2, follow: false, - prefetch: 50 * time.Millisecond, + prefetch: 200 * time.Millisecond, writer: writer, formatter: newLogFormatter(false, flags.OutputText), } - ctx, cancel := context.WithTimeout(t.Context(), time.Second) + ctx, cancel := context.WithTimeout(t.Context(), 10*time.Second) defer cancel() - done := make(chan error, 1) - go func() { - done <- streamer.Run(ctx) - }() - - require.Eventually(t, writer.hasWrite, 150*time.Millisecond, 10*time.Millisecond, "expected tail logs to flush before the server closed the socket") - require.NoError(t, <-done) + // Without follow, Run returns once the prefetch timer flushes the tail. + // The server never closes the socket on its own, so a flush that depends on + // the close would hang here until the context deadline. + require.NoError(t, streamer.Run(ctx), "expected tail logs to flush before the server closed the socket") lines := strings.Split(strings.TrimSpace(writer.String()), "\n") require.Len(t, lines, 2) assert.Contains(t, lines[0], "line3") @@ -461,7 +487,7 @@ func TestLogStreamerFollowTailWithoutPrefetchEmitsRequestedLines(t *testing.T) { }) defer server.Close() - writer := newNotifyBuffer() + writer := &syncBuffer{} streamer := &logStreamer{ dialer: &websocket.Dialer{}, url: toWebSocketURL(server.URL), @@ -519,7 +545,7 @@ func TestLogStreamerFollowTailDoesNotReplayAfterReconnect(t *testing.T) { }) defer server.Close() - writer := newNotifyBuffer() + writer := &syncBuffer{} streamer := &logStreamer{ dialer: &websocket.Dialer{}, url: toWebSocketURL(server.URL), @@ -694,42 +720,22 @@ func TestLogStreamerTimeoutStopsQuietFollowStream(t *testing.T) { } } -type notifyBuffer struct { +// syncBuffer is a bytes.Buffer safe for concurrent writes and reads. +type syncBuffer struct { mu sync.Mutex buf bytes.Buffer - ch chan struct{} -} - -func newNotifyBuffer() *notifyBuffer { - return ¬ifyBuffer{ch: make(chan struct{}, 1)} } -func (n *notifyBuffer) Write(p []byte) (int, error) { - n.mu.Lock() - written, err := n.buf.Write(p) - n.mu.Unlock() - if err == nil { - select { - case n.ch <- struct{}{}: - default: - } - } - return written, err -} - -func (n *notifyBuffer) String() string { - n.mu.Lock() - defer n.mu.Unlock() - return n.buf.String() +func (b *syncBuffer) Write(p []byte) (int, error) { + b.mu.Lock() + defer b.mu.Unlock() + return b.buf.Write(p) } -func (n *notifyBuffer) hasWrite() bool { - select { - case <-n.ch: - return true - default: - return false - } +func (b *syncBuffer) String() string { + b.mu.Lock() + defer b.mu.Unlock() + return b.buf.String() } func TestAppStatusCheckerStopsFollowing(t *testing.T) { From 53efda5189d33185e1be564ecc4cc0ad8059acb2 Mon Sep 17 00:00:00 2001 From: "denis.bilenko" Date: Wed, 11 Nov 2026 20:15:09 +0000 Subject: [PATCH 2/2] logstream test: close conn on ctx in waitDialer, clearer failure message Co-authored-by: Isaac --- libs/apps/logstream/streamer_test.go | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/libs/apps/logstream/streamer_test.go b/libs/apps/logstream/streamer_test.go index cda1c05c077..dde1d2c7962 100644 --- a/libs/apps/logstream/streamer_test.go +++ b/libs/apps/logstream/streamer_test.go @@ -373,9 +373,11 @@ func (d *waitDialer) DialContext(ctx context.Context, urlStr string, requestHead } select { case <-d.wait: + return conn, resp, nil case <-ctx.Done(): + _ = conn.Close() + return nil, resp, ctx.Err() } - return conn, resp, nil } func newTestLogServer(t *testing.T, handler func(int, *websocket.Conn)) *httptest.Server { @@ -429,8 +431,6 @@ func TestLogStreamerTailFlushesWithoutFollow(t *testing.T) { sent := make(chan struct{}) release := make(chan struct{}) - // Deferred calls run before t.Cleanup, which waits for the handler to return. - defer close(release) server := newTestLogServer(t, func(id int, conn *websocket.Conn) { defer conn.Close() @@ -444,13 +444,15 @@ func TestLogStreamerTailFlushesWithoutFollow(t *testing.T) { _ = conn.WriteControl(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""), time.Now().Add(time.Second)) }) defer server.Close() + // Deferred calls run before t.Cleanup, which waits for the handler to return. + defer close(release) writer := &syncBuffer{} streamer := &logStreamer{ // The prefetch timer starts once the connection is established. Waiting - // for the server to finish writing means all entries are already buffered - // on the client socket when it starts, so a slow runner cannot fire the - // timer before they are read and flush a partial tail. + // for the server to finish writing means all entries are already on the + // wire when it starts, so a slow server cannot make the timer flush a + // partial tail. dialer: &waitDialer{inner: &websocket.Dialer{}, wait: sent}, url: toWebSocketURL(server.URL), token: "token", @@ -467,7 +469,7 @@ func TestLogStreamerTailFlushesWithoutFollow(t *testing.T) { // Without follow, Run returns once the prefetch timer flushes the tail. // The server never closes the socket on its own, so a flush that depends on // the close would hang here until the context deadline. - require.NoError(t, streamer.Run(ctx), "expected tail logs to flush before the server closed the socket") + require.NoError(t, streamer.Run(ctx), "expected the prefetch timer to flush the tail while the server keeps the socket open") lines := strings.Split(strings.TrimSpace(writer.String()), "\n") require.Len(t, lines, 2) assert.Contains(t, lines[0], "line3")