This repository has been archived by the owner on Dec 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmanager_test.go
301 lines (283 loc) · 7.23 KB
/
manager_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
// Copyright 2016-present Oliver Eilhard. All rights reserved.
// Use of this source code is governed by a MIT-license.
// See http://olivere.mit-license.org/license.txt for details.
package jobqueue
import (
"context"
"errors"
"fmt"
"testing"
"time"
)
type stringLogger struct {
Lines []string
}
func (l *stringLogger) Printf(format string, v ...interface{}) {
l.Lines = append(l.Lines, fmt.Sprintf(format, v...))
}
func TestManagerDefaults(t *testing.T) {
m := New()
if m.st == nil {
t.Fatal("Store is nil")
}
if have, want := len(m.concurrency), 1; have != want {
t.Fatalf("len(concurrency) = %v, want %v", have, want)
}
if have, want := m.concurrency[0], defaultConcurrency; have != want {
t.Fatalf("concurrency = %v, want %v", have, want)
}
if have, want := m.started, false; have != want {
t.Fatalf("concurrency = %t, want %t", have, want)
}
if have, want := 0, len(m.workers); have != want {
t.Fatalf("len(workers) = %d, want %d", have, want)
}
}
func TestManagerRegisterDuplicateTopic(t *testing.T) {
m := New()
f := func(job *Job) error { return nil }
err := m.Register("topic", f)
if err != nil {
t.Fatalf("Register failed with %v", err)
}
err = m.Register("topic", f)
if err == nil {
t.Fatalf("expected Register to fail")
}
}
func TestManagerStartStop(t *testing.T) {
m := New()
started := make(chan struct{}, 1)
stopped := make(chan struct{}, 1)
m.testManagerStarted = func() { started <- struct{}{} }
m.testManagerStopped = func() { stopped <- struct{}{} }
err := m.Start()
if err != nil {
t.Fatalf("Start failed with %v", err)
}
select {
case <-started:
case <-time.After(1 * time.Second):
t.Fatal("Start timed out")
}
err = m.Stop()
if err != nil {
t.Fatalf("Stop failed with %v", err)
}
select {
case <-stopped:
case <-time.After(1 * time.Second):
t.Fatal("Stop timed out")
}
}
// TestJobSuccess is the green case where a job is called and it is
// processed without problems.
func TestJobSuccess(t *testing.T) {
scheduled := make(chan struct{}, 1)
started := make(chan struct{}, 1)
succeeded := make(chan struct{}, 1)
jobDone := make(chan struct{}, 1)
m := New()
m.testJobScheduled = func() { scheduled <- struct{}{} }
m.testJobStarted = func() { started <- struct{}{} }
m.testJobSucceeded = func() { succeeded <- struct{}{} }
f := func(job *Job) error {
if len(job.Args) != 1 {
return fmt.Errorf("expected len(args) == 1, have %d", len(job.Args))
}
s, ok := job.Args[0].(string)
if !ok {
return fmt.Errorf("expected type of 1st arg == string, have %T", job.Args[0])
}
if have, want := s, "Hello"; have != want {
return fmt.Errorf("expected 1st arg = %q, have %q", want, have)
}
jobDone <- struct{}{}
return nil
}
err := m.Register("topic", f)
if err != nil {
t.Fatalf("Register failed with %v", err)
}
err = m.Start()
if err != nil {
t.Fatalf("Start failed with %v", err)
}
job := &Job{Topic: "topic", Args: []interface{}{"Hello"}}
err = m.Add(context.Background(), job)
if err != nil {
t.Fatalf("Add failed with %v", err)
}
if job.ID == "" {
t.Fatalf("Job ID = %q", job.ID)
}
timeout := 2 * time.Second
select {
case <-scheduled:
case <-time.After(timeout):
t.Fatal("Scheduler timed out")
}
select {
case <-started:
case <-time.After(timeout):
t.Fatal("Job Start timed out")
}
select {
case <-jobDone:
case <-time.After(timeout):
t.Fatal("Processor func timed out")
}
select {
case <-succeeded:
case <-time.After(timeout):
t.Fatal("Job Completion timed out")
}
}
// TestJobFailure will try to process a job that fails. We check that it
// will end up in the Failed state.
func TestJobFailure(t *testing.T) {
scheduled := make(chan struct{}, 1)
started := make(chan struct{}, 1)
failed := make(chan struct{}, 1)
jobDone := make(chan struct{}, 1)
l := &stringLogger{}
m := New(SetLogger(l))
m.testJobScheduled = func() { scheduled <- struct{}{} }
m.testJobStarted = func() { started <- struct{}{} }
m.testJobFailed = func() { failed <- struct{}{} }
f := func(job *Job) error {
if len(job.Args) != 1 {
return fmt.Errorf("expected len(args) == 1, have %d", len(job.Args))
}
s, ok := job.Args[0].(string)
if !ok {
return fmt.Errorf("expected type of 1st arg == string, have %T", job.Args[0])
}
if have, want := s, "Hello"; have != want {
return fmt.Errorf("expected 1st arg = %q, have %q", want, have)
}
jobDone <- struct{}{}
return errors.New("failed job")
}
err := m.Register("topic", f)
if err != nil {
t.Fatalf("Register failed with %v", err)
}
err = m.Start()
if err != nil {
t.Fatalf("Start failed with %v", err)
}
job := &Job{Topic: "topic", Args: []interface{}{"Hello"}}
err = m.Add(context.Background(), job)
if err != nil {
t.Fatalf("Add failed with %v", err)
}
if job.ID == "" {
t.Fatalf("Job ID = %q", job.ID)
}
timeout := 2 * time.Second
select {
case <-scheduled:
case <-time.After(timeout):
t.Fatal("Scheduler timed out")
}
select {
case <-started:
case <-time.After(timeout):
t.Fatal("Job Start timed out")
}
select {
case <-jobDone:
case <-time.After(timeout):
t.Fatal("Processor func timed out")
}
select {
case <-failed:
case <-time.After(timeout):
t.Fatal("Job failure timed out")
}
if have, want := len(l.Lines), 1; have != want {
t.Fatal("expected lines written to Logger")
}
}
// TestJobSuccessAfterRetry will schedule a job that will fail on the 1st
// call, but succeed on the 2nd. We check that the retry invoked and it
// will succeed after the 2nd run.
func TestJobSuccessAfterRetry(t *testing.T) {
scheduled := make(chan struct{}, 1)
started := make(chan struct{}, 1)
succeeded := make(chan struct{}, 1)
retry := make(chan struct{}, 1)
jobDone := make(chan struct{}, 2)
l := &stringLogger{}
m := New(SetLogger(l))
m.testJobScheduled = func() { scheduled <- struct{}{} }
m.testJobStarted = func() { started <- struct{}{} }
m.testJobRetry = func() { retry <- struct{}{} }
m.testJobSucceeded = func() { succeeded <- struct{}{} }
var call int
f := func(_ *Job) error {
call++
jobDone <- struct{}{}
// only fail on first call
if call == 1 {
return errors.New("failed job on 1st call")
}
return nil
}
err := m.Register("topic", f)
if err != nil {
t.Fatalf("Register failed with %v", err)
}
err = m.Start()
if err != nil {
t.Fatalf("Start failed with %v", err)
}
job := &Job{Topic: "topic", MaxRetry: 1, Args: []interface{}{"Hello"}}
err = m.Add(context.Background(), job)
if err != nil {
t.Fatalf("Add failed with %v", err)
}
if job.ID == "" {
t.Fatalf("Job ID = %q", job.ID)
}
timeout := 2 * time.Second
select {
case <-scheduled:
case <-time.After(timeout):
t.Fatal("Scheduler timed out")
}
select {
case <-started:
case <-time.After(timeout):
t.Fatal("Job Start timed out")
}
select {
case <-jobDone:
case <-time.After(timeout):
t.Fatal("Processor func timed out")
}
select {
case <-retry:
case <-time.After(timeout):
t.Fatal("Job retry timed out")
}
select {
case <-started:
case <-time.After(timeout):
t.Fatal("Job Start timed out")
}
select {
case <-jobDone:
case <-time.After(timeout):
t.Fatal("Processor func timed out")
}
select {
case <-succeeded:
case <-time.After(timeout):
t.Fatal("Job success timed out")
}
if have, want := len(l.Lines), 1; have != want {
t.Fatal("expected lines written to Logger")
}
}