From f07dcd443d7335d09dc0de7a47485e2e6c87d725 Mon Sep 17 00:00:00 2001 From: Antonio Ojea Date: Thu, 24 Jul 2025 21:56:42 +0000 Subject: [PATCH] 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 --- .../pkg/util/proxy/streamtranslator_test.go | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/staging/src/k8s.io/apiserver/pkg/util/proxy/streamtranslator_test.go b/staging/src/k8s.io/apiserver/pkg/util/proxy/streamtranslator_test.go index 89f82a7f0d9..c3452202236 100644 --- a/staging/src/k8s.io/apiserver/pkg/util/proxy/streamtranslator_test.go +++ b/staging/src/k8s.io/apiserver/pkg/util/proxy/streamtranslator_test.go @@ -755,15 +755,21 @@ func TestStreamTranslator_WebSocketServerErrors(t *testing.T) { t.Errorf("expected websocket bad handshake error, got (%s)", err) } } - // Validate the streamtranslator metrics; should have one 500 failure. + // Validate the streamtranslator metrics; should have one 400 failure. + // Use polling to wait for the metric to be updated asynchronously. metricNames := []string{"apiserver_stream_translator_requests_total"} expected := ` # HELP apiserver_stream_translator_requests_total [ALPHA] Total number of requests that were handled by the StreamTranslatorProxy, which processes streaming RemoteCommand/V5 # TYPE apiserver_stream_translator_requests_total counter apiserver_stream_translator_requests_total{code="400"} 1 ` - if err := testutil.GatherAndCompare(legacyregistry.DefaultGatherer, strings.NewReader(expected), metricNames...); err != nil { - t.Fatal(err) + if err := wait.PollUntilContextTimeout(context.Background(), 100*time.Millisecond, 2*time.Second, true, func(ctx context.Context) (bool, error) { + if testutil.GatherAndCompare(legacyregistry.DefaultGatherer, strings.NewReader(expected), metricNames...) == nil { + return true, nil + } + return false, nil + }); err != nil { + t.Fatalf("Failed to observe metric after waiting 2 seconds: %v", err) } }