Skip to content

Commit

Permalink
assert: fix flaky TestNeverTrue
Browse files Browse the repository at this point in the history
Fix flaky TestNeverTrue: use a channel to make a synchronized counter to
avoid a race condition.
  • Loading branch information
dolmen committed Jul 10, 2023
1 parent 78aedbf commit 55b9534
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions assert/assertions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2794,14 +2794,19 @@ func TestNeverFalse(t *testing.T) {
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) {
mockT := new(testing.T)
state := 0

// A synchronized counter using a channel
counter := make(chan int, 2)
counter <- 1
counter <- 2
defer close(counter)

// Will return true on second call.
condition := func() bool {
defer func() {
state = state + 1
}()
return state == 2
return 2 == <-counter
}

False(t, Never(mockT, condition, 100*time.Millisecond, 20*time.Millisecond))
Expand Down

0 comments on commit 55b9534

Please sign in to comment.