mirror of https://github.com/stretchr/testify.git
assert: fix flaky TestNeverTrue
Fix flaky TestNeverTrue: use a channel to make a synchronized list of return values to avoid a race condition.pull/1428/head
parent
862e41010c
commit
81667ad920
|
@ -2794,14 +2794,20 @@ func TestNeverFalse(t *testing.T) {
|
||||||
True(t, Never(t, condition, 100*time.Millisecond, 20*time.Millisecond))
|
True(t, Never(t, condition, 100*time.Millisecond, 20*time.Millisecond))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TestNeverTrue checks Never with a condition that returns true on second call.
|
||||||
func TestNeverTrue(t *testing.T) {
|
func TestNeverTrue(t *testing.T) {
|
||||||
mockT := new(testing.T)
|
mockT := new(testing.T)
|
||||||
state := 0
|
|
||||||
|
// A list of values returned by condition.
|
||||||
|
// Channel protects against concurrent access.
|
||||||
|
returns := make(chan bool, 2)
|
||||||
|
returns <- false
|
||||||
|
returns <- true
|
||||||
|
defer close(returns)
|
||||||
|
|
||||||
|
// Will return true on second call.
|
||||||
condition := func() bool {
|
condition := func() bool {
|
||||||
defer func() {
|
return <-returns
|
||||||
state = state + 1
|
|
||||||
}()
|
|
||||||
return state == 2
|
|
||||||
}
|
}
|
||||||
|
|
||||||
False(t, Never(mockT, condition, 100*time.Millisecond, 20*time.Millisecond))
|
False(t, Never(mockT, condition, 100*time.Millisecond, 20*time.Millisecond))
|
||||||
|
|
Loading…
Reference in New Issue