1
2
3
4
5 package race_test
6
7 import (
8 "testing"
9 )
10
11 type P struct {
12 x, y int
13 }
14
15 type S struct {
16 s1, s2 P
17 }
18
19 func TestNoRaceComp(t *testing.T) {
20 c := make(chan bool, 1)
21 var s S
22 go func() {
23 s.s2.x = 1
24 c <- true
25 }()
26 s.s2.y = 2
27 <-c
28 }
29
30 func TestNoRaceComp2(t *testing.T) {
31 c := make(chan bool, 1)
32 var s S
33 go func() {
34 s.s1.x = 1
35 c <- true
36 }()
37 s.s1.y = 2
38 <-c
39 }
40
41 func TestRaceComp(t *testing.T) {
42 c := make(chan bool, 1)
43 var s S
44 go func() {
45 s.s2.y = 1
46 c <- true
47 }()
48 s.s2.y = 2
49 <-c
50 }
51
52 func TestRaceComp2(t *testing.T) {
53 c := make(chan bool, 1)
54 var s S
55 go func() {
56 s.s1.x = 1
57 c <- true
58 }()
59 s = S{}
60 <-c
61 }
62
63 func TestRaceComp3(t *testing.T) {
64 c := make(chan bool, 1)
65 var s S
66 go func() {
67 s.s2.y = 1
68 c <- true
69 }()
70 s = S{}
71 <-c
72 }
73
74 func TestRaceCompArray(t *testing.T) {
75 c := make(chan bool, 1)
76 s := make([]S, 10)
77 x := 4
78 go func() {
79 s[x].s2.y = 1
80 c <- true
81 }()
82 x = 5
83 <-c
84 }
85
86 type P2 P
87 type S2 S
88
89 func TestRaceConv1(t *testing.T) {
90 c := make(chan bool, 1)
91 var p P2
92 go func() {
93 p.x = 1
94 c <- true
95 }()
96 _ = P(p).x
97 <-c
98 }
99
100 func TestRaceConv2(t *testing.T) {
101 c := make(chan bool, 1)
102 var p P2
103 go func() {
104 p.x = 1
105 c <- true
106 }()
107 ptr := &p
108 _ = P(*ptr).x
109 <-c
110 }
111
112 func TestRaceConv3(t *testing.T) {
113 c := make(chan bool, 1)
114 var s S2
115 go func() {
116 s.s1.x = 1
117 c <- true
118 }()
119 _ = P2(S(s).s1).x
120 <-c
121 }
122
123 type X struct {
124 V [4]P
125 }
126
127 type X2 X
128
129 func TestRaceConv4(t *testing.T) {
130 c := make(chan bool, 1)
131 var x X2
132 go func() {
133 x.V[1].x = 1
134 c <- true
135 }()
136 _ = P2(X(x).V[1]).x
137 <-c
138 }
139
140 type Ptr struct {
141 s1, s2 *P
142 }
143
144 func TestNoRaceCompPtr(t *testing.T) {
145 c := make(chan bool, 1)
146 p := Ptr{&P{}, &P{}}
147 go func() {
148 p.s1.x = 1
149 c <- true
150 }()
151 p.s1.y = 2
152 <-c
153 }
154
155 func TestNoRaceCompPtr2(t *testing.T) {
156 c := make(chan bool, 1)
157 p := Ptr{&P{}, &P{}}
158 go func() {
159 p.s1.x = 1
160 c <- true
161 }()
162 _ = p
163 <-c
164 }
165
166 func TestRaceCompPtr(t *testing.T) {
167 c := make(chan bool, 1)
168 p := Ptr{&P{}, &P{}}
169 go func() {
170 p.s2.x = 1
171 c <- true
172 }()
173 p.s2.x = 2
174 <-c
175 }
176
177 func TestRaceCompPtr2(t *testing.T) {
178 c := make(chan bool, 1)
179 p := Ptr{&P{}, &P{}}
180 go func() {
181 p.s2.x = 1
182 c <- true
183 }()
184 p.s2 = &P{}
185 <-c
186 }
187
View as plain text