@@ -244,13 +244,19 @@ var _ = Describe("BackgroundCmd", func() {
244244 sut .KeepAlive ()
245245 sut .KeepAlive () // we call it twice voluntarily to test the invariant that only a single goroutine is created
246246
247- // Wait enough interval to have at least expected calls (times + 20%)
248- <- time .After (cmdKeepAliveTickDuration * time .Duration (times ) + cmdBootstrapAllowedDuration / 5 )
249-
250247 underSut , ok := sut .(* backgroundCmd )
251248 Expect (ok ).To (BeTrue ())
252249 Expect (underSut ).ToNot (BeNil ())
253250
251+ // Ensure the keepAlive goroutine has fully exited before mock cleanup runs,
252+ // preventing panics when mocks are called after the test finishes.
253+ DeferCleanup (func () {
254+ <- underSut .keepAliveDone
255+ })
256+
257+ // Wait enough interval to have at least expected calls (times + 20%)
258+ <- time .After (cmdKeepAliveTickDuration * time .Duration (times ) + cmdBootstrapAllowedDuration / 5 )
259+
254260 // Stop timer ASAP to have realistic amount of calls
255261 underSut .keepAliveQuit <- 0
256262 },
@@ -268,12 +274,17 @@ var _ = Describe("BackgroundCmd", func() {
268274 sut .KeepAlive ()
269275 sut .KeepAlive () // we call it twice voluntarily to test the invariant that only a single goroutine is created
270276
271- // Wait enough interval to have at least expected calls (times + 20%)
272- <- time .After (cmdKeepAliveTickDuration * time .Duration (times ) + cmdBootstrapAllowedDuration / 5 )
273-
274277 underSut , ok := sut .(* backgroundCmd )
275278 Expect (ok ).To (BeTrue ())
276279 Expect (underSut ).ToNot (BeNil ())
280+
281+ // Ensure the keepAlive goroutine has fully exited before mock cleanup runs.
282+ DeferCleanup (func () {
283+ <- underSut .keepAliveDone
284+ })
285+
286+ // Wait enough interval to have at least expected calls (times + 20%)
287+ <- time .After (cmdKeepAliveTickDuration * time .Duration (times ) + cmdBootstrapAllowedDuration / 5 )
277288 },
278289 Entry ("Find fails once" , nil , errors .New ("find fails" ), nil , 1 ),
279290 Entry ("Signal fails once" , & os.Process {}, nil , errors .New ("signal fails" ), 1 ),
0 commit comments