...

Text file src/cmd/go/testdata/script/test_race.txt

Documentation: cmd/go/testdata/script

     1[short] skip
     2[!race] skip
     3
     4go test testrace
     5
     6! go test -race testrace
     7stdout 'FAIL: TestRace'
     8! stdout 'PASS'
     9! stderr 'PASS'
    10
    11! go test -race testrace -run XXX -bench .
    12stdout 'FAIL: BenchmarkRace'
    13! stdout 'PASS'
    14! stderr 'PASS'
    15
    16-- go.mod --
    17module testrace
    18
    19go 1.16
    20-- race_test.go --
    21package testrace
    22
    23import "testing"
    24
    25func 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
    39func 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}

View as plain text