-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathcsvutil_race_test.go
120 lines (107 loc) · 1.97 KB
/
csvutil_race_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
//go:build race
// +build race
package csvutil
import (
"bytes"
"encoding/csv"
"io"
"sync"
"testing"
)
func TestCacheDataRaces(t *testing.T) {
const routines = 50
const rows = 1000
v := TypeF{
Int: 1,
Pint: ptr(2),
Int8: 3,
Pint8: ptr[int8](4),
Int16: 5,
Pint16: ptr[int16](6),
Int32: 7,
Pint32: ptr[int32](8),
Int64: 9,
Pint64: ptr[int64](10),
UInt: 11,
Puint: ptr[uint](12),
Uint8: 13,
Puint8: ptr[uint8](14),
Uint16: 15,
Puint16: ptr[uint16](16),
Uint32: 17,
Puint32: ptr[uint32](18),
Uint64: 19,
Puint64: ptr[uint64](20),
Float32: 21,
Pfloat32: ptr[float32](22),
Float64: 23,
Pfloat64: ptr[float64](24),
String: "25",
PString: ptr("26"),
Bool: true,
Pbool: ptr(true),
V: pptr(100),
Pv: ptr[any](pptr(200)),
Binary: Binary,
PBinary: &Binary,
}
t.Run("encoding", func(t *testing.T) {
var wg sync.WaitGroup
for i := 0; i < routines; i++ {
tag := "csv"
if i%2 == 0 {
tag = "custom"
}
wg.Add(1)
go func() {
defer wg.Done()
var buf bytes.Buffer
w := csv.NewWriter(&buf)
enc := NewEncoder(w)
enc.Tag = tag
for i := 0; i < rows; i++ {
if err := enc.Encode(v); err != nil {
panic(err)
}
}
w.Flush()
}()
}
wg.Wait()
})
t.Run("decoding", func(t *testing.T) {
vs := make([]*TypeF, 0, rows)
for i := 0; i < rows; i++ {
vs = append(vs, &v)
}
data, err := Marshal(vs)
if err != nil {
t.Fatal(err)
}
var wg sync.WaitGroup
for i := 0; i < routines; i++ {
tag := "csv"
if i%2 == 0 {
tag = "custom"
}
wg.Add(1)
go func() {
defer wg.Done()
dec, err := NewDecoder(csv.NewReader(bytes.NewReader(data)))
if err != nil {
t.Fatal(err)
}
dec.Tag = tag
for {
var val TypeF
if err := dec.Decode(&val); err == io.EOF {
break
} else if err != nil {
panic(err)
}
}
}()
}
wg.Wait()
})
}