| 1 |
// Copyright 2025 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. |
| 4 |
|
| 5 |
package race_test |
| 6 |
|
| 7 |
import ( |
| 8 |
"internal/synctest" |
| 9 |
"testing" |
| 10 |
"time" |
| 11 |
) |
| 12 |
|
| 13 |
func TestRaceSynctestGoroutinesExit(t *testing.T) { |
| 14 |
synctest.Run(func() { |
| 15 |
x := 0 |
| 16 |
_ = x |
| 17 |
f := func() { |
| 18 |
x = 1 |
| 19 |
} |
| 20 |
go f() |
| 21 |
go f() |
| 22 |
}) |
| 23 |
} |
| 24 |
|
| 25 |
func TestNoRaceSynctestGoroutinesExit(t *testing.T) { |
| 26 |
synctest.Run(func() { |
| 27 |
x := 0 |
| 28 |
_ = x |
| 29 |
f := func() { |
| 30 |
x = 1 |
| 31 |
} |
| 32 |
go f() |
| 33 |
synctest.Wait() |
| 34 |
go f() |
| 35 |
}) |
| 36 |
} |
| 37 |
|
| 38 |
func TestRaceSynctestGoroutinesRecv(t *testing.T) { |
| 39 |
synctest.Run(func() { |
| 40 |
x := 0 |
| 41 |
_ = x |
| 42 |
ch := make(chan struct{}) |
| 43 |
f := func() { |
| 44 |
x = 1 |
| 45 |
<-ch |
| 46 |
} |
| 47 |
go f() |
| 48 |
go f() |
| 49 |
close(ch) |
| 50 |
}) |
| 51 |
} |
| 52 |
|
| 53 |
func TestRaceSynctestGoroutinesUnblocked(t *testing.T) { |
| 54 |
synctest.Run(func() { |
| 55 |
x := 0 |
| 56 |
_ = x |
| 57 |
ch := make(chan struct{}) |
| 58 |
f := func() { |
| 59 |
<-ch |
| 60 |
x = 1 |
| 61 |
} |
| 62 |
go f() |
| 63 |
go f() |
| 64 |
close(ch) |
| 65 |
}) |
| 66 |
} |
| 67 |
|
| 68 |
func TestRaceSynctestGoroutinesSleep(t *testing.T) { |
| 69 |
synctest.Run(func() { |
| 70 |
x := 0 |
| 71 |
_ = x |
| 72 |
go func() { |
| 73 |
time.Sleep(1 * time.Second) |
| 74 |
x = 1 |
| 75 |
time.Sleep(2 * time.Second) |
| 76 |
}() |
| 77 |
go func() { |
| 78 |
time.Sleep(2 * time.Second) |
| 79 |
x = 1 |
| 80 |
time.Sleep(1 * time.Second) |
| 81 |
}() |
| 82 |
time.Sleep(5 * time.Second) |
| 83 |
}) |
| 84 |
} |
| 85 |
|
| 86 |
func TestRaceSynctestTimers(t *testing.T) { |
| 87 |
synctest.Run(func() { |
| 88 |
x := 0 |
| 89 |
_ = x |
| 90 |
f := func() { |
| 91 |
x = 1 |
| 92 |
} |
| 93 |
time.AfterFunc(1*time.Second, f) |
| 94 |
time.AfterFunc(2*time.Second, f) |
| 95 |
time.Sleep(5 * time.Second) |
| 96 |
}) |
| 97 |
} |