-
Notifications
You must be signed in to change notification settings - Fork 0
/
queue_test.go
253 lines (229 loc) · 5.73 KB
/
queue_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
package cq
import (
"errors"
"fmt"
"testing"
"time"
)
func TestQueueGetters(t *testing.T) {
tests := []struct {
name string
want func(*Queue) error
}{
{
name: "capacity",
want: func(q *Queue) error {
want := 1
if c := q.Capacity(); c != want {
return fmt.Errorf("Capacity() = %v, want %v", c, want)
}
return nil
},
},
{
name: "running",
want: func(q *Queue) error {
want := 1
if rw := q.RunningWorkers(); rw != want {
return fmt.Errorf("RunningWorkers() = %v, want %v", rw, want)
}
return nil
},
},
{
name: "idle",
want: func(q *Queue) error {
want := 1
if iw := q.IdleWorkers(); iw != want {
return fmt.Errorf("IdleWorkers() = %v, want %v", iw, want)
}
return nil
},
},
{
name: "range",
want: func(q *Queue) error {
wantMin := 1
wantMax := 2
wmin, wmax := q.WorkerRange()
if wmin != wantMin || wmax != wantMax {
return fmt.Errorf("WorkerRange() = %v:%v, want %v:%v", wmin, wmax, wantMin, wantMax)
}
return nil
},
},
}
for _, tt := range tests {
q := NewQueue(1, 2, 1)
q.Start()
defer q.Stop(false)
t.Run(tt.name, func(t *testing.T) {
if err := tt.want(q); err != nil {
t.Error(err)
}
})
}
}
func TestQueueEnqueue(t *testing.T) {
var called bool // Was job called?
q := NewQueue(1, 1, 1)
q.Start()
q.Enqueue(func() error {
called = true
return nil
})
q.Stop(true)
if !called {
t.Error("Enqueue(): expected job to enqueued and executed")
}
}
func TestQueueDelayEnqueue(t *testing.T) {
delay := time.Duration(500 * time.Millisecond)
sleep := time.Duration(600 * time.Millisecond)
q := NewQueue(1, 1, 1)
q.Start()
defer q.Stop(true)
q.DelayEnqueue(
func() error {
return nil
},
delay,
)
if njc := q.TallyOf(JobStateCreated); njc != 0 {
t.Errorf("DelayQueue(): TallyOf(JobStateCreated): should not have created job yet, got %v, want 0", njc)
}
time.Sleep(sleep) // Give job time to enqueue and run.
if njc := q.TallyOf(JobStateCreated); njc != 1 {
t.Errorf("DelayQueue(): TallyOf(JobStateCreated): should have created job, got %v, want 1", njc)
}
}
func TestQueueEnqueueMaxed(t *testing.T) {
jobs := 150 // Number of jobs.
delay := time.Duration(500 * time.Millisecond) // Job sleep.
sleep := time.Duration(1 * time.Second) // Check sleep.
var pMsg string // Panic message.
q := NewQueue(1, 1, 0, WithPanicHandler(func(err interface{}) {
pMsg = fmt.Sprintf("%v", err)
}))
q.Start()
// Spam the queue with 0 capacity.
for i := 0; i < jobs; i += 1 {
go func() {
q.Enqueue(func() error {
time.Sleep(delay)
return nil
})
}()
}
q.Stop(false)
time.Sleep(sleep) // Allow panic to run.
if pMsg != "send on closed channel" {
t.Error("WithPanicHandler(): expected panic to happen")
}
}
func TestQueueEnqueueIfStopped(t *testing.T) {
q := NewQueue(1, 1, 1)
q.Start()
q.Stop(true)
job := func() error { return nil }
if ok := q.TryEnqueue(job); ok {
t.Error("TryEnqueue() = true, want false")
}
}
func TestQueueTallies(t *testing.T) {
tests := []struct {
state JobState
job Job
want func(*Queue) error
}{
{
state: JobStateCompleted,
job: func() error {
return nil
},
want: func(q *Queue) error {
want := 1
if jc := q.TallyOf(JobStateCompleted); jc != want {
return fmt.Errorf("TallyOf(JobStateCompleted) = %v, want %v", jc, want)
}
if jc := q.TallyOf(JobStateCreated); jc != want {
return fmt.Errorf("TallyOf(JobStateCreated) = %v, want %v", jc, want)
}
return nil
},
},
{
state: JobStateFailed,
job: func() error {
return errors.New("error")
},
want: func(q *Queue) error {
want := 1
if jc := q.TallyOf(JobStateFailed); jc != want {
return fmt.Errorf("TallyOf(JobStateFailed) = %v, want %v", jc, want)
}
if jc := q.TallyOf(JobStateCreated); jc != want {
return fmt.Errorf("TallyOf(JobStateCreated) = %v, want %v", jc, want)
}
return nil
},
},
}
for _, tt := range tests {
t.Run(tt.state.String(), func(t *testing.T) {
q := NewQueue(1, 1, 1)
q.Start()
q.Enqueue(tt.job)
q.Stop(true)
if err := tt.want(q); err != nil {
t.Errorf("%T: %v", tt.state, err)
}
})
}
}
func TestQueuePanic(t *testing.T) {
var called bool
q := NewQueue(1, 1, 1, WithPanicHandler(func(err interface{}) {
called = true
}))
q.Start()
q.Enqueue(func() error {
panic("panic")
})
q.Stop(true)
if !called {
t.Error("WithPanicHandler(): should have executed")
}
}
func TestIdleWorkerTick(t *testing.T) {
var atMax bool // Running workers at max?
wmin, wmax := 1, 2 // Min. & max. workers.
jobs := 50 // Number of jobs.
delay := time.Duration(50 * time.Millisecond) // Delay of each job.
idle := time.Duration(150 * time.Millisecond) // Idle tick.
sleep := (time.Duration(jobs * int(delay))) + (2 * time.Second) // Check delay with 2s buffer.
q := NewQueue(wmin, wmax, 0, WithWorkerIdleTick(idle))
q.Start()
defer q.Stop(true)
for i := 0; i < jobs; i += 1 {
q.Enqueue(func() error {
time.Sleep(delay)
return nil
})
// At some point, we should have wmax workers.
if !atMax {
if rw := q.RunningWorkers(); rw == wmax {
atMax = true
}
}
}
if !atMax {
// Because so many jobs were spammed, this should be more than worker min.
t.Error("Queue: did not spin up the desired workers")
}
time.Sleep(sleep) // Let the cleanup happen.
// Should be back to worker min.
if nrw := q.RunningWorkers(); nrw != wmin {
t.Errorf("RunningWorkers() = %v, want %v", nrw, wmin)
}
}