forked from digitalocean/go-workers2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanager_test.go
339 lines (290 loc) · 7.87 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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
package workers
import (
"context"
"crypto/tls"
"sync"
"testing"
"time"
"github.com/go-redis/redis/v8"
"github.com/stretchr/testify/assert"
)
func newTestManager(opts Options) (*Manager, error) {
mgr, err := NewManager(opts)
if mgr != nil {
mgr.opts.client.FlushDB(context.Background()).Result()
}
return mgr, err
}
func TestNewManager(t *testing.T) {
namespace := "prod"
opts := testOptionsWithNamespace(namespace)
mgr, err := NewManager(opts)
assert.NoError(t, err)
assert.NotEmpty(t, mgr.uuid)
assert.Equal(t, namespace+":", mgr.opts.Namespace)
}
func TestNewManagerWithRedisClient(t *testing.T) {
namespace := "prod"
opts := Options{
ProcessID: "1",
Namespace: namespace,
}
client := redis.NewClient(&redis.Options{
IdleTimeout: 1,
Password: "ab",
DB: 2,
TLSConfig: &tls.Config{ServerName: "test_tls2"},
})
mgr, err := NewManagerWithRedisClient(opts, client)
assert.NoError(t, err)
assert.NotEmpty(t, mgr.uuid)
assert.Equal(t, namespace+":", mgr.opts.Namespace)
assert.NotNil(t, mgr.GetRedisClient())
assert.NotNil(t, mgr.GetRedisClient().Options().TLSConfig)
assert.Equal(t, "test_tls2", mgr.GetRedisClient().Options().TLSConfig.ServerName)
}
func TestNewManagerWithRedisClientNoProcessID(t *testing.T) {
namespace := "prod"
opts := Options{
Namespace: namespace,
}
client := redis.NewClient(&redis.Options{
IdleTimeout: 1,
Password: "ab",
DB: 2,
TLSConfig: &tls.Config{ServerName: "test_tls2"},
})
mgr, err := NewManagerWithRedisClient(opts, client)
assert.Error(t, err)
assert.Nil(t, mgr)
}
func TestManager_AddBeforeStartHooks(t *testing.T) {
namespace := "prod"
opts := testOptionsWithNamespace(namespace)
opts.PollInterval = time.Second
mgr, err := newTestManager(opts)
assert.NoError(t, err)
var beforeStartCalled int
mgr.AddBeforeStartHooks(func() {
beforeStartCalled++
})
ch := make(chan bool)
go func() {
mgr.Run()
ch <- true
mgr.Run()
ch <- true
}()
time.Sleep(time.Second)
assert.Equal(t, 1, beforeStartCalled)
mgr.Stop()
<-ch
time.Sleep(time.Second)
assert.Equal(t, 2, beforeStartCalled)
mgr.Stop()
<-ch
}
func TestManager_AddDuringDrainHooks(t *testing.T) {
namespace := "prod"
opts := testOptionsWithNamespace(namespace)
opts.PollInterval = time.Second
mgr, err := newTestManager(opts)
assert.NoError(t, err)
var duringDrainCalled int
mgr.AddDuringDrainHooks(func() {
duringDrainCalled++
})
ch := make(chan bool)
go func() {
mgr.Run()
ch <- true
mgr.Run()
ch <- true
}()
time.Sleep(time.Second)
mgr.Stop()
assert.Equal(t, 1, duringDrainCalled)
<-ch
time.Sleep(time.Second)
mgr.Stop()
assert.Equal(t, 2, duringDrainCalled)
<-ch
}
func TestManager_AddWorker(t *testing.T) {
namespace := "prod"
opts := testOptionsWithNamespace(namespace)
opts.PollInterval = time.Second
mgr, err := NewManager(opts)
assert.NoError(t, err)
var handlerCalled bool
var defaultMidCalled bool
baseMids := defaultMiddlewares
defaultMiddlewares = NewMiddlewares(
func(queue string, mgr *Manager, next JobFunc) JobFunc {
return func(message *Msg) (result error) {
defaultMidCalled = true
result = next(message)
return
}
},
)
mgr.AddWorker("someq", 1, func(m *Msg) error {
handlerCalled = true
return nil
})
assert.Len(t, mgr.workers, 1)
assert.Equal(t, "someq", mgr.workers[0].queue)
msg, _ := NewMsg("{}")
mgr.workers[0].handler(msg)
assert.True(t, defaultMidCalled)
assert.True(t, handlerCalled)
var midCalled bool
mgr.workers = nil
mgr.AddWorker("someq", 1, func(m *Msg) error {
handlerCalled = true
return nil
}, func(queue string, mgr *Manager, next JobFunc) JobFunc {
return func(message *Msg) (result error) {
midCalled = true
result = next(message)
return
}
})
defaultMidCalled = false
handlerCalled = false
mgr.workers[0].handler(msg)
assert.False(t, defaultMidCalled)
assert.True(t, midCalled)
assert.True(t, handlerCalled)
defaultMiddlewares = baseMids
}
func TestManager_Run(t *testing.T) {
namespace := "mgrruntest"
opts := testOptionsWithNamespace(namespace)
opts.PollInterval = time.Second
mgr, err := newTestManager(opts)
assert.NoError(t, err)
prod := mgr.Producer()
q1cc := newCallCounter()
q2cc := newCallCounter()
mgr.AddWorker("queue1", 1, q1cc.F, NopMiddleware)
mgr.AddWorker("queue2", 2, q2cc.F, NopMiddleware)
var wg sync.WaitGroup
go func() {
wg.Add(1)
mgr.Run()
wg.Done()
}()
// Test that messages process
_, err = prod.Enqueue("queue1", "any", q1cc.syncMsg().Args().Interface())
assert.NoError(t, err)
// This channel read will timeout the test if messages don't process
<-q1cc.syncCh
q1cc.ackSyncCh <- true
// Test that the manager is registered in the stats server
assert.Contains(t, globalAPIServer.managers, mgr.uuid)
// Test that it runs a scheduledWorker
_, err = prod.EnqueueIn("queue1", "any", 2, q1cc.syncMsg().Args().Interface())
assert.NoError(t, err)
// This channel read will timeout the test if the scheduled message doesn't process
<-q1cc.syncCh
q1cc.ackSyncCh <- true
mgr.Stop()
wg.Wait()
// Test that the manager is deregistered from the stats server
assert.NotContains(t, globalAPIServer.managers, mgr.uuid)
// Test that we can restart the manager
go func() {
wg.Add(1)
mgr.Run()
wg.Done()
}()
// Test that messages process
_, err = prod.Enqueue("queue1", "any", q1cc.syncMsg().Args().Interface())
assert.NoError(t, err)
// This channel read will timeout the test if messages don't process, which
// means the manager didn't restart
<-q1cc.syncCh
q1cc.ackSyncCh <- true
// Test that we're back in the global stats server
assert.Contains(t, globalAPIServer.managers, mgr.uuid)
mgr.Stop()
wg.Wait()
}
func TestManager_inProgressMessages(t *testing.T) {
namespace := "mgrruntest"
opts := testOptionsWithNamespace(namespace)
opts.PollInterval = time.Second
mgr, err := newTestManager(opts)
assert.NoError(t, err)
prod, err := NewProducer(opts)
assert.NoError(t, err)
q1cc := newCallCounter()
q2cc := newCallCounter()
mgr.AddWorker("queue1", 1, q1cc.F, NopMiddleware)
mgr.AddWorker("queue2", 2, q2cc.F, NopMiddleware)
var wg sync.WaitGroup
go func() {
wg.Add(1)
mgr.Run()
wg.Done()
}()
// None
ipm := mgr.inProgressMessages()
assert.Len(t, ipm, 2)
assert.Contains(t, ipm, "queue1")
assert.Contains(t, ipm, "queue2")
assert.Empty(t, ipm["queue1"])
assert.Empty(t, ipm["queue2"])
// One in Queue1
_, err = prod.Enqueue("queue1", "any", q1cc.syncMsg().Args().Interface())
assert.NoError(t, err)
<-q1cc.syncCh
ipm = mgr.inProgressMessages()
assert.Len(t, ipm, 2)
assert.Contains(t, ipm, "queue1")
assert.Contains(t, ipm, "queue2")
assert.Len(t, ipm["queue1"], 1)
assert.Empty(t, ipm["queue2"])
// One in Queue2
_, err = prod.Enqueue("queue2", "any", q2cc.syncMsg().Args().Interface())
assert.NoError(t, err)
<-q2cc.syncCh
ipm = mgr.inProgressMessages()
assert.Len(t, ipm, 2)
assert.Contains(t, ipm, "queue1")
assert.Contains(t, ipm, "queue2")
assert.Len(t, ipm["queue1"], 1)
assert.Len(t, ipm["queue2"], 1)
// Another in Queue2
_, err = prod.Enqueue("queue2", "any", q2cc.syncMsg().Args().Interface())
assert.NoError(t, err)
<-q2cc.syncCh
ipm = mgr.inProgressMessages()
assert.Len(t, ipm, 2)
assert.Contains(t, ipm, "queue1")
assert.Contains(t, ipm, "queue2")
assert.Len(t, ipm["queue1"], 1)
assert.Len(t, ipm["queue2"], 2)
// Release two from Queue2
q2cc.ackSyncCh <- true
q2cc.ackSyncCh <- true
time.Sleep(2 * time.Second)
ipm = mgr.inProgressMessages()
assert.Len(t, ipm, 2)
assert.Contains(t, ipm, "queue1")
assert.Contains(t, ipm, "queue2")
assert.Len(t, ipm["queue1"], 1)
assert.Len(t, ipm["queue2"], 0)
// Release last from Queue1 - should have one left in queue2
q1cc.ackSyncCh <- true
time.Sleep(2 * time.Second)
ipm = mgr.inProgressMessages()
assert.Len(t, ipm, 2)
assert.Contains(t, ipm, "queue1")
assert.Contains(t, ipm, "queue2")
assert.Len(t, ipm["queue1"], 0)
assert.Len(t, ipm["queue2"], 0)
mgr.Stop()
wg.Wait()
}