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