...
1# Run parallel chatty tests.
2# Check that multiple parallel outputs continue running.
3! go test -parallel 3 chatty_parallel_test.go -v
4
5stdout -count=1 '^=== CONT TestChattyParallel/sub-0'
6stdout -count=1 '^=== CONT TestChattyParallel/sub-1'
7stdout -count=1 '^=== CONT TestChattyParallel/sub-2'
8
9stdout -count=1 '^=== (CONT|NAME) TestChattyParallel/sub-0\n chatty_parallel_test.go:38: error from sub-0$'
10stdout -count=1 '^=== (CONT|NAME) TestChattyParallel/sub-1\n chatty_parallel_test.go:38: error from sub-1$'
11stdout -count=1 '^=== (CONT|NAME) TestChattyParallel/sub-2\n chatty_parallel_test.go:38: error from sub-2$'
12
13# Run parallel chatty tests with -json.
14# Check that each output is attributed to the right test.
15! go test -json -parallel 3 chatty_parallel_test.go -v
16stdout -count=1 '"Test":"TestChattyParallel/sub-0","Output":" chatty_parallel_test.go:38: error from sub-0\\n"'
17stdout -count=1 '"Test":"TestChattyParallel/sub-1","Output":" chatty_parallel_test.go:38: error from sub-1\\n"'
18stdout -count=1 '"Test":"TestChattyParallel/sub-2","Output":" chatty_parallel_test.go:38: error from sub-2\\n"'
19
20-- chatty_parallel_test.go --
21package chatty_parallel_test
22
23import (
24 "testing"
25 "fmt"
26 "flag"
27)
28
29// This test ensures the order of CONT lines in parallel chatty tests.
30func TestChattyParallel(t *testing.T) {
31 t.Parallel()
32
33 // The number of concurrent tests running. This is closely tied to the
34 // -parallel test flag, so we grab it from the flag rather than setting it
35 // to some constant.
36 parallel := flag.Lookup("test.parallel").Value.(flag.Getter).Get().(int)
37
38 // ready is a synchronization mechanism that causes subtests to execute
39 // round robin.
40 ready := make([]chan bool, parallel)
41 for i := range ready {
42 ready[i] = make(chan bool, 1)
43 }
44 ready[0] <- true
45
46 for i := range ready {
47 i := i
48 t.Run(fmt.Sprintf("sub-%d", i), func(t *testing.T) {
49 t.Parallel()
50
51 // Some basic log output to precede the failures.
52 <-ready[i]
53 t.Logf("this is sub-%d", i)
54 ready[(i+1)%len(ready)] <- true
55
56 // The actual failure messages we care about.
57 <-ready[i]
58 t.Errorf("error from sub-%d", i)
59 ready[(i+1)%len(ready)] <- true
60 })
61 }
62}
View as plain text