Skip to content

Commit dde23bb

Browse files
jonyhy96chenwen
authored andcommitted
apiserver: fix data race in apf tests in server/filters package
Signed-off-by: jonyhy96 <[email protected]> Co-authored-by: chenwen <[email protected]>
1 parent 60c4c2b commit dde23bb

File tree

1 file changed

+15
-14
lines changed

1 file changed

+15
-14
lines changed

staging/src/k8s.io/apiserver/pkg/server/filters/priority-and-fairness_test.go

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"reflect"
2727
"strings"
2828
"sync"
29+
"sync/atomic"
2930
"testing"
3031
"time"
3132

@@ -128,20 +129,20 @@ func newApfServerWithSingleRequest(t *testing.T, decision mockDecision) *httptes
128129
t.Errorf("execute should not be invoked")
129130
}
130131
// atomicReadOnlyExecuting can be either 0 or 1 as we test one request at a time.
131-
if decision != decisionSkipFilter && atomicReadOnlyExecuting != 1 {
132-
t.Errorf("Wanted %d requests executing, got %d", 1, atomicReadOnlyExecuting)
132+
if want, got := int32(1), atomic.LoadInt32(&atomicReadOnlyExecuting); decision != decisionSkipFilter && want != got {
133+
t.Errorf("Wanted %d requests executing, got %d", want, got)
133134
}
134135
}
135136
postExecuteFunc := func() {}
136137
// atomicReadOnlyWaiting can be either 0 or 1 as we test one request at a time.
137138
postEnqueueFunc := func() {
138-
if atomicReadOnlyWaiting != 1 {
139-
t.Errorf("Wanted %d requests in queue, got %d", 1, atomicReadOnlyWaiting)
139+
if want, got := int32(1), atomic.LoadInt32(&atomicReadOnlyWaiting); want != got {
140+
t.Errorf("Wanted %d requests in queue, got %d", want, got)
140141
}
141142
}
142143
postDequeueFunc := func() {
143-
if atomicReadOnlyWaiting != 0 {
144-
t.Errorf("Wanted %d requests in queue, got %d", 0, atomicReadOnlyWaiting)
144+
if want, got := int32(0), atomic.LoadInt32(&atomicReadOnlyWaiting); want != got {
145+
t.Errorf("Wanted %d requests in queue, got %d", want, got)
145146
}
146147
}
147148
return newApfServerWithHooks(t, decision, onExecuteFunc, postExecuteFunc, postEnqueueFunc, postDequeueFunc)
@@ -179,8 +180,8 @@ func newApfHandlerWithFilter(t *testing.T, flowControlFilter utilflowcontrol.Int
179180
}))
180181
apfHandler.ServeHTTP(w, r)
181182
postExecute()
182-
if atomicReadOnlyExecuting != 0 {
183-
t.Errorf("Wanted %d requests executing, got %d", 0, atomicReadOnlyExecuting)
183+
if want, got := int32(0), atomic.LoadInt32(&atomicReadOnlyExecuting); want != got {
184+
t.Errorf("Wanted %d requests executing, got %d", want, got)
184185
}
185186
}), requestInfoFactory)
186187

@@ -270,8 +271,8 @@ func TestApfExecuteMultipleRequests(t *testing.T) {
270271
onExecuteFunc := func() {
271272
preStartExecute.Done()
272273
preStartExecute.Wait()
273-
if int(atomicReadOnlyExecuting) != concurrentRequests {
274-
t.Errorf("Wanted %d requests executing, got %d", concurrentRequests, atomicReadOnlyExecuting)
274+
if want, got := int32(concurrentRequests), atomic.LoadInt32(&atomicReadOnlyExecuting); want != got {
275+
t.Errorf("Wanted %d requests executing, got %d", want, got)
275276
}
276277
postStartExecute.Done()
277278
postStartExecute.Wait()
@@ -280,8 +281,8 @@ func TestApfExecuteMultipleRequests(t *testing.T) {
280281
postEnqueueFunc := func() {
281282
preEnqueue.Done()
282283
preEnqueue.Wait()
283-
if int(atomicReadOnlyWaiting) != concurrentRequests {
284-
t.Errorf("Wanted %d requests in queue, got %d", 1, atomicReadOnlyWaiting)
284+
if want, got := int32(concurrentRequests), atomic.LoadInt32(&atomicReadOnlyWaiting); want != got {
285+
t.Errorf("Wanted %d requests in queue, got %d", want, got)
285286

286287
}
287288
postEnqueue.Done()
@@ -291,8 +292,8 @@ func TestApfExecuteMultipleRequests(t *testing.T) {
291292
postDequeueFunc := func() {
292293
preDequeue.Done()
293294
preDequeue.Wait()
294-
if atomicReadOnlyWaiting != 0 {
295-
t.Errorf("Wanted %d requests in queue, got %d", 0, atomicReadOnlyWaiting)
295+
if want, got := int32(0), atomic.LoadInt32(&atomicReadOnlyWaiting); want != got {
296+
t.Errorf("Wanted %d requests in queue, got %d", want, got)
296297
}
297298
postDequeue.Done()
298299
postDequeue.Wait()

0 commit comments

Comments
 (0)