mirror of
https://github.com/kubernetes/kubernetes.git
synced 2026-05-28 04:04:39 -04:00
ktesting: fix potential log panic
If the goroutine happens to log after the test has already terminated, testing.T.Log panics. We must ensure that the goroutine has stopped before allowing the test to terminate.
This commit is contained in:
parent
dbe44e3584
commit
8a2d153817
1 changed files with 6 additions and 0 deletions
|
|
@ -48,11 +48,17 @@ func withTimeout(ctx context.Context, tb TB, timeout time.Duration, timeoutCause
|
|||
cancelCtx, cancel := context.WithCancelCause(ctx)
|
||||
after := time.NewTimer(timeout)
|
||||
stopCtx, stop := context.WithCancel(ctx) // Only used internally, doesn't need a cause.
|
||||
done := make(chan struct{})
|
||||
tb.Cleanup(func() {
|
||||
cancel(cleanupErr(tb.Name()))
|
||||
stop()
|
||||
// Wait for goroutine termination. This is important because
|
||||
// otherwise the goroutine might log through tb *after* the
|
||||
// test has already finished, which causes a panic.
|
||||
<-done
|
||||
})
|
||||
go func() {
|
||||
defer close(done)
|
||||
select {
|
||||
case <-stopCtx.Done():
|
||||
after.Stop()
|
||||
|
|
|
|||
Loading…
Reference in a new issue