| 1 |
[short] skip |
| 2 |
[!race] skip |
| 3 |
|
| 4 |
go test testrace |
| 5 |
|
| 6 |
! go test -race testrace |
| 7 |
stdout 'FAIL: TestRace' |
| 8 |
! stdout 'PASS' |
| 9 |
! stderr 'PASS' |
| 10 |
|
| 11 |
! go test -race testrace -run XXX -bench . |
| 12 |
stdout 'FAIL: BenchmarkRace' |
| 13 |
! stdout 'PASS' |
| 14 |
! stderr 'PASS' |
| 15 |
|
| 16 |
-- go.mod -- |
| 17 |
module testrace |
| 18 |
|
| 19 |
go 1.16 |
| 20 |
-- race_test.go -- |
| 21 |
package testrace |
| 22 |
|
| 23 |
import "testing" |
| 24 |
|
| 25 |
func TestRace(t *testing.T) { |
| 26 |
for i := 0; i < 10; i++ { |
| 27 |
c := make(chan int) |
| 28 |
x := 1 |
| 29 |
go func() { |
| 30 |
x = 2 |
| 31 |
c <- 1 |
| 32 |
}() |
| 33 |
x = 3 |
| 34 |
<-c |
| 35 |
_ = x |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
func BenchmarkRace(b *testing.B) { |
| 40 |
for i := 0; i < b.N; i++ { |
| 41 |
c := make(chan int) |
| 42 |
x := 1 |
| 43 |
go func() { |
| 44 |
x = 2 |
| 45 |
c <- 1 |
| 46 |
}() |
| 47 |
x = 3 |
| 48 |
<-c |
| 49 |
_ = x |
| 50 |
} |
| 51 |
} |