forked from italolelis/outboxer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sqs_test.go
113 lines (98 loc) · 3.04 KB
/
sqs_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
package sqs
import (
"context"
"errors"
"fmt"
"os"
"testing"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/sqs"
"github.com/italolelis/outboxer"
)
func TestSQS_EventStream(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
endpoint := os.Getenv("SQS_ENDPOINT")
if endpoint == "" {
endpoint = "http://localhost:4566"
}
sess, err := session.NewSession(&aws.Config{
CredentialsChainVerboseErrors: aws.Bool(true),
Credentials: credentials.NewStaticCredentials("foo", "var", ""),
Endpoint: aws.String(endpoint),
Region: aws.String("us-east-1"),
})
if err != nil {
t.Fatalf("failed to setup an aws session: %s", err)
}
queueName := aws.String("test")
queueArn := aws.String(fmt.Sprintf("%v/000000000000/%v", endpoint, *queueName))
sqsClient := sqs.New(sess)
if _, err := sqsClient.CreateQueueWithContext(ctx, &sqs.CreateQueueInput{QueueName: queueName}); err != nil {
var kErr awserr.Error
if errors.As(err, &kErr) {
if kErr.Code() == "ResourceInUseException" {
t.Log(kErr.Message())
}
} else {
t.Fatalf("failed to create queue: %s", err)
}
}
es := New(sqsClient)
if err := es.Send(ctx, &outboxer.OutboxMessage{
Payload: []byte("test payload"),
Options: map[string]interface{}{
QueueNameOption: *queueArn,
},
}); err != nil {
t.Fatalf("an error was not expected: %s", err)
}
}
func TestSQSFIFO_EventStream(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
endpoint := os.Getenv("SQS_ENDPOINT")
if endpoint == "" {
endpoint = "http://localhost:4566"
}
sess, err := session.NewSession(&aws.Config{
CredentialsChainVerboseErrors: aws.Bool(true),
Credentials: credentials.NewStaticCredentials("foo", "var", ""),
Endpoint: aws.String(endpoint),
Region: aws.String("us-east-1"),
})
if err != nil {
t.Fatalf("failed to setup an aws session: %s", err)
}
queueName := aws.String("test.fifo")
queueArn := aws.String(fmt.Sprintf("%v/000000000000/%v", endpoint, *queueName))
groupId := aws.String("123")
attrs := map[string]*string{"FifoQueue": aws.String("true")}
sqsClient := sqs.New(sess)
if _, err := sqsClient.CreateQueueWithContext(ctx, &sqs.CreateQueueInput{QueueName: queueName, Attributes: attrs}); err != nil {
var kErr awserr.Error
if errors.As(err, &kErr) {
if kErr.Code() == "ResourceInUseException" {
t.Log(kErr.Message())
}
} else {
t.Fatalf("failed to create queue: %s", err)
}
}
es := New(sqsClient)
if err := es.Send(ctx, &outboxer.OutboxMessage{
Payload: []byte("test fifo payload"),
Options: map[string]interface{}{
QueueNameOption: *queueArn,
MessageGroupIdOption: *groupId,
},
Headers: map[string]interface{}{
"HeaderKey": *groupId,
},
}); err != nil {
t.Fatalf("an error was not expected: %s", err)
}
}