-
Notifications
You must be signed in to change notification settings - Fork 0
/
goller_test.go
324 lines (267 loc) · 8.3 KB
/
goller_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
package goller_test
import (
"context"
"errors"
"fmt"
"strings"
"sync"
"testing"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/aws/awserr"
"github.com/aws/aws-sdk-go-v2/service/sqs"
"github.com/aws/aws-sdk-go-v2/service/sqs/sqsiface"
"github.com/prometheus/client_golang/prometheus"
"github.com/rcrowe/goller"
"github.com/sirupsen/logrus"
)
type dummyWriter struct {
lock sync.Mutex
msg string
}
func (w *dummyWriter) Write(p []byte) (n int, err error) {
w.lock.Lock()
defer w.lock.Unlock()
w.msg = string(p)
return 0, nil
}
func (w *dummyWriter) ReadMsg() string {
w.lock.Lock()
defer w.lock.Unlock()
return w.msg
}
type stackedWriter struct {
msgs []string
}
func (w *stackedWriter) Write(p []byte) (n int, err error) {
w.msgs = append(w.msgs, string(p))
return 0, nil
}
func TestNewSetsQueueURL(t *testing.T) {
expected := "foobar"
w := goller.New(sqs.New(aws.Config{}), expected, 1)
if w.Config().QueueURL != expected {
t.Errorf("expected queue URL `%s` did not equal `%s`", expected, w.Config().QueueURL)
}
}
func TestListenLogsRunOnce(t *testing.T) {
// Logger so we can check what was written
dummyWriter := &dummyWriter{}
logger := logrus.New()
logger.Out = dummyWriter
// Custom config so we can stop any consumers starting
cfg := goller.NewDefaultConfig("foobar", 0)
cfg.RunOnce()
cfg.Consumer.Count = 0
listenOnce(sqs.New(aws.Config{}), logger, cfg)
if !strings.Contains(dummyWriter.msg, "level=debug") {
t.Errorf("expected `level=debug` in the log but saw `%s`", dummyWriter.msg)
}
if !strings.Contains(dummyWriter.msg, "run-once") {
t.Errorf("expected `run-once` in the log but saw `%s`", dummyWriter.msg)
}
if !strings.Contains(dummyWriter.msg, "enabled") {
t.Errorf("expected `enabled` in the log but saw `%s`", dummyWriter.msg)
}
}
func TestListenLogsRunSlowly(t *testing.T) {
// Logger so we can check what was written
dummyWriter := &dummyWriter{}
logger := logrus.New()
logger.Out = dummyWriter
// Custom config so we can stop any consumers starting
cfg := goller.NewDefaultConfig("", 0)
cfg.RunSlowly(5 * time.Second)
cfg.Consumer.Count = 0
listenOnce(sqs.New(aws.Config{}), logger, cfg)
if !strings.Contains(dummyWriter.msg, "level=debug") {
t.Errorf("expected `level=debug` in the log but saw `%s`", dummyWriter.msg)
}
if !strings.Contains(dummyWriter.msg, "run-slowly") {
t.Errorf("expected `run-once` in the log but saw `%s`", dummyWriter.msg)
}
if !strings.Contains(dummyWriter.msg, "enabled") {
t.Errorf("expected `enabled` in the log but saw `%s`", dummyWriter.msg)
}
if !strings.Contains(dummyWriter.msg, "slowly=5s") {
t.Errorf("expected `slowly=5s` in the log but saw `%s`", dummyWriter.msg)
}
}
func TestListenCompletesRunSlowly(t *testing.T) {
// Logger so we can check what was written
stackedWriter := &stackedWriter{}
logger := logrus.New()
logger.Level = logrus.DebugLevel
logger.Out = stackedWriter
// Custom config so we can stop any consumers starting
cfg := goller.NewDefaultConfig("", 1)
cfg.RunSlowly(11 * time.Millisecond)
cfg.Consumer.RunOnce = true
w := goller.NewFromConfig(&receiveSQSClient{}, cfg)
w.WithLogger(logger)
w.Listen(context.Background(), func(ctx context.Context, j goller.Job) error {
return nil
})
errLog := stackedWriter.msgs[len(stackedWriter.msgs)-2]
if !strings.Contains(errLog, "level=debug") {
t.Errorf("expected `level=debug` in the log but saw `%s`", errLog)
}
if !strings.Contains(errLog, "`run-slowly` kicking in") {
t.Errorf("expected \"`run-slowly` kicking in\" in the log but saw `%s`", errLog)
}
if !strings.Contains(errLog, "sleep=11ms") {
t.Errorf("expected `sleep=10ms` in the log but saw `%s`", errLog)
}
}
func TestListenLogsContextClosed(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
// Logger so we can check what was written
dummyWriter := &dummyWriter{}
logger := logrus.New()
logger.Level = logrus.DebugLevel
logger.Out = dummyWriter
// Custom config so we can stop any consumers starting
cfg := goller.NewDefaultConfig("", 0)
w := goller.NewFromConfig(sqs.New(aws.Config{}), cfg)
w.WithLogger(logger)
w.Listen(ctx, func(ctx context.Context, j goller.Job) error {
return nil
})
cancel()
// Give enough time to flush the log
<-time.Tick(10 * time.Millisecond)
if !strings.Contains(dummyWriter.ReadMsg(), "shutting down") {
t.Errorf("expected shutdown message but saw `%s`", dummyWriter.msg)
}
}
type receiveErroredSQSClient struct {
sqsiface.SQSAPI
Input *sqs.ReceiveMessageInput
Output sqs.ReceiveMessageOutput
err error
}
func (c *receiveErroredSQSClient) ReceiveMessageRequest(input *sqs.ReceiveMessageInput) sqs.ReceiveMessageRequest {
c.Input = input
if c.err == nil {
c.err = errors.New("i just errored")
}
return sqs.ReceiveMessageRequest{
Request: &aws.Request{
Error: c.err,
},
}
}
type receiveSQSClient struct {
sqsiface.SQSAPI
Input *sqs.ReceiveMessageInput
Output sqs.ReceiveMessageOutput
}
func (c *receiveSQSClient) ReceiveMessageRequest(input *sqs.ReceiveMessageInput) sqs.ReceiveMessageRequest {
c.Input = input
return sqs.ReceiveMessageRequest{
Request: &aws.Request{
Data: &c.Output,
},
}
}
func TestReceiveMessageRequestTakesCorrectParams(t *testing.T) {
svc := &receiveSQSClient{}
cfg := goller.NewDefaultConfig("https://foo/bar", 1)
cfg.RunOnce()
// Customise from defaults
cfg.Consumer.RetrievalMaxNumberOfMessages = 7
cfg.QueueURL = "eggs"
cfg.Consumer.RetrievalVisibilityTimeout = 130
cfg.Consumer.RetrievalWaitTimeSeconds = 11
listenOnce(svc, nil, cfg)
if aws.Int64Value(svc.Input.MaxNumberOfMessages) != 7 {
t.Errorf("expected MaxNumberOfMessages to be `7` but got `%d`", aws.Int64Value(svc.Input.MaxNumberOfMessages))
}
if aws.StringValue(svc.Input.QueueUrl) != "eggs" {
t.Errorf("expected QueueUrl to be `eggs` but got `%s`", aws.StringValue(svc.Input.QueueUrl))
}
if aws.Int64Value(svc.Input.VisibilityTimeout) != 130 {
t.Errorf("expected VisibilityTimeout to be `130` but got `%d`", aws.Int64Value(svc.Input.VisibilityTimeout))
}
if aws.Int64Value(svc.Input.WaitTimeSeconds) != 11 {
t.Errorf("expected VisibilityTimeout to be `11` but got `%d`", aws.Int64Value(svc.Input.WaitTimeSeconds))
}
}
func TestReceiveMessageErroringLogsAWSCode(t *testing.T) {
expected := awserr.New("520", "aws error peeps", nil)
svc := &receiveErroredSQSClient{err: expected}
stackedWriter := &stackedWriter{}
logger := logrus.New()
logger.Out = stackedWriter
listenOnce(svc, logger, nil)
errLog := stackedWriter.msgs[len(stackedWriter.msgs)-2]
if !strings.Contains(errLog, "level=error") {
t.Fail()
}
if !strings.Contains(errLog, fmt.Sprintf("code=%s", expected.Code())) {
t.Fail()
}
if !strings.Contains(errLog, fmt.Sprintf("error=\"%s\"", expected.Message())) {
t.Fail()
}
}
func TestReceiveMessageErroringLogs(t *testing.T) {
svc := &receiveErroredSQSClient{err: errors.New("one two kayak")}
stackedWriter := &stackedWriter{}
logger := logrus.New()
logger.Out = stackedWriter
listenOnce(svc, logger, nil)
errLog := stackedWriter.msgs[len(stackedWriter.msgs)-2]
if !strings.Contains(errLog, "level=error") {
t.Fail()
}
if !strings.Contains(errLog, "error=\"one two kayak\"") {
t.Fail()
}
}
func TestUnhandledJob(t *testing.T) {
// Build up service to return messages
svc := &receiveSQSClient{
Output: sqs.ReceiveMessageOutput{
Messages: []sqs.Message{
{
MessageId: aws.String("abc123"),
Body: aws.String("hello from test"),
},
},
},
}
listenOnce(svc, nil, nil)
metricFamilies, err := prometheus.DefaultGatherer.Gather()
if err != nil {
t.Fail()
return
}
expectedMetric := "goller_job_error_total"
var metricValue float64
for _, metricFamily := range metricFamilies {
if *metricFamily.Name != expectedMetric {
continue
}
metricValue = metricFamily.Metric[0].Counter.GetValue()
}
if metricValue != 1 {
t.Errorf("expected error metric to be incremented but got `%f`", metricValue)
}
}
func listenOnce(svc sqsiface.SQSAPI, logger *logrus.Logger, cfg *goller.Config) {
if logger == nil {
logger = logrus.New()
logger.Out = &stackedWriter{}
}
logger.Level = logrus.DebugLevel
if cfg == nil {
cfg = goller.NewDefaultConfig("foo", 1)
cfg.RunOnce()
}
w := goller.NewFromConfig(svc, cfg)
w.WithLogger(logger)
w.Listen(context.Background(), func(ctx context.Context, j goller.Job) error {
return nil
})
}