@@ -46,6 +46,7 @@ func TestDeletePods(t *testing.T) {
46
46
description string
47
47
interval time.Duration
48
48
timeout time.Duration
49
+ ctxTimeoutEarly bool
49
50
expectPendingPods bool
50
51
expectError bool
51
52
expectedError * error
@@ -91,6 +92,22 @@ func TestDeletePods(t *testing.T) {
91
92
return nil , fmt .Errorf ("%q: not found" , name )
92
93
},
93
94
},
95
+ {
96
+ description : "Context Canceled" ,
97
+ interval : 1000 * time .Millisecond ,
98
+ timeout : 5 * time .Second ,
99
+ ctxTimeoutEarly : true ,
100
+ expectPendingPods : true ,
101
+ expectError : true ,
102
+ expectedError : & wait .ErrWaitTimeout ,
103
+ getPodFn : func (namespace , name string ) (* corev1.Pod , error ) {
104
+ oldPodMap , _ := createPods (false )
105
+ if oldPod , found := oldPodMap [name ]; found {
106
+ return & oldPod , nil
107
+ }
108
+ return nil , fmt .Errorf ("%q: not found" , name )
109
+ },
110
+ },
94
111
{
95
112
description : "Client error could be passed out" ,
96
113
interval : 200 * time .Millisecond ,
@@ -107,14 +124,22 @@ func TestDeletePods(t *testing.T) {
107
124
for _ , test := range tests {
108
125
t .Run (test .description , func (t * testing.T ) {
109
126
_ , pods := createPods (false )
110
- ctx := context .TODO ()
127
+ ctx := context .Background ()
128
+ if test .ctxTimeoutEarly {
129
+ ctx , _ = context .WithTimeout (ctx , 100 * time .Millisecond )
130
+ }
131
+ start := time .Now ()
111
132
pendingPods , err := waitForDelete (ctx , pods , test .interval , test .timeout , false , test .getPodFn , nil , time .Duration (math .MaxInt64 ))
112
-
133
+ elapsed := time . Since ( start )
113
134
if test .expectError {
114
135
if err == nil {
115
136
t .Fatalf ("%s: unexpected non-error" , test .description )
116
137
} else if test .expectedError != nil {
117
- if * test .expectedError != err {
138
+ if test .ctxTimeoutEarly {
139
+ if elapsed >= test .timeout {
140
+ t .Fatalf ("%s: the supplied context did not effectively cancel the waitForDelete" , test .description )
141
+ }
142
+ } else if * test .expectedError != err {
118
143
t .Fatalf ("%s: the error does not match expected error" , test .description )
119
144
}
120
145
}
0 commit comments