-
Notifications
You must be signed in to change notification settings - Fork 0
/
mvr_test.go
314 lines (237 loc) · 5.95 KB
/
mvr_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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
// +build !windows
/*
Copyright (c) 2018,2019 Maxim Konakov
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software without
specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package mvr
import (
"bytes"
"context"
"errors"
"log"
"math/rand"
"runtime"
"strconv"
"sync"
"sync/atomic"
"testing"
"time"
)
func TestMain(m *testing.M) {
rand.Seed(time.Now().UnixNano())
log.SetFlags(0)
log.SetOutput(&loggerOutput) // to capture logger output
Run(m.Run)
}
var loggerOutput bytes.Buffer
func TestLogger(t *testing.T) {
// write some log
log.Println("zzz")
log.Println("aaa")
log.Println("bbb")
// wait a moment
time.Sleep(10 * time.Millisecond)
// check
if res := loggerOutput.String(); res != "zzz\naaa\nbbb\n" {
t.Errorf("Unexpected string: %q", res)
return
}
}
func TestGo(t *testing.T) {
const N = 100
var res int32
var wg sync.WaitGroup
wg.Add(N)
for i := 0; i < N; i++ {
Go(func() {
defer wg.Done()
time.Sleep(time.Duration(rand.Intn(100)) * time.Millisecond)
atomic.AddInt32(&res, 1)
})
}
wg.Wait()
if atomic.LoadInt32(&res) != N {
t.Error("Invalid number of iterations:", res, "instead of", N)
return
}
}
func TestAsync(t *testing.T) {
const msg = "ERROR"
var res int32
err := <-Async(func() error {
time.Sleep(20 * time.Millisecond)
atomic.AddInt32(&res, 1)
return errors.New(msg)
})
if err == nil {
t.Error("Missing error message")
return
}
if err.Error() != msg {
t.Error("Unexpected error message:", err.Error())
return
}
if atomic.LoadInt32(&res) != 1 {
t.Error("Unexpected result:", res, "instead of 1")
return
}
}
func TestParallel(t *testing.T) {
nums := []int{1, runtime.NumCPU(), 50, rand.Intn(10) + 1}
for _, nprocs := range nums {
if !testParallel(nprocs, rand.Intn(nprocs*10), t) {
return
}
}
}
func testParallel(nprocs, njobs int, t *testing.T) bool {
var counter int32
errs, cancel := Parallel(nprocs, counterJobs(&counter, njobs))
defer cancel()
for err := range errs {
t.Error("nprocs:", nprocs, ", failure:", err)
return false
}
if counter != int32(njobs) {
t.Error("nprocs:", nprocs, ", failure: Invalid number of completed jobs:", counter, "instead of", njobs)
return false
}
return true
}
func TestParallelForEach(t *testing.T) {
input := []string{"1", "2", "3", "4"}
var res int32
errs, cancel := Parallel(0, ForEachString(input, func(s string) error {
val, err := strconv.Atoi(s)
if err != nil {
return err
}
atomic.AddInt32(&res, int32(val))
return nil
}))
defer cancel()
for err := range errs {
t.Error(err)
return
}
if res != 10 {
t.Error("Invalid number of completed jobs:", res, "instead of 10")
return
}
}
func TestParallelForEachError(t *testing.T) {
input := []string{"1", "2", "zzz", "4"}
var res int32
errs, cancel := Parallel(0, ForEachString(input, func(s string) error {
val, err := strconv.Atoi(s)
if err != nil {
return err
}
atomic.AddInt32(&res, int32(val))
return nil
}))
defer cancel()
err := <-errs
if err == nil {
t.Error("Missing error")
return
}
if _, ok := err.(*strconv.NumError); !ok {
t.Error("Unexpected error:", err)
return
}
if err := <-errs; err != nil {
t.Error("Unexpected second error:", err)
return
}
if res > 7 {
t.Error("Invalid number of completed jobs:", res)
return
}
}
func TestParallelCancel(t *testing.T) {
ctx, stop := context.WithCancel(Context())
var res int32
errs, cancel := ParallelCtx(ctx, 0, counterJobs(&res, 5))
defer cancel()
stop()
err := <-errs
if err == nil {
t.Error("Missing error")
return
}
if err != context.Canceled {
t.Error("Unexpected error:", err)
return
}
if err = <-errs; err != nil {
t.Error("Unexpected second error:", err)
return
}
}
func counterJobs(pcnt *int32, njobs int) (jobs chan func() error) {
jobs = make(chan func() error, njobs)
defer close(jobs)
for i := 0; i < njobs; i++ {
jobs <- func() error {
time.Sleep(time.Duration(rand.Intn(20)) * time.Millisecond)
atomic.AddInt32(pcnt, 1)
return nil
}
}
return
}
func TestParallelFeed(t *testing.T) {
const N = 10
var res int32
// input tasks channel
inch := make(chan func() error)
// start feeder
Go(func() {
defer close(inch)
time.Sleep(10 * time.Millisecond)
for i := 0; i < N; i++ {
inch <- func() error {
atomic.AddInt32(&res, 1)
return nil
}
}
})
// launch tasks
errs, cancel := Parallel(0, inch)
defer cancel()
// check errors
for err := range errs {
t.Error(err)
return
}
if res != 10 {
t.Error("Unexpected result:", res, "instead of", N)
return
}
}
func _TestSignal(t *testing.T) {
// press Ctrl+c to continue
<-Done()
t.Log(Err())
}