| 76 | } |
| 77 | |
| 78 | func TestNew_Race(t *testing.T) { |
| 79 | p := pool.New(func() *pooledValue[int] { |
| 80 | return &pooledValue[int]{ |
| 81 | value: -1, |
| 82 | } |
| 83 | }) |
| 84 | |
| 85 | var wg sync.WaitGroup |
| 86 | defer wg.Wait() |
| 87 | |
| 88 | // Run a number of goroutines that read and write pool object fields to |
| 89 | // tease out races. |
| 90 | for i := 0; i < 1_000; i++ { |
| 91 | i := i |
| 92 | |
| 93 | wg.Add(1) |
| 94 | go func() { |
| 95 | defer wg.Done() |
| 96 | |
| 97 | x := p.Get() |
| 98 | defer p.Put(x) |
| 99 | |
| 100 | // Must both read and write the field. |
| 101 | if n := x.value; n >= -1 { |
| 102 | x.value = i |
| 103 | } |
| 104 | }() |
| 105 | } |
| 106 | } |