Skip to content

Commit 5e3e42f

Browse files
committed
remove race conditions in tests
1 parent 5e08ad2 commit 5e3e42f

File tree

1 file changed

+40
-39
lines changed

1 file changed

+40
-39
lines changed

pool_test.go

Lines changed: 40 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package gopool_test
22

33
import (
44
"fmt"
5+
"sync/atomic"
56
"testing"
67
"time"
78

@@ -18,10 +19,10 @@ type typeB struct {
1819
}
1920

2021
func TestConcurrentClient(t *testing.T) {
21-
numInvocations := 0
22+
var numInvocations uint32
2223

2324
tFunc := func() error {
24-
numInvocations++
25+
atomic.AddUint32(&numInvocations, 1)
2526
return nil
2627
}
2728

@@ -39,22 +40,22 @@ func TestConcurrentClient(t *testing.T) {
3940
})
4041

4142
t.Run("3 requests done", func(t *testing.T) {
42-
assert.Equal(t, 3, numInvocations)
43+
assert.Equal(t, 3, int(numInvocations))
4344
})
4445
}
4546

4647
func TestConcurrentClientWithError(t *testing.T) {
47-
numInvocations := 0
48+
var numInvocations uint32
4849

4950
tFunc := func() error {
50-
numInvocations++
51+
atomic.AddUint32(&numInvocations, 1)
5152
return nil
5253
}
5354

5455
requests := gopool.Workers{
5556
gopool.NewTask(tFunc),
5657
gopool.NewTask(func() error {
57-
numInvocations++
58+
atomic.AddUint32(&numInvocations, 1)
5859
return fmt.Errorf("bye")
5960
}),
6061
gopool.NewTask(tFunc),
@@ -69,16 +70,16 @@ func TestConcurrentClientWithError(t *testing.T) {
6970
})
7071

7172
t.Run("3 requests done", func(t *testing.T) {
72-
assert.Equal(t, 3, numInvocations)
73+
assert.Equal(t, 3, int(numInvocations))
7374
})
7475
}
7576

7677
func TestConcurrentClientWithRetry(t *testing.T) {
77-
numInvocations := 0
78+
var numInvocations uint32
7879
numRetries := 0
7980

8081
tFunc := func() error {
81-
numInvocations++
82+
atomic.AddUint32(&numInvocations, 1)
8283
return nil
8384
}
8485

@@ -87,7 +88,7 @@ func TestConcurrentClientWithRetry(t *testing.T) {
8788
gopool.NewTask(tFunc),
8889
gopool.NewTask(tFunc),
8990
gopool.NewTask(func() error {
90-
numInvocations++
91+
atomic.AddUint32(&numInvocations, 1)
9192

9293
if numRetries < 2 {
9394
numRetries++
@@ -102,7 +103,7 @@ func TestConcurrentClientWithRetry(t *testing.T) {
102103
err := pool.Go(requests...).Wait()
103104

104105
t.Run("6 requests done", func(t *testing.T) {
105-
assert.Equal(t, 6, numInvocations)
106+
assert.Equal(t, 6, int(numInvocations))
106107
})
107108

108109
t.Run("2 retries done", func(t *testing.T) {
@@ -115,11 +116,11 @@ func TestConcurrentClientWithRetry(t *testing.T) {
115116
}
116117

117118
func TestConcurrentClientWithRetryFailure(t *testing.T) {
118-
numInvocations := 0
119+
var numInvocations uint32
119120
numRetries := 0
120121

121122
atFunc := gopool.NewTask(func() error {
122-
numInvocations++
123+
atomic.AddUint32(&numInvocations, 1)
123124
return nil
124125
})
125126

@@ -128,7 +129,7 @@ func TestConcurrentClientWithRetryFailure(t *testing.T) {
128129
atFunc,
129130
atFunc,
130131
gopool.NewTask(func() error {
131-
numInvocations++
132+
atomic.AddUint32(&numInvocations, 1)
132133
numRetries++
133134

134135
return fmt.Errorf("bye")
@@ -139,7 +140,7 @@ func TestConcurrentClientWithRetryFailure(t *testing.T) {
139140

140141
err := pool.Go(requests...).Wait()
141142
t.Run("6 requests done", func(t *testing.T) {
142-
assert.Equal(t, 6, numInvocations)
143+
assert.Equal(t, 6, int(numInvocations))
143144
})
144145

145146
t.Run("3 retries done", func(t *testing.T) {
@@ -153,17 +154,17 @@ func TestConcurrentClientWithRetryFailure(t *testing.T) {
153154
}
154155

155156
func TestConcurrentClientWithAllRetry(t *testing.T) {
156-
numInvocations := 0
157+
var numInvocations uint32
157158

158159
requests := gopool.Workers{
159160
gopool.NewTask(func() error {
160-
numInvocations++
161+
atomic.AddUint32(&numInvocations, 1)
161162
return fmt.Errorf("bye 1")
162163
}),
163164
gopool.NewTask(func() error {
164-
numInvocations++
165+
atomic.AddUint32(&numInvocations, 1)
165166

166-
if numInvocations > 2 {
167+
if atomic.LoadUint32(&numInvocations) > 2 {
167168
return nil
168169
}
169170

@@ -176,7 +177,7 @@ func TestConcurrentClientWithAllRetry(t *testing.T) {
176177

177178
err := pool.Go(requests...).Wait()
178179
t.Run("5 requests done", func(t *testing.T) {
179-
assert.Equal(t, 5, numInvocations)
180+
assert.Equal(t, 5, int(numInvocations))
180181
})
181182

182183
t.Run("Returns the first error", func(t *testing.T) {
@@ -186,12 +187,12 @@ func TestConcurrentClientWithAllRetry(t *testing.T) {
186187
}
187188

188189
func TestConcurrentClientWithTaskChannel(t *testing.T) {
189-
numInvocations := 0
190+
var numInvocations uint32
190191

191192
output := gopool.NewDrainer[typeA]() // auto-buffered channel
192193

193194
tFunc := func() error {
194-
numInvocations++
195+
atomic.AddUint32(&numInvocations, 1)
195196
output.Send(typeA{value: "hello-world!"})
196197
return nil
197198
}
@@ -213,7 +214,7 @@ func TestConcurrentClientWithTaskChannel(t *testing.T) {
213214
})
214215

215216
t.Run("1 request done", func(t *testing.T) {
216-
assert.Equal(t, 1, numInvocations)
217+
assert.Equal(t, 1, int(numInvocations))
217218
})
218219

219220
t.Run("results drained", func(t *testing.T) {
@@ -224,18 +225,18 @@ func TestConcurrentClientWithTaskChannel(t *testing.T) {
224225
}
225226

226227
func TestConcurrentClientWith2WorkersameChannel(t *testing.T) {
227-
numInvocations := 0
228+
var numInvocations uint32
228229

229230
output := gopool.NewDrainer[typeA]() // auto-buffered channel
230231

231232
tFunc := func() error {
232-
numInvocations++
233+
atomic.AddUint32(&numInvocations, 1)
233234
output.Send(typeA{value: "hello-world!"})
234235
return nil
235236
}
236237

237238
tFunc2 := func() error {
238-
numInvocations++
239+
atomic.AddUint32(&numInvocations, 1)
239240
output.Send(typeA{value: "hello-world!2"})
240241
return nil
241242
}
@@ -258,7 +259,7 @@ func TestConcurrentClientWith2WorkersameChannel(t *testing.T) {
258259
})
259260

260261
t.Run("2 request done", func(t *testing.T) {
261-
assert.Equal(t, 2, numInvocations)
262+
assert.Equal(t, 2, int(numInvocations))
262263
})
263264

264265
t.Run("results drained", func(t *testing.T) {
@@ -270,19 +271,19 @@ func TestConcurrentClientWith2WorkersameChannel(t *testing.T) {
270271
}
271272

272273
func TestConcurrentClientWith2TaskDiffTypes(t *testing.T) {
273-
numInvocations := 0
274+
var numInvocations uint32
274275

275276
output := gopool.NewDrainer[typeA]() // auto-buffered channel
276277
output2 := gopool.NewDrainer[typeB]() // auto-buffered channel
277278

278279
tFunc := func() error {
279-
numInvocations++
280+
atomic.AddUint32(&numInvocations, 1)
280281
output.Send(typeA{value: "hello-world!"})
281282
return nil
282283
}
283284

284285
tFunc2 := func() error {
285-
numInvocations++
286+
atomic.AddUint32(&numInvocations, 1)
286287
output2.Send(typeB{value: 2000.75})
287288
return nil
288289
}
@@ -306,7 +307,7 @@ func TestConcurrentClientWith2TaskDiffTypes(t *testing.T) {
306307
})
307308

308309
t.Run("2 request done", func(t *testing.T) {
309-
assert.Equal(t, 2, numInvocations)
310+
assert.Equal(t, 2, int(numInvocations))
310311
})
311312

312313
t.Run("results drained", func(t *testing.T) {
@@ -321,18 +322,18 @@ func TestConcurrentClientWith2TaskDiffTypes(t *testing.T) {
321322
}
322323

323324
func TestConcurrentClientWith2TaskDiffTypes1Output(t *testing.T) {
324-
numInvocations := 0
325+
var numInvocations uint32
325326

326327
output := gopool.NewDrainer[typeA]() // auto-buffered channel
327328

328329
tFunc := func() error {
329-
numInvocations++
330+
atomic.AddUint32(&numInvocations, 1)
330331
output.Send(typeA{value: "hello-world!"})
331332
return nil
332333
}
333334

334335
tFunc2 := func() error {
335-
numInvocations++
336+
atomic.AddUint32(&numInvocations, 1)
336337
return nil
337338
}
338339

@@ -354,7 +355,7 @@ func TestConcurrentClientWith2TaskDiffTypes1Output(t *testing.T) {
354355
})
355356

356357
t.Run("2 request done", func(t *testing.T) {
357-
assert.Equal(t, 2, numInvocations)
358+
assert.Equal(t, 2, int(numInvocations))
358359
})
359360

360361
t.Run("results drained", func(t *testing.T) {
@@ -365,20 +366,20 @@ func TestConcurrentClientWith2TaskDiffTypes1Output(t *testing.T) {
365366
}
366367

367368
func TestConcurrentClientWith2TaskDiffTypes1Output1Input(t *testing.T) {
368-
numInvocations := 0
369+
var numInvocations uint32
369370
initial := typeB{
370371
value: 2000,
371372
}
372373
output := gopool.NewDrainer[typeA]() // auto-buffered channel
373374

374375
tFunc := func() error {
375-
numInvocations++
376+
atomic.AddUint32(&numInvocations, 1)
376377
output.Send(typeA{value: "hello-world!"})
377378
return nil
378379
}
379380

380381
tFunc2 := func() error {
381-
numInvocations++
382+
atomic.AddUint32(&numInvocations, 1)
382383

383384
// update
384385
initial.value = 3500
@@ -403,7 +404,7 @@ func TestConcurrentClientWith2TaskDiffTypes1Output1Input(t *testing.T) {
403404
})
404405

405406
t.Run("2 request done", func(t *testing.T) {
406-
assert.Equal(t, 2, numInvocations)
407+
assert.Equal(t, 2, int(numInvocations))
407408
})
408409

409410
t.Run("results drained", func(t *testing.T) {

0 commit comments

Comments
 (0)