Skip to content

Commit f07dcd4

Browse files
committed
fix flake on TestStreamTranslator_WebSocketServerErrors
The metrics assertion race with the metric update, and since this happens at the serverside, we use an active look to check the metrics instead of expecting to be updated immidiatly. Change-Id: I9a64b66301d5f4ac3df0c0a01de10602a20f89ea
1 parent 7ba2270 commit f07dcd4

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

staging/src/k8s.io/apiserver/pkg/util/proxy/streamtranslator_test.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -755,15 +755,21 @@ func TestStreamTranslator_WebSocketServerErrors(t *testing.T) {
755755
t.Errorf("expected websocket bad handshake error, got (%s)", err)
756756
}
757757
}
758-
// Validate the streamtranslator metrics; should have one 500 failure.
758+
// Validate the streamtranslator metrics; should have one 400 failure.
759+
// Use polling to wait for the metric to be updated asynchronously.
759760
metricNames := []string{"apiserver_stream_translator_requests_total"}
760761
expected := `
761762
# HELP apiserver_stream_translator_requests_total [ALPHA] Total number of requests that were handled by the StreamTranslatorProxy, which processes streaming RemoteCommand/V5
762763
# TYPE apiserver_stream_translator_requests_total counter
763764
apiserver_stream_translator_requests_total{code="400"} 1
764765
`
765-
if err := testutil.GatherAndCompare(legacyregistry.DefaultGatherer, strings.NewReader(expected), metricNames...); err != nil {
766-
t.Fatal(err)
766+
if err := wait.PollUntilContextTimeout(context.Background(), 100*time.Millisecond, 2*time.Second, true, func(ctx context.Context) (bool, error) {
767+
if testutil.GatherAndCompare(legacyregistry.DefaultGatherer, strings.NewReader(expected), metricNames...) == nil {
768+
return true, nil
769+
}
770+
return false, nil
771+
}); err != nil {
772+
t.Fatalf("Failed to observe metric after waiting 2 seconds: %v", err)
767773
}
768774
}
769775

0 commit comments

Comments
 (0)