@@ -38,6 +38,7 @@ import (
38
38
39
39
corev1 "k8s.io/kubernetes/pkg/apis/core/v1"
40
40
41
+ "k8s.io/kubernetes/test/utils/ktesting"
41
42
"k8s.io/utils/clock"
42
43
testingclock "k8s.io/utils/clock/testing"
43
44
@@ -105,7 +106,7 @@ func TestSecretCache(t *testing.T) {
105
106
ObjectMeta : metav1.ObjectMeta {Name : "name" , Namespace : "ns" , ResourceVersion : "125" },
106
107
}
107
108
fakeWatch .Add (secret )
108
- getFn := func () (bool , error ) {
109
+ getFn := func (_ context. Context ) (bool , error ) {
109
110
object , err := store .Get ("ns" , "name" )
110
111
if err != nil {
111
112
if apierrors .IsNotFound (err ) {
@@ -119,13 +120,15 @@ func TestSecretCache(t *testing.T) {
119
120
}
120
121
return true , nil
121
122
}
122
- if err := wait .PollImmediate (10 * time .Millisecond , time .Second , getFn ); err != nil {
123
+
124
+ tCtx := ktesting .Init (t )
125
+ if err := wait .PollUntilContextCancel (tCtx , 10 * time .Millisecond , true , getFn ); err != nil {
123
126
t .Errorf ("unexpected error: %v" , err )
124
127
}
125
128
126
129
// Eventually we should observer secret deletion.
127
130
fakeWatch .Delete (secret )
128
- getFn = func () (bool , error ) {
131
+ getFn = func (_ context. Context ) (bool , error ) {
129
132
_ , err := store .Get ("ns" , "name" )
130
133
if err != nil {
131
134
if apierrors .IsNotFound (err ) {
@@ -135,7 +138,9 @@ func TestSecretCache(t *testing.T) {
135
138
}
136
139
return false , nil
137
140
}
138
- if err := wait .PollImmediate (10 * time .Millisecond , time .Second , getFn ); err != nil {
141
+ deadlineCtx , deadlineCancel := context .WithTimeout (tCtx , time .Second )
142
+ defer deadlineCancel ()
143
+ if err := wait .PollUntilContextCancel (deadlineCtx , 10 * time .Millisecond , true , getFn ); err != nil {
139
144
t .Errorf ("unexpected error: %v" , err )
140
145
}
141
146
@@ -166,7 +171,7 @@ func TestSecretCacheMultipleRegistrations(t *testing.T) {
166
171
167
172
store .AddReference ("ns" , "name" , "pod" )
168
173
// This should trigger List and Watch actions eventually.
169
- actionsFn := func () (bool , error ) {
174
+ actionsFn := func (_ context. Context ) (bool , error ) {
170
175
actions := fakeClient .Actions ()
171
176
if len (actions ) > 2 {
172
177
return false , fmt .Errorf ("too many actions: %v" , actions )
@@ -179,7 +184,8 @@ func TestSecretCacheMultipleRegistrations(t *testing.T) {
179
184
}
180
185
return true , nil
181
186
}
182
- if err := wait .PollImmediate (10 * time .Millisecond , time .Second , actionsFn ); err != nil {
187
+ tCtx := ktesting .Init (t )
188
+ if err := wait .PollUntilContextCancel (tCtx , 10 * time .Millisecond , true , actionsFn ); err != nil {
183
189
t .Errorf ("unexpected error: %v" , err )
184
190
}
185
191
@@ -271,7 +277,7 @@ func TestImmutableSecretStopsTheReflector(t *testing.T) {
271
277
store := newSecretCache (fakeClient , fakeClock , time .Minute )
272
278
273
279
key := objectKey {namespace : "ns" , name : "name" }
274
- itemExists := func () (bool , error ) {
280
+ itemExists := func (_ context. Context ) (bool , error ) {
275
281
store .lock .Lock ()
276
282
defer store .lock .Unlock ()
277
283
_ , ok := store .items [key ]
@@ -289,7 +295,8 @@ func TestImmutableSecretStopsTheReflector(t *testing.T) {
289
295
290
296
// AddReference should start reflector.
291
297
store .AddReference ("ns" , "name" , "pod" )
292
- if err := wait .Poll (10 * time .Millisecond , time .Second , itemExists ); err != nil {
298
+ tCtx := ktesting .Init (t )
299
+ if err := wait .PollUntilContextCancel (tCtx , 10 * time .Millisecond , false , itemExists ); err != nil {
293
300
t .Errorf ("item wasn't added to cache" )
294
301
}
295
302
@@ -309,7 +316,7 @@ func TestImmutableSecretStopsTheReflector(t *testing.T) {
309
316
fakeWatch .Add (tc .eventual )
310
317
311
318
// Eventually Get should return that secret.
312
- getFn := func () (bool , error ) {
319
+ getFn := func (_ context. Context ) (bool , error ) {
313
320
object , err := store .Get ("ns" , "name" )
314
321
if err != nil {
315
322
if apierrors .IsNotFound (err ) {
@@ -320,7 +327,9 @@ func TestImmutableSecretStopsTheReflector(t *testing.T) {
320
327
secret := object .(* v1.Secret )
321
328
return apiequality .Semantic .DeepEqual (tc .eventual , secret ), nil
322
329
}
323
- if err := wait .PollImmediate (10 * time .Millisecond , time .Second , getFn ); err != nil {
330
+ deadlineCtx , deadlineCancel := context .WithTimeout (tCtx , time .Second )
331
+ defer deadlineCancel ()
332
+ if err := wait .PollUntilContextCancel (deadlineCtx , 10 * time .Millisecond , true , getFn ); err != nil {
324
333
t .Errorf ("unexpected error: %v" , err )
325
334
}
326
335
@@ -358,7 +367,7 @@ func TestMaxIdleTimeStopsTheReflector(t *testing.T) {
358
367
store := newSecretCache (fakeClient , fakeClock , time .Minute )
359
368
360
369
key := objectKey {namespace : "ns" , name : "name" }
361
- itemExists := func () (bool , error ) {
370
+ itemExists := func (_ context. Context ) (bool , error ) {
362
371
store .lock .Lock ()
363
372
defer store .lock .Unlock ()
364
373
_ , ok := store .items [key ]
@@ -377,7 +386,8 @@ func TestMaxIdleTimeStopsTheReflector(t *testing.T) {
377
386
378
387
// AddReference should start reflector.
379
388
store .AddReference ("ns" , "name" , "pod" )
380
- if err := wait .Poll (10 * time .Millisecond , 10 * time .Second , itemExists ); err != nil {
389
+ tCtx := ktesting .Init (t )
390
+ if err := wait .PollUntilContextCancel (tCtx , 10 * time .Millisecond , false , itemExists ); err != nil {
381
391
t .Errorf ("item wasn't added to cache" )
382
392
}
383
393
@@ -440,7 +450,7 @@ func TestReflectorNotStoppedOnSlowInitialization(t *testing.T) {
440
450
store := newSecretCache (fakeClient , fakeClock , time .Minute )
441
451
442
452
key := objectKey {namespace : "ns" , name : "name" }
443
- itemExists := func () (bool , error ) {
453
+ itemExists := func (_ context. Context ) (bool , error ) {
444
454
store .lock .Lock ()
445
455
defer store .lock .Unlock ()
446
456
_ , ok := store .items [key ]
@@ -457,7 +467,7 @@ func TestReflectorNotStoppedOnSlowInitialization(t *testing.T) {
457
467
return ! item .stopped
458
468
}
459
469
460
- reflectorInitialized := func () (bool , error ) {
470
+ reflectorInitialized := func (_ context. Context ) (bool , error ) {
461
471
store .lock .Lock ()
462
472
defer store .lock .Unlock ()
463
473
item := store .items [key ]
@@ -469,7 +479,8 @@ func TestReflectorNotStoppedOnSlowInitialization(t *testing.T) {
469
479
470
480
// AddReference should start reflector.
471
481
store .AddReference ("ns" , "name" , "pod" )
472
- if err := wait .Poll (10 * time .Millisecond , 10 * time .Second , itemExists ); err != nil {
482
+ tCtx := ktesting .Init (t )
483
+ if err := wait .PollUntilContextCancel (tCtx , 10 * time .Millisecond , false , itemExists ); err != nil {
473
484
t .Errorf ("item wasn't added to cache" )
474
485
}
475
486
@@ -479,7 +490,7 @@ func TestReflectorNotStoppedOnSlowInitialization(t *testing.T) {
479
490
// Reflector didn't yet initialize, so it shouldn't be stopped.
480
491
// However, Get should still be failing.
481
492
assert .True (t , reflectorRunning ())
482
- initialized , _ := reflectorInitialized ()
493
+ initialized , _ := reflectorInitialized (tCtx )
483
494
assert .False (t , initialized )
484
495
_ , err := store .Get ("ns" , "name" )
485
496
if err == nil || ! strings .Contains (err .Error (), "failed to sync" ) {
@@ -488,7 +499,9 @@ func TestReflectorNotStoppedOnSlowInitialization(t *testing.T) {
488
499
489
500
// Initialization should successfully finish.
490
501
fakeClock .Step (30 * time .Second )
491
- if err := wait .Poll (10 * time .Millisecond , time .Second , reflectorInitialized ); err != nil {
502
+ deadlineCtx , deadlineCancel := context .WithTimeout (tCtx , time .Second )
503
+ defer deadlineCancel ()
504
+ if err := wait .PollUntilContextCancel (deadlineCtx , 10 * time .Millisecond , false , reflectorInitialized ); err != nil {
492
505
t .Errorf ("reflector didn't iniailize correctly" )
493
506
}
494
507
0 commit comments