Merge pull request #17476 from sammyqtran/web-test-speedup
Some checks are pending
buf.build / lint and publish (push) Waiting to run
CI / Go tests (push) Waiting to run
CI / More Go tests (push) Waiting to run
CI / Go tests with previous Go version (push) Waiting to run
CI / UI tests (push) Waiting to run
CI / Go tests on Windows (push) Waiting to run
CI / Mixins tests (push) Waiting to run
CI / Build Prometheus for common architectures (push) Waiting to run
CI / Build Prometheus for all architectures (push) Waiting to run
CI / Report status of build Prometheus for all architectures (push) Blocked by required conditions
CI / Check generated parser (push) Waiting to run
CI / golangci-lint (push) Waiting to run
CI / fuzzing (push) Waiting to run
CI / codeql (push) Waiting to run
CI / Publish main branch artifacts (push) Blocked by required conditions
CI / Publish release artefacts (push) Blocked by required conditions
CI / Publish UI on npm Registry (push) Blocked by required conditions
Scorecards supply-chain security / Scorecards analysis (push) Waiting to run

test(web): Wait for server ready instead of sleeping 5 seconds.
This commit is contained in:
Bryan Boreham 2026-02-17 11:34:48 +00:00 committed by GitHub
commit f11e1c9fe1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -118,12 +118,10 @@ func TestReadyAndHealthy(t *testing.T) {
}
}()
// Give some time for the web goroutine to run since we need the server
// to be up before starting tests.
time.Sleep(5 * time.Second)
baseURL := "http://localhost" + port
waitForServerReady(t, baseURL, 5*time.Second)
resp, err := http.Get(baseURL + "/-/healthy")
require.NoError(t, err)
require.Equal(t, http.StatusOK, resp.StatusCode)
@ -256,12 +254,10 @@ func TestRoutePrefix(t *testing.T) {
}
}()
// Give some time for the web goroutine to run since we need the server
// to be up before starting tests.
time.Sleep(5 * time.Second)
baseURL := "http://localhost" + port
waitForServerReady(t, baseURL+opts.RoutePrefix, 5*time.Second)
resp, err := http.Get(baseURL + opts.RoutePrefix + "/-/healthy")
require.NoError(t, err)
require.Equal(t, http.StatusOK, resp.StatusCode)
@ -449,9 +445,9 @@ func TestShutdownWithStaleConnection(t *testing.T) {
close(closed)
}()
// Give some time for the web goroutine to run since we need the server
// to be up before starting tests.
time.Sleep(5 * time.Second)
baseURL := "http://localhost" + port
waitForServerReady(t, baseURL, 5*time.Second)
// Open a socket, and don't use it. This connection should then be closed
// after the ReadTimeout.
@ -500,12 +496,10 @@ func TestHandleMultipleQuitRequests(t *testing.T) {
close(closed)
}()
// Give some time for the web goroutine to run since we need the server
// to be up before starting tests.
time.Sleep(5 * time.Second)
baseURL := opts.ExternalURL.Scheme + "://" + opts.ExternalURL.Host
waitForServerReady(t, baseURL, 5*time.Second)
start := make(chan struct{})
var wg sync.WaitGroup
for range 3 {
@ -578,11 +572,10 @@ func TestAgentAPIEndPoints(t *testing.T) {
}
}()
// Give some time for the web goroutine to run since we need the server
// to be up before starting tests.
time.Sleep(5 * time.Second)
baseURL := "http://localhost" + port + "/api/v1"
waitForServerReady(t, "http://localhost"+port, 5*time.Second)
// Test for non-available endpoints in the Agent mode.
for path, methods := range map[string][]string{
"/labels": {http.MethodGet, http.MethodPost},
@ -711,9 +704,7 @@ func TestMultipleListenAddresses(t *testing.T) {
}
}()
// Give some time for the web goroutine to run since we need the server
// to be up before starting tests.
time.Sleep(5 * time.Second)
waitForServerReady(t, "http://localhost"+port1, 5*time.Second)
// Set to ready.
webHandler.SetReady(Ready)
@ -732,3 +723,24 @@ func TestMultipleListenAddresses(t *testing.T) {
cleanupTestResponse(t, resp)
}
}
// Give some time for the web goroutine to run since we need the server
// to be up before starting tests.
func waitForServerReady(t *testing.T, baseURL string, timeout time.Duration) {
t.Helper()
interval := 100 * time.Millisecond
deadline := time.Now().Add(timeout)
for time.Now().Before(deadline) {
resp, err := http.Get(baseURL + "/-/healthy")
if resp != nil {
cleanupTestResponse(t, resp)
}
if err == nil && resp.StatusCode == http.StatusOK {
return
}
time.Sleep(interval)
}
t.Fatalf("Server did not become ready within %v", timeout)
}