...
1
2
3
4
5
6
7 package race_test
8
9 import (
10 "fmt"
11 "reflect"
12 "runtime"
13 "strings"
14 "testing"
15 )
16
17 func TestRandomScheduling(t *testing.T) {
18
19
20 defer runtime.GOMAXPROCS(runtime.GOMAXPROCS(1))
21 const N = 10
22 out := make([][]int, N)
23 for i := 0; i < N; i++ {
24 c := make(chan int, N)
25 for j := 0; j < N; j++ {
26 go func(j int) {
27 c <- j
28 }(j)
29 }
30 row := make([]int, N)
31 for j := 0; j < N; j++ {
32 row[j] = <-c
33 }
34 out[i] = row
35 }
36
37 for i := 0; i < N; i++ {
38 if !reflect.DeepEqual(out[0], out[i]) {
39 return
40 }
41 }
42
43 var buf strings.Builder
44 for i := 0; i < N; i++ {
45 fmt.Fprintf(&buf, "%v\n", out[i])
46 }
47 t.Fatalf("consistent goroutine execution order:\n%v", buf.String())
48 }
49
View as plain text