Skip to content

Commit

Permalink
tests: wait for metrics server to be up before testing (#378)
Browse files Browse the repository at this point in the history
  • Loading branch information
ranakan19 authored Dec 15, 2023
1 parent a38640a commit 318f38d
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions pkg/metrics/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
clientmodel "github.com/prometheus/client_model/go"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"io"
"net/http"
"strings"
"testing"
Expand Down Expand Up @@ -63,6 +64,30 @@ func TestMetricsServer(t *testing.T) {
testMetrics := NewProxyMetrics(reg)
server := testMetrics.StartMetricsServer()
require.NotNil(t, server)
// Wait up to N seconds for the Metrics server to start
ready := false
sec := 10
for i := 0; i < sec; i++ {
req, err := http.NewRequest("GET", "http://localhost:8082/metrics", nil)
require.NoError(t, err)
require.NotNil(t, req)
resp, err := http.DefaultClient.Do(req)
if err != nil {
time.Sleep(time.Second)
continue
}
_, _ = io.Copy(io.Discard, resp.Body)
_ = resp.Body.Close()
if resp.StatusCode != http.StatusOK {
// The server may be running but still not fully ready to accept requests
time.Sleep(time.Second)
continue
}
// Server is up and running!
ready = true
break
}
require.True(t, ready, "Metrics Server is not ready after %d seconds", sec)
defer func() {
_ = server.Close()
}()
Expand Down

0 comments on commit 318f38d

Please sign in to comment.