Skip to content

Commit 60195c6

Browse files
committed
fix(condition): in async testing, allow condition goroutine to exit
Signed-off-by: Frederic BIDON <fredbi@yahoo.com>
1 parent 22c49f1 commit 60195c6

File tree

1 file changed

+28
-4
lines changed

1 file changed

+28
-4
lines changed

internal/assertions/condition.go

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -558,9 +558,21 @@ func (p *conditionPoller) executeCondition(parentCtx, ctx context.Context, failF
558558
case <-ctx.Done():
559559
return // timeout = success
560560
case fn := <-p.conditionChan:
561-
if err := fn(ctx); err != nil {
562-
close(p.doneChan) // (condition true <=> returns error) = failure for Never and Consistently
561+
var conditionWg sync.WaitGroup
562+
conditionWg.Add(1)
563+
go func() { // guards against the condition issue an early GoExit
564+
defer conditionWg.Done()
565+
566+
if err := fn(ctx); err != nil {
567+
close(p.doneChan) // (condition true <=> returns error) = failure for Never and Consistently
568+
}
569+
}()
570+
conditionWg.Wait()
571+
572+
select {
573+
case <-p.doneChan: // done: early exit
563574
return
575+
default:
564576
}
565577
}
566578
}
@@ -577,9 +589,21 @@ func (p *conditionPoller) executeCondition(parentCtx, ctx context.Context, failF
577589
failFunc(ctx.Err().Error())
578590
return
579591
case fn := <-p.conditionChan:
580-
if err := fn(ctx); err == nil {
581-
close(p.doneChan) // (condition true <=> err == nil) = success for Eventually
592+
var conditionWg sync.WaitGroup
593+
conditionWg.Add(1)
594+
go func() { // guards against the condition issue an early GoExit
595+
defer conditionWg.Done()
596+
597+
if err := fn(ctx); err == nil {
598+
close(p.doneChan) // (condition true <=> err == nil) = success for Eventually
599+
}
600+
}()
601+
conditionWg.Wait()
602+
603+
select {
604+
case <-p.doneChan: // done: early exit
582605
return
606+
default:
583607
}
584608
}
585609
}

0 commit comments

Comments
 (0)