Skip to content

Commit 288ee23

Browse files
author
Uwe Jugel
committed
fix typos and race cond for changed logic
1 parent 57058b6 commit 288ee23

File tree

2 files changed

+20
-10
lines changed

2 files changed

+20
-10
lines changed

assert/assertions.go

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"runtime"
1414
"runtime/debug"
1515
"strings"
16+
"sync/atomic"
1617
"time"
1718
"unicode"
1819
"unicode/utf8"
@@ -2151,7 +2152,8 @@ func EventuallyWithT(t TestingT, condition func(collect *CollectT), waitFor time
21512152
conditionSucceeded
21522153
)
21532154

2154-
var lastFinishedTickErrs []error
2155+
var lastFinishedTickErrs atomic.Value // of []error
2156+
lastFinishedTickErrs.Store([]error{})
21552157
ch := make(chan int, 1)
21562158

21572159
checkCond := func() {
@@ -2165,7 +2167,9 @@ func EventuallyWithT(t TestingT, condition func(collect *CollectT), waitFor time
21652167
}
21662168
// Keep the collected tick errors, so that they can be copied to 't'
21672169
// when timeout is reached or there is an unexpected exit.
2168-
lastFinishedTickErrs = collect.errors
2170+
// Always store the actual value of collect.errors, even if nil
2171+
lastFinishedTickErrs.Store(collect.errors)
2172+
21692173
ch <- result
21702174
}()
21712175
condition(collect)
@@ -2176,6 +2180,16 @@ func EventuallyWithT(t TestingT, condition func(collect *CollectT), waitFor time
21762180
}
21772181
}
21782182

2183+
applyLastFinishedTickErrs := func() {
2184+
errs, ok := lastFinishedTickErrs.Load().([]error)
2185+
if !ok {
2186+
return
2187+
}
2188+
for _, err := range errs {
2189+
t.Errorf("%v", err)
2190+
}
2191+
}
2192+
21792193
timer := time.NewTimer(waitFor)
21802194
defer timer.Stop()
21812195

@@ -2190,19 +2204,15 @@ func EventuallyWithT(t TestingT, condition func(collect *CollectT), waitFor time
21902204
for {
21912205
select {
21922206
case <-timer.C:
2193-
for _, err := range lastFinishedTickErrs {
2194-
t.Errorf("%v", err)
2195-
}
2207+
applyLastFinishedTickErrs()
21962208
return Fail(t, "Condition never satisfied", msgAndArgs...)
21972209
case <-tickC:
21982210
tickC = nil
21992211
go checkCond()
22002212
case result := <-ch:
22012213
switch result {
22022214
case conditionExitedUnexpectedly:
2203-
for _, err := range lastFinishedTickErrs {
2204-
t.Errorf("%v", err)
2205-
}
2215+
applyLastFinishedTickErrs()
22062216
return Fail(t, "Condition exited unexpectedly", msgAndArgs...)
22072217
case conditionSucceeded:
22082218
return true

require/requirements_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -803,6 +803,6 @@ func TestEventuallyWithTOuterFailNow(t *testing.T) {
803803
}
804804

805805
EventuallyWithT(mockT, condition, 100*time.Millisecond, 20*time.Millisecond)
806-
True(t, mockT.Failed, "Check should pass")
807-
Equal(t, 1, counter, "Condition is expected to be called 1 times")
806+
True(t, mockT.Failed, "Check should fail")
807+
Equal(t, 1, counter, "Condition is expected to be called once")
808808
}

0 commit comments

Comments
 (0)