-
Notifications
You must be signed in to change notification settings - Fork 7
/
pipeliner_test.go
84 lines (75 loc) · 1.61 KB
/
pipeliner_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
package pipeliner
import (
"context"
"errors"
"github.com/stretchr/testify/require"
"math/rand"
"sync"
"testing"
"time"
)
func TestPipeliner(t *testing.T) {
v, err := testPipeliner(false, false)
require.NoError(t, err)
for i, e := range v {
require.Equal(t, i, e)
}
}
func TestPipelinerError(t *testing.T) {
testPipelinerError(t)
}
func testPipelinerError(t *testing.T) {
v, err := testPipeliner(true, false)
require.Error(t, err)
if err != nil {
require.Equal(t, err.Error(), "errored out")
}
for _, e := range v[28:] {
require.Equal(t, 0, e)
}
}
func TestPipelinerCancel(t *testing.T) {
v, err := testPipeliner(false, true)
require.Error(t, err)
require.Equal(t, err.Error(), "context canceled")
for _, e := range v[28:] {
require.Equal(t, 0, e)
}
}
func TestPipelinerErrorStress(t *testing.T) {
for i := 0; i < 200; i++ {
testPipelinerError(t)
}
}
func testPipeliner(doError bool, doCancel bool) ([]int, error) {
v := make([]int, 100)
var vlock sync.Mutex
pipeliner := NewPipeliner(4)
ctx := context.Background()
var cancelFunc func()
if doCancel {
ctx, cancelFunc = context.WithCancel(ctx)
}
f := func(ctx context.Context, i int) error {
if doError && i == 20 {
return errors.New("errored out")
}
if cancelFunc != nil && i == 20 {
cancelFunc()
}
vlock.Lock()
v[i] = i
vlock.Unlock()
time.Sleep(time.Microsecond * time.Duration((rand.Int() % 17)))
return nil
}
for i := range v {
err := pipeliner.WaitForRoom(ctx)
if err != nil {
return v, err
}
go func(i int) { pipeliner.CompleteOne(f(ctx, i)) }(i)
}
err := pipeliner.Flush(ctx)
return v, err
}