Skip to content

Commit 3be783b

Browse files
authored
fix(scaletest/workspacetraffic): wait for non-zero metrics before cancelling in TestRun (#9663)
1 parent 254f459 commit 3be783b

File tree

1 file changed

+49
-22
lines changed

1 file changed

+49
-22
lines changed

scaletest/workspacetraffic/run_test.go

Lines changed: 49 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/coder/coder/v2/provisionersdk/proto"
1717
"github.com/coder/coder/v2/scaletest/workspacetraffic"
1818
"github.com/coder/coder/v2/testutil"
19+
"golang.org/x/exp/slices"
1920

2021
"github.com/google/uuid"
2122
"github.com/stretchr/testify/assert"
@@ -97,7 +98,6 @@ func TestRun(t *testing.T) {
9798
var (
9899
bytesPerTick = 1024
99100
tickInterval = 1000 * time.Millisecond
100-
cancelAfter = 1500 * time.Millisecond
101101
fudgeWrite = 12 // The ReconnectingPTY payload incurs some overhead
102102
readMetrics = &testMetrics{}
103103
writeMetrics = &testMetrics{}
@@ -113,12 +113,32 @@ func TestRun(t *testing.T) {
113113
})
114114

115115
var logs strings.Builder
116-
// Stop the test after one 'tick'. This will cause an EOF.
116+
117+
runDone := make(chan struct{})
117118
go func() {
118-
<-time.After(cancelAfter)
119-
cancel()
119+
defer close(runDone)
120+
err := runner.Run(ctx, "", &logs)
121+
assert.NoError(t, err, "unexpected error calling Run()")
122+
}()
123+
124+
gotMetrics := make(chan struct{})
125+
go func() {
126+
defer close(gotMetrics)
127+
// Wait until we get some non-zero metrics before canceling.
128+
assert.Eventually(t, func() bool {
129+
readLatencies := readMetrics.Latencies()
130+
writeLatencies := writeMetrics.Latencies()
131+
return len(readLatencies) > 0 &&
132+
len(writeLatencies) > 0 &&
133+
slices.ContainsFunc(readLatencies, func(f float64) bool { return f > 0.0 }) &&
134+
slices.ContainsFunc(writeLatencies, func(f float64) bool { return f > 0.0 })
135+
}, testutil.WaitLong, testutil.IntervalMedium, "expected non-zero metrics")
120136
}()
121-
require.NoError(t, runner.Run(ctx, "", &logs), "unexpected error calling Run()")
137+
138+
// Stop the test after we get some non-zero metrics.
139+
<-gotMetrics
140+
cancel()
141+
<-runDone
122142

123143
t.Logf("read errors: %.0f\n", readMetrics.Errors())
124144
t.Logf("write errors: %.0f\n", writeMetrics.Errors())
@@ -132,12 +152,6 @@ func TestRun(t *testing.T) {
132152
assert.NotZero(t, readMetrics.Total())
133153
// Latency should report non-zero values.
134154
assert.NotEmpty(t, readMetrics.Latencies())
135-
for _, l := range readMetrics.Latencies()[1:] { // skip the first one, which is always zero
136-
assert.NotZero(t, l)
137-
}
138-
for _, l := range writeMetrics.Latencies()[1:] { // skip the first one, which is always zero
139-
assert.NotZero(t, l)
140-
}
141155
assert.NotEmpty(t, writeMetrics.Latencies())
142156
// Should not report any errors!
143157
assert.Zero(t, readMetrics.Errors())
@@ -210,7 +224,6 @@ func TestRun(t *testing.T) {
210224
var (
211225
bytesPerTick = 1024
212226
tickInterval = 1000 * time.Millisecond
213-
cancelAfter = 1500 * time.Millisecond
214227
fudgeWrite = 2 // We send \r\n, which is two bytes
215228
readMetrics = &testMetrics{}
216229
writeMetrics = &testMetrics{}
@@ -226,12 +239,32 @@ func TestRun(t *testing.T) {
226239
})
227240

228241
var logs strings.Builder
229-
// Stop the test after one 'tick'. This will cause an EOF.
242+
243+
runDone := make(chan struct{})
230244
go func() {
231-
<-time.After(cancelAfter)
232-
cancel()
245+
defer close(runDone)
246+
err := runner.Run(ctx, "", &logs)
247+
assert.NoError(t, err, "unexpected error calling Run()")
248+
}()
249+
250+
gotMetrics := make(chan struct{})
251+
go func() {
252+
defer close(gotMetrics)
253+
// Wait until we get some non-zero metrics before canceling.
254+
assert.Eventually(t, func() bool {
255+
readLatencies := readMetrics.Latencies()
256+
writeLatencies := writeMetrics.Latencies()
257+
return len(readLatencies) > 0 &&
258+
len(writeLatencies) > 0 &&
259+
slices.ContainsFunc(readLatencies, func(f float64) bool { return f > 0.0 }) &&
260+
slices.ContainsFunc(writeLatencies, func(f float64) bool { return f > 0.0 })
261+
}, testutil.WaitLong, testutil.IntervalMedium, "expected non-zero metrics")
233262
}()
234-
require.NoError(t, runner.Run(ctx, "", &logs), "unexpected error calling Run()")
263+
264+
// Stop the test after we get some non-zero metrics.
265+
<-gotMetrics
266+
cancel()
267+
<-runDone
235268

236269
t.Logf("read errors: %.0f\n", readMetrics.Errors())
237270
t.Logf("write errors: %.0f\n", writeMetrics.Errors())
@@ -245,12 +278,6 @@ func TestRun(t *testing.T) {
245278
assert.NotZero(t, readMetrics.Total())
246279
// Latency should report non-zero values.
247280
assert.NotEmpty(t, readMetrics.Latencies())
248-
for _, l := range readMetrics.Latencies()[1:] { // skip the first one, which is always zero
249-
assert.NotZero(t, l)
250-
}
251-
for _, l := range writeMetrics.Latencies()[1:] { // skip the first one, which is always zero
252-
assert.NotZero(t, l)
253-
}
254281
assert.NotEmpty(t, writeMetrics.Latencies())
255282
// Should not report any errors!
256283
assert.Zero(t, readMetrics.Errors())

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