@@ -171,22 +171,24 @@ func TestCheckForUpgradesScheduler(t *testing.T) {
171
171
const testUpgradeCheckURL = "http://localhost:8080"
172
172
173
173
t .Run ("panic from checkForUpgrades doesn't bubble up" , func (t * testing.T ) {
174
- done := make (chan bool , 1 )
174
+ ctx , cancel := context .WithCancel (context .Background ())
175
+ defer cancel ()
176
+
175
177
// capture logs
176
178
var calls []string
177
- logging .SetLogFunc ( 1 , func (input genericr.Entry ) {
179
+ ctx = logging .NewContext ( ctx , genericr . New ( func (input genericr.Entry ) {
178
180
calls = append (calls , input .Message )
179
- })
181
+ }))
180
182
181
183
// A panicking call
182
184
funcFoo = func () (* http.Response , error ) {
183
185
panic (fmt .Errorf ("oh no!" ))
184
186
}
185
187
186
- go CheckForUpgradesScheduler (done , "4.7.3" , testUpgradeCheckURL , fakeClient , cfg , false ,
188
+ go CheckForUpgradesScheduler (ctx , "4.7.3" , testUpgradeCheckURL , fakeClient , cfg , false ,
187
189
& MockCacheClient {works : true })
188
190
time .Sleep (1 * time .Second )
189
- done <- true
191
+ cancel ()
190
192
191
193
// Sleeping leads to some non-deterministic results, but we expect at least 1 execution
192
194
// plus one log for the failure to apply the configmap
@@ -195,32 +197,36 @@ func TestCheckForUpgradesScheduler(t *testing.T) {
195
197
})
196
198
197
199
t .Run ("cache sync fail leads to log and exit" , func (t * testing.T ) {
198
- done := make (chan bool , 1 )
200
+ ctx , cancel := context .WithCancel (context .Background ())
201
+ defer cancel ()
202
+
199
203
// capture logs
200
204
var calls []string
201
- logging .SetLogFunc ( 1 , func (input genericr.Entry ) {
205
+ ctx = logging .NewContext ( ctx , genericr . New ( func (input genericr.Entry ) {
202
206
calls = append (calls , input .Message )
203
- })
207
+ }))
204
208
205
209
// Set loop time to 1s and sleep for 2s before sending the done signal -- though the cache sync
206
210
// failure will exit the func before the sleep ends
207
211
upgradeCheckPeriod = 1 * time .Second
208
- go CheckForUpgradesScheduler (done , "4.7.3" , testUpgradeCheckURL , fakeClient , cfg , false ,
212
+ go CheckForUpgradesScheduler (ctx , "4.7.3" , testUpgradeCheckURL , fakeClient , cfg , false ,
209
213
& MockCacheClient {works : false })
210
214
time .Sleep (2 * time .Second )
211
- done <- true
215
+ cancel ()
212
216
213
217
assert .Assert (t , len (calls ) == 1 )
214
218
assert .Equal (t , calls [0 ], `unable to sync cache for upgrade check` )
215
219
})
216
220
217
221
t .Run ("successful log each loop, ticker works" , func (t * testing.T ) {
218
- done := make (chan bool , 1 )
222
+ ctx , cancel := context .WithCancel (context .Background ())
223
+ defer cancel ()
224
+
219
225
// capture logs
220
226
var calls []string
221
- logging .SetLogFunc ( 1 , func (input genericr.Entry ) {
227
+ ctx = logging .NewContext ( ctx , genericr . New ( func (input genericr.Entry ) {
222
228
calls = append (calls , input .Message )
223
- })
229
+ }))
224
230
225
231
// A successful call
226
232
funcFoo = func () (* http.Response , error ) {
@@ -233,10 +239,10 @@ func TestCheckForUpgradesScheduler(t *testing.T) {
233
239
234
240
// Set loop time to 1s and sleep for 2s before sending the done signal
235
241
upgradeCheckPeriod = 1 * time .Second
236
- go CheckForUpgradesScheduler (done , "4.7.3" , testUpgradeCheckURL , fakeClient , cfg , false ,
242
+ go CheckForUpgradesScheduler (ctx , "4.7.3" , testUpgradeCheckURL , fakeClient , cfg , false ,
237
243
& MockCacheClient {works : true })
238
244
time .Sleep (2 * time .Second )
239
- done <- true
245
+ cancel ()
240
246
241
247
// Sleeping leads to some non-deterministic results, but we expect at least 2 executions
242
248
// plus one log for the failure to apply the configmap
0 commit comments