File size: 4,501 Bytes
04dc9f2 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 | // Copyright 2025 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package synctest_test
import (
"fmt"
"internal/testenv"
"os"
"regexp"
"testing"
"testing/synctest"
"time"
)
// Tests for interactions between synctest bubbles and the testing package.
// Other bubble behaviors are tested in internal/synctest.
func TestSuccess(t *testing.T) {
synctest.Test(t, func(t *testing.T) {
})
}
func TestFatal(t *testing.T) {
runTest(t, nil, func() {
synctest.Test(t, func(t *testing.T) {
t.Fatal("fatal")
})
}, `^--- FAIL: TestFatal.*
synctest_test.go:.* fatal
FAIL
$`)
}
func TestError(t *testing.T) {
runTest(t, nil, func() {
synctest.Test(t, func(t *testing.T) {
t.Error("error")
})
}, `^--- FAIL: TestError.*
synctest_test.go:.* error
FAIL
$`)
}
func TestVerboseError(t *testing.T) {
runTest(t, []string{"-test.v"}, func() {
synctest.Test(t, func(t *testing.T) {
t.Error("error")
})
}, `^=== RUN TestVerboseError
synctest_test.go:.* error
--- FAIL: TestVerboseError.*
FAIL
$`)
}
func TestSkip(t *testing.T) {
runTest(t, nil, func() {
synctest.Test(t, func(t *testing.T) {
t.Skip("skip")
})
}, `^PASS
$`)
}
func TestVerboseSkip(t *testing.T) {
runTest(t, []string{"-test.v"}, func() {
synctest.Test(t, func(t *testing.T) {
t.Skip("skip")
})
}, `^=== RUN TestVerboseSkip
synctest_test.go:.* skip
--- PASS: TestVerboseSkip.*
PASS
$`)
}
func TestCleanup(t *testing.T) {
done := false
synctest.Test(t, func(t *testing.T) {
ch := make(chan struct{})
t.Cleanup(func() {
// This cleanup function should execute inside the test's bubble.
// (If it doesn't the runtime will panic.)
close(ch)
})
// synctest.Test will wait for this goroutine to exit before returning.
// The cleanup function signals the goroutine to exit before the wait starts.
go func() {
<-ch
done = true
}()
})
if !done {
t.Fatalf("background goroutine did not return")
}
}
func TestContext(t *testing.T) {
state := "not started"
synctest.Test(t, func(t *testing.T) {
go func() {
state = "waiting on context"
<-t.Context().Done()
state = "done"
}()
// Wait blocks until the goroutine above is blocked on t.Context().Done().
synctest.Wait()
if got, want := state, "waiting on context"; got != want {
t.Fatalf("state = %q, want %q", got, want)
}
})
// t.Context() is canceled before the test completes,
// and synctest.Test does not return until the goroutine has set its state to "done".
if got, want := state, "done"; got != want {
t.Fatalf("state = %q, want %q", got, want)
}
}
func TestDeadline(t *testing.T) {
synctest.Test(t, func(t *testing.T) {
defer wantPanic(t, "testing: t.Deadline called inside synctest bubble")
_, _ = t.Deadline()
})
}
func TestParallel(t *testing.T) {
synctest.Test(t, func(t *testing.T) {
defer wantPanic(t, "testing: t.Parallel called inside synctest bubble")
t.Parallel()
})
}
func TestRun(t *testing.T) {
synctest.Test(t, func(t *testing.T) {
defer wantPanic(t, "testing: t.Run called inside synctest bubble")
t.Run("subtest", func(t *testing.T) {
})
})
}
func TestHelper(t *testing.T) {
runTest(t, []string{"-test.v"}, func() {
synctest.Test(t, func(t *testing.T) {
helperLog(t, "log in helper")
})
}, `^=== RUN TestHelper
synctest_test.go:.* log in helper
--- PASS: TestHelper.*
PASS
$`)
}
func wantPanic(t *testing.T, want string) {
if e := recover(); e != nil {
if got := fmt.Sprint(e); got != want {
t.Errorf("got panic message %q, want %q", got, want)
}
} else {
t.Errorf("got no panic, want one")
}
}
func runTest(t *testing.T, args []string, f func(), pattern string) {
if os.Getenv("GO_WANT_HELPER_PROCESS") == "1" {
f()
return
}
t.Helper()
re := regexp.MustCompile(pattern)
testenv.MustHaveExec(t)
cmd := testenv.Command(t, testenv.Executable(t), "-test.run=^"+regexp.QuoteMeta(t.Name())+"$", "-test.count=1")
cmd.Args = append(cmd.Args, args...)
cmd = testenv.CleanCmdEnv(cmd)
cmd.Env = append(cmd.Env, "GO_WANT_HELPER_PROCESS=1")
out, _ := cmd.CombinedOutput()
if !re.Match(out) {
t.Errorf("got output:\n%s\nwant matching:\n%s", out, pattern)
}
}
func TestNow(t *testing.T) {
synctest.Test(t, func(t *testing.T) {
if got, want := time.Now(), time.Date(2000, time.January, 1, 0, 0, 0, 0, time.UTC); !got.Equal(want) {
t.Errorf("time.Now() = %v, want %v", got, want)
}
})
}
|