| 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. |
| 3 |
go test -parallel 3 chatty_parallel_test.go -v |
| 4 |
stdout -count=2 '^=== (CONT|NAME) TestChattyParallel/sub-0\n chatty_parallel_test.go:32: this is sub-0$' |
| 5 |
stdout -count=2 '^=== (CONT|NAME) TestChattyParallel/sub-1\n chatty_parallel_test.go:32: this is sub-1$' |
| 6 |
stdout -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. |
| 10 |
go test -json -parallel 3 chatty_parallel_test.go -v |
| 11 |
stdout -count=2 '"Test":"TestChattyParallel/sub-0","Output":" chatty_parallel_test.go:32: this is sub-0\\n"' |
| 12 |
stdout -count=2 '"Test":"TestChattyParallel/sub-1","Output":" chatty_parallel_test.go:32: this is sub-1\\n"' |
| 13 |
stdout -count=2 '"Test":"TestChattyParallel/sub-2","Output":" chatty_parallel_test.go:32: this is sub-2\\n"' |
| 14 |
|
| 15 |
-- chatty_parallel_test.go -- |
| 16 |
package chatty_parallel_test |
| 17 |
|
| 18 |
import ( |
| 19 |
"testing" |
| 20 |
"fmt" |
| 21 |
"flag" |
| 22 |
) |
| 23 |
|
| 24 |
// This test ensures the order of CONT lines in parallel chatty tests. |
| 25 |
func 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 |
} |