forked from rewardStyle/kinetic
-
Notifications
You must be signed in to change notification settings - Fork 1
/
consumer_test.go
324 lines (289 loc) · 9.61 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
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
package kinetic
import (
"context"
"fmt"
"math/rand"
"strconv"
"sync"
"sync/atomic"
"testing"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/request"
"github.com/aws/aws-sdk-go/service/kinesis"
. "github.com/smartystreets/goconvey/convey"
)
func putRecord(l *Consumer, b []byte) (*string, error) {
resp, err := l.reader.(*KinesisReader).client.PutRecord(&kinesis.PutRecordInput{
Data: b,
PartitionKey: aws.String("dummy"),
StreamName: aws.String(l.reader.(*KinesisReader).stream),
})
if err != nil {
return nil, err
}
return resp.SequenceNumber, nil
}
func TestConsumer(t *testing.T) {
Convey("given a consumer", t, func() {
k, err := NewKinetic(
AwsConfigCredentials("some-access-key", "some-secret-key", "some-security-token"),
AwsConfigRegion("some-region"),
AwsConfigEndpoint("http://127.0.0.1:4567"),
)
So(k, ShouldNotBeNil)
So(err, ShouldBeNil)
stream := "some-consumer-stream"
err = k.CreateStream(stream, 1)
So(err, ShouldBeNil)
err = k.WaitUntilStreamExists(context.TODO(), stream,
request.WithWaiterDelay(request.ConstantWaiterDelay(time.Second)))
So(err, ShouldBeNil)
shards, err := k.GetShards(stream)
So(err, ShouldBeNil)
So(len(shards), ShouldEqual, 1)
So(k.Session, ShouldNotBeNil)
So(k.Session.Config, ShouldNotBeNil)
r, err := NewKinesisReader(k.Session.Config, stream, shards[0],
KinesisReaderBatchSize(5),
//KinesisReaderShardIterator(),
KinesisReaderResponseReadTimeout(time.Second),
KinesisReaderLogLevel(aws.LogOff),
)
So(r, ShouldNotBeNil)
So(err, ShouldBeNil)
l, err := NewConsumer(k.Session.Config, stream, shards[0],
ConsumerReader(r),
ConsumerQueueDepth(10),
ConsumerConcurrency(10),
ConsumerLogLevel(aws.LogOff),
ConsumerStats(&NilConsumerStatsCollector{}),
)
So(l, ShouldNotBeNil)
So(err, ShouldBeNil)
Convey("given a kinesis reader", func() {
Convey("check that the reader was initialized correctly", func() {
So(l.reader, ShouldNotBeNil)
So(l.reader, ShouldEqual, r)
})
r := l.reader.(*KinesisReader)
Convey("check that the reader was initialized with the correct stream name", func() {
So(r.stream, ShouldEqual, stream)
})
Convey("check that the reader was initialized with the correct shard", func() {
So(r.shard, ShouldEqual, shards[0])
})
Convey("check that the kinesis client was initialized correctly", func() {
So(r.client, ShouldNotBeNil)
})
})
Convey("check that setting an empty shard iterator returns an error", func() {
err := l.reader.(*KinesisReader).setNextShardIterator("")
So(err, ShouldEqual, ErrEmptyShardIterator)
})
Convey("check that setting an empty sequence number returns an error", func() {
err := l.reader.(*KinesisReader).setSequenceNumber("")
So(err, ShouldEqual, ErrEmptySequenceNumber)
})
Convey("check that we can get the TRIM_HORIZON shard iterator", func() {
err := l.reader.(*KinesisReader).ensureShardIterator()
So(err, ShouldBeNil)
So(l.reader.(*KinesisReader).nextShardIterator, ShouldNotBeEmpty)
})
Convey("check that we can retrieve records one by one", func() {
data := []string{"foo", "bar"}
for n, datum := range data {
seq, err := putRecord(l, []byte(datum))
So(err, ShouldBeNil)
So(seq, ShouldNotBeNil)
msg, err := l.Retrieve()
So(err, ShouldBeNil)
So(string(msg.Data), ShouldEqual, datum)
Convey(fmt.Sprintf("check that iteration %d properly advanced the shard iterator", n), func() {
So(l.reader.(*KinesisReader).shardIterator.shardIteratorType, ShouldEqual, "AT_SEQUENCE_NUMBER")
So(l.reader.(*KinesisReader).shardIterator.sequenceNumber, ShouldEqual, *seq)
})
}
})
Convey("check that retrieve will block until record comes", func(c C) {
start := time.Now()
data := "hello"
go func() {
<-time.After(1 * time.Second)
_, err := putRecord(l, []byte(data))
c.So(err, ShouldBeNil)
}()
msg, err := l.Retrieve()
elapsed := time.Since(start)
Printf("(it blocked %f seconds)\n", elapsed.Seconds())
So(err, ShouldBeNil)
So(string(msg.Data), ShouldEqual, data)
So(elapsed.Seconds(), ShouldBeGreaterThan, 1)
})
Convey("check that we can use a context to cancel the retrieve", func() {
start := time.Now()
ctx, cancel := context.WithTimeout(context.TODO(), 1*time.Second)
defer cancel()
_, err := l.RetrieveWithContext(ctx)
elapsed := time.Since(start)
Printf("(it blocked %f seconds)\n", elapsed.Seconds())
So(err, ShouldNotBeNil)
So(err, ShouldHaveSameTypeAs, context.DeadlineExceeded)
So(elapsed.Seconds(), ShouldBeGreaterThan, 1)
})
Convey("check that we can use a context to cancel the retrieve (again)", func() {
start := time.Now()
ctx, cancel := context.WithTimeout(context.TODO(), 10*time.Millisecond)
defer cancel()
_, err := l.RetrieveWithContext(ctx)
elapsed := time.Since(start)
Printf("(it blocked %f seconds)\n", elapsed.Seconds())
So(err, ShouldNotBeNil)
So(err, ShouldHaveSameTypeAs, context.DeadlineExceeded)
So(elapsed.Seconds(), ShouldBeGreaterThan, 0.01)
})
Convey("check that retrieve still works with a canceller if a message comes before the deadline", func(c C) {
ctx, cancel := context.WithTimeout(context.TODO(), 5*time.Second)
defer cancel()
data := "goodbye"
go func() {
<-time.After(1 * time.Second)
_, err := putRecord(l, []byte(data))
c.So(err, ShouldBeNil)
}()
msg, err := l.RetrieveWithContext(ctx)
So(err, ShouldBeNil)
So(string(msg.Data), ShouldEqual, data)
})
Convey("check that retrieve properly blocks other retrieves and attempts to set the shard id", func(c C) {
var wg sync.WaitGroup
wg.Add(1)
go func() {
ctx, cancel := context.WithTimeout(context.TODO(), 1000*time.Millisecond)
defer cancel()
_, err := l.RetrieveWithContext(ctx)
c.So(err, ShouldNotBeNil)
c.So(err, ShouldHaveSameTypeAs, context.DeadlineExceeded)
wg.Done()
}()
<-time.After(10 * time.Millisecond)
_, err := l.Retrieve()
So(err, ShouldEqual, ErrAlreadyConsuming)
wg.Wait()
})
Convey("check that listen and retrieve can not be called concurrently", func(c C) {
var wg sync.WaitGroup
wg.Add(1)
ctx, cancel := context.WithTimeout(context.TODO(), 1000*time.Millisecond)
go func() {
l.ListenWithContext(ctx, func(msg *Message) error {
return nil
})
wg.Done()
}()
<-time.After(10 * time.Millisecond)
cancel()
_, err := l.Retrieve()
So(err, ShouldEqual, ErrAlreadyConsuming)
wg.Wait()
})
Convey("check that throttle mechanism prevents more than 5 calls to get records", func() {
start := time.Now()
secs := []float64{}
for i := 1; i <= 6; i++ {
start := time.Now()
l.reader.GetRecord(context.TODO(), func(msg *Message) error {
return nil
})
secs = append(secs, time.Since(start).Seconds())
}
elapsed := time.Since(start).Seconds()
So(elapsed, ShouldBeGreaterThan, 1)
Printf("%f seconds total, (%v)", elapsed, secs)
})
Convey("check that retrievefn can deliver messages to the fn", func(c C) {
called := false
data := "retrieved"
_, err := putRecord(l, []byte(data))
So(err, ShouldBeNil)
err = l.RetrieveFn(func(msg *Message) error {
called = true
// Note that because this is called in a goroutine, we have to use
// the goconvey context
c.So(string(msg.Data), ShouldEqual, data)
return nil
})
So(err, ShouldBeNil)
So(called, ShouldBeTrue)
})
Convey("check that listen can deliver messages to fn", func(c C) {
planets := []string{"mercury", "venus", "earth", "mars", "jupiter", "saturn", "neptune", "uranus"}
var count int64
var wg sync.WaitGroup
wg.Add(1)
ctx, cancel := context.WithCancel(context.TODO())
go func() {
defer wg.Done()
l.ListenWithContext(ctx, func(msg *Message) error {
atomic.AddInt64(&count, 1)
return nil
})
}()
for _, planet := range planets {
_, err := putRecord(l, []byte(planet))
So(err, ShouldBeNil)
}
timeout := time.After(10 * time.Second)
// FIXME: Not too thrilled with this implementation, but
// there is probably a race condition between when the
// last planet is put onto the Kinesis stream (and
// subsequently read by consume) with when closing the
// pipeOfDeath (which will shut down the consume loop)
// such that we may not see all the planets inside
// Listen.
stop:
for {
select {
case <-time.After(1 * time.Second):
if atomic.LoadInt64(&count) == int64(len(planets)) {
break stop
}
case <-timeout:
break stop
}
}
// FIXME: probably a race condition here as consume may
// not have grabbed all data from the channel yet.
cancel()
wg.Wait()
So(atomic.LoadInt64(&count), ShouldEqual, len(planets))
})
Convey("check that listen can be cancelled by context", func(c C) {
for i := 0; i < 20; i++ {
_, err := putRecord(l, []byte(fmt.Sprintf("%d", i)))
So(err, ShouldBeNil)
}
var count int64
ctx, cancel := context.WithCancel(context.TODO())
go func() {
l.ListenWithContext(ctx, func(m *Message) error {
time.AfterFunc(time.Duration(rand.Intn(3))*time.Second, func() {
n, err := strconv.Atoi(string(m.Data))
c.So(n, ShouldBeBetweenOrEqual, 0, 19)
c.So(err, ShouldBeNil)
atomic.AddInt64(&count, 1)
})
return nil
})
}()
<-time.After(1 * time.Second)
cancel()
So(atomic.LoadInt64(&count), ShouldBeBetweenOrEqual, 1, 20)
Printf("(count was %d)", atomic.LoadInt64(&count))
})
Reset(func() {
k.DeleteStream(stream)
k.WaitUntilStreamDeleted(context.TODO(), stream, request.WithWaiterDelay(request.ConstantWaiterDelay(1*time.Second)))
})
})
}