From c648d24ce44b62d6494ec69f1296e37f72fe0d01 Mon Sep 17 00:00:00 2001 From: Anton Miniailo Date: Sun, 28 Jul 2024 22:53:26 -0400 Subject: [PATCH 1/2] Fix flaky test again. --- lib/srv/alpnproxy/local_proxy_test.go | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/lib/srv/alpnproxy/local_proxy_test.go b/lib/srv/alpnproxy/local_proxy_test.go index 1a7321457d112..40773aa6f114c 100644 --- a/lib/srv/alpnproxy/local_proxy_test.go +++ b/lib/srv/alpnproxy/local_proxy_test.go @@ -533,12 +533,18 @@ func TestKubeMiddleware(t *testing.T) { err := km.CheckAndSetDefaults() require.NoError(t, err) - rw := responsewriters.NewMemoryResponseWriter() - // HandleRequest will reissue certificate if needed. - km.HandleRequest(rw, req) - - // request timed out. - require.Equal(t, http.StatusInternalServerError, rw.Status()) + var rw *responsewriters.MemoryResponseWriter + // We use `require.Eventually` to avoid a very rare test flakiness case when reissue goroutine manages to + // successfully finish before the parent goroutine has a chance to check the context (and see that it's expired). + require.Eventually(t, func() bool { + rw = responsewriters.NewMemoryResponseWriter() + // HandleRequest will reissue certificate if needed. + km.HandleRequest(rw, req) + + // request timed out. + return rw.Status() == http.StatusInternalServerError + + }, time.Second, 100*time.Millisecond) require.Contains(t, rw.Buffer().String(), "context canceled") // just let the reissuing goroutine some time to replace certs. From a3806cf30c556874eea755128a82386cd5e1c9d9 Mon Sep 17 00:00:00 2001 From: Anton Miniailo Date: Mon, 29 Jul 2024 23:16:12 -0400 Subject: [PATCH 2/2] Increase timeout to 5 seconds. --- lib/srv/alpnproxy/local_proxy_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/srv/alpnproxy/local_proxy_test.go b/lib/srv/alpnproxy/local_proxy_test.go index 40773aa6f114c..f7940ef22c069 100644 --- a/lib/srv/alpnproxy/local_proxy_test.go +++ b/lib/srv/alpnproxy/local_proxy_test.go @@ -544,7 +544,7 @@ func TestKubeMiddleware(t *testing.T) { // request timed out. return rw.Status() == http.StatusInternalServerError - }, time.Second, 100*time.Millisecond) + }, 5*time.Second, 100*time.Millisecond) require.Contains(t, rw.Buffer().String(), "context canceled") // just let the reissuing goroutine some time to replace certs.