-
Notifications
You must be signed in to change notification settings - Fork 0
/
job_test.go
423 lines (358 loc) · 10.4 KB
/
job_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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
package goller_test
import (
"errors"
"io/ioutil"
"testing"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/sqs"
"github.com/aws/aws-sdk-go-v2/service/sqs/sqsiface"
"github.com/rcrowe/goller"
"github.com/sirupsen/logrus"
)
type mockSQSClient struct {
sqsiface.SQSAPI
}
func TestAttribute(t *testing.T) {
cfg := goller.NewDefaultConfig("", 1)
logger := logrus.New()
logger.Out = ioutil.Discard
// Not set
{
attr, ok := goller.NewJob(cfg, logger, sqs.Message{}, &mockSQSClient{}).Attribute("foo")
if attr != "" {
t.Errorf("expected empty attribute but got `%s`", attr)
}
if ok {
t.Error("expected `ok=false` but got `ok=true`")
}
}
// Set
{
msg := sqs.Message{
MessageAttributes: map[string]sqs.MessageAttributeValue{
"foo": sqs.MessageAttributeValue{
StringValue: aws.String("bar"),
},
},
}
attr, ok := goller.NewJob(cfg, logger, msg, &mockSQSClient{}).Attribute("foo")
if attr != "bar" {
t.Errorf("expected attribute `bar` but got `%s`", attr)
}
if !ok {
t.Error("expected `ok=true` but got `ok=false`")
}
}
}
func TestID(t *testing.T) {
cfg := goller.NewDefaultConfig("", 1)
expected := "43244-32423-23423423"
msg := sqs.Message{
MessageId: aws.String(expected),
}
logger := logrus.New()
logger.Out = ioutil.Discard
if id := goller.NewJob(cfg, logger, msg, &mockSQSClient{}).ID(); id != expected {
t.Error("incorrect job id")
}
}
func TestBody(t *testing.T) {
cfg := goller.NewDefaultConfig("", 1)
logger := logrus.New()
logger.Out = ioutil.Discard
// Empty body
{
body, err := goller.NewJob(cfg, logger, sqs.Message{}, &mockSQSClient{}).Body()
if body != "" {
t.Error("expected an empty message body")
}
if err == nil {
t.Error("expected an empty message body")
}
}
// Valid body
{
expected := "this is some text"
msg := sqs.Message{
Body: aws.String(expected),
}
body, err := goller.NewJob(cfg, logger, msg, &mockSQSClient{}).Body()
if body != expected {
t.Errorf("expected body to equal `%s` but got `%s`", expected, body)
}
if err != nil {
t.Error("expected no error returned from a valid body")
}
}
}
func TestTries(t *testing.T) {
cfg := goller.NewDefaultConfig("", 1)
logger := logrus.New()
logger.Out = ioutil.Discard
// Unable to get receive count
{
tries, err := goller.NewJob(cfg, logger, sqs.Message{}, &mockSQSClient{}).Tries()
if tries != 0 {
t.Fail()
}
if err == nil {
t.Fail()
}
}
// Returns 1 less than SQS says
{
msg := sqs.Message{
Attributes: map[string]string{
string(sqs.MessageSystemAttributeNameApproximateReceiveCount): "3",
},
}
tries, err := goller.NewJob(cfg, logger, msg, &mockSQSClient{}).Tries()
if err != nil {
t.Fail()
}
if tries != 2 {
t.Fail()
}
}
}
type erroredDeleteSQSClient struct {
sqsiface.SQSAPI
Input *sqs.DeleteMessageInput
}
func (c *erroredDeleteSQSClient) DeleteMessageRequest(input *sqs.DeleteMessageInput) sqs.DeleteMessageRequest {
c.Input = input
return sqs.DeleteMessageRequest{
Request: &aws.Request{
Error: errors.New("i just errored"),
},
}
}
type deleteSQSClient struct {
sqsiface.SQSAPI
Input *sqs.DeleteMessageInput
Output sqs.DeleteMessageOutput
}
func (c *deleteSQSClient) DeleteMessageRequest(input *sqs.DeleteMessageInput) sqs.DeleteMessageRequest {
c.Input = input
return sqs.DeleteMessageRequest{
Request: &aws.Request{
Data: &c.Output,
},
}
}
func TestDelete(t *testing.T) {
expectedQueueURL := "http://some/queue/url"
cfg := goller.NewDefaultConfig(expectedQueueURL, 1)
logger := logrus.New()
logger.Out = ioutil.Discard
// On error, is handled still false
{
svc := &erroredDeleteSQSClient{}
j := goller.NewJob(cfg, logger, sqs.Message{}, svc)
err := j.Delete()
if err == nil {
t.Error("delete request should have failed")
}
if j.Handled() {
t.Error("job was not deleted, so should not be marked as handled")
}
if aws.StringValue(svc.Input.QueueUrl) != expectedQueueURL {
t.Errorf(
"expected `%s` for queue URL but got `%s`",
expectedQueueURL,
aws.StringValue(svc.Input.QueueUrl),
)
}
}
// Successfully delete marks job as handled
{
svc := &deleteSQSClient{Output: sqs.DeleteMessageOutput{}}
j := goller.NewJob(cfg, logger, sqs.Message{MessageId: aws.String("123")}, svc)
err := j.Delete()
if err != nil {
t.Errorf("delete request should have succedded but got `%s`", err)
}
if !j.Handled() {
t.Error("job was deleted, so job should be marked as handled")
}
}
// Job can not be deleted twice
{
svc := &deleteSQSClient{Output: sqs.DeleteMessageOutput{}}
j := goller.NewJob(cfg, logger, sqs.Message{MessageId: aws.String("123")}, svc)
err := j.Delete()
if err != nil {
t.Errorf("delete request should have succedded but got `%s`", err)
}
err = j.Delete()
if err == nil {
t.Error("expected calling delete twice to error because already handled")
}
if err != goller.ErrAlreadyHandled {
t.Errorf("expected an instance of ErrAlreadyHandled to be returned, instead `%s` was", err)
}
}
}
type erroredReleaseSQSClient struct {
sqsiface.SQSAPI
Input *sqs.ChangeMessageVisibilityInput
}
func (c *erroredReleaseSQSClient) ChangeMessageVisibilityRequest(input *sqs.ChangeMessageVisibilityInput) sqs.ChangeMessageVisibilityRequest {
c.Input = input
return sqs.ChangeMessageVisibilityRequest{
Request: &aws.Request{
Error: errors.New("i just errored"),
},
}
}
type releaseSQSClient struct {
sqsiface.SQSAPI
Input *sqs.ChangeMessageVisibilityInput
Output sqs.ChangeMessageVisibilityOutput
}
func (c *releaseSQSClient) ChangeMessageVisibilityRequest(input *sqs.ChangeMessageVisibilityInput) sqs.ChangeMessageVisibilityRequest {
c.Input = input
return sqs.ChangeMessageVisibilityRequest{
Request: &aws.Request{
Data: &c.Output,
},
}
}
func TestRelease(t *testing.T) {
expectedQueueURL := "http://some/queue/url"
cfg := goller.NewDefaultConfig(expectedQueueURL, 1)
logger := logrus.New()
logger.Out = ioutil.Discard
// On error, is handled still false
{
svc := &erroredReleaseSQSClient{}
j := goller.NewJob(cfg, logger, sqs.Message{}, svc)
err := j.Release(50)
if err == nil {
t.Error("release request should have failed")
}
if j.Handled() {
t.Error("job was not released, so should not be marked as handled")
}
if aws.StringValue(svc.Input.QueueUrl) != expectedQueueURL {
t.Errorf(
"expected `%s` for queue URL but got `%s`",
expectedQueueURL,
aws.StringValue(svc.Input.QueueUrl),
)
}
}
// Successfully delete marks job as handled
{
expected := int64(50)
svc := &releaseSQSClient{Output: sqs.ChangeMessageVisibilityOutput{}}
j := goller.NewJob(cfg, logger, sqs.Message{MessageId: aws.String("123")}, svc)
err := j.Release(expected)
if err != nil {
t.Errorf("release request should have succedded but got `%s`", err)
}
if !j.Handled() {
t.Error("job was released, so job should be marked as handled")
}
if aws.Int64Value(svc.Input.VisibilityTimeout) != expected {
t.Errorf("incorrect visibility timeout. expected `%d` but got `%d`", expected, svc.Input.VisibilityTimeout)
}
}
}
func TestReleaseConfigOverrides(t *testing.T) {
expectedQueueURL := "http://some/queue/url"
logger := logrus.New()
logger.Out = ioutil.Discard
// Min visibility config overrides release request
{
expected := int64(100)
cfg := goller.NewDefaultConfig(expectedQueueURL, 1)
cfg.Job.MinVisibilityTimeout = expected
svc := &releaseSQSClient{Output: sqs.ChangeMessageVisibilityOutput{}}
j := goller.NewJob(cfg, logger, sqs.Message{MessageId: aws.String("123")}, svc)
j.Release(50)
if aws.Int64Value(svc.Input.VisibilityTimeout) != expected {
t.Error("incorrect visibility timeout")
}
}
// Max visibility config overrides release request
{
expected := int64(40)
cfg := goller.NewDefaultConfig(expectedQueueURL, 1)
cfg.Job.MaxVisibilityTimeout = expected
svc := &releaseSQSClient{Output: sqs.ChangeMessageVisibilityOutput{}}
j := goller.NewJob(cfg, logger, sqs.Message{MessageId: aws.String("123")}, svc)
j.Release(expected + 10)
if aws.Int64Value(svc.Input.VisibilityTimeout) != expected {
t.Error("incorrect visibility timeout")
}
}
// Max visibility can not go over (12hrs - 1second) as per SQS API
{
expected := int64(((12 * time.Hour) - 1).Seconds())
cfg := goller.NewDefaultConfig(expectedQueueURL, 1)
svc := &releaseSQSClient{Output: sqs.ChangeMessageVisibilityOutput{}}
j := goller.NewJob(cfg, logger, sqs.Message{MessageId: aws.String("123")}, svc)
j.Release(int64((12 * time.Hour).Seconds()))
if aws.Int64Value(svc.Input.VisibilityTimeout) != expected {
t.Errorf("incorrect visibility timeout. expected `%d` but got `%d`", expected, aws.Int64Value(svc.Input.VisibilityTimeout))
}
}
}
func TestReleaseCantDeleteTwice(t *testing.T) {
expectedQueueURL := "http://some/queue/url"
cfg := goller.NewDefaultConfig(expectedQueueURL, 1)
logger := logrus.New()
logger.Out = ioutil.Discard
// Job can not be deleted twice
{
svc := &releaseSQSClient{Output: sqs.ChangeMessageVisibilityOutput{}}
j := goller.NewJob(cfg, logger, sqs.Message{MessageId: aws.String("123")}, svc)
err := j.Release(50)
if err != nil {
t.Errorf("release request should have succedded but got `%s`", err)
}
err = j.Release(50)
if err == nil {
t.Error("expected calling release twice to error because already handled")
}
if err != goller.ErrAlreadyHandled {
t.Errorf("expected an instance of ErrAlreadyHandled to be returned, instead `%s` was", err)
}
}
}
func TestBackoff(t *testing.T) {
logger := logrus.New()
logger.Out = ioutil.Discard
// Unable to Backoff when unable to get number of tries
{
cfg := goller.NewDefaultConfig("", 1)
err := goller.NewJob(cfg, logger, sqs.Message{}, &mockSQSClient{}).Backoff()
if err == nil {
t.Fail()
}
}
// Uses BackoffCalc to get release time
{
cfg := goller.NewDefaultConfig("", 1)
cfg.Job.BackoffCalc = func(try int64) int64 {
return 12
}
msg := sqs.Message{
MessageId: aws.String("123"),
Attributes: map[string]string{
string(sqs.MessageSystemAttributeNameApproximateReceiveCount): "3",
},
}
svc := &releaseSQSClient{Output: sqs.ChangeMessageVisibilityOutput{}}
j := goller.NewJob(cfg, logger, msg, svc)
err := j.Backoff()
if err != nil {
t.Errorf("expected backoff to succeed. received err `%s`", err)
}
if aws.Int64Value(svc.Input.VisibilityTimeout) != 12 {
t.Error("incorrect visibility timeout")
}
}
}