-
Notifications
You must be signed in to change notification settings - Fork 9
/
sqs_client_test.go
136 lines (113 loc) · 2.86 KB
/
sqs_client_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
//go:build sqsint
package main
import (
"testing"
"github.com/goamz/goamz/sqs"
"github.com/stretchr/testify/require"
)
// These tests are hardcoded to point to actual sqs queues and sns topics.
// You should think of them as "integration" tests, and in general you don't
// want to be running these all the time. You will need to provide your own
// credentials and queue below to run these.
var config = AWSConfig{
AccessKey: "",
SecretKey: "",
Region: "",
}
var queueName = ""
func queueHandle() *sqs.Queue {
sqsHandle, err := sqs.NewFrom(config.AccessKey, config.SecretKey, config.Region)
if err != nil {
panic(err)
}
queue, err := sqsHandle.GetQueue(queueName)
if err != nil {
panic(err)
}
return queue
}
func TestSQS_Init(t *testing.T) {
_, err := NewAWSSQSClient(config, queueName)
require.NoError(t, err)
// wrong region
_, err = NewAWSSQSClient(
AWSConfig{
AccessKey: config.AccessKey,
SecretKey: config.SecretKey,
Region: "us.best",
},
queueName,
)
require.Error(t, err)
// wrong creds
_, err = NewAWSSQSClient(
AWSConfig{
AccessKey: "super",
SecretKey: "secret",
Region: config.Region,
},
queueName,
)
require.Error(t, err)
// wrong queue
_, err = NewAWSSQSClient(
AWSConfig{
AccessKey: config.AccessKey,
SecretKey: config.SecretKey,
Region: config.Region,
},
"fake-queue", // hopefully nobody ever makes this
)
require.Error(t, err)
}
func TestSQS_FetchDelete(t *testing.T) {
recd := map[string]int{
"foo": 0,
"bar": 0,
"baz": 0,
}
queue := queueHandle()
_, err := queue.SendMessage("foo")
require.NoError(t, err)
_, err = queue.SendMessage("bar")
require.NoError(t, err)
_, err = queue.SendMessage("baz")
require.NoError(t, err)
client, err := NewAWSSQSClient(config, queueName)
require.NoError(t, err)
// Loop over and read from the queue unitl there are no messages left.
// Doing it this way because even though we set max messages to 10, it
// seems that aws almost always gives us back only one anyway
for {
messages, err := client.Fetch()
require.NoError(t, err)
if len(messages) == 0 {
break
}
for _, msg := range messages {
recd[msg.Body] += 1
err := client.Delete(msg)
require.NoError(t, err)
}
}
require.Equal(t, recd["foo"], 1)
require.Equal(t, recd["bar"], 1)
require.Equal(t, recd["baz"], 1)
}
func TestSQS_FetchMany(t *testing.T) {
queue := queueHandle()
// We're filling up the queue to ensure that a call to Fetch will
// actually return 10 messages. More sanity than anything else, don't
// be too concerned if this fails
for i := 0; i < 100; i++ {
_, err := queue.SendMessage("foo")
require.NoError(t, err)
}
client, err := NewAWSSQSClient(config, queueName)
require.NoError(t, err)
messages, err := client.Fetch()
require.NoError(t, err)
require.Equal(t, len(messages), 10)
_, err = queue.Purge()
require.NoError(t, err)
}