/
test_util.go
252 lines (201 loc) · 6.69 KB
/
test_util.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
package triton
// Mock Kinesis Service
import (
"bytes"
"fmt"
"log"
"strings"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/kinesis"
"github.com/tinylib/msgp/msgp"
)
type testKinesisRecords struct {
sn string
recordData [][]byte
}
type testKinesisShard struct {
records []testKinesisRecords
}
func (s *testKinesisShard) AddRecord(sn string, rec Record) {
b := bytes.NewBuffer(make([]byte, 0, 1024))
w := msgp.NewWriter(b)
err := w.WriteMapStrIntf(rec)
if err != nil {
panic(err)
}
w.Flush()
s.AddData(sn, b.Bytes())
}
func (s *testKinesisShard) AddData(sn string, data []byte) {
rs := testKinesisRecords{sn, [][]byte{data}}
s.records = append(s.records, rs)
}
func (s *testKinesisShard) PopData() (string, []byte) {
r := s.records[0]
s.records = s.records[1:]
return r.sn, r.recordData[0]
}
func (s *testKinesisShard) PopRecord() (string, Record) {
sn, data := s.PopData()
b := bytes.NewBuffer(data)
r := make(Record)
w := msgp.NewReader(b)
w.ReadMapStrIntf(r)
return sn, r
}
func (s *testKinesisShard) NextSequenceNumber() string {
return time.Now().String()
}
func newTestKinesisShard() *testKinesisShard {
return &testKinesisShard{make([]testKinesisRecords, 0)}
}
type testKinesisStream struct {
StreamName string
shards map[string]*testKinesisShard
}
func (s *testKinesisStream) AddShard(sid string, ts *testKinesisShard) {
s.shards[sid] = ts
}
func newTestKinesisStream(name string) *testKinesisStream {
return &testKinesisStream{name, make(map[string]*testKinesisShard)}
}
type testKinesisService struct {
streams map[string]*testKinesisStream
}
func newTestKinesisService() *testKinesisService {
return &testKinesisService{make(map[string]*testKinesisStream)}
}
func (s *testKinesisService) AddStream(stream *testKinesisStream) {
s.streams[stream.StreamName] = stream
}
func parseIterator(iterVal string) (string, string, string) {
vals := strings.Split(iterVal, ":")
return vals[0], vals[1], vals[2]
}
func (s *testKinesisService) GetShardIterator(i *kinesis.GetShardIteratorInput) (*kinesis.GetShardIteratorOutput, error) {
iterVal := fmt.Sprintf("%s:%s:%s", *i.StreamName, *i.ShardId, "")
gso := &kinesis.GetShardIteratorOutput{ShardIterator: aws.String(iterVal)}
return gso, nil
}
func (s *testKinesisService) GetRecords(gri *kinesis.GetRecordsInput) (*kinesis.GetRecordsOutput, error) {
streamName, shardID, sn := parseIterator(*gri.ShardIterator)
records := []*kinesis.Record{}
stream, ok := s.streams[streamName]
if !ok {
return nil, fmt.Errorf("Failed to find stream")
}
shard, ok := stream.shards[shardID]
if !ok {
return nil, fmt.Errorf("Failed to find shard")
}
// For our mock implementation, we just assume iterator == sequence number
nextSn := ""
for _, r := range shard.records {
if r.sn > sn {
for _, rd := range r.recordData {
records = append(records, &kinesis.Record{SequenceNumber: aws.String(string(r.sn)), Data: rd})
}
nextSn = string(r.sn)
break
}
}
// If we didn't find a new next iterator, just keep the original
nextIter := *gri.ShardIterator
if nextSn != "" {
nextIter = fmt.Sprintf("%s:%s:%s", streamName, shardID, nextSn)
}
log.Printf("%s - serving %d records. Next iter %s", *gri.ShardIterator, len(records), nextIter)
gso := &kinesis.GetRecordsOutput{
NextShardIterator: aws.String(nextIter),
MillisBehindLatest: aws.Int64(0),
Records: records,
}
return gso, nil
}
func (s *testKinesisService) DescribeStream(input *kinesis.DescribeStreamInput) (*kinesis.DescribeStreamOutput, error) {
stream, ok := s.streams[*input.StreamName]
if !ok {
// TODO: Probably a real error condition we could simulate
return nil, fmt.Errorf("Failed to find stream")
}
var shards []*kinesis.Shard
for sid := range stream.shards {
shards = append(shards, &kinesis.Shard{ShardId: aws.String(string(sid))})
}
dso := &kinesis.DescribeStreamOutput{
StreamDescription: &kinesis.StreamDescription{
Shards: shards,
StreamARN: aws.String("test"),
StreamName: input.StreamName,
StreamStatus: aws.String("ACTIVE"),
},
}
return dso, nil
}
func (s *testKinesisService) PutRecords(input *kinesis.PutRecordsInput) (*kinesis.PutRecordsOutput, error) {
stream, ok := s.streams[*input.StreamName]
if !ok {
return nil, fmt.Errorf("Failed to find stream")
}
records := make([]*kinesis.PutRecordsResultEntry, len(input.Records))
for i, r := range input.Records {
shard, ok := stream.shards[*r.PartitionKey]
if !ok {
return nil, fmt.Errorf("Failed to find shard")
}
sn := shard.NextSequenceNumber()
shard.AddData(sn, r.Data)
records[i] = &kinesis.PutRecordsResultEntry{
SequenceNumber: aws.String(string(sn)),
ShardId: r.PartitionKey,
}
}
output := &kinesis.PutRecordsOutput{
Records: records,
FailedRecordCount: aws.Int64(0),
}
return output, nil
}
type nullKinesisService struct{}
func (s *nullKinesisService) GetShardIterator(*kinesis.GetShardIteratorInput) (*kinesis.GetShardIteratorOutput, error) {
gso := &kinesis.GetShardIteratorOutput{ShardIterator: aws.String("123")}
return gso, nil
}
func (s *nullKinesisService) GetRecords(*kinesis.GetRecordsInput) (*kinesis.GetRecordsOutput, error) {
records := []*kinesis.Record{}
gso := &kinesis.GetRecordsOutput{
NextShardIterator: aws.String("124"),
MillisBehindLatest: aws.Int64(0),
Records: records,
}
return gso, nil
}
func (s *nullKinesisService) DescribeStream(input *kinesis.DescribeStreamInput) (*kinesis.DescribeStreamOutput, error) {
return nil, fmt.Errorf("Not Implemented")
}
func (s *nullKinesisService) PutRecords(input *kinesis.PutRecordsInput) (*kinesis.PutRecordsOutput, error) {
results := []*kinesis.PutRecordsResultEntry{}
gso := &kinesis.PutRecordsOutput{
Records: results,
FailedRecordCount: aws.Int64(0),
}
return gso, nil
}
type failingKinesisService struct{}
func (s *failingKinesisService) DescribeStream(input *kinesis.DescribeStreamInput) (*kinesis.DescribeStreamOutput, error) {
return nil, fmt.Errorf("Not Implemented")
}
func (s *failingKinesisService) GetRecords(*kinesis.GetRecordsInput) (*kinesis.GetRecordsOutput, error) {
err := awserr.New("ProvisionedThroughputExceededException", "slow down dummy", fmt.Errorf("error"))
return nil, err
}
func (s *failingKinesisService) GetShardIterator(*kinesis.GetShardIteratorInput) (*kinesis.GetShardIteratorOutput, error) {
gso := &kinesis.GetShardIteratorOutput{ShardIterator: aws.String("123")}
return gso, nil
}
func (s *failingKinesisService) PutRecords(input *kinesis.PutRecordsInput) (*kinesis.PutRecordsOutput, error) {
err := awserr.New("ProvisionedThroughputExceededException", "slow down dummy", fmt.Errorf("error"))
return nil, err
}