-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix panics setting TestResult asynchronously (#36)
Panic handling is deferred before the defer that marks tests complete. This could lead to a test being marked complete, returning a result but then mutating that result after returning. This happened infrequently but could lead to a flaky result where a panicked test isn't marked as `Failed()`. Added test runs many iterations so we are more likely to trigger a failure on flakes.
- Loading branch information
Showing
4 changed files
with
55 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// Package syncutil has helpers for running concurrent code. | ||
package syncutil | ||
|
||
import "sync" | ||
|
||
// RunN runs n items in parallel, with no concurrency limiting. | ||
func RunN(n int, fn func(i int)) { | ||
var wg sync.WaitGroup | ||
for i := 0; i < n; i++ { | ||
wg.Add(1) | ||
|
||
go func(i int) { | ||
defer wg.Done() | ||
|
||
fn(i) | ||
}(i) | ||
} | ||
wg.Wait() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package syncutil | ||
|
||
import ( | ||
"sync/atomic" | ||
"testing" | ||
|
||
"github.com/prashantv/faket/internal/want" | ||
) | ||
|
||
func TestRunN(t *testing.T) { | ||
const n = 10 | ||
var counter atomic.Int32 | ||
done := make(chan struct{}) | ||
RunN(n, func(i int) { | ||
if counter.Add(1) == n { | ||
close(done) | ||
} | ||
<-done | ||
}) | ||
want.Equal(t, "counter", counter.Load(), 10) | ||
} |