Skip to content

Commit 389e88e

Browse files
authored
chore(cli): refactor TestServer/Prometheus to use testutil.Eventually (#17295)
Updates coder/internal#282 Refactors existing tests to use `testutil.Eventually` which plays nicer with `testutil.Context`.
1 parent 44ddc9f commit 389e88e

File tree

1 file changed

+44
-44
lines changed

1 file changed

+44
-44
lines changed

cli/server_test.go

Lines changed: 44 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1208,17 +1208,15 @@ func TestServer(t *testing.T) {
12081208
}
12091209
}
12101210
return htmlFirstServedFound
1211-
}, testutil.WaitMedium, testutil.IntervalFast, "no html_first_served telemetry item")
1211+
}, testutil.WaitLong, testutil.IntervalSlow, "no html_first_served telemetry item")
12121212
})
12131213
t.Run("Prometheus", func(t *testing.T) {
12141214
t.Parallel()
12151215

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

1219-
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitShort)
1220-
defer cancel()
1221-
1219+
ctx := testutil.Context(t, testutil.WaitLong)
12221220
randPort := testutil.RandomPort(t)
12231221
inv, cfg := clitest.New(t,
12241222
"server",
@@ -1235,46 +1233,45 @@ func TestServer(t *testing.T) {
12351233
clitest.Start(t, inv)
12361234
_ = waitAccessURL(t, cfg)
12371235

1238-
var res *http.Response
1239-
require.Eventually(t, func() bool {
1240-
req, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("http://127.0.0.1:%d", randPort), nil)
1241-
assert.NoError(t, err)
1236+
testutil.Eventually(ctx, t, func(ctx context.Context) bool {
1237+
req, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("http://127.0.0.1:%d/metrics", randPort), nil)
1238+
if err != nil {
1239+
t.Logf("error creating request: %s", err.Error())
1240+
return false
1241+
}
12421242
// nolint:bodyclose
1243-
res, err = http.DefaultClient.Do(req)
1243+
res, err := http.DefaultClient.Do(req)
12441244
if err != nil {
1245+
t.Logf("error hitting prometheus endpoint: %s", err.Error())
12451246
return false
12461247
}
12471248
defer res.Body.Close()
1248-
12491249
scanner := bufio.NewScanner(res.Body)
1250-
hasActiveUsers := false
1250+
var activeUsersFound bool
1251+
var scannedOnce bool
12511252
for scanner.Scan() {
1253+
line := scanner.Text()
1254+
if !scannedOnce {
1255+
t.Logf("scanned: %s", line) // avoid spamming logs
1256+
scannedOnce = true
1257+
}
1258+
if strings.HasPrefix(line, "coderd_db_query_latencies_seconds") {
1259+
t.Errorf("db metrics should not be tracked when --prometheus-collect-db-metrics is not enabled")
1260+
}
12521261
// This metric is manually registered to be tracked in the server. That's
12531262
// why we test it's tracked here.
1254-
if strings.HasPrefix(scanner.Text(), "coderd_api_active_users_duration_hour") {
1255-
hasActiveUsers = true
1256-
continue
1257-
}
1258-
if strings.HasPrefix(scanner.Text(), "coderd_db_query_latencies_seconds") {
1259-
t.Fatal("db metrics should not be tracked when --prometheus-collect-db-metrics is not enabled")
1263+
if strings.HasPrefix(line, "coderd_api_active_users_duration_hour") {
1264+
activeUsersFound = true
12601265
}
1261-
t.Logf("scanned %s", scanner.Text())
1262-
}
1263-
if scanner.Err() != nil {
1264-
t.Logf("scanner err: %s", scanner.Err().Error())
1265-
return false
12661266
}
1267-
1268-
return hasActiveUsers
1269-
}, testutil.WaitShort, testutil.IntervalFast, "didn't find coderd_api_active_users_duration_hour in time")
1267+
return activeUsersFound
1268+
}, testutil.IntervalSlow, "didn't find coderd_api_active_users_duration_hour in time")
12701269
})
12711270

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

1275-
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitShort)
1276-
defer cancel()
1277-
1274+
ctx := testutil.Context(t, testutil.WaitLong)
12781275
randPort := testutil.RandomPort(t)
12791276
inv, cfg := clitest.New(t,
12801277
"server",
@@ -1291,31 +1288,34 @@ func TestServer(t *testing.T) {
12911288
clitest.Start(t, inv)
12921289
_ = waitAccessURL(t, cfg)
12931290

1294-
var res *http.Response
1295-
require.Eventually(t, func() bool {
1296-
req, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("http://127.0.0.1:%d", randPort), nil)
1297-
assert.NoError(t, err)
1291+
testutil.Eventually(ctx, t, func(ctx context.Context) bool {
1292+
req, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("http://127.0.0.1:%d/metrics", randPort), nil)
1293+
if err != nil {
1294+
t.Logf("error creating request: %s", err.Error())
1295+
return false
1296+
}
12981297
// nolint:bodyclose
1299-
res, err = http.DefaultClient.Do(req)
1298+
res, err := http.DefaultClient.Do(req)
13001299
if err != nil {
1300+
t.Logf("error hitting prometheus endpoint: %s", err.Error())
13011301
return false
13021302
}
13031303
defer res.Body.Close()
1304-
13051304
scanner := bufio.NewScanner(res.Body)
1306-
hasDBMetrics := false
1305+
var dbMetricsFound bool
1306+
var scannedOnce bool
13071307
for scanner.Scan() {
1308-
if strings.HasPrefix(scanner.Text(), "coderd_db_query_latencies_seconds") {
1309-
hasDBMetrics = true
1308+
line := scanner.Text()
1309+
if !scannedOnce {
1310+
t.Logf("scanned: %s", line) // avoid spamming logs
1311+
scannedOnce = true
1312+
}
1313+
if strings.HasPrefix(line, "coderd_db_query_latencies_seconds") {
1314+
dbMetricsFound = true
13101315
}
1311-
t.Logf("scanned %s", scanner.Text())
1312-
}
1313-
if scanner.Err() != nil {
1314-
t.Logf("scanner err: %s", scanner.Err().Error())
1315-
return false
13161316
}
1317-
return hasDBMetrics
1318-
}, testutil.WaitShort, testutil.IntervalFast, "didn't find coderd_db_query_latencies_seconds in time")
1317+
return dbMetricsFound
1318+
}, testutil.IntervalSlow, "didn't find coderd_db_query_latencies_seconds in time")
13191319
})
13201320
})
13211321
t.Run("GitHubOAuth", func(t *testing.T) {

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy