Skip to content
This repository was archived by the owner on Jul 18, 2025. It is now read-only.

Commit 5deb197

Browse files
committed
Fix utils race and error while in -race mode
- Using a safe map instead of a simple map for `TestInParallel` and `TestInParallelError` to be sure we don't have a data race while running the tests. - Putting these two (`TestInParallel` and `TestInParallelError`) into a separate test file with build tag `!race`. `InParallel` relies on `sync.Pool` and `sync.Pool` is no-op in `-race` mode ; this means `TestInParallel` is always OK, `TestInParallelError` is always KO when race detector is on. Signed-off-by: Vincent Demeester <[email protected]>
1 parent 09595c4 commit 5deb197

File tree

2 files changed

+84
-52
lines changed

2 files changed

+84
-52
lines changed

utils/util_inparallel_test.go

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
// +build !race
2+
3+
package utils
4+
5+
import (
6+
"fmt"
7+
"sync"
8+
"testing"
9+
)
10+
11+
type safeMap struct {
12+
mu sync.RWMutex
13+
m map[int]bool
14+
}
15+
16+
func (s *safeMap) Add(index int, ok bool) {
17+
s.mu.Lock()
18+
defer s.mu.Unlock()
19+
s.m[index] = ok
20+
}
21+
22+
func (s *safeMap) Read() map[int]bool {
23+
s.mu.RLock()
24+
defer s.mu.RUnlock()
25+
return s.m
26+
}
27+
28+
func TestInParallel(t *testing.T) {
29+
size := 5
30+
booleanMap := safeMap{
31+
m: make(map[int]bool, size+1),
32+
}
33+
tasks := InParallel{}
34+
for i := 0; i < size; i++ {
35+
task := func(index int) func() error {
36+
return func() error {
37+
booleanMap.Add(index, true)
38+
return nil
39+
}
40+
}(i)
41+
tasks.Add(task)
42+
}
43+
err := tasks.Wait()
44+
if err != nil {
45+
t.Fatal(err)
46+
}
47+
// Make sure every value is true
48+
for _, value := range booleanMap.Read() {
49+
if !value {
50+
t.Fatalf("booleanMap expected to contain only true values, got at least one false")
51+
}
52+
}
53+
}
54+
55+
func TestInParallelError(t *testing.T) {
56+
size := 5
57+
booleanMap := safeMap{
58+
m: make(map[int]bool, size+1),
59+
}
60+
tasks := InParallel{}
61+
for i := 0; i < size; i++ {
62+
task := func(index int) func() error {
63+
return func() error {
64+
booleanMap.Add(index, true)
65+
t.Log("index", index)
66+
if index%2 == 0 {
67+
t.Log("return an error for", index)
68+
return fmt.Errorf("Error with %v", index)
69+
}
70+
return nil
71+
}
72+
}(i)
73+
tasks.Add(task)
74+
}
75+
err := tasks.Wait()
76+
if err == nil {
77+
t.Fatalf("Expected an error on Wait, got nothing.")
78+
}
79+
for key, value := range booleanMap.Read() {
80+
if key%2 != 0 && !value {
81+
t.Fatalf("booleanMap expected to contain true values on odd number, got %v", booleanMap)
82+
}
83+
}
84+
}

utils/util_test.go

Lines changed: 0 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -17,58 +17,6 @@ type jsonto struct {
1717
Elt3 int
1818
}
1919

20-
func TestInParallel(t *testing.T) {
21-
size := 5
22-
booleanMap := make(map[int]bool, size+1)
23-
tasks := InParallel{}
24-
for i := 0; i < size; i++ {
25-
task := func(index int) func() error {
26-
return func() error {
27-
booleanMap[index] = true
28-
return nil
29-
}
30-
}(i)
31-
tasks.Add(task)
32-
}
33-
err := tasks.Wait()
34-
if err != nil {
35-
t.Fatal(err)
36-
}
37-
// Make sure every value is true
38-
for _, value := range booleanMap {
39-
if !value {
40-
t.Fatalf("booleanMap expected to contain only true values, got at least one false")
41-
}
42-
}
43-
}
44-
45-
func TestInParallelError(t *testing.T) {
46-
size := 5
47-
booleanMap := make(map[int]bool, size+1)
48-
tasks := InParallel{}
49-
for i := 0; i < size; i++ {
50-
task := func(index int) func() error {
51-
return func() error {
52-
booleanMap[index] = true
53-
if index%2 == 0 {
54-
return fmt.Errorf("Error with %v", index)
55-
}
56-
return nil
57-
}
58-
}(i)
59-
tasks.Add(task)
60-
}
61-
err := tasks.Wait()
62-
if err == nil {
63-
t.Fatalf("Expected an error on Wait, got nothing.")
64-
}
65-
for key, value := range booleanMap {
66-
if key%2 != 0 && !value {
67-
t.Fatalf("booleanMap expected to contain true values on odd number, got %v", booleanMap)
68-
}
69-
}
70-
}
71-
7220
func TestConvertByJSON(t *testing.T) {
7321
valids := []struct {
7422
src jsonfrom

0 commit comments

Comments
 (0)