Repository navigation
Fix flaky TestLogStreamerTailFlushesWithoutFollow #7505
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -360,6 +360,26 @@ 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: | ||
| return conn, resp, nil | ||
| case <-ctx.Done(): | ||
| _ = conn.Close() | ||
| return nil, resp, ctx.Err() | ||
| } | ||
| } | ||
|
|
||
| func newTestLogServer(t *testing.T, handler func(int, *websocket.Conn)) *httptest.Server { | ||
| upgrader := websocket.Upgrader{} | ||
| var connCount atomic.Int32 | ||
|
|
@@ -409,39 +429,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{}) | ||
|
|
||
| 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() | ||
| // Deferred calls run before t.Cleanup, which waits for the handler to return. | ||
| defer close(release) | ||
|
|
||
| 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 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", | ||
| 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. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| // 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 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") | ||
|
|
@@ -461,7 +489,7 @@ func TestLogStreamerFollowTailWithoutPrefetchEmitsRequestedLines(t *testing.T) { | |
| }) | ||
| defer server.Close() | ||
|
|
||
| writer := newNotifyBuffer() | ||
| writer := &syncBuffer{} | ||
| streamer := &logStreamer{ | ||
| dialer: &websocket.Dialer{}, | ||
| url: toWebSocketURL(server.URL), | ||
|
|
@@ -519,7 +547,7 @@ func TestLogStreamerFollowTailDoesNotReplayAfterReconnect(t *testing.T) { | |
| }) | ||
| defer server.Close() | ||
|
|
||
| writer := newNotifyBuffer() | ||
| writer := &syncBuffer{} | ||
| streamer := &logStreamer{ | ||
| dialer: &websocket.Dialer{}, | ||
| url: toWebSocketURL(server.URL), | ||
|
|
@@ -694,42 +722,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) { | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
t.Context()is canceled before Cleanup runs, so<-t.Context().Done()here (as thestopCtxhandlers do) would dropreleaseand the defer-ordering comment. Worth it?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, worth it:
t.Context()is canceled before the cleanup that waits for the handler, so the handler can wait on it directly. Done in #7543, which dropsreleaseand the defer-ordering comment.AI agent (harness_auto, opus5.5, isaac_claude)