mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-22 03:11:40 +00:00
Fix a data race in TestDirty
This uses atomic.Bool as updating and reading a boolean-type variable concurrently is not thread-safe.
This commit is contained in:
parent
9b213330f5
commit
e7d8dfb5a0
@ -23,6 +23,7 @@ import (
|
|||||||
"sort"
|
"sort"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync/atomic"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"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
|
// Test that a handler associated with an APIService gets pinged after the
|
||||||
// APIService has been marked as dirty
|
// APIService has been marked as dirty
|
||||||
func TestDirty(t *testing.T) {
|
func TestDirty(t *testing.T) {
|
||||||
pinged := false
|
var pinged atomic.Bool
|
||||||
service := discoveryendpoint.NewResourceManager()
|
service := discoveryendpoint.NewResourceManager()
|
||||||
aggregatedResourceManager := discoveryendpoint.NewResourceManager()
|
aggregatedResourceManager := discoveryendpoint.NewResourceManager()
|
||||||
|
|
||||||
@ -152,7 +153,7 @@ func TestDirty(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
}, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
}, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
pinged = true
|
pinged.Store(true)
|
||||||
service.ServeHTTP(w, r)
|
service.ServeHTTP(w, r)
|
||||||
}))
|
}))
|
||||||
testCtx, cancel := context.WithCancel(context.Background())
|
testCtx, cancel := context.WithCancel(context.Background())
|
||||||
@ -162,7 +163,7 @@ func TestDirty(t *testing.T) {
|
|||||||
require.True(t, waitForEmptyQueue(testCtx.Done(), aggregatedManager))
|
require.True(t, waitForEmptyQueue(testCtx.Done(), aggregatedManager))
|
||||||
|
|
||||||
// immediately check for ping, since Run() should block for local services
|
// immediately check for ping, since Run() should block for local services
|
||||||
if !pinged {
|
if !pinged.Load() {
|
||||||
t.Errorf("service handler never pinged")
|
t.Errorf("service handler never pinged")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user