-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtaskrunner_test.go
204 lines (165 loc) · 4.2 KB
/
taskrunner_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
package taskrunner
import (
"context"
"testing"
"time"
"github.com/pkg/errors"
"golang.org/x/sync/errgroup"
)
var errMockFailure = errors.New("mock op failure")
type mockTask struct {
withError bool
task func(context.Context) (interface{}, error)
}
func (w *mockTask) Task(ctx context.Context) (interface{}, error) {
if w.withError {
return nil, errMockFailure
}
return w.task(ctx)
}
func TestNewTaskRunner(t *testing.T) {
tests := []struct {
name string
options []func(*TaskRunner) error
wantErr bool
}{
{
"Pool Start/Teardown",
[]func(*TaskRunner) error{},
false,
},
{
"FAIL - Invalid Amount Of Workers",
[]func(*TaskRunner) error{OptionMaxGoroutines(-1)},
true,
},
{
"Success - Valid goroutines",
[]func(*TaskRunner) error{OptionMaxGoroutines(100)},
false,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
_, err := NewTaskRunner(test.options...)
if (err != nil && !test.wantErr) || (err == nil && test.wantErr) {
t.Errorf("unexpected result creating taskrunner - expected result=%v - err=%v\n", test.wantErr, err)
}
})
}
}
func TestTaskRunnerRun(t *testing.T) {
timeout := time.Duration(10) * time.Millisecond
tests := []struct {
name string
workers int
runs int
task func(context.Context) (interface{}, error)
wantErr bool
}{
{
"Success - Lots of workers",
10,
1,
func(context.Context) (interface{}, error) {
time.Sleep(timeout / 2)
return nil, nil
},
false,
},
{
"Failure - Too many tasks, task too slow, exceeded timeout",
1,
2,
func(context.Context) (interface{}, error) {
time.Sleep(timeout * 2)
return nil, nil
},
true,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
runner, err := NewTaskRunner(OptionMaxGoroutines(test.workers))
if err != nil {
t.Errorf("unexpected error creating taskrunner - err=%v\n", err)
t.FailNow()
}
if err := runner.Start(); err != nil {
t.Errorf("unexpected error starting taskrunner - err=%+v", err)
t.FailNow()
}
var runError error
for i := 0; i < test.runs; i++ {
ctx, done := context.WithTimeout(context.Background(), timeout)
promise := runner.Run(ctx, &mockTask{false, test.task})
if _, err := promise(); err != nil {
runError = err
}
done()
}
if (runError != nil && !test.wantErr) || (runError == nil && test.wantErr) {
t.Errorf("unexpected result running task - expected result=%v - err=%v\n", test.wantErr, runError)
}
if err := runner.Stop(); err != nil {
t.Errorf("unexpected error stopping taskrunner - err=%+v", err)
t.FailNow()
}
})
}
}
func TestTaskRunnerMultipleStartStop(t *testing.T) {
runner, err := NewTaskRunner()
if err != nil {
t.Errorf("unexpected error creating taskrunner - err=%v\n", err)
t.FailNow()
}
var eg errgroup.Group
for i := 0; i < 10; i++ {
eg.Go(runner.Start)
}
if err := eg.Wait(); err == nil {
t.Error("unexpected nil error from multiple attempted starts")
}
for i := 0; i < 10; i++ {
eg.Go(runner.Stop)
}
if err := eg.Wait(); err == nil {
t.Error("unexpected nil error from multiple attempted stops")
}
for i := 0; i < 10; i++ {
eg.Go(runner.Start)
eg.Go(runner.Stop)
}
if err := eg.Wait(); err == nil {
t.Errorf("unexpected nil error from concurrency start-stop of runner")
}
}
func TestTaskRunnerConcurrentStartRunStop(t *testing.T) {
runner, err := NewTaskRunner()
if err != nil {
t.Errorf("unexpected error creating taskrunner - err=%v\n", err)
t.FailNow()
}
if err := runner.Start(); err != nil {
t.Errorf("unexpected error starting taskrunner - err=%+v", err)
t.FailNow()
}
var eg errgroup.Group
for i := 0; i < 100; i++ {
eg.Go(func() error {
promise := runner.Run(context.TODO(), &mockTask{false, func(context.Context) (interface{}, error) {
time.Sleep(time.Duration(10) * time.Millisecond)
return "test", nil
}})
_, err := promise()
return err
})
}
if err := eg.Wait(); err != nil {
t.Errorf("unexpected error from concurrency start-run-stop of runner - err=%+v", err)
}
if err := runner.Stop(); err != nil {
t.Errorf("unexpected error from stopping task runner - err=%+v", err)
}
}