Skip to content

Commit f8d08a3

Browse files
authored
Fix flakey vstream metrics test (vitessio#18287)
Signed-off-by: twthorn <[email protected]>
1 parent 1140923 commit f8d08a3

File tree

1 file changed

+41
-23
lines changed

1 file changed

+41
-23
lines changed

go/vt/vtgate/vstream_manager_test.go

Lines changed: 41 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package vtgate
1919
import (
2020
"context"
2121
"fmt"
22+
"reflect"
2223
"strings"
2324
"sync"
2425
"sync/atomic"
@@ -382,30 +383,47 @@ func TestVStreamsMetrics(t *testing.T) {
382383
<-ch
383384
expectedLabels1 := "TestVStream.-20.PRIMARY"
384385
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")
404386

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")
409427
}
410428

411429
func TestVStreamsMetricsErrors(t *testing.T) {

0 commit comments

Comments
 (0)