Skip to content

Commit f204a89

Browse files
committed
fix: resolve server initialization race condition in TestWorkspaceAgent/GoogleCloud
The test was flaky due to a race condition where the agent CLI would start and attempt authentication before the HTTP server's handler was fully initialized. The test server starts immediately but the handler is set asynchronously, creating a window where requests return 404. This fix waits for the server to be ready by making a test request to /api/v2/buildinfo before starting the agent, ensuring the HTTP handler is properly set up. Fixes: coder/internal#778
1 parent a9b110d commit f204a89

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

cli/agent_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,18 @@ func TestWorkspaceAgent(t *testing.T) {
159159
return agents
160160
}).Do()
161161

162+
// Wait for the server to be ready to handle requests.
163+
// This prevents a race condition where the agent tries to authenticate
164+
// before the server's HTTP handler is fully set up.
165+
require.Eventually(t, func() bool {
166+
resp, err := client.Request(context.Background(), "GET", "/api/v2/buildinfo", nil)
167+
if err != nil {
168+
return false
169+
}
170+
defer resp.Body.Close()
171+
return resp.StatusCode == http.StatusOK
172+
}, testutil.WaitMedium, testutil.IntervalFast)
173+
162174
inv, cfg := clitest.New(t, "agent", "--auth", "google-instance-identity", "--agent-url", client.URL.String())
163175
clitest.SetupConfig(t, member, cfg)
164176

0 commit comments

Comments
 (0)