| 1 |
[short] skip |
| 2 |
[!race] skip |
| 3 |
|
| 4 |
# Make sure test is functional. |
| 5 |
go test testrace |
| 6 |
|
| 7 |
# Now, check that -race -covermode=set is not allowed. |
| 8 |
! go test -race -covermode=set testrace |
| 9 |
stderr '-covermode must be "atomic", not "set", when -race is enabled' |
| 10 |
! stdout PASS |
| 11 |
! stderr PASS |
| 12 |
|
| 13 |
-- go.mod -- |
| 14 |
module testrace |
| 15 |
|
| 16 |
go 1.16 |
| 17 |
-- race_test.go -- |
| 18 |
package testrace |
| 19 |
|
| 20 |
import "testing" |
| 21 |
|
| 22 |
func TestRace(t *testing.T) { |
| 23 |
for i := 0; i < 10; i++ { |
| 24 |
c := make(chan int) |
| 25 |
x := 1 |
| 26 |
go func() { |
| 27 |
x = 2 |
| 28 |
c <- 1 |
| 29 |
}() |
| 30 |
x = 3 |
| 31 |
<-c |
| 32 |
_ = x |
| 33 |
} |
| 34 |
} |
| 35 |
|
| 36 |
func BenchmarkRace(b *testing.B) { |
| 37 |
for i := 0; i < b.N; i++ { |
| 38 |
c := make(chan int) |
| 39 |
x := 1 |
| 40 |
go func() { |
| 41 |
x = 2 |
| 42 |
c <- 1 |
| 43 |
}() |
| 44 |
x = 3 |
| 45 |
<-c |
| 46 |
_ = x |
| 47 |
} |
| 48 |
} |