Thanks to visit codestin.com
Credit goes to github.com

Skip to content

chore(cli): refactor TestServer/Prometheus to use testutil.Eventually #17295

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 1 commit into from
Apr 8, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 44 additions & 44 deletions cli/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1208,17 +1208,15 @@ func TestServer(t *testing.T) {
}
}
return htmlFirstServedFound
}, testutil.WaitMedium, testutil.IntervalFast, "no html_first_served telemetry item")
}, testutil.WaitLong, testutil.IntervalSlow, "no html_first_served telemetry item")
})
t.Run("Prometheus", func(t *testing.T) {
t.Parallel()

t.Run("DBMetricsDisabled", func(t *testing.T) {
t.Parallel()

ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitShort)
defer cancel()

ctx := testutil.Context(t, testutil.WaitLong)
randPort := testutil.RandomPort(t)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my experience, this is a huge culprit for this test flaking. But fixing it is tricky, we'd have to 1) start a listener and hand it over to the cmd, or 2) use :0 but somehow signal the test what port to access.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah agreed -- follow-up

inv, cfg := clitest.New(t,
"server",
Expand All @@ -1235,46 +1233,45 @@ func TestServer(t *testing.T) {
clitest.Start(t, inv)
_ = waitAccessURL(t, cfg)

var res *http.Response
require.Eventually(t, func() bool {
req, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("http://127.0.0.1:%d", randPort), nil)
assert.NoError(t, err)
testutil.Eventually(ctx, t, func(ctx context.Context) bool {
req, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("http://127.0.0.1:%d/metrics", randPort), nil)
if err != nil {
t.Logf("error creating request: %s", err.Error())
return false
}
// nolint:bodyclose
res, err = http.DefaultClient.Do(req)
res, err := http.DefaultClient.Do(req)
if err != nil {
t.Logf("error hitting prometheus endpoint: %s", err.Error())
return false
}
defer res.Body.Close()

scanner := bufio.NewScanner(res.Body)
hasActiveUsers := false
var activeUsersFound bool
var scannedOnce bool
for scanner.Scan() {
line := scanner.Text()
if !scannedOnce {
t.Logf("scanned: %s", line) // avoid spamming logs
scannedOnce = true
}
if strings.HasPrefix(line, "coderd_db_query_latencies_seconds") {
t.Errorf("db metrics should not be tracked when --prometheus-collect-db-metrics is not enabled")
}
// This metric is manually registered to be tracked in the server. That's
// why we test it's tracked here.
if strings.HasPrefix(scanner.Text(), "coderd_api_active_users_duration_hour") {
hasActiveUsers = true
continue
}
if strings.HasPrefix(scanner.Text(), "coderd_db_query_latencies_seconds") {
t.Fatal("db metrics should not be tracked when --prometheus-collect-db-metrics is not enabled")
if strings.HasPrefix(line, "coderd_api_active_users_duration_hour") {
activeUsersFound = true
}
t.Logf("scanned %s", scanner.Text())
}
if scanner.Err() != nil {
t.Logf("scanner err: %s", scanner.Err().Error())
return false
}

return hasActiveUsers
}, testutil.WaitShort, testutil.IntervalFast, "didn't find coderd_api_active_users_duration_hour in time")
return activeUsersFound
}, testutil.IntervalSlow, "didn't find coderd_api_active_users_duration_hour in time")
})

t.Run("DBMetricsEnabled", func(t *testing.T) {
t.Parallel()

ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitShort)
defer cancel()

ctx := testutil.Context(t, testutil.WaitLong)
randPort := testutil.RandomPort(t)
inv, cfg := clitest.New(t,
"server",
Expand All @@ -1291,31 +1288,34 @@ func TestServer(t *testing.T) {
clitest.Start(t, inv)
_ = waitAccessURL(t, cfg)

var res *http.Response
require.Eventually(t, func() bool {
req, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("http://127.0.0.1:%d", randPort), nil)
assert.NoError(t, err)
testutil.Eventually(ctx, t, func(ctx context.Context) bool {
req, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("http://127.0.0.1:%d/metrics", randPort), nil)
if err != nil {
t.Logf("error creating request: %s", err.Error())
return false
}
// nolint:bodyclose
res, err = http.DefaultClient.Do(req)
res, err := http.DefaultClient.Do(req)
if err != nil {
t.Logf("error hitting prometheus endpoint: %s", err.Error())
return false
}
defer res.Body.Close()

scanner := bufio.NewScanner(res.Body)
hasDBMetrics := false
var dbMetricsFound bool
var scannedOnce bool
for scanner.Scan() {
if strings.HasPrefix(scanner.Text(), "coderd_db_query_latencies_seconds") {
hasDBMetrics = true
line := scanner.Text()
if !scannedOnce {
t.Logf("scanned: %s", line) // avoid spamming logs
scannedOnce = true
}
if strings.HasPrefix(line, "coderd_db_query_latencies_seconds") {
dbMetricsFound = true
}
t.Logf("scanned %s", scanner.Text())
}
if scanner.Err() != nil {
t.Logf("scanner err: %s", scanner.Err().Error())
return false
}
return hasDBMetrics
}, testutil.WaitShort, testutil.IntervalFast, "didn't find coderd_db_query_latencies_seconds in time")
return dbMetricsFound
}, testutil.IntervalSlow, "didn't find coderd_db_query_latencies_seconds in time")
})
})
t.Run("GitHubOAuth", func(t *testing.T) {
Expand Down
Loading