forked from sendgridlabs/go-kinesis
-
Notifications
You must be signed in to change notification settings - Fork 1
/
kinesis_test.go
198 lines (162 loc) · 4.79 KB
/
kinesis_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
package kinesis
import (
"errors"
"fmt"
"os"
"strings"
"testing"
"time"
)
const localEndpoint = "http://127.0.0.1:4567"
func TestKinesisClientInterfaceIsImplemented(t *testing.T) {
var client KinesisClient = &Kinesis{}
if client == nil {
t.Error("Invalid nil kinesis client")
}
}
func TestRegions(t *testing.T) {
os.Setenv(RegionEnvName, "REGION_TEST")
if NewRegionFromEnv() != "REGION_TEST" {
t.Errorf("Invalid value read from the %s environment variable", RegionEnvName)
}
os.Setenv(RegionEnvName, "")
}
func TestAddRecord(t *testing.T) {
args := NewArgs()
args.AddRecord(
[]byte("data"),
"partition_key",
)
if len(args.Records) != 1 {
t.Errorf("%q != %q", len(args.Records), 1)
}
}
func TestListStreams(t *testing.T) {
auth := NewAuth("BAD_ACCESS_KEY", "BAD_SECRET_KEY", "BAD_SECURITY_TOKEN")
client := NewWithEndpoint(auth, USEast1, localEndpoint)
resp, err := client.ListStreams(NewArgs())
if resp == nil {
t.Error("resp == nil")
}
if err != nil {
t.Errorf("%q != nil", err)
}
}
func TestCreateStream(t *testing.T) {
auth := NewAuth("BAD_ACCESS_KEY", "BAD_SECRET_KEY", "BAD_SECURITY_TOKEN")
client := NewWithEndpoint(auth, USEast1, localEndpoint)
streamName := "test2"
err := client.CreateStream(streamName, 1)
if err != nil {
t.Errorf("%q != nil", err)
}
err = waitForStreamStatus(client, streamName, "ACTIVE")
if err != nil {
t.Errorf("%q != nil", err)
}
client.DeleteStream(streamName)
err = waitForStreamDeletion(client, streamName)
if err != nil {
t.Errorf("%q != nil", err)
}
}
// Older, lower-level way to use PutRecord
func TestPutRecordWithAddData(t *testing.T) {
auth := NewAuth("BAD_ACCESS_KEY", "BAD_SECRET_KEY", "BAD_SECURITY_TOKEN")
client := NewWithEndpoint(auth, USEast1, localEndpoint)
streamName := "pizza"
err := createStream(client, streamName, 1)
if err != nil {
t.Errorf("%q != nil", err)
}
args := NewArgs()
args.Add("StreamName", streamName)
args.AddData([]byte("The cheese is old and moldy, where is the bathroom?"))
args.Add("PartitionKey", "key-1")
resp, err := client.PutRecord(args)
if resp == nil {
t.Error("resp == nil")
}
if err != nil {
t.Errorf("%q != nil", err)
}
client.DeleteStream(streamName)
err = waitForStreamDeletion(client, streamName)
if err != nil {
t.Errorf("%q != nil", err)
}
}
// Newer, higher-level way to use PutRecord
func TestPutRecordWithAddRecord(t *testing.T) {
auth := NewAuth("BAD_ACCESS_KEY", "BAD_SECRET_KEY", "BAD_SECURITY_TOKEN")
client := NewWithEndpoint(auth, USEast1, localEndpoint)
streamName := "pizza"
err := createStream(client, streamName, 1)
if err != nil {
t.Errorf("%q != nil", err)
}
args := NewArgs()
args.Add("StreamName", streamName)
args.AddRecord([]byte("The cheese is old and moldy, where is the bathroom?"), "key-1")
resp, err := client.PutRecord(args)
if resp == nil {
t.Error("resp == nil")
}
if err != nil {
t.Errorf("%q != nil", err)
}
client.DeleteStream(streamName)
err = waitForStreamDeletion(client, streamName)
if err != nil {
t.Errorf("%q != nil", err)
}
}
// waitForStreamStatus will poll for a stream status repeatedly, once every MS, for up to 1000 MS,
// blocking until the stream has the desired status. It will return an error if the stream never
// achieves the desired status. If a stream doesn’t exist then an error will be returned.
func waitForStreamStatus(client KinesisClient, streamName string, statusToAwait string) error {
args := NewArgs()
args.Add("StreamName", streamName)
var resp3 *DescribeStreamResp
var err error
for i := 1; i < 1000; i++ {
resp3, err = client.DescribeStream(args)
if err != nil {
return err
}
if resp3.StreamDescription.StreamStatus == statusToAwait {
break
} else {
time.Sleep(1 * time.Millisecond)
}
}
if resp3 == nil {
return errors.New("Could not get Stream Description")
}
if resp3.StreamDescription.StreamStatus != statusToAwait {
return errors.New(fmt.Sprintf("Timed out waiting for stream to enter status %v; last status was %v.", statusToAwait, resp3.StreamDescription.StreamStatus))
}
return nil
}
// waitForStreamDeletion will poll for a stream status repeatedly, once every MS, for up to 1000 MS,
// blocking until the stream has been deleted. It will return an error if the stream is never deleted
// or some other error occurs. If it succeeds then the return value will be nil.
func waitForStreamDeletion(client KinesisClient, streamName string) error {
err := waitForStreamStatus(client, streamName, "FOO")
if !strings.Contains(err.Error(), "not found") {
return err
}
return nil
}
// helper
func createStream(client KinesisClient, streamName string, partitions int) error {
err := client.CreateStream(streamName, partitions)
if err != nil {
return err
}
err = waitForStreamStatus(client, streamName, "ACTIVE")
if err != nil {
return err
}
return nil
}