github.com/twelsh-aw/go/src@v0.0.0-20230516233729-a56fe86a7c81/math/rand/race_test.go (about)

     1  // Copyright 2016 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  package rand_test
     6  
     7  import (
     8  	. "math/rand"
     9  	"sync"
    10  	"testing"
    11  )
    12  
    13  // TestConcurrent exercises the rand API concurrently, triggering situations
    14  // where the race detector is likely to detect issues.
    15  func TestConcurrent(t *testing.T) {
    16  	const (
    17  		numRoutines = 10
    18  		numCycles   = 10
    19  	)
    20  	var wg sync.WaitGroup
    21  	defer wg.Wait()
    22  	wg.Add(numRoutines)
    23  	for i := 0; i < numRoutines; i++ {
    24  		go func(i int) {
    25  			defer wg.Done()
    26  			buf := make([]byte, 997)
    27  			for j := 0; j < numCycles; j++ {
    28  				var seed int64
    29  				seed += int64(ExpFloat64())
    30  				seed += int64(Float32())
    31  				seed += int64(Float64())
    32  				seed += int64(Intn(Int()))
    33  				seed += int64(Int31n(Int31()))
    34  				seed += int64(Int63n(Int63()))
    35  				seed += int64(NormFloat64())
    36  				seed += int64(Uint32())
    37  				seed += int64(Uint64())
    38  				for _, p := range Perm(10) {
    39  					seed += int64(p)
    40  				}
    41  				Read(buf)
    42  				for _, b := range buf {
    43  					seed += int64(b)
    44  				}
    45  				Seed(int64(i*j) * seed)
    46  			}
    47  		}(i)
    48  	}
    49  }