1 // Copyright 2023 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
19 // Test that racy access to the default functions behaves reasonably.
20 func TestDefaultRace(t *testing.T) {
21 // Skip the test in short mode, but even in short mode run
22 // the test if we are using the race detector, because part
23 // of this is to see whether the race detector reports any problems.
24 if testing.Short() && !race.Enabled {
25 t.Skip("skipping starting another executable in short mode")
28 const env = "GO_RAND_TEST_HELPER_CODE"
29 if v := os.Getenv(env); v != "" {
36 for i := 0; i < 6; i++ {
38 t.Run(strconv.Itoa(i), func(t *testing.T) {
40 exe, err := os.Executable()
44 cmd := testenv.Command(t, exe, "-test.run=TestDefaultRace")
45 cmd = testenv.CleanCmdEnv(cmd)
46 cmd.Env = append(cmd.Env, fmt.Sprintf("GO_RAND_TEST_HELPER_CODE=%d", i/2))
48 cmd.Env = append(cmd.Env, "GODEBUG=randautoseed=0")
50 out, err := cmd.CombinedOutput()
61 // doDefaultTest should be run before there have been any calls to the
62 // top-level math/rand functions. Make sure that we can make concurrent
63 // calls to top-level functions and to Seed without any duplicate values.
64 // This will also give the race detector a change to report any problems.
65 func doDefaultTest(t *testing.T, v string) {
66 code, err := strconv.Atoi(v)
68 t.Fatalf("internal error: unrecognized code %q", v)
71 goroutines := runtime.GOMAXPROCS(0)
76 ch := make(chan uint64, goroutines*3)
79 // The various tests below should not cause race detector reports
80 // and should not produce duplicate results.
82 // Note: these tests can theoretically fail when using fastrand64
83 // in that it is possible to coincidentally get the same random
84 // number twice. That could happen something like 1 / 2**64 times,
85 // which is rare enough that it may never happen. We don't worry
90 // Call Seed and Uint64 concurrently.
92 for i := 0; i < goroutines; i++ {
99 for i := 0; i < goroutines; i++ {
106 // Call Uint64 concurrently with no Seed.
108 for i := 0; i < goroutines; i++ {
115 // Start with Uint64 to pick the fast source, then call
116 // Seed and Uint64 concurrently.
119 for i := 0; i < goroutines; i++ {
126 for i := 0; i < goroutines; i++ {
133 t.Fatalf("internal error: unrecognized code %d", code)
141 m := make(map[uint64]bool)
144 t.Errorf("saw %d twice", i)