@@ -16,6 +16,7 @@ import (
16
16
"github.com/coder/coder/v2/provisionersdk/proto"
17
17
"github.com/coder/coder/v2/scaletest/workspacetraffic"
18
18
"github.com/coder/coder/v2/testutil"
19
+ "golang.org/x/exp/slices"
19
20
20
21
"github.com/google/uuid"
21
22
"github.com/stretchr/testify/assert"
@@ -97,7 +98,6 @@ func TestRun(t *testing.T) {
97
98
var (
98
99
bytesPerTick = 1024
99
100
tickInterval = 1000 * time .Millisecond
100
- cancelAfter = 1500 * time .Millisecond
101
101
fudgeWrite = 12 // The ReconnectingPTY payload incurs some overhead
102
102
readMetrics = & testMetrics {}
103
103
writeMetrics = & testMetrics {}
@@ -113,12 +113,32 @@ func TestRun(t *testing.T) {
113
113
})
114
114
115
115
var logs strings.Builder
116
- // Stop the test after one 'tick'. This will cause an EOF.
116
+
117
+ runDone := make (chan struct {})
117
118
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" )
120
136
}()
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
122
142
123
143
t .Logf ("read errors: %.0f\n " , readMetrics .Errors ())
124
144
t .Logf ("write errors: %.0f\n " , writeMetrics .Errors ())
@@ -132,12 +152,6 @@ func TestRun(t *testing.T) {
132
152
assert .NotZero (t , readMetrics .Total ())
133
153
// Latency should report non-zero values.
134
154
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
- }
141
155
assert .NotEmpty (t , writeMetrics .Latencies ())
142
156
// Should not report any errors!
143
157
assert .Zero (t , readMetrics .Errors ())
@@ -210,7 +224,6 @@ func TestRun(t *testing.T) {
210
224
var (
211
225
bytesPerTick = 1024
212
226
tickInterval = 1000 * time .Millisecond
213
- cancelAfter = 1500 * time .Millisecond
214
227
fudgeWrite = 2 // We send \r\n, which is two bytes
215
228
readMetrics = & testMetrics {}
216
229
writeMetrics = & testMetrics {}
@@ -226,12 +239,32 @@ func TestRun(t *testing.T) {
226
239
})
227
240
228
241
var logs strings.Builder
229
- // Stop the test after one 'tick'. This will cause an EOF.
242
+
243
+ runDone := make (chan struct {})
230
244
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" )
233
262
}()
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
235
268
236
269
t .Logf ("read errors: %.0f\n " , readMetrics .Errors ())
237
270
t .Logf ("write errors: %.0f\n " , writeMetrics .Errors ())
@@ -245,12 +278,6 @@ func TestRun(t *testing.T) {
245
278
assert .NotZero (t , readMetrics .Total ())
246
279
// Latency should report non-zero values.
247
280
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
- }
254
281
assert .NotEmpty (t , writeMetrics .Latencies ())
255
282
// Should not report any errors!
256
283
assert .Zero (t , readMetrics .Errors ())
0 commit comments