-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathconsumer_test.go
216 lines (187 loc) · 5.81 KB
/
consumer_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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package gokini
import (
"fmt"
"strings"
"sync"
"testing"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/kinesis"
"github.com/aws/aws-sdk-go/service/kinesis/kinesisiface"
)
type testConsumer struct {
ShardID string
Records []*Records
IsShutdown bool
}
func (tc *testConsumer) Init(shardID string) error {
tc.ShardID = shardID
return nil
}
func (tc *testConsumer) ProcessRecords(records []*Records, consumer *KinesisConsumer) {
tc.Records = append(tc.Records, records...)
}
func (tc *testConsumer) Shutdown() {
tc.IsShutdown = true
return
}
type mockKinesisClient struct {
kinesisiface.KinesisAPI
NumberRecordsBeforeClosing int
numberRecordsSent int
getShardIteratorCalled bool
RecordData []byte
numShards int
}
func (k *mockKinesisClient) GetShardIterator(args *kinesis.GetShardIteratorInput) (*kinesis.GetShardIteratorOutput, error) {
k.getShardIteratorCalled = true
return &kinesis.GetShardIteratorOutput{
ShardIterator: aws.String("0123456789ABCDEF"),
}, nil
}
func (k *mockKinesisClient) DescribeStream(args *kinesis.DescribeStreamInput) (*kinesis.DescribeStreamOutput, error) {
shards := []*kinesis.Shard{}
for i := 0; i < k.numShards; i++ {
shards = append(shards, &kinesis.Shard{
ShardId: aws.String(fmt.Sprintf("0000000%d", i)),
})
}
return &kinesis.DescribeStreamOutput{
StreamDescription: &kinesis.StreamDescription{
StreamStatus: aws.String("ACTIVE"),
Shards: shards,
HasMoreShards: aws.Bool(false),
},
}, nil
}
type mockCheckpointer struct {
checkpointFound bool
checkpoint map[string]*shardStatus
checkpointerCalled bool
sync.Mutex
}
func (c *mockCheckpointer) Init() error {
c.checkpoint = make(map[string]*shardStatus)
return nil
}
func (c *mockCheckpointer) GetLease(shard *shardStatus, assignTo string) error {
shard.Lock()
shard.AssignedTo = assignTo
shard.LeaseTimeout = time.Now()
shard.Unlock()
return nil
}
func (c *mockCheckpointer) CheckpointSequence(shard *shardStatus) error {
c.Lock()
defer c.Unlock()
c.checkpoint[shard.ID] = shard
c.checkpointerCalled = true
return nil
}
func (c *mockCheckpointer) FetchCheckpoint(shard *shardStatus) error {
if c.checkpointFound {
if checkpointShard, ok := c.checkpoint[shard.ID]; ok {
shard.Checkpoint = checkpointShard.Checkpoint
shard.AssignedTo = checkpointShard.AssignedTo
} else {
shard.Checkpoint = "ABCD124"
shard.AssignedTo = "abcdef-1234567"
}
}
return nil
}
func (c *mockCheckpointer) ListActiveWorkers() (map[string][]string, error) {
return nil, nil
}
func (c *mockCheckpointer) ClaimShard(*shardStatus, string) error {
return nil
}
func (k *mockKinesisClient) GetRecords(args *kinesis.GetRecordsInput) (*kinesis.GetRecordsOutput, error) {
k.numberRecordsSent++
var nextShardIterator *string
if k.NumberRecordsBeforeClosing == 0 || k.numberRecordsSent < k.NumberRecordsBeforeClosing {
nextShardIterator = aws.String("ABCD1234")
}
return &kinesis.GetRecordsOutput{
MillisBehindLatest: aws.Int64(0),
NextShardIterator: nextShardIterator,
Records: []*kinesis.Record{
&kinesis.Record{
Data: k.RecordData,
PartitionKey: aws.String("abcdefg"),
SequenceNumber: aws.String(strings.Join([]string{"0000", string(k.numberRecordsSent)}, "")),
},
},
}, nil
}
func createConsumer(t *testing.T, numRecords int, checkpointFound bool, shutdown bool) (consumer *testConsumer, kinesisSvc *mockKinesisClient, checkpointer *mockCheckpointer) {
consumer = &testConsumer{}
kinesisSvc = &mockKinesisClient{
NumberRecordsBeforeClosing: numRecords,
RecordData: []byte("Hello World"),
numShards: 1,
}
checkpointer = &mockCheckpointer{
checkpointFound: checkpointFound,
}
kc := &KinesisConsumer{
StreamName: "FOO",
ShardIteratorType: "TRIM_HORIZON",
RecordConsumer: consumer,
checkpointer: checkpointer,
svc: kinesisSvc,
eventLoopSleepMs: 1,
}
err := kc.StartConsumer()
if err != nil {
t.Fatalf("Got unexpected error from StartConsumer: %s", err)
}
time.Sleep(200 * time.Millisecond)
if shutdown {
kc.Shutdown()
}
return
}
func TestStartConsumer(t *testing.T) {
consumer, kinesisSvc, _ := createConsumer(t, 1, false, true)
if consumer.ShardID != "00000000" {
t.Errorf("Expected shardId to be set to 00000000, but got: %s", consumer.ShardID)
}
if len(consumer.Records) != 1 {
t.Fatalf("Expected there to be one record from Kinesis, got %d", len(consumer.Records))
}
if string(consumer.Records[0].Data) != "Hello World" {
t.Errorf("Expected record to be \"Hello World\", got %s", consumer.Records[0].Data)
}
if string(consumer.Records[0].ShardID) != "00000000" {
t.Errorf("Expected Shard ID to be \"00000000\", got %s", consumer.Records[0].ShardID)
}
if !consumer.IsShutdown {
t.Errorf("Expected consumer to be shutdown but it was not")
}
consumer, kinesisSvc, _ = createConsumer(t, 2, true, true)
if len(consumer.Records) != 2 {
t.Errorf("Expected there to be two records from Kinesis, got %v", consumer.Records)
}
if !kinesisSvc.getShardIteratorCalled {
t.Errorf("Expected shard iterator to be called, but it was not")
}
if !consumer.IsShutdown {
t.Errorf("Expected consumer to be shutdown but it was not")
}
consumer, kinesisSvc, _ = createConsumer(t, 1, true, true)
if !kinesisSvc.getShardIteratorCalled {
t.Errorf("Expected shard iterator not to be called, but it was")
}
}
func TestScaleDownShards(t *testing.T) {
consumer, kinesisSvc, _ := createConsumer(t, 0, false, false)
kinesisSvc.numShards = 2
time.Sleep(10 * time.Millisecond)
// Shards don't just "dissapear" they rather just return a nil nextIteratorShard
kinesisSvc.NumberRecordsBeforeClosing = 1
time.Sleep(10 * time.Millisecond)
if consumer.IsShutdown {
t.Errorf("Expected consumer to not be shutdown but it was")
}
}