Skip to content

Commit

Permalink
Fix panics setting TestResult asynchronously (#36)
Browse files Browse the repository at this point in the history
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
prashantv authored Jan 23, 2025
1 parent 4f381dd commit 62c3f50
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 7 deletions.
9 changes: 2 additions & 7 deletions fake_tb.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,9 @@ func RunTest(testFn func(t testing.TB)) TestResult {
tb := newFakeTB()

go func() {
defer close(tb.completed)
defer tb.checkPanic()
defer tb.postTest()
defer tb.runCleanups()

testFn(tb)
}()
Expand Down Expand Up @@ -102,12 +103,6 @@ func (tb *fakeTB) checkPanic() {
}
}

func (tb *fakeTB) postTest() {
defer close(tb.completed)

tb.runCleanups()
}

func (tb *fakeTB) runCleanups() {
// Set cleanupRoot so log callers can use cleanup's callers.
if self := getCaller(withSelf); self != 0 {
Expand Down
13 changes: 13 additions & 0 deletions faket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package faket
import (
"testing"

"github.com/prashantv/faket/internal/syncutil"
"github.com/prashantv/faket/internal/want"
)

Expand Down Expand Up @@ -41,3 +42,15 @@ func TestFakeT_Helpers(t *testing.T) {
func testHelper1(t testing.TB) { t.Helper() }
func testHelper2(t testing.TB) {}
func testHelper3(t testing.TB) { t.Helper() }

func TestFakeT_PanicRace(t *testing.T) {
syncutil.RunN(100, func(int) {
for i := 0; i < 100; i++ {
res := RunTest(func(t testing.TB) {
panic("panicked")
})
want.Equal(t, "Failed", res.Failed(), true)
want.Equal(t, "Panicked", res.Panicked(), true)
}
})
}
19 changes: 19 additions & 0 deletions internal/syncutil/syncutil.go
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()
}
21 changes: 21 additions & 0 deletions internal/syncutil/syncutil_test.go
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)
}

0 comments on commit 62c3f50

Please sign in to comment.