...
Text file
src/cmd/go/testdata/script/test_chatty_parallel_success.txt
1# Run parallel chatty tests. Assert on CONT or NAME lines. This test makes sure that
2# multiple parallel outputs have the appropriate test name lines between them.
3go test -parallel 3 chatty_parallel_test.go -v
4stdout -count=2 '^=== (CONT|NAME) TestChattyParallel/sub-0\n chatty_parallel_test.go:32: this is sub-0$'
5stdout -count=2 '^=== (CONT|NAME) TestChattyParallel/sub-1\n chatty_parallel_test.go:32: this is sub-1$'
6stdout -count=2 '^=== (CONT|NAME) TestChattyParallel/sub-2\n chatty_parallel_test.go:32: this is sub-2$'
7
8# Run parallel chatty tests with -json.
9# Assert test2json has properly attributed output.
10go test -json -parallel 3 chatty_parallel_test.go -v
11stdout -count=2 '"Test":"TestChattyParallel/sub-0","Output":" chatty_parallel_test.go:32: this is sub-0\\n"'
12stdout -count=2 '"Test":"TestChattyParallel/sub-1","Output":" chatty_parallel_test.go:32: this is sub-1\\n"'
13stdout -count=2 '"Test":"TestChattyParallel/sub-2","Output":" chatty_parallel_test.go:32: this is sub-2\\n"'
14
15-- chatty_parallel_test.go --
16package chatty_parallel_test
17
18import (
19 "testing"
20 "fmt"
21 "flag"
22)
23
24// This test ensures the order of CONT lines in parallel chatty tests.
25func TestChattyParallel(t *testing.T) {
26 t.Parallel()
27
28 // The number of concurrent tests running. This is closely tied to the
29 // -parallel test flag, so we grab it from the flag rather than setting it
30 // to some constant.
31 parallel := flag.Lookup("test.parallel").Value.(flag.Getter).Get().(int)
32
33 // ready is a synchronization mechanism that causes subtests to execute
34 // round robin.
35 ready := make([]chan bool, parallel)
36 for i := range ready {
37 ready[i] = make(chan bool, 1)
38 }
39 ready[0] <- true
40
41 for i := range ready {
42 i := i
43 t.Run(fmt.Sprintf("sub-%d", i), func(t *testing.T) {
44 t.Parallel()
45 for j := 0; j < 2; j++ {
46 <-ready[i]
47 t.Logf("this is sub-%d", i)
48 ready[(i+1)%len(ready)] <- true
49 }
50 })
51 }
52}
View as plain text