Merge pull request #127089 from sanposhiho/revert-126574

Revert: Fix data race in APF tests
This commit is contained in:
Kubernetes Prow Robot 2024-09-03 15:13:16 +01:00 committed by GitHub
commit 49ffd6192f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -18,7 +18,6 @@ package filters
import ( import (
"context" "context"
"errors"
"fmt" "fmt"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
@ -27,7 +26,6 @@ import (
"reflect" "reflect"
"strings" "strings"
"sync" "sync"
"sync/atomic"
"testing" "testing"
"time" "time"
@ -130,20 +128,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 want, got := int32(1), atomic.LoadInt32(&atomicReadOnlyExecuting); decision != decisionSkipFilter && want != got { if decision != decisionSkipFilter && atomicReadOnlyExecuting != 1 {
t.Errorf("Wanted %d requests executing, got %d", want, got) t.Errorf("Wanted %d requests executing, got %d", 1, atomicReadOnlyExecuting)
} }
} }
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 want, got := int32(1), atomic.LoadInt32(&atomicReadOnlyWaiting); want != got { if atomicReadOnlyWaiting != 1 {
t.Errorf("Wanted %d requests in queue, got %d", want, got) t.Errorf("Wanted %d requests in queue, got %d", 1, atomicReadOnlyWaiting)
} }
} }
postDequeueFunc := func() { postDequeueFunc := func() {
if want, got := int32(0), atomic.LoadInt32(&atomicReadOnlyWaiting); want != got { if atomicReadOnlyWaiting != 0 {
t.Errorf("Wanted %d requests in queue, got %d", want, got) t.Errorf("Wanted %d requests in queue, got %d", 0, atomicReadOnlyWaiting)
} }
} }
return newApfServerWithHooks(t, decision, onExecuteFunc, postExecuteFunc, postEnqueueFunc, postDequeueFunc) return newApfServerWithHooks(t, decision, onExecuteFunc, postExecuteFunc, postEnqueueFunc, postDequeueFunc)
@ -179,19 +177,11 @@ func newApfHandlerWithFilter(t *testing.T, flowControlFilter utilflowcontrol.Int
r = r.WithContext(apirequest.WithUser(r.Context(), &user.DefaultInfo{ r = r.WithContext(apirequest.WithUser(r.Context(), &user.DefaultInfo{
Groups: []string{user.AllUnauthenticated}, Groups: []string{user.AllUnauthenticated},
})) }))
func() {
// the defer ensures that the following assertion is
// executed, even if the APF handler panics
// TODO: all test(s) using this filter must run serially to each other
defer func() {
t.Logf("the APF handler has finished, checking atomicReadOnlyExecuting")
if want, got := int32(0), atomic.LoadInt32(&atomicReadOnlyExecuting); want != got {
t.Errorf("Wanted %d requests executing, got %d", want, got)
}
}()
apfHandler.ServeHTTP(w, r) apfHandler.ServeHTTP(w, r)
postExecute() postExecute()
}() if atomicReadOnlyExecuting != 0 {
t.Errorf("Wanted %d requests executing, got %d", 0, atomicReadOnlyExecuting)
}
}), requestInfoFactory) }), requestInfoFactory)
return handler return handler
@ -280,8 +270,8 @@ func TestApfExecuteMultipleRequests(t *testing.T) {
onExecuteFunc := func() { onExecuteFunc := func() {
preStartExecute.Done() preStartExecute.Done()
preStartExecute.Wait() preStartExecute.Wait()
if want, got := int32(concurrentRequests), atomic.LoadInt32(&atomicReadOnlyExecuting); want != got { if int(atomicReadOnlyExecuting) != concurrentRequests {
t.Errorf("Wanted %d requests executing, got %d", want, got) t.Errorf("Wanted %d requests executing, got %d", concurrentRequests, atomicReadOnlyExecuting)
} }
postStartExecute.Done() postStartExecute.Done()
postStartExecute.Wait() postStartExecute.Wait()
@ -290,8 +280,8 @@ func TestApfExecuteMultipleRequests(t *testing.T) {
postEnqueueFunc := func() { postEnqueueFunc := func() {
preEnqueue.Done() preEnqueue.Done()
preEnqueue.Wait() preEnqueue.Wait()
if want, got := int32(concurrentRequests), atomic.LoadInt32(&atomicReadOnlyWaiting); want != got { if int(atomicReadOnlyWaiting) != concurrentRequests {
t.Errorf("Wanted %d requests in queue, got %d", want, got) t.Errorf("Wanted %d requests in queue, got %d", 1, atomicReadOnlyWaiting)
} }
postEnqueue.Done() postEnqueue.Done()
@ -301,8 +291,8 @@ func TestApfExecuteMultipleRequests(t *testing.T) {
postDequeueFunc := func() { postDequeueFunc := func() {
preDequeue.Done() preDequeue.Done()
preDequeue.Wait() preDequeue.Wait()
if want, got := int32(0), atomic.LoadInt32(&atomicReadOnlyWaiting); want != got { if atomicReadOnlyWaiting != 0 {
t.Errorf("Wanted %d requests in queue, got %d", want, got) t.Errorf("Wanted %d requests in queue, got %d", 0, atomicReadOnlyWaiting)
} }
postDequeue.Done() postDequeue.Done()
postDequeue.Wait() postDequeue.Wait()
@ -355,21 +345,19 @@ func TestApfCancelWaitRequest(t *testing.T) {
} }
type fakeWatchApfFilter struct { type fakeWatchApfFilter struct {
t *testing.T
lock sync.Mutex lock sync.Mutex
inflight int inflight int
capacity int capacity int
postExecutePanic error postExecutePanic bool
preExecutePanic error preExecutePanic bool
utilflowcontrol.WatchTracker utilflowcontrol.WatchTracker
utilflowcontrol.MaxSeatsTracker utilflowcontrol.MaxSeatsTracker
} }
func newFakeWatchApfFilter(t *testing.T, capacity int) *fakeWatchApfFilter { func newFakeWatchApfFilter(capacity int) *fakeWatchApfFilter {
return &fakeWatchApfFilter{ return &fakeWatchApfFilter{
t: t,
capacity: capacity, capacity: capacity,
WatchTracker: utilflowcontrol.NewWatchTracker(), WatchTracker: utilflowcontrol.NewWatchTracker(),
MaxSeatsTracker: utilflowcontrol.NewMaxSeatsTracker(), MaxSeatsTracker: utilflowcontrol.NewMaxSeatsTracker(),
@ -397,23 +385,17 @@ func (f *fakeWatchApfFilter) Handle(ctx context.Context,
return return
} }
func() { if f.preExecutePanic {
defer func() { panic("pre-exec-panic")
}
execFn()
if f.postExecutePanic {
panic("post-exec-panic")
}
f.lock.Lock() f.lock.Lock()
defer f.lock.Unlock() defer f.lock.Unlock()
f.inflight-- f.inflight--
}()
if f.preExecutePanic != nil {
f.t.Logf("going to panic (pre-exec) as expected with error: %v, fakeWatchApfFilter: %#v", f.preExecutePanic, f)
panic(f.preExecutePanic)
}
execFn()
if f.postExecutePanic != nil {
f.t.Logf("going to panic (post-exec) as expected with error: %v, fakeWatchApfFilter: %#v", f.postExecutePanic, f)
panic(f.postExecutePanic)
}
}()
} }
func (f *fakeWatchApfFilter) Run(stopCh <-chan struct{}) error { func (f *fakeWatchApfFilter) Run(stopCh <-chan struct{}) error {
@ -465,7 +447,7 @@ func TestApfExecuteWatchRequestsWithInitializationSignal(t *testing.T) {
allRunning := sync.WaitGroup{} allRunning := sync.WaitGroup{}
allRunning.Add(2 * concurrentRequests) allRunning.Add(2 * concurrentRequests)
fakeFilter := newFakeWatchApfFilter(t, concurrentRequests) fakeFilter := newFakeWatchApfFilter(concurrentRequests)
onExecuteFunc := func() { onExecuteFunc := func() {
firstRunning.Done() firstRunning.Done()
@ -511,7 +493,7 @@ func TestApfExecuteWatchRequestsWithInitializationSignal(t *testing.T) {
} }
func TestApfRejectWatchRequestsWithInitializationSignal(t *testing.T) { func TestApfRejectWatchRequestsWithInitializationSignal(t *testing.T) {
fakeFilter := newFakeWatchApfFilter(t, 0) fakeFilter := newFakeWatchApfFilter(0)
onExecuteFunc := func() { onExecuteFunc := func() {
t.Errorf("Request unexepectedly executing") t.Errorf("Request unexepectedly executing")
@ -530,7 +512,7 @@ func TestApfWatchPanic(t *testing.T) {
epmetrics.Register() epmetrics.Register()
fcmetrics.Register() fcmetrics.Register()
fakeFilter := newFakeWatchApfFilter(t, 1) fakeFilter := newFakeWatchApfFilter(1)
onExecuteFunc := func() { onExecuteFunc := func() {
panic("test panic") panic("test panic")
@ -557,11 +539,11 @@ func TestApfWatchPanic(t *testing.T) {
func TestApfWatchHandlePanic(t *testing.T) { func TestApfWatchHandlePanic(t *testing.T) {
epmetrics.Register() epmetrics.Register()
fcmetrics.Register() fcmetrics.Register()
preExecutePanicingFilter := newFakeWatchApfFilter(t, 1) preExecutePanicingFilter := newFakeWatchApfFilter(1)
preExecutePanicingFilter.preExecutePanic = http.ErrAbortHandler preExecutePanicingFilter.preExecutePanic = true
postExecutePanicingFilter := newFakeWatchApfFilter(t, 1) postExecutePanicingFilter := newFakeWatchApfFilter(1)
postExecutePanicingFilter.postExecutePanic = http.ErrAbortHandler postExecutePanicingFilter.postExecutePanic = true
testCases := []struct { testCases := []struct {
name string name string
@ -577,31 +559,18 @@ func TestApfWatchHandlePanic(t *testing.T) {
}, },
} }
for _, test := range testCases {
t.Run(test.name, func(t *testing.T) {
onExecuteFunc := func() { onExecuteFunc := func() {
time.Sleep(5 * time.Second) time.Sleep(5 * time.Second)
// this function should not be executed if
// pre-execute panic is set
if test.filter.preExecutePanic != nil {
t.Errorf("did not expect the execute function to be executed")
}
t.Logf("on-execute function invoked")
}
// we either panic before the execute function, or after,
// so the following function should never be executed.
postExecuteFunc := func() {
t.Errorf("did not expect the post-execute function to be invoked")
} }
postExecuteFunc := func() {}
for _, test := range testCases {
t.Run(test.name, func(t *testing.T) {
apfHandler := newApfHandlerWithFilter(t, test.filter, time.Minute/4, onExecuteFunc, postExecuteFunc) apfHandler := newApfHandlerWithFilter(t, test.filter, time.Minute/4, onExecuteFunc, postExecuteFunc)
handler := func(w http.ResponseWriter, r *http.Request) { handler := func(w http.ResponseWriter, r *http.Request) {
defer func() { defer func() {
recovered := recover() if err := recover(); err == nil {
if err, ok := recovered.(error); !ok || !errors.Is(err, http.ErrAbortHandler) { t.Errorf("expected panic, got %v", err)
t.Errorf("expected panic with error: %v, but got: %v", http.ErrAbortHandler, err)
} }
}() }()
apfHandler.ServeHTTP(w, r) apfHandler.ServeHTTP(w, r)