race_test.go 1.1 KB

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