1
- // Mock Kinesis Service
2
1
package triton
3
2
3
+ // Mock Kinesis Service
4
+
4
5
import (
5
6
"bytes"
6
7
"fmt"
7
8
"log"
8
9
"strings"
10
+ "time"
9
11
10
12
"github.com/aws/aws-sdk-go/aws"
11
13
"github.com/aws/aws-sdk-go/service/kinesis"
@@ -21,18 +23,44 @@ type testKinesisShard struct {
21
23
records []testKinesisRecords
22
24
}
23
25
24
- func (s * testKinesisShard ) AddRecord (sn SequenceNumber , rec map [ string ] interface {} ) {
26
+ func (s * testKinesisShard ) AddRecord (sn SequenceNumber , rec Record ) {
25
27
b := bytes .NewBuffer (make ([]byte , 0 , 1024 ))
26
28
w := msgp .NewWriter (b )
27
29
err := w .WriteMapStrIntf (rec )
28
30
if err != nil {
29
31
panic (err )
30
32
}
31
33
w .Flush ()
32
- rs := testKinesisRecords {sn , [][]byte {b .Bytes ()}}
34
+ s .AddData (sn , b .Bytes ())
35
+ }
36
+
37
+ func (s * testKinesisShard ) AddData (sn SequenceNumber , data []byte ) {
38
+ rs := testKinesisRecords {sn , [][]byte {data }}
33
39
s .records = append (s .records , rs )
34
40
}
35
41
42
+ func (s * testKinesisShard ) PopData () (SequenceNumber , []byte ) {
43
+ r := s .records [0 ]
44
+ s .records = s .records [1 :]
45
+ return r .sn , r .recordData [0 ]
46
+ }
47
+
48
+ func (s * testKinesisShard ) PopRecord () (SequenceNumber , Record ) {
49
+ sn , data := s .PopData ()
50
+
51
+ b := bytes .NewBuffer (data )
52
+ r := make (Record )
53
+
54
+ w := msgp .NewReader (b )
55
+ w .ReadMapStrIntf (r )
56
+
57
+ return sn , r
58
+ }
59
+
60
+ func (s * testKinesisShard ) NextSequenceNumber () SequenceNumber {
61
+ return SequenceNumber (time .Now ().String ())
62
+ }
63
+
36
64
func newTestKinesisShard () * testKinesisShard {
37
65
return & testKinesisShard {make ([]testKinesisRecords , 0 )}
38
66
}
@@ -117,15 +145,15 @@ func (s *testKinesisService) GetRecords(gri *kinesis.GetRecordsInput) (*kinesis.
117
145
}
118
146
119
147
func (s * testKinesisService ) DescribeStream (input * kinesis.DescribeStreamInput ) (* kinesis.DescribeStreamOutput , error ) {
120
- shards := make ([]* kinesis.Shard , 0 )
121
148
122
149
stream , ok := s .streams [* input .StreamName ]
123
150
if ! ok {
124
151
// TODO: Probably a real error condition we could simulate
125
152
return nil , fmt .Errorf ("Failed to find stream" )
126
153
}
127
154
128
- for sid , _ := range stream .shards {
155
+ var shards []* kinesis.Shard
156
+ for sid := range stream .shards {
129
157
shards = append (shards , & kinesis.Shard {ShardId : aws .String (string (sid ))})
130
158
}
131
159
@@ -140,3 +168,33 @@ func (s *testKinesisService) DescribeStream(input *kinesis.DescribeStreamInput)
140
168
141
169
return dso , nil
142
170
}
171
+
172
+ func (s * testKinesisService ) PutRecords (input * kinesis.PutRecordsInput ) (* kinesis.PutRecordsOutput , error ) {
173
+ stream , ok := s .streams [* input .StreamName ]
174
+ if ! ok {
175
+ return nil , fmt .Errorf ("Failed to find stream" )
176
+ }
177
+
178
+ records := make ([]* kinesis.PutRecordsResultEntry , len (input .Records ))
179
+ for i , r := range input .Records {
180
+ shard , ok := stream .shards [ShardID (* r .PartitionKey )]
181
+ if ! ok {
182
+ return nil , fmt .Errorf ("Failed to find shard" )
183
+ }
184
+
185
+ sn := shard .NextSequenceNumber ()
186
+ shard .AddData (sn , r .Data )
187
+
188
+ records [i ] = & kinesis.PutRecordsResultEntry {
189
+ SequenceNumber : aws .String (string (sn )),
190
+ ShardId : r .PartitionKey ,
191
+ }
192
+ }
193
+
194
+ output := & kinesis.PutRecordsOutput {
195
+ Records : records ,
196
+ FailedRecordCount : aws .Int64 (0 ),
197
+ }
198
+
199
+ return output , nil
200
+ }
0 commit comments