-
Notifications
You must be signed in to change notification settings - Fork 1
/
consumer_test.go
110 lines (91 loc) · 2.74 KB
/
consumer_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
package consumer
import (
"errors"
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/endpoints"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/sqs"
"math/rand"
"syscall"
"testing"
"time"
)
func TestConsumeQueueEventSuccessfully(t *testing.T) {
queueURL := "http://localhost:4566/000000000000/my-queue"
sess := session.Must(session.NewSession(&aws.Config{
Credentials: credentials.NewStaticCredentials("not", "empty", ""),
DisableSSL: aws.Bool(true),
Region: aws.String(endpoints.UsEast1RegionID),
Endpoint: aws.String("http://localhost:4566"),
}))
q := sqs.New(sess)
sendMessage(q, queueURL)
c := New(queueURL, func(m *sqs.Message) error {
_ = syscall.Kill(syscall.Getpid(), syscall.SIGINT)
return nil
}, &Config{
AwsSession: sess,
SqsMaxNumberOfMessages: 10,
SqsMessageVisibilityTimeout: 10,
Receivers: 1,
PollDelayInMilliseconds: 100,
})
c.Start()
empty := isQueueEmpty(q, queueURL)
if !empty {
t.Errorf("Queue is not empty, expected %v, got %v", true, empty)
}
}
func TestWithErrorWhileProcessingMessage(t *testing.T) {
queueURL := "http://localhost:4566/000000000000/my-queue"
sess := session.Must(session.NewSession(&aws.Config{
Credentials: credentials.NewStaticCredentials("not", "empty", ""),
DisableSSL: aws.Bool(true),
Region: aws.String(endpoints.UsEast1RegionID),
Endpoint: aws.String("http://localhost:4566"),
}))
q := sqs.New(sess)
sendMessage(q, queueURL)
c := New(queueURL, func(m *sqs.Message) error {
go func() {
time.Sleep(time.Millisecond * 200)
_ = syscall.Kill(syscall.Getpid(), syscall.SIGINT)
}()
return errors.New("database offline")
}, &Config{
AwsSession: sess,
SqsMaxNumberOfMessages: 10,
SqsMessageVisibilityTimeout: 0,
Receivers: 1,
PollDelayInMilliseconds: 100,
})
c.Start()
empty := isQueueEmpty(q, queueURL)
if empty {
t.Errorf("Queue is empty, expected %v, got %v", false, empty)
}
}
func sendMessage(queue *sqs.SQS, queueURL string) {
s1 := rand.NewSource(time.Now().UnixNano())
r1 := rand.New(s1)
_, err := queue.SendMessage(&sqs.SendMessageInput{
DelaySeconds: aws.Int64(0),
MessageBody: aws.String(fmt.Sprintf("Message %d", r1.Intn(100))),
QueueUrl: &queueURL,
})
if err != nil {
fmt.Println("Could not send message:", err)
}
}
func isQueueEmpty(queue *sqs.SQS, queueURL string) bool {
sizeAttribute := "ApproximateNumberOfMessages"
output, _ := queue.GetQueueAttributes(&sqs.GetQueueAttributesInput{
AttributeNames: []*string{
aws.String(sizeAttribute),
},
QueueUrl: &queueURL,
})
return *output.Attributes[sizeAttribute] == "0"
}