@@ -549,15 +549,22 @@ var _ = Describe("controller", func() {
549
549
ctrl .startWatches = []source.TypedSource [reconcile.Request ]{src }
550
550
551
551
By ("Calling startEventSourcesLocked asynchronously" )
552
+ wg := sync.WaitGroup {}
552
553
go func () {
553
554
defer GinkgoRecover ()
555
+ defer wg .Done ()
556
+
557
+ wg .Add (1 )
554
558
Expect (ctrl .startEventSourcesLocked (ctx )).To (Succeed ())
555
559
}()
556
560
557
561
By ("Calling startEventSourcesLocked again" )
558
562
var didSubsequentCallComplete atomic.Bool
559
563
go func () {
560
564
defer GinkgoRecover ()
565
+ defer wg .Done ()
566
+
567
+ wg .Add (1 )
561
568
Expect (ctrl .startEventSourcesLocked (ctx )).To (Succeed ())
562
569
didSubsequentCallComplete .Store (true )
563
570
}()
@@ -570,6 +577,7 @@ var _ = Describe("controller", func() {
570
577
571
578
// Assert that second call to startEventSourcesLocked is now complete
572
579
Eventually (didSubsequentCallComplete .Load ).Should (BeTrue (), "startEventSourcesLocked should complete after source is started and synced" )
580
+ wg .Wait ()
573
581
})
574
582
575
583
It ("should reset c.startWatches to nil after returning" , func () {
@@ -1155,30 +1163,26 @@ var _ = Describe("controller", func() {
1155
1163
}),
1156
1164
}
1157
1165
1158
- // Wait for the goroutines to finish before returning to avoid racing with the
1159
- // assignment in BeforeEach block.
1160
- var wg sync.WaitGroup
1166
+ // channel to prevent the goroutine from outliving the It test
1167
+ waitChan := make (chan struct {})
1161
1168
1162
1169
// Invoked in a goroutine because Warmup will block
1163
- wg .Add (1 )
1164
1170
go func () {
1165
1171
defer GinkgoRecover ()
1166
- defer wg . Done ( )
1172
+ defer close ( waitChan )
1167
1173
Expect (ctrl .Warmup (ctx )).To (Succeed ())
1168
1174
}()
1169
1175
1170
1176
cancel ()
1171
- wg . Wait ()
1177
+ <- waitChan
1172
1178
})
1173
1179
1174
1180
It ("should be called before leader election runnables if warmup is enabled" , func () {
1175
1181
// This unit test exists to ensure that a warmup enabled controller will actually be
1176
1182
// called in the warmup phase before the leader election runnables are started. It
1177
1183
// catches regressions in the controller that would not implement warmupRunnable from
1178
1184
// pkg/manager.
1179
-
1180
1185
ctx , cancel := context .WithCancel (context .Background ())
1181
- defer cancel ()
1182
1186
1183
1187
By ("Creating a channel to track execution order" )
1184
1188
runnableExecutionOrderChan := make (chan string , 2 )
@@ -1229,14 +1233,19 @@ var _ = Describe("controller", func() {
1229
1233
Expect (m .Add (nonWarmupCtrl )).To (Succeed ())
1230
1234
1231
1235
By ("Starting the manager" )
1236
+ waitChan := make (chan struct {})
1232
1237
go func () {
1233
1238
defer GinkgoRecover ()
1239
+ defer close (waitChan )
1234
1240
Expect (m .Start (ctx )).To (Succeed ())
1235
1241
}()
1236
1242
1237
1243
<- m .Elected ()
1238
1244
Expect (<- runnableExecutionOrderChan ).To (Equal (warmupRunnableName ))
1239
1245
Expect (<- runnableExecutionOrderChan ).To (Equal (nonWarmupRunnableName ))
1246
+
1247
+ cancel ()
1248
+ <- waitChan
1240
1249
})
1241
1250
1242
1251
It ("should not cause a data race when called concurrently" , func () {
@@ -1299,7 +1308,6 @@ var _ = Describe("controller", func() {
1299
1308
1300
1309
It ("should start sources added after Warmup is called" , func () {
1301
1310
ctx , cancel := context .WithCancel (context .Background ())
1302
- defer cancel ()
1303
1311
1304
1312
ctrl .CacheSyncTimeout = time .Second
1305
1313
@@ -1312,12 +1320,17 @@ var _ = Describe("controller", func() {
1312
1320
return nil
1313
1321
}))).To (Succeed ())
1314
1322
1323
+ waitChan := make (chan struct {})
1315
1324
go func () {
1316
1325
defer GinkgoRecover ()
1317
1326
Expect (ctrl .Start (ctx )).To (Succeed ())
1327
+ close (waitChan )
1318
1328
}()
1319
1329
1320
1330
Eventually (didWatchStart .Load ).Should (BeTrue (), "watch should be started if it is added after Warmup" )
1331
+
1332
+ cancel ()
1333
+ <- waitChan
1321
1334
})
1322
1335
1323
1336
DescribeTable ("should not leak goroutines when manager is stopped with warmup runnable" ,
@@ -1349,19 +1362,22 @@ var _ = Describe("controller", func() {
1349
1362
// ignore needs to go after the testenv.Start() call to ignore the apiserver
1350
1363
// process
1351
1364
currentGRs := goleak .IgnoreCurrent ()
1365
+ waitChan := make (chan struct {})
1352
1366
go func () {
1353
1367
defer GinkgoRecover ()
1354
1368
Expect (m .Start (ctx )).To (Succeed ())
1369
+ close (waitChan )
1355
1370
}()
1356
1371
1357
1372
<- m .Elected ()
1358
1373
By ("stopping the manager via context" )
1359
1374
cancel ()
1360
1375
1361
1376
Eventually (func () error { return goleak .Find (currentGRs ) }).Should (Succeed ())
1377
+ <- waitChan
1362
1378
},
1363
- Entry ("manager with leader election enabled" , true ),
1364
- Entry ("manager without leader election enabled" , false ),
1379
+ Entry ("and with leader election enabled" , true ),
1380
+ Entry ("and without leader election enabled" , false ),
1365
1381
)
1366
1382
})
1367
1383
@@ -1373,7 +1389,6 @@ var _ = Describe("controller", func() {
1373
1389
It ("should not start sources when Warmup is called if warmup is disabled but start it when Start is called." , func () {
1374
1390
// Setup controller with sources that complete successfully
1375
1391
ctx , cancel := context .WithCancel (context .Background ())
1376
- defer cancel ()
1377
1392
1378
1393
ctrl .CacheSyncTimeout = time .Second
1379
1394
var isSourceStarted atomic.Bool
@@ -1391,12 +1406,16 @@ var _ = Describe("controller", func() {
1391
1406
Expect (isSourceStarted .Load ()).To (BeFalse ())
1392
1407
1393
1408
By ("Calling Start when EnableWarmup is false" )
1394
- // Now call Start
1409
+ waitChan := make (chan struct {})
1410
+
1395
1411
go func () {
1396
1412
defer GinkgoRecover ()
1397
1413
Expect (ctrl .Start (ctx )).To (Succeed ())
1414
+ close (waitChan )
1398
1415
}()
1399
1416
Eventually (isSourceStarted .Load ).Should (BeTrue ())
1417
+ cancel ()
1418
+ <- waitChan
1400
1419
})
1401
1420
})
1402
1421
})
0 commit comments