From e7d8dfb5a0a5ec6f321c07f2da8374524afd9afc Mon Sep 17 00:00:00 2001 From: Gunju Kim Date: Sat, 18 Feb 2023 03:03:12 +0900 Subject: [PATCH] Fix a data race in TestDirty This uses atomic.Bool as updating and reading a boolean-type variable concurrently is not thread-safe. --- .../pkg/apiserver/handler_discovery_test.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/staging/src/k8s.io/kube-aggregator/pkg/apiserver/handler_discovery_test.go b/staging/src/k8s.io/kube-aggregator/pkg/apiserver/handler_discovery_test.go index 838127fd574..e4fff6cdcd9 100644 --- a/staging/src/k8s.io/kube-aggregator/pkg/apiserver/handler_discovery_test.go +++ b/staging/src/k8s.io/kube-aggregator/pkg/apiserver/handler_discovery_test.go @@ -23,6 +23,7 @@ import ( "sort" "strconv" "strings" + "sync/atomic" "testing" "time" @@ -134,7 +135,7 @@ func checkAPIGroups(t *testing.T, api apidiscoveryv2beta1.APIGroupDiscoveryList, // Test that a handler associated with an APIService gets pinged after the // APIService has been marked as dirty func TestDirty(t *testing.T) { - pinged := false + var pinged atomic.Bool service := discoveryendpoint.NewResourceManager() aggregatedResourceManager := discoveryendpoint.NewResourceManager() @@ -152,7 +153,7 @@ func TestDirty(t *testing.T) { }, }, }, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - pinged = true + pinged.Store(true) service.ServeHTTP(w, r) })) testCtx, cancel := context.WithCancel(context.Background()) @@ -162,7 +163,7 @@ func TestDirty(t *testing.T) { require.True(t, waitForEmptyQueue(testCtx.Done(), aggregatedManager)) // immediately check for ping, since Run() should block for local services - if !pinged { + if !pinged.Load() { t.Errorf("service handler never pinged") } }