@@ -18,6 +18,7 @@ package filters
18
18
19
19
import (
20
20
"context"
21
+ "errors"
21
22
"fmt"
22
23
"net/http"
23
24
"net/http/httptest"
@@ -178,11 +179,19 @@ func newApfHandlerWithFilter(t *testing.T, flowControlFilter utilflowcontrol.Int
178
179
r = r .WithContext (apirequest .WithUser (r .Context (), & user.DefaultInfo {
179
180
Groups : []string {user .AllUnauthenticated },
180
181
}))
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
+ }()
186
195
}), requestInfoFactory )
187
196
188
197
return handler
@@ -346,19 +355,21 @@ func TestApfCancelWaitRequest(t *testing.T) {
346
355
}
347
356
348
357
type fakeWatchApfFilter struct {
358
+ t * testing.T
349
359
lock sync.Mutex
350
360
inflight int
351
361
capacity int
352
362
353
- postExecutePanic bool
354
- preExecutePanic bool
363
+ postExecutePanic error
364
+ preExecutePanic error
355
365
356
366
utilflowcontrol.WatchTracker
357
367
utilflowcontrol.MaxSeatsTracker
358
368
}
359
369
360
- func newFakeWatchApfFilter (capacity int ) * fakeWatchApfFilter {
370
+ func newFakeWatchApfFilter (t * testing. T , capacity int ) * fakeWatchApfFilter {
361
371
return & fakeWatchApfFilter {
372
+ t : t ,
362
373
capacity : capacity ,
363
374
WatchTracker : utilflowcontrol .NewWatchTracker (),
364
375
MaxSeatsTracker : utilflowcontrol .NewMaxSeatsTracker (),
@@ -386,17 +397,23 @@ func (f *fakeWatchApfFilter) Handle(ctx context.Context,
386
397
return
387
398
}
388
399
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
+ }()
396
406
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
+ }()
400
417
}
401
418
402
419
func (f * fakeWatchApfFilter ) Run (stopCh <- chan struct {}) error {
@@ -448,7 +465,7 @@ func TestApfExecuteWatchRequestsWithInitializationSignal(t *testing.T) {
448
465
allRunning := sync.WaitGroup {}
449
466
allRunning .Add (2 * concurrentRequests )
450
467
451
- fakeFilter := newFakeWatchApfFilter (concurrentRequests )
468
+ fakeFilter := newFakeWatchApfFilter (t , concurrentRequests )
452
469
453
470
onExecuteFunc := func () {
454
471
firstRunning .Done ()
@@ -494,7 +511,7 @@ func TestApfExecuteWatchRequestsWithInitializationSignal(t *testing.T) {
494
511
}
495
512
496
513
func TestApfRejectWatchRequestsWithInitializationSignal (t * testing.T ) {
497
- fakeFilter := newFakeWatchApfFilter (0 )
514
+ fakeFilter := newFakeWatchApfFilter (t , 0 )
498
515
499
516
onExecuteFunc := func () {
500
517
t .Errorf ("Request unexepectedly executing" )
@@ -513,7 +530,7 @@ func TestApfWatchPanic(t *testing.T) {
513
530
epmetrics .Register ()
514
531
fcmetrics .Register ()
515
532
516
- fakeFilter := newFakeWatchApfFilter (1 )
533
+ fakeFilter := newFakeWatchApfFilter (t , 1 )
517
534
518
535
onExecuteFunc := func () {
519
536
panic ("test panic" )
@@ -540,11 +557,11 @@ func TestApfWatchPanic(t *testing.T) {
540
557
func TestApfWatchHandlePanic (t * testing.T ) {
541
558
epmetrics .Register ()
542
559
fcmetrics .Register ()
543
- preExecutePanicingFilter := newFakeWatchApfFilter (1 )
544
- preExecutePanicingFilter .preExecutePanic = true
560
+ preExecutePanicingFilter := newFakeWatchApfFilter (t , 1 )
561
+ preExecutePanicingFilter .preExecutePanic = http . ErrAbortHandler
545
562
546
- postExecutePanicingFilter := newFakeWatchApfFilter (1 )
547
- postExecutePanicingFilter .postExecutePanic = true
563
+ postExecutePanicingFilter := newFakeWatchApfFilter (t , 1 )
564
+ postExecutePanicingFilter .postExecutePanic = http . ErrAbortHandler
548
565
549
566
testCases := []struct {
550
567
name string
@@ -560,18 +577,31 @@ func TestApfWatchHandlePanic(t *testing.T) {
560
577
},
561
578
}
562
579
563
- onExecuteFunc := func () {
564
- time .Sleep (5 * time .Second )
565
- }
566
- postExecuteFunc := func () {}
567
-
568
580
for _ , test := range testCases {
569
581
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
+
570
599
apfHandler := newApfHandlerWithFilter (t , test .filter , time .Minute / 4 , onExecuteFunc , postExecuteFunc )
571
600
handler := func (w http.ResponseWriter , r * http.Request ) {
572
601
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 )
575
605
}
576
606
}()
577
607
apfHandler .ServeHTTP (w , r )
0 commit comments