Skip to content

Commit 8fa3e61

Browse files
committed
apiserver: improve logging for apf tests in server/filters package
1 parent dde23bb commit 8fa3e61

File tree

1 file changed

+62
-32
lines changed

1 file changed

+62
-32
lines changed

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

Lines changed: 62 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package filters
1818

1919
import (
2020
"context"
21+
"errors"
2122
"fmt"
2223
"net/http"
2324
"net/http/httptest"
@@ -178,11 +179,19 @@ func newApfHandlerWithFilter(t *testing.T, flowControlFilter utilflowcontrol.Int
178179
r = r.WithContext(apirequest.WithUser(r.Context(), &user.DefaultInfo{
179180
Groups: []string{user.AllUnauthenticated},
180181
}))
181-
apfHandler.ServeHTTP(w, r)
182-
postExecute()
183-
if want, got := int32(0), atomic.LoadInt32(&atomicReadOnlyExecuting); want != got {
184-
t.Errorf("Wanted %d requests executing, got %d", want, got)
185-
}
182+
func() {
183+
// the defer ensures that the following assertion is
184+
// executed, even if the APF handler panics
185+
// TODO: all test(s) using this filter must run serially to each other
186+
defer func() {
187+
t.Logf("the APF handler has finished, checking atomicReadOnlyExecuting")
188+
if want, got := int32(0), atomic.LoadInt32(&atomicReadOnlyExecuting); want != got {
189+
t.Errorf("Wanted %d requests executing, got %d", want, got)
190+
}
191+
}()
192+
apfHandler.ServeHTTP(w, r)
193+
postExecute()
194+
}()
186195
}), requestInfoFactory)
187196

188197
return handler
@@ -346,19 +355,21 @@ func TestApfCancelWaitRequest(t *testing.T) {
346355
}
347356

348357
type fakeWatchApfFilter struct {
358+
t *testing.T
349359
lock sync.Mutex
350360
inflight int
351361
capacity int
352362

353-
postExecutePanic bool
354-
preExecutePanic bool
363+
postExecutePanic error
364+
preExecutePanic error
355365

356366
utilflowcontrol.WatchTracker
357367
utilflowcontrol.MaxSeatsTracker
358368
}
359369

360-
func newFakeWatchApfFilter(capacity int) *fakeWatchApfFilter {
370+
func newFakeWatchApfFilter(t *testing.T, capacity int) *fakeWatchApfFilter {
361371
return &fakeWatchApfFilter{
372+
t: t,
362373
capacity: capacity,
363374
WatchTracker: utilflowcontrol.NewWatchTracker(),
364375
MaxSeatsTracker: utilflowcontrol.NewMaxSeatsTracker(),
@@ -386,17 +397,23 @@ func (f *fakeWatchApfFilter) Handle(ctx context.Context,
386397
return
387398
}
388399

389-
if f.preExecutePanic {
390-
panic("pre-exec-panic")
391-
}
392-
execFn()
393-
if f.postExecutePanic {
394-
panic("post-exec-panic")
395-
}
400+
func() {
401+
defer func() {
402+
f.lock.Lock()
403+
defer f.lock.Unlock()
404+
f.inflight--
405+
}()
396406

397-
f.lock.Lock()
398-
defer f.lock.Unlock()
399-
f.inflight--
407+
if f.preExecutePanic != nil {
408+
f.t.Logf("going to panic (pre-exec) as expected with error: %v, fakeWatchApfFilter: %#v", f.preExecutePanic, f)
409+
panic(f.preExecutePanic)
410+
}
411+
execFn()
412+
if f.postExecutePanic != nil {
413+
f.t.Logf("going to panic (post-exec) as expected with error: %v, fakeWatchApfFilter: %#v", f.postExecutePanic, f)
414+
panic(f.postExecutePanic)
415+
}
416+
}()
400417
}
401418

402419
func (f *fakeWatchApfFilter) Run(stopCh <-chan struct{}) error {
@@ -448,7 +465,7 @@ func TestApfExecuteWatchRequestsWithInitializationSignal(t *testing.T) {
448465
allRunning := sync.WaitGroup{}
449466
allRunning.Add(2 * concurrentRequests)
450467

451-
fakeFilter := newFakeWatchApfFilter(concurrentRequests)
468+
fakeFilter := newFakeWatchApfFilter(t, concurrentRequests)
452469

453470
onExecuteFunc := func() {
454471
firstRunning.Done()
@@ -494,7 +511,7 @@ func TestApfExecuteWatchRequestsWithInitializationSignal(t *testing.T) {
494511
}
495512

496513
func TestApfRejectWatchRequestsWithInitializationSignal(t *testing.T) {
497-
fakeFilter := newFakeWatchApfFilter(0)
514+
fakeFilter := newFakeWatchApfFilter(t, 0)
498515

499516
onExecuteFunc := func() {
500517
t.Errorf("Request unexepectedly executing")
@@ -513,7 +530,7 @@ func TestApfWatchPanic(t *testing.T) {
513530
epmetrics.Register()
514531
fcmetrics.Register()
515532

516-
fakeFilter := newFakeWatchApfFilter(1)
533+
fakeFilter := newFakeWatchApfFilter(t, 1)
517534

518535
onExecuteFunc := func() {
519536
panic("test panic")
@@ -540,11 +557,11 @@ func TestApfWatchPanic(t *testing.T) {
540557
func TestApfWatchHandlePanic(t *testing.T) {
541558
epmetrics.Register()
542559
fcmetrics.Register()
543-
preExecutePanicingFilter := newFakeWatchApfFilter(1)
544-
preExecutePanicingFilter.preExecutePanic = true
560+
preExecutePanicingFilter := newFakeWatchApfFilter(t, 1)
561+
preExecutePanicingFilter.preExecutePanic = http.ErrAbortHandler
545562

546-
postExecutePanicingFilter := newFakeWatchApfFilter(1)
547-
postExecutePanicingFilter.postExecutePanic = true
563+
postExecutePanicingFilter := newFakeWatchApfFilter(t, 1)
564+
postExecutePanicingFilter.postExecutePanic = http.ErrAbortHandler
548565

549566
testCases := []struct {
550567
name string
@@ -560,18 +577,31 @@ func TestApfWatchHandlePanic(t *testing.T) {
560577
},
561578
}
562579

563-
onExecuteFunc := func() {
564-
time.Sleep(5 * time.Second)
565-
}
566-
postExecuteFunc := func() {}
567-
568580
for _, test := range testCases {
569581
t.Run(test.name, func(t *testing.T) {
582+
onExecuteFunc := func() {
583+
time.Sleep(5 * time.Second)
584+
585+
// this function should not be executed if
586+
// pre-execute panic is set
587+
if test.filter.preExecutePanic != nil {
588+
t.Errorf("did not expect the execute function to be executed")
589+
}
590+
t.Logf("on-execute function invoked")
591+
}
592+
593+
// we either panic before the execute function, or after,
594+
// so the following function should never be executed.
595+
postExecuteFunc := func() {
596+
t.Errorf("did not expect the post-execute function to be invoked")
597+
}
598+
570599
apfHandler := newApfHandlerWithFilter(t, test.filter, time.Minute/4, onExecuteFunc, postExecuteFunc)
571600
handler := func(w http.ResponseWriter, r *http.Request) {
572601
defer func() {
573-
if err := recover(); err == nil {
574-
t.Errorf("expected panic, got %v", err)
602+
recovered := recover()
603+
if err, ok := recovered.(error); !ok || !errors.Is(err, http.ErrAbortHandler) {
604+
t.Errorf("expected panic with error: %v, but got: %v", http.ErrAbortHandler, err)
575605
}
576606
}()
577607
apfHandler.ServeHTTP(w, r)

0 commit comments

Comments
 (0)