-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgoamz_test.go
107 lines (93 loc) · 2.15 KB
/
goamz_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
package main
import (
"fmt"
"testing"
"time"
"github.com/goamz/goamz/aws"
"github.com/goamz/goamz/sqs"
)
func TestGoamzSQS(t *testing.T) {
Setup()
r := aws.Region{SQSEndpoint: fmt.Sprintf("http://localhost:%d", *port)}
s := sqs.New(aws.Auth{AccessKey: "x", SecretKey: "x"}, r)
qname := "hello"
_, err := s.CreateQueue(qname)
if err != nil {
t.Fatal(err)
}
q, err := s.GetQueue(qname)
if err != nil {
t.Fatal(err)
}
m := receive(t, q)
if len(m.Messages) != 0 {
t.Fatalf("messages: %d, expected 0", len(m.Messages))
}
body := "qoooo"
send(t, q, body)
m = receive(t, q)
if len(m.Messages) != 1 {
t.Fatalf("messages: %d, expected 1", len(m.Messages))
}
if m.Messages[0].Body != body {
t.Fatalf("message body: %q, expected %q", m.Messages[0].Body, body)
}
m = receive(t, q)
if len(m.Messages) != 0 {
t.Fatalf("messages: %d, expected 0", len(m.Messages))
}
body2 := "asdf"
send(t, q, body2)
m = receive(t, q)
if len(m.Messages) != 1 {
t.Fatalf("messages: %d, expected 1", len(m.Messages))
}
if m.Messages[0].Body != body2 {
t.Fatalf("message body: %q, expected %q", m.Messages[0].Body, body2)
}
deleteAll(t, q, m)
body3 := "zxcv"
go func() {
time.Sleep(20 * time.Millisecond)
send(t, q, body3)
}()
m = receiveWait(t, q)
if len(m.Messages) != 1 {
t.Fatalf("messages: %d, expected 1", len(m.Messages))
}
if m.Messages[0].Body != body3 {
t.Fatalf("message body: %q, expected %q", m.Messages[0].Body, body3)
}
}
func send(t *testing.T, q *sqs.Queue, body string) {
_, err := q.SendMessage(body)
if err != nil {
t.Fatal(err)
}
}
func receive(t *testing.T, q *sqs.Queue) *sqs.ReceiveMessageResponse {
m, err := q.ReceiveMessage(1)
if err != nil {
t.Fatal(err)
}
return m
}
func receiveWait(t *testing.T, q *sqs.Queue) *sqs.ReceiveMessageResponse {
params := map[string]string{
"MaxNumberOfMessages": "1",
"WaitTimeSeconds": "2",
}
m, err := q.ReceiveMessageWithParameters(params)
if err != nil {
t.Fatal(err)
}
return m
}
func deleteAll(t *testing.T, q *sqs.Queue, r *sqs.ReceiveMessageResponse) {
for _, mm := range r.Messages {
_, err := q.DeleteMessage(&mm)
if err != nil {
t.Fatal(err)
}
}
}