@@ -61,7 +61,7 @@ import (
61
61
"k8s.io/utils/pointer"
62
62
)
63
63
64
- func newTestCacherWithoutSyncing (s storage.Interface ) (* Cacher , storage.Versioner , error ) {
64
+ func newTestCacherWithoutSyncing (s storage.Interface , c clock. WithTicker ) (* Cacher , storage.Versioner , error ) {
65
65
prefix := "pods"
66
66
config := Config {
67
67
Storage : s ,
@@ -85,15 +85,15 @@ func newTestCacherWithoutSyncing(s storage.Interface) (*Cacher, storage.Versione
85
85
NewFunc : func () runtime.Object { return & example.Pod {} },
86
86
NewListFunc : func () runtime.Object { return & example.PodList {} },
87
87
Codec : codecs .LegacyCodec (examplev1 .SchemeGroupVersion ),
88
- Clock : clock. RealClock {} ,
88
+ Clock : c ,
89
89
}
90
90
cacher , err := NewCacherFromConfig (config )
91
91
92
92
return cacher , storage.APIObjectVersioner {}, err
93
93
}
94
94
95
95
func newTestCacher (s storage.Interface ) (* Cacher , storage.Versioner , error ) {
96
- cacher , versioner , err := newTestCacherWithoutSyncing (s )
96
+ cacher , versioner , err := newTestCacherWithoutSyncing (s , clock. RealClock {} )
97
97
if err != nil {
98
98
return nil , versioner , err
99
99
}
@@ -435,17 +435,15 @@ apiserver_watch_cache_consistent_read_total{fallback="true", resource="pods", su
435
435
podList .ResourceVersion = tc .watchCacheRV
436
436
return nil
437
437
}
438
- // TODO: Use fake clock for this test to reduce execution time.
439
- cacher , _ , err := newTestCacher (backingStorage )
438
+ c := testingclock . NewFakeClock ( time .Now ())
439
+ cacher , _ , err := newTestCacherWithoutSyncing (backingStorage , c )
440
440
if err != nil {
441
441
t .Fatalf ("Couldn't create cacher: %v" , err )
442
442
}
443
443
defer cacher .Stop ()
444
444
proxy := NewCacheProxy (cacher , backingStorage )
445
- if ! utilfeature .DefaultFeatureGate .Enabled (features .ResilientWatchCacheInitialization ) {
446
- if err := cacher .ready .wait (context .Background ()); err != nil {
447
- t .Fatalf ("unexpected error waiting for the cache to be ready" )
448
- }
445
+ if err := cacher .ready .wait (context .Background ()); err != nil {
446
+ t .Fatalf ("unexpected error waiting for the cache to be ready" )
449
447
}
450
448
451
449
if fmt .Sprintf ("%d" , cacher .watchCache .resourceVersion ) != tc .watchCacheRV {
@@ -466,8 +464,28 @@ apiserver_watch_cache_consistent_read_total{fallback="true", resource="pods", su
466
464
return nil
467
465
}
468
466
result := & example.PodList {}
467
+
468
+ ctx , clockStepCancelFn := context .WithTimeout (context .TODO (), time .Minute )
469
+ defer clockStepCancelFn ()
470
+ if tc .expectBlock {
471
+ go func (ctx context.Context ) {
472
+ for {
473
+ select {
474
+ case <- ctx .Done ():
475
+ return
476
+ default :
477
+ if c .HasWaiters () {
478
+ c .Step (blockTimeout )
479
+ }
480
+ time .Sleep (time .Millisecond )
481
+ }
482
+ }
483
+ }(ctx )
484
+ }
485
+
469
486
start := cacher .clock .Now ()
470
487
err = proxy .GetList (context .TODO (), "pods/ns" , storage.ListOptions {ResourceVersion : "" }, result )
488
+ clockStepCancelFn ()
471
489
duration := cacher .clock .Since (start )
472
490
if (err != nil ) != tc .expectError {
473
491
t .Fatalf ("Unexpected error err: %v" , err )
@@ -605,7 +623,7 @@ func TestTooManyRequestsNotReturned(t *testing.T) {
605
623
606
624
dummyErr := fmt .Errorf ("dummy" )
607
625
backingStorage := & dummyStorage {err : dummyErr }
608
- cacher , _ , err := newTestCacherWithoutSyncing (backingStorage )
626
+ cacher , _ , err := newTestCacherWithoutSyncing (backingStorage , clock. RealClock {} )
609
627
if err != nil {
610
628
t .Fatalf ("Couldn't create cacher: %v" , err )
611
629
}
@@ -732,7 +750,7 @@ func TestWatchNotHangingOnStartupFailure(t *testing.T) {
732
750
// constantly failing lists to the underlying storage.
733
751
dummyErr := fmt .Errorf ("dummy" )
734
752
backingStorage := & dummyStorage {err : dummyErr }
735
- cacher , _ , err := newTestCacherWithoutSyncing (backingStorage )
753
+ cacher , _ , err := newTestCacherWithoutSyncing (backingStorage , clock. RealClock {} )
736
754
if err != nil {
737
755
t .Fatalf ("Couldn't create cacher: %v" , err )
738
756
}
0 commit comments