This repository has been archived by the owner on Sep 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
kinesis_test.go
132 lines (114 loc) · 3.22 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
package main
import (
"log"
"os"
"reflect"
"testing"
)
func TestChunk(t *testing.T) {
testText := "apple"
expectedChunks := []string{"ap", "pl", "e"}
actualChunks := chunkData(testText, 2)
if len(actualChunks) != len(expectedChunks) {
t.Errorf("Expected %d chunks, but got %d", len(expectedChunks), len(actualChunks))
}
for ind := range actualChunks {
if actualChunks[ind] != expectedChunks[ind] {
t.Errorf("index=%d:\nExpected chunk=%s, but got chunk=%s", ind, expectedChunks[ind], actualChunks[ind])
}
}
}
func TestGetRegionOverride(t *testing.T) {
err := os.Setenv("AWS_REGION", "")
if err != nil {
t.Fatalf("Could not set environment variable for test: %v\n", err)
}
defaultName := getRegion()
expectedDefault := "us-west-2"
if defaultName != expectedDefault {
t.Errorf("Expected default region=%s, actual=%s", expectedDefault, defaultName)
}
err = os.Setenv("AWS_REGION", "foobar")
if err != nil {
t.Errorf("Could not set env var: %v", err)
}
name := getRegion()
if name != "foobar" {
t.Errorf("Stream name was %s", name)
}
}
func TestStreamHasSSEError(t *testing.T) {
mockKinesis := &mockKinesisClient{}
hasSSE, err := streamHasSSE("simulate_empty_response", mockKinesis)
if err == nil {
t.Errorf("Expected error, but got <nil>")
}
if hasSSE {
t.Errorf("Expected 'hasSSE' == FALSE, but was TRUE")
}
hasSSE, err = streamHasSSE("success", mockKinesis)
if err != nil {
t.Errorf("Expected no error, but got %s", err)
}
if !hasSSE {
t.Errorf("Expected 'hasSSE' == TRUE, but was FALSE")
}
}
func TestBuildMessages(t *testing.T) {
expected := EventChunk{
ChunkNumber: 0,
NumChunks: 1,
UUID: "",
Data: "foobar",
}
messages := buildMessages("foobar")
if len(messages) != 1 {
log.Fatalf("Expected 1 message, got %d", len(messages))
}
first := messages[0]
expected.UUID = first.UUID
if !reflect.DeepEqual(first, expected) {
log.Fatalf("Kinesis message is not the expected, got %v", first)
}
}
func TestSendToStreamMarshalError(t *testing.T) {
mockKinesis := &mockKinesisClient{}
wrapper := &kinesisWrapper{client: mockKinesis}
// json.Marshal can't Marshal certain types, like channels
err := wrapper.sendToStream(make(chan int), "test")
if mockKinesis.timesCalled > 0 {
t.Error("Expected mock Kinesis client not to be called, but it was")
}
if err == nil {
t.Error("Expected an error, but got <nil>")
}
}
func TestSendToStreamKinesisError(t *testing.T) {
mockKinesis := &mockKinesisClient{}
wrapper := &kinesisWrapper{client: mockKinesis}
err := wrapper.sendToStream(`{"data": "test"}`, "simulate_error")
if mockKinesis.timesCalled == 0 {
t.Error("Expected mock Kinesis client to be called, but it was NOT")
}
if err == nil {
t.Errorf("Expected error, but got <nil>")
}
}
func TestSendToStreamKinesisSuccess(t *testing.T) {
mockKinesis := &mockKinesisClient{}
wrapper := &kinesisWrapper{
client: mockKinesis,
logger: nopLog,
}
err := wrapper.sendToStream(`{"data": "test"}`, "test")
if mockKinesis.timesCalled == 0 {
t.Error("Expected mock Kinesis client to be called, but it was NOT")
}
if err != nil {
t.Errorf("Expected no error, but got %s", err)
}
}
func TestFlushKinesisBuffer(t *testing.T) {
wrapper := onlineHandler{}
wrapper.flushBuffer()
}