@@ -19,6 +19,7 @@ package vtgate
19
19
import (
20
20
"context"
21
21
"fmt"
22
+ "reflect"
22
23
"strings"
23
24
"sync"
24
25
"sync/atomic"
@@ -382,30 +383,47 @@ func TestVStreamsMetrics(t *testing.T) {
382
383
<- ch
383
384
expectedLabels1 := "TestVStream.-20.PRIMARY"
384
385
expectedLabels2 := "TestVStream.20-40.PRIMARY"
385
- wantVStreamsCreated := make (map [string ]int64 )
386
- wantVStreamsCreated [expectedLabels1 ] = 1
387
- wantVStreamsCreated [expectedLabels2 ] = 1
388
- assert .Equal (t , wantVStreamsCreated , vsm .vstreamsCreated .Counts (), "vstreamsCreated matches" )
389
-
390
- wantVStreamsLag := make (map [string ]int64 )
391
- wantVStreamsLag [expectedLabels1 ] = 5
392
- wantVStreamsLag [expectedLabels2 ] = 7
393
- assert .Equal (t , wantVStreamsLag , vsm .vstreamsLag .Counts (), "vstreamsLag matches" )
394
-
395
- wantVStreamsCount := make (map [string ]int64 )
396
- wantVStreamsCount [expectedLabels1 ] = 1
397
- wantVStreamsCount [expectedLabels2 ] = 1
398
- assert .Equal (t , wantVStreamsCount , vsm .vstreamsCount .Counts (), "vstreamsCount matches" )
399
-
400
- wantVStreamsEventsStreamed := make (map [string ]int64 )
401
- wantVStreamsEventsStreamed [expectedLabels1 ] = 2
402
- wantVStreamsEventsStreamed [expectedLabels2 ] = 2
403
- assert .Equal (t , wantVStreamsEventsStreamed , vsm .vstreamsEventsStreamed .Counts (), "vstreamsEventsStreamed matches" )
404
386
405
- wantVStreamsEndedWithErrors := make (map [string ]int64 )
406
- wantVStreamsEndedWithErrors [expectedLabels1 ] = 0
407
- wantVStreamsEndedWithErrors [expectedLabels2 ] = 0
408
- assert .Equal (t , wantVStreamsEndedWithErrors , vsm .vstreamsEndedWithErrors .Counts (), "vstreamsEndedWithErrors matches" )
387
+ wantVStreamsCreated := map [string ]int64 {
388
+ expectedLabels1 : 1 ,
389
+ expectedLabels2 : 1 ,
390
+ }
391
+ waitForMetricsMatch (t , vsm .vstreamsCreated .Counts , wantVStreamsCreated )
392
+
393
+ wantVStreamsLag := map [string ]int64 {
394
+ expectedLabels1 : 5 ,
395
+ expectedLabels2 : 7 ,
396
+ }
397
+ waitForMetricsMatch (t , vsm .vstreamsLag .Counts , wantVStreamsLag )
398
+
399
+ wantVStreamsCount := map [string ]int64 {
400
+ expectedLabels1 : 1 ,
401
+ expectedLabels2 : 1 ,
402
+ }
403
+ waitForMetricsMatch (t , vsm .vstreamsCount .Counts , wantVStreamsCount )
404
+
405
+ wantVEventsCount := map [string ]int64 {
406
+ expectedLabels1 : 2 ,
407
+ expectedLabels2 : 2 ,
408
+ }
409
+ waitForMetricsMatch (t , vsm .vstreamsEventsStreamed .Counts , wantVEventsCount )
410
+
411
+ wantVStreamsEndedWithErrors := map [string ]int64 {
412
+ expectedLabels1 : 0 ,
413
+ expectedLabels2 : 0 ,
414
+ }
415
+ waitForMetricsMatch (t , vsm .vstreamsEndedWithErrors .Counts , wantVStreamsEndedWithErrors )
416
+ }
417
+
418
+ func waitForMetricsMatch (t * testing.T , getActual func () map [string ]int64 , want map [string ]int64 ) {
419
+ deadline := time .Now ().Add (1 * time .Second )
420
+ for time .Now ().Before (deadline ) {
421
+ if reflect .DeepEqual (getActual (), want ) {
422
+ return
423
+ }
424
+ time .Sleep (10 * time .Millisecond )
425
+ }
426
+ assert .Equal (t , want , getActual (), "metrics did not match within timeout" )
409
427
}
410
428
411
429
func TestVStreamsMetricsErrors (t * testing.T ) {
0 commit comments