-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbatch4_test.go
233 lines (171 loc) · 3.68 KB
/
batch4_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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package batch_test
import (
"context"
"flag"
"runtime"
"sync"
"testing"
"nikand.dev/go/batch"
)
var jobs = flag.Int("jobs", 5, "parallel jobs in tests")
func TestCoordinatorSmoke(tb *testing.T) {
const N = 3
ctx := context.Background()
var sum, total int
bc := batch.Coordinator[int]{
CommitFunc: func(ctx context.Context) (int, error) {
tb.Logf("commit %v", sum)
total += sum
return sum, nil
},
}
var wg sync.WaitGroup
for j := 0; j < *jobs; j++ {
j := j
wg.Add(1)
go func() {
defer wg.Done()
for i := 0; i < N; i++ {
i := i
func() {
bc.Queue().In()
runtime.Gosched()
idx := bc.Enter(true)
defer bc.Exit()
tb.Logf("worker %2d iter %2d enters %2d", j, i, idx)
if idx == 0 {
sum = 0
}
sum += i
res, err := bc.Commit(ctx)
if err != nil {
tb.Errorf("commit: %v", err)
}
tb.Logf("worker %2d iter %2d res %2d %v", j, i, res, err)
}()
}
}()
}
wg.Wait()
if exp := *jobs * N * (N - 1) / 2; exp != total {
tb.Errorf("expected total %v got %v", exp, total)
}
}
func TestCoordinatorAllCases(tb *testing.T) {
ctx := context.Background()
var sum int
var commitPanics bool
bc := batch.Coordinator[int]{
CommitFunc: func(ctx context.Context) (int, error) {
if commitPanics {
tb.Logf("commit PANICS")
panic("commit PaNiC")
}
runtime.Gosched()
tb.Logf("commit %v", sum)
return sum, nil
},
}
var wg sync.WaitGroup
for j := 0; j < *jobs; j++ {
j := j
wg.Add(1)
go func() {
defer wg.Done()
for i := 0; i <= 8; i++ {
i := i
func() {
if j == 1 && (i == 3 || i == 7) {
defer func() {
_ = recover()
}()
}
bc.Queue().In()
runtime.Gosched()
if j == 1 && i == 6 {
bc.Queue().Out()
bc.Notify()
return
}
idx := bc.Enter(j != 0)
if idx < 0 {
tb.Logf("worker %2d iter %2d didn't enter %2d", j, i, idx)
return
}
defer bc.Exit()
runtime.Gosched()
if idx == 0 {
tb.Logf(" * * * ")
sum = 0
}
tb.Logf("worker %2d iter %2d enters %2d", j, i, idx)
if j == 1 && i == 1 {
tb.Logf("worker %2d iter %2d LEFT", j, i)
return
}
sum += i
if j == 1 && i == 2 {
_, err := bc.Cancel(ctx, nil)
tb.Logf("worker %2d iter %2d CANCEL %v", j, i, err)
return
}
if j == 1 && i == 3 {
tb.Logf("worker %2d iter %2d PANICS", j, i)
panic("pAnIc")
}
if j == 1 {
commitPanics = i == 4
}
if j == 1 && i == 5 {
bc.Trigger()
}
res, err := bc.Commit(ctx)
if err != nil {
_ = err
}
if pe, ok := batch.AsPanicError(err); ok {
tb.Logf("worker %2d iter %2d panic %v", j, i, pe)
} else {
tb.Logf("worker %2d iter %2d res %2d %v", j, i, res, err)
}
if j == 1 && i == 7 {
tb.Logf("worker %2d iter %2d PANICS after commit", j, i)
panic("panIC")
}
}()
}
}()
}
wg.Wait()
}
func BenchmarkCoordinator(tb *testing.B) {
tb.ReportAllocs()
ctx := context.Background()
var sum int
var bc batch.Coordinator[int]
bc.Init(func(ctx context.Context) (int, error) {
return sum, nil
})
tb.RunParallel(func(tb *testing.PB) {
for tb.Next() {
func() {
bc.Queue().In()
// runtime.Gosched()
idx := bc.Enter(true)
defer bc.Exit()
// tb.Logf("worker %2d iter %2d enters %2d", j, i, idx)
if idx == 0 {
sum = 0
}
sum += 1
res, err := bc.Commit(ctx)
if err != nil {
// tb.Errorf("commit: %v", err)
_ = err
}
// tb.Logf("worker %2d iter %2d res %2d %v", j, i, res, err)
_ = res
}()
}
})
}