File size: 1,467 Bytes
e36aeda
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// 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 race_test

import (
	"internal/synctest"
	"testing"
	"time"
)

func TestRaceSynctestGoroutinesExit(t *testing.T) {
	synctest.Run(func() {
		x := 0
		_ = x
		f := func() {
			x = 1
		}
		go f()
		go f()
	})
}

func TestNoRaceSynctestGoroutinesExit(t *testing.T) {
	synctest.Run(func() {
		x := 0
		_ = x
		f := func() {
			x = 1
		}
		go f()
		synctest.Wait()
		go f()
	})
}

func TestRaceSynctestGoroutinesRecv(t *testing.T) {
	synctest.Run(func() {
		x := 0
		_ = x
		ch := make(chan struct{})
		f := func() {
			x = 1
			<-ch
		}
		go f()
		go f()
		close(ch)
	})
}

func TestRaceSynctestGoroutinesUnblocked(t *testing.T) {
	synctest.Run(func() {
		x := 0
		_ = x
		ch := make(chan struct{})
		f := func() {
			<-ch
			x = 1
		}
		go f()
		go f()
		close(ch)
	})
}

func TestRaceSynctestGoroutinesSleep(t *testing.T) {
	synctest.Run(func() {
		x := 0
		_ = x
		go func() {
			time.Sleep(1 * time.Second)
			x = 1
			time.Sleep(2 * time.Second)
		}()
		go func() {
			time.Sleep(2 * time.Second)
			x = 1
			time.Sleep(1 * time.Second)
		}()
		time.Sleep(5 * time.Second)
	})
}

func TestRaceSynctestTimers(t *testing.T) {
	synctest.Run(func() {
		x := 0
		_ = x
		f := func() {
			x = 1
		}
		time.AfterFunc(1*time.Second, f)
		time.AfterFunc(2*time.Second, f)
		time.Sleep(5 * time.Second)
	})
}