apiserver: fix data race in apf tests in server/filters package

Signed-off-by: jonyhy96 <hy352144278@gmail.com>
Co-authored-by: chenwen  <wen.chen@daocloud.io>
This commit is contained in:
jonyhy96 2022-01-28 15:03:11 +08:00 committed by Abu Kashem
parent 60c4c2b252
commit dde23bb0b1
No known key found for this signature in database
GPG Key ID: E5ECC1124B5F9C68

View File

@ -26,6 +26,7 @@ import (
"reflect" "reflect"
"strings" "strings"
"sync" "sync"
"sync/atomic"
"testing" "testing"
"time" "time"
@ -128,20 +129,20 @@ func newApfServerWithSingleRequest(t *testing.T, decision mockDecision) *httptes
t.Errorf("execute should not be invoked") t.Errorf("execute should not be invoked")
} }
// atomicReadOnlyExecuting can be either 0 or 1 as we test one request at a time. // atomicReadOnlyExecuting can be either 0 or 1 as we test one request at a time.
if decision != decisionSkipFilter && atomicReadOnlyExecuting != 1 { if want, got := int32(1), atomic.LoadInt32(&atomicReadOnlyExecuting); decision != decisionSkipFilter && want != got {
t.Errorf("Wanted %d requests executing, got %d", 1, atomicReadOnlyExecuting) t.Errorf("Wanted %d requests executing, got %d", want, got)
} }
} }
postExecuteFunc := func() {} postExecuteFunc := func() {}
// atomicReadOnlyWaiting can be either 0 or 1 as we test one request at a time. // atomicReadOnlyWaiting can be either 0 or 1 as we test one request at a time.
postEnqueueFunc := func() { postEnqueueFunc := func() {
if atomicReadOnlyWaiting != 1 { if want, got := int32(1), atomic.LoadInt32(&atomicReadOnlyWaiting); want != got {
t.Errorf("Wanted %d requests in queue, got %d", 1, atomicReadOnlyWaiting) t.Errorf("Wanted %d requests in queue, got %d", want, got)
} }
} }
postDequeueFunc := func() { postDequeueFunc := func() {
if atomicReadOnlyWaiting != 0 { if want, got := int32(0), atomic.LoadInt32(&atomicReadOnlyWaiting); want != got {
t.Errorf("Wanted %d requests in queue, got %d", 0, atomicReadOnlyWaiting) t.Errorf("Wanted %d requests in queue, got %d", want, got)
} }
} }
return newApfServerWithHooks(t, decision, onExecuteFunc, postExecuteFunc, postEnqueueFunc, postDequeueFunc) return newApfServerWithHooks(t, decision, onExecuteFunc, postExecuteFunc, postEnqueueFunc, postDequeueFunc)
@ -179,8 +180,8 @@ func newApfHandlerWithFilter(t *testing.T, flowControlFilter utilflowcontrol.Int
})) }))
apfHandler.ServeHTTP(w, r) apfHandler.ServeHTTP(w, r)
postExecute() postExecute()
if atomicReadOnlyExecuting != 0 { if want, got := int32(0), atomic.LoadInt32(&atomicReadOnlyExecuting); want != got {
t.Errorf("Wanted %d requests executing, got %d", 0, atomicReadOnlyExecuting) t.Errorf("Wanted %d requests executing, got %d", want, got)
} }
}), requestInfoFactory) }), requestInfoFactory)
@ -270,8 +271,8 @@ func TestApfExecuteMultipleRequests(t *testing.T) {
onExecuteFunc := func() { onExecuteFunc := func() {
preStartExecute.Done() preStartExecute.Done()
preStartExecute.Wait() preStartExecute.Wait()
if int(atomicReadOnlyExecuting) != concurrentRequests { if want, got := int32(concurrentRequests), atomic.LoadInt32(&atomicReadOnlyExecuting); want != got {
t.Errorf("Wanted %d requests executing, got %d", concurrentRequests, atomicReadOnlyExecuting) t.Errorf("Wanted %d requests executing, got %d", want, got)
} }
postStartExecute.Done() postStartExecute.Done()
postStartExecute.Wait() postStartExecute.Wait()
@ -280,8 +281,8 @@ func TestApfExecuteMultipleRequests(t *testing.T) {
postEnqueueFunc := func() { postEnqueueFunc := func() {
preEnqueue.Done() preEnqueue.Done()
preEnqueue.Wait() preEnqueue.Wait()
if int(atomicReadOnlyWaiting) != concurrentRequests { if want, got := int32(concurrentRequests), atomic.LoadInt32(&atomicReadOnlyWaiting); want != got {
t.Errorf("Wanted %d requests in queue, got %d", 1, atomicReadOnlyWaiting) t.Errorf("Wanted %d requests in queue, got %d", want, got)
} }
postEnqueue.Done() postEnqueue.Done()
@ -291,8 +292,8 @@ func TestApfExecuteMultipleRequests(t *testing.T) {
postDequeueFunc := func() { postDequeueFunc := func() {
preDequeue.Done() preDequeue.Done()
preDequeue.Wait() preDequeue.Wait()
if atomicReadOnlyWaiting != 0 { if want, got := int32(0), atomic.LoadInt32(&atomicReadOnlyWaiting); want != got {
t.Errorf("Wanted %d requests in queue, got %d", 0, atomicReadOnlyWaiting) t.Errorf("Wanted %d requests in queue, got %d", want, got)
} }
postDequeue.Done() postDequeue.Done()
postDequeue.Wait() postDequeue.Wait()