...

Source file src/runtime/race/sched_test.go

Documentation: runtime/race

     1  // Copyright 2015 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  //go:build race
     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  	// Scheduler is most consistent with GOMAXPROCS=1.
    19  	// Use that to make the test most likely to fail.
    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 // found a different order
    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