|
| 1 | +//go:build go1.25 |
| 2 | + |
| 3 | +package rapid |
| 4 | + |
| 5 | +import ( |
| 6 | + "testing" |
| 7 | + "testing/synctest" |
| 8 | +) |
| 9 | + |
| 10 | +// SyncTest runs prop within a testing/synctest bubble. |
| 11 | +// Callers must already be executing inside a rapid.Check-style helper; |
| 12 | +// SyncTest forwards failures to the parent *rapid.T and restores its state afterwards. |
| 13 | +func SyncTest(t *T, prop func(*T)) { |
| 14 | + if t == nil { |
| 15 | + panic("rapid.SyncTest requires *rapid.T") |
| 16 | + } |
| 17 | + |
| 18 | + t.Helper() |
| 19 | + |
| 20 | + testT, ok := underlyingTestingT(t.tb) |
| 21 | + if !ok { |
| 22 | + t.Fatalf("[rapid] SyncTest requires a *testing.T backing the current rapid test") |
| 23 | + return |
| 24 | + } |
| 25 | + |
| 26 | + syncTestWithinRapid(t, testT, prop) |
| 27 | +} |
| 28 | + |
| 29 | +func syncTestWithinRapid(t *T, parent *testing.T, prop func(*T)) { |
| 30 | + // synctest.Test converts failures inside the bubble into parent.FailNow (runtime.Goexit), |
| 31 | + // which would bypass rapid's panic-based failure capture/shrinking. Run the bubble in a |
| 32 | + // separate goroutine, swallow failures inside the bubble, and re-panic outside as a |
| 33 | + // *testError so checkOnce can shrink and generate failfiles as usual. |
| 34 | + resultCh := make(chan *testError, 1) |
| 35 | + |
| 36 | + go func() { |
| 37 | + var captured *testError |
| 38 | + returned := false |
| 39 | + defer func() { |
| 40 | + if r := recover(); r != nil { |
| 41 | + captured = panicToError(r, 3) |
| 42 | + } else if !returned && captured == nil { |
| 43 | + captured = panicToError(stopTest("[rapid] SyncTest aborted via testing.FailNow"), 3) |
| 44 | + } |
| 45 | + resultCh <- captured |
| 46 | + }() |
| 47 | + |
| 48 | + synctest.Test(parent, func(st *testing.T) { |
| 49 | + st.Helper() |
| 50 | + |
| 51 | + prevTB := t.tb |
| 52 | + prevTBLog := t.tbLog // preserved so we keep the original logging behaviour |
| 53 | + prevCtx := t.ctx |
| 54 | + prevCancel := t.cancelCtx |
| 55 | + prevCleanups := t.cleanups |
| 56 | + prevCleaning := t.cleaning.Load() |
| 57 | + |
| 58 | + t.tb = st |
| 59 | + // Reset per-run state before the property runs in the bubble. |
| 60 | + // No lock is needed because no other goroutine touches t before we hand control to prop. |
| 61 | + t.ctx = nil |
| 62 | + t.cancelCtx = nil |
| 63 | + t.cleanups = nil |
| 64 | + t.cleaning.Store(false) |
| 65 | + |
| 66 | + var panicValue any |
| 67 | + defer func() { |
| 68 | + if r := recover(); r != nil { |
| 69 | + panicValue = r |
| 70 | + } |
| 71 | + |
| 72 | + func() { |
| 73 | + // Always run rapid cleanups, even if the property panicked. |
| 74 | + defer func() { |
| 75 | + if r := recover(); r != nil { |
| 76 | + panicValue = r |
| 77 | + } |
| 78 | + }() |
| 79 | + t.cleanup() |
| 80 | + }() |
| 81 | + |
| 82 | + t.tb = prevTB |
| 83 | + t.tbLog = prevTBLog |
| 84 | + t.ctx = prevCtx |
| 85 | + t.cancelCtx = prevCancel |
| 86 | + t.cleanups = prevCleanups |
| 87 | + t.cleaning.Store(prevCleaning) |
| 88 | + |
| 89 | + if panicValue != nil { |
| 90 | + captured = panicToError(panicValue, 3) |
| 91 | + } |
| 92 | + }() |
| 93 | + |
| 94 | + prop(t) |
| 95 | + t.failOnError() |
| 96 | + }) |
| 97 | + |
| 98 | + returned = true |
| 99 | + }() |
| 100 | + |
| 101 | + if err := <-resultCh; err != nil { |
| 102 | + panic(err) |
| 103 | + } |
| 104 | +} |
0 commit comments