aboutsummaryrefslogtreecommitdiff
path: root/tests/core/race/race_test.go
blob: 815b2b18a4eaff0cebd7b4910dee55f1248bab87 (plain)
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
// Copyright 2019 The Bazel Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package race_test

import (
	"bytes"
	"errors"
	"fmt"
	"os/exec"
	"runtime"
	"strings"
	"testing"

	"github.com/bazelbuild/rules_go/go/tools/bazel_testing"
)

func TestMain(m *testing.M) {
	bazel_testing.TestMain(m, bazel_testing.Args{
		Main: `
-- BUILD.bazel --
load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library", "go_test")

go_library(
    name = "racy",
    srcs = [
        "race_off.go",
        "race_on.go",
        "racy.go",
        "empty.s", # verify #2143
    ],
    importpath = "example.com/racy",
)

go_binary(
    name = "racy_cmd",
    srcs = ["main.go"],
    embed = [":racy"],
)

go_binary(
    name = "racy_cmd_race_mode",
    srcs = ["main.go"],
    embed = [":racy"],
    race = "on",
)

go_test(
    name = "racy_test",
    srcs = ["racy_test.go"],
    embed = [":racy"],
)

go_test(
    name = "racy_test_race_mode",
    srcs = ["racy_test.go"],
    embed = [":racy"],
    race = "on",
)

go_binary(
    name = "pure_bin",
    srcs = ["pure_bin.go"],
    pure = "on",
)

go_binary(
    name = "pure_race_bin",
    srcs = ["pure_bin.go"],
    pure = "on",
    race = "on",
)

go_library(
		name = "coverrace",
		srcs = ["coverrace.go"],
		importpath = "example.com/coverrace",
)

go_test(
		name = "coverrace_test",
		srcs = ["coverrace_test.go"],
		embed = [":coverrace"],
    race = "on",
)
-- race_off.go --
// +build !race

package main

const RaceEnabled = false

-- race_on.go --
// +build race

package main

const RaceEnabled = true

-- racy.go --
package main

import (
	"flag"
	"fmt"
	"os"
)

var wantRace = flag.Bool("wantrace", false, "")

func Race() {
	if *wantRace != RaceEnabled {
		fmt.Fprintf(os.Stderr, "!!! -wantrace is %v, but RaceEnabled is %v\n", *wantRace, RaceEnabled)
		os.Exit(1)
	}

	done := make(chan bool)
	m := make(map[string]string)
	m["name"] = "world"
	go func() {
		m["name"] = "data race"
		done <- true
	}()
	fmt.Println("Hello,", m["name"])
	<-done
}

-- main.go --
package main

import "flag"

func main() {
	flag.Parse()
	Race()
}

-- racy_test.go --
package main

import "testing"

func TestRace(t *testing.T) {
	Race()
}

-- empty.s --
-- pure_bin.go --
// +build !race

// pure_bin will not build in race mode, since its sources will be excluded.
package main

func main() {}

-- coverrace.go --
package coverrace
// copied from https://hermanschaaf.com/running-the-go-race-detector-with-cover/
func add100() int {
	total := 0
	c := make(chan int, 1)
	for i := 0; i < 100; i++ {
		go func(chan int) {
			c <- 1
		}(c)
	}
	for u := 0; u < 100; u++ {
		total += <-c
	}
	return total
}

-- coverrace_test.go --
package coverrace
// copied from https://hermanschaaf.com/running-the-go-race-detector-with-cover/

import "testing"

func TestCoverRace(t *testing.T) {
	got := add100()
	if got != 100 {
		t.Errorf("got %d, want %d", got, 100)
	}
}
`,
	})
}

func Test(t *testing.T) {
	for _, test := range []struct {
		desc, cmd, target                    string
		featureFlag, wantRace, wantBuildFail bool
	}{
		{
			desc:   "cmd_auto",
			cmd:    "run",
			target: "//:racy_cmd",
		}, {
			desc:     "cmd_attr",
			cmd:      "run",
			target:   "//:racy_cmd_race_mode",
			wantRace: true,
		}, {
			desc:        "cmd_feature",
			cmd:         "run",
			target:      "//:racy_cmd",
			featureFlag: true,
			wantRace:    true,
		}, {
			desc:   "test_auto",
			cmd:    "test",
			target: "//:racy_test",
		}, {
			desc:     "test_attr",
			cmd:      "test",
			target:   "//:racy_test_race_mode",
			wantRace: true,
		}, {
			desc:        "test_feature",
			cmd:         "test",
			target:      "//:racy_test",
			featureFlag: true,
			wantRace:    true,
		}, {
			desc:        "pure_bin",
			cmd:         "build",
			target:      "//:pure_bin",
			featureFlag: true,
		}, {
			desc:          "pure_race_bin",
			cmd:           "build",
			target:        "//:pure_race_bin",
			wantBuildFail: true,
		}, {
			desc:        "cover_race",
			cmd:         "coverage",
			target:      "//:coverrace_test",
			featureFlag: true,
		},
	} {
		t.Run(test.desc, func(t *testing.T) {
			// TODO(#2518): fix coverage tests on Windows
			if test.cmd == "coverage" && runtime.GOOS == "windows" {
				t.Skip("TODO(#2518): fix and enable coverage tests on Windows")
			}
			args := []string{test.cmd}
			if test.featureFlag {
				args = append(args, "--@io_bazel_rules_go//go/config:race")
			}
			args = append(args, test.target)
			if test.cmd == "test" {
				args = append(args, fmt.Sprintf("--test_arg=-wantrace=%v", test.wantRace))
			} else if test.cmd == "run" {
				args = append(args, "--", fmt.Sprintf("-wantrace=%v", test.wantRace))
			}
			cmd := bazel_testing.BazelCmd(args...)
			stderr := &bytes.Buffer{}
			cmd.Stderr = stderr
			t.Logf("running: bazel %s", strings.Join(args, " "))
			if err := cmd.Run(); err != nil {
				var xerr *exec.ExitError
				if !errors.As(err, &xerr) {
					t.Fatalf("unexpected error: %v", err)
				}
				if xerr.ExitCode() == bazel_testing.BUILD_FAILURE {
					if !test.wantBuildFail {
						t.Fatalf("unexpected build failure: %v\nstderr:\n%s", err, stderr.Bytes())
					}
					return
				} else if xerr.ExitCode() == bazel_testing.TESTS_FAILED {
					if bytes.Contains(stderr.Bytes(), []byte("!!!")) {
						t.Fatalf("error running %s:\n%s", strings.Join(cmd.Args, " "), stderr.Bytes())
					} else if !test.wantRace {
						t.Fatalf("error running %s without race enabled\n%s", strings.Join(cmd.Args, " "), stderr.Bytes())
					}
				} else if test.wantRace {
					if !bytes.Contains(stderr.Bytes(), []byte("WARNING: DATA RACE")) {
						t.Fatalf("wanted data race; command failed with: %v\nstderr:\n%s", err, stderr.Bytes())
					}
					return
				} else {
					t.Fatalf("unexpected error: %v\nstderr:\n%s", err, stderr.Bytes())
				}
			} else if test.wantRace {
				t.Fatalf("command %s with race enabled did not fail", strings.Join(cmd.Args, " "))
			} else if test.wantBuildFail {
				t.Fatalf("target %s did not fail to build", test.target)
			}
		})
	}
}