Source file
src/sync/counter_test.go
1
2
3
4
5 package sync_test
6
7 import (
8 "math"
9 "runtime"
10 "sync"
11 "testing"
12 )
13
14 func equal[T ~int64 | ~uint64](a, b T) bool {
15 return a == b
16 }
17
18 func testCounterAdd[T ~int64 | ~uint64 | ~float64](t *testing.T, checkVal func(got, want T) bool) {
19 wantCount := int64(100000)
20 if testing.Short() {
21 wantCount = 1000
22 }
23
24 runAdders := func(want int64, gCount int) {
25 var c sync.Counter[T]
26 var wg sync.WaitGroup
27 for range gCount {
28 wg.Go(func() {
29 for range want {
30 c.Add(1)
31 }
32 })
33 }
34 wg.Wait()
35 wantTotal := want * int64(gCount)
36 if got := c.Value(); !checkVal(got, T(wantTotal)) {
37 t.Errorf("Add want %v, got %v", wantTotal, got)
38 }
39 }
40
41 t.Run("single goroutine add", func(t *testing.T) {
42 runAdders(wantCount, 1)
43 })
44
45 t.Run("GOMAXPROCS goroutines adding", func(t *testing.T) {
46 runAdders(wantCount, runtime.GOMAXPROCS(0))
47 })
48
49 t.Run("GOMAXPROCS multiple goroutines adding", func(t *testing.T) {
50 runAdders(wantCount, runtime.GOMAXPROCS(0)*4)
51 })
52 }
53
54 func testCountersAdd[T ~int64 | ~uint64 | ~float64](t *testing.T, checkVal func(got, want T) bool) {
55 wantCount := int64(10000)
56 outer := 100
57 if testing.Short() {
58 wantCount = 1000
59 outer = 10
60 }
61
62 runAdders := func(outerWG *sync.WaitGroup, want int64, gCount int) {
63 defer outerWG.Done()
64
65 var c sync.Counter[T]
66 var wg sync.WaitGroup
67 for range gCount {
68 wg.Go(func() {
69 for range want {
70 c.Add(1)
71 }
72 })
73 }
74 wg.Wait()
75 wantTotal := want * int64(gCount)
76 if got := c.Value(); !checkVal(got, T(wantTotal)) {
77 t.Errorf("Add want %v, got %v", wantTotal, got)
78 }
79 }
80
81 t.Run("single goroutine add", func(t *testing.T) {
82 var wg sync.WaitGroup
83 for range outer {
84 wg.Add(1)
85 go runAdders(&wg, wantCount, 1)
86 }
87 wg.Wait()
88 })
89
90 t.Run("GOMAXPROCS goroutines adding", func(t *testing.T) {
91 var wg sync.WaitGroup
92 for range outer {
93 wg.Add(1)
94 go runAdders(&wg, wantCount, runtime.GOMAXPROCS(0))
95 }
96 wg.Wait()
97 })
98
99 t.Run("GOMAXPROCS multiple goroutines adding", func(t *testing.T) {
100 var wg sync.WaitGroup
101 for range outer {
102 wg.Add(1)
103 go runAdders(&wg, wantCount, runtime.GOMAXPROCS(0)*4)
104 }
105 wg.Wait()
106 })
107 }
108
109 func TestCounterAddInt64(t *testing.T) {
110 testCounterAdd(t, equal[int64])
111 }
112
113 func TestCountersAddInt64(t *testing.T) {
114 testCountersAdd(t, equal[int64])
115 }
116
117 func TestCounterAddUint64(t *testing.T) {
118 testCounterAdd(t, equal[uint64])
119 }
120
121 func TestCountersAddUint64(t *testing.T) {
122 testCountersAdd(t, equal[uint64])
123 }
124
125 func TestCounterUint64WrapAround(t *testing.T) {
126 var c sync.Counter[uint64]
127 c.Add(math.MaxUint64)
128 c.Add(5)
129 if got, want := c.Value(), uint64(4); got != want {
130 t.Errorf("Value after wrap around: want %d, got %d", want, got)
131 }
132 }
133
134 func TestCounterUint64WrapAroundParallel(t *testing.T) {
135 var c sync.Counter[uint64]
136 c.Add(math.MaxUint64 - 100)
137
138 var wg sync.WaitGroup
139 for range 105 {
140 wg.Add(1)
141 go func() {
142 defer wg.Done()
143 c.Add(1)
144 }()
145 }
146 wg.Wait()
147
148 if got, want := c.Value(), uint64(4); got != want {
149 t.Errorf("Value after parallel wrap around: want %d, got %d", want, got)
150 }
151 }
152
153 func TestCounterAddFloat64(t *testing.T) {
154 testCounterAdd(t, floatEqual)
155 }
156
157 func TestCountersAddFloat64(t *testing.T) {
158 testCountersAdd(t, floatEqual)
159 }
160
161 func TestCounterZeroValue(t *testing.T) {
162 var ci sync.Counter[int64]
163 if got := ci.Value(); got != 0 {
164 t.Errorf("zero Counter[int64].Value() = %d, want 0", got)
165 }
166 var cu sync.Counter[uint64]
167 if got := cu.Value(); got != 0 {
168 t.Errorf("zero Counter[uint64].Value() = %d, want 0", got)
169 }
170 var cf sync.Counter[float64]
171 if got := cf.Value(); got != 0 {
172 t.Errorf("zero Counter[float64].Value() = %v, want 0", got)
173 }
174 }
175
176 func TestCounterNamedTypes(t *testing.T) {
177 type myInt64 int64
178 type myUint64 uint64
179 type myFloat64 float64
180
181 testCounterAdd(t, equal[myInt64])
182 testCounterAdd(t, equal[myUint64])
183 testCounterAdd(t, func(got, want myFloat64) bool {
184 return floatEqual(float64(got), float64(want))
185 })
186 }
187
188 func TestCounterInt64NegativeAndWrapAround(t *testing.T) {
189 t.Run("negative deltas", func(t *testing.T) {
190 var c sync.Counter[int64]
191 cpus := int64(runtime.GOMAXPROCS(0))
192 const iters = 1000
193 var wg sync.WaitGroup
194 for range cpus {
195 wg.Add(1)
196 go func() {
197 defer wg.Done()
198 for range iters {
199 c.Add(2)
200 c.Add(-5)
201 }
202 }()
203 }
204 wg.Wait()
205 want := cpus * iters * (-3)
206 if got := c.Value(); got != want {
207 t.Errorf("Value() = %d, want %d", got, want)
208 }
209 })
210
211 t.Run("wrap around MaxInt64 and MinInt64", func(t *testing.T) {
212 var c sync.Counter[int64]
213 c.Add(math.MaxInt64)
214 c.Add(1)
215 if got, want := c.Value(), int64(math.MinInt64); got != want {
216 t.Errorf("after MaxInt64 + 1: got %d, want %d", got, want)
217 }
218 c.Add(-1)
219 if got, want := c.Value(), int64(math.MaxInt64); got != want {
220 t.Errorf("after MinInt64 - 1: got %d, want %d", got, want)
221 }
222 })
223 }
224
225 func TestCounterFloat64FractionalAndNegative(t *testing.T) {
226 var c sync.Counter[float64]
227 cpus := runtime.GOMAXPROCS(0)
228 const iters = 1000
229 var wg sync.WaitGroup
230 for range cpus {
231 wg.Add(1)
232 go func() {
233 defer wg.Done()
234 for range iters {
235 c.Add(0.1)
236 c.Add(-0.25)
237 }
238 }()
239 }
240 wg.Wait()
241
242 want := float64(cpus*iters) * (0.1 - 0.25)
243 if got := c.Value(); !floatEqual(got, want) {
244 t.Errorf("Value() = %v, want %v", got, want)
245 }
246 }
247
248 func floatEqual(a, b float64) bool {
249 if a == b {
250 return true
251 }
252 const epsilon = 1e-9
253 diff := math.Abs(a - b)
254 if a == 0 || b == 0 || diff < math.SmallestNonzeroFloat64 {
255 return diff <= epsilon
256 }
257 return diff <= epsilon*math.Max(math.Abs(a), math.Abs(b))
258 }
259
260
261
262
263
264 func TestCounterValueConcurrentWithAdd(t *testing.T) {
265 if runtime.GOARCH == "wasm" {
266 t.Skip("skipping on wasm: the spinning reader is never preempted")
267 }
268 const phase = 1000
269 procs := runtime.GOMAXPROCS(0)
270 var c sync.Counter[int64]
271
272
273 addConcurrently := func(wg *sync.WaitGroup, total int) {
274 for g := range procs {
275 n := total / procs
276 if g < total%procs {
277 n++
278 }
279 wg.Go(func() {
280 for range n {
281 c.Add(1)
282 }
283 })
284 }
285 }
286
287
288 var wg sync.WaitGroup
289 addConcurrently(&wg, phase)
290 wg.Wait()
291
292
293 var adders sync.WaitGroup
294 addConcurrently(&adders, phase)
295 done := make(chan struct{})
296 var reader sync.WaitGroup
297 reader.Go(func() {
298 var prev int64
299 for {
300 got := c.Value()
301 if got < phase || got > 2*phase {
302 t.Errorf("concurrent Value() = %d, want in [%d, %d]", got, phase, 2*phase)
303 return
304 }
305 if got < prev {
306 t.Errorf("concurrent Value() decreased: %d after %d", got, prev)
307 return
308 }
309 prev = got
310 select {
311 case <-done:
312 return
313 default:
314 }
315 }
316 })
317 adders.Wait()
318 close(done)
319 reader.Wait()
320
321 if got := c.Value(); got != 2*phase {
322 t.Errorf("final Value() = %d, want %d", got, 2*phase)
323 }
324 }
325
View as plain text