-
Notifications
You must be signed in to change notification settings - Fork 13
test(runtime): fix flaky server-readiness wait (port auto-increment race) #529
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
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
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
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
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
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 |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| package runtime | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "net" | ||
| "net/http" | ||
| "testing" | ||
| "time" | ||
| ) | ||
|
|
||
| // TestWaitForServer_ResolvesAutoIncrementedPort proves the fix for the CI flake | ||
| // in TestRunner_JSONRPC_WorkflowContextThreadsThroughDispatcher: when the port | ||
| // findFreePort handed out is stolen before the runner binds, server.Start | ||
| // auto-increments and the server comes up on a higher port. waitForServer must | ||
| // discover it by scanning the increment window and return the real URL, rather | ||
| // than time out polling the requested port. | ||
| func TestWaitForServer_ResolvesAutoIncrementedPort(t *testing.T) { | ||
| ln, err := net.Listen("tcp", "127.0.0.1:0") | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| actualPort := ln.Addr().(*net.TCPAddr).Port | ||
|
|
||
| mux := http.NewServeMux() | ||
| mux.HandleFunc("/healthz", func(w http.ResponseWriter, _ *http.Request) { w.WriteHeader(http.StatusOK) }) | ||
| srv := &http.Server{Handler: mux, ReadHeaderTimeout: time.Second} | ||
| go func() { _ = srv.Serve(ln) }() | ||
| t.Cleanup(func() { _ = srv.Close() }) | ||
|
|
||
| // The caller believes the server is one port lower (as if its requested | ||
| // port was taken and Start incremented to actualPort). | ||
| requested := fmt.Sprintf("http://127.0.0.1:%d", actualPort-1) | ||
| got := waitForServer(t, requested, 3*time.Second) | ||
| if want := fmt.Sprintf("http://127.0.0.1:%d", actualPort); got != want { | ||
| t.Errorf("waitForServer resolved %q, want %q (should scan up to the incremented port)", got, want) | ||
| } | ||
| } |
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
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.
Scan is correct for the fix. Note (follow-up #530, non-blocking): probing
/healthzacross the window has no server-identity check — under heavy parallel overlap it could match a neighbor test's server on a lower port in the window. Rare and strictly better than the pre-fix timeout, but the durable fix is to have the runner expose its resolved port so tests never guess (tracked in #530, alongside surfacingRun's error in the 6_ = runner.Run(ctx)sites).