forked from rewardStyle/kinetic
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathproducer_test.go
170 lines (147 loc) · 4.49 KB
/
producer_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
package kinetic
import (
"context"
"sync"
"testing"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/request"
. "github.com/smartystreets/goconvey/convey"
)
func TestProducer(t *testing.T) {
Convey("given a producer", 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-producer-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)
w, err := NewKinesisWriter(k.Session.Config, stream)
So(w, ShouldNotBeNil)
So(err, ShouldBeNil)
p, err := NewProducer(k.Session.Config, stream,
ProducerWriter(w),
ProducerBatchSize(5),
ProducerBatchTimeout(time.Second),
ProducerMaxRetryAttempts(3),
ProducerQueueDepth(10),
ProducerConcurrency(2),
ProducerShardCheckFrequency(time.Minute),
ProducerDataSpillFn(func(msg *Message) error {
//log.Printf("Message was dropped: [%s]\n", string(msg.Data))
return nil
}),
ProducerLogLevel(aws.LogOff),
//ProducerStats(),
)
So(p, ShouldNotBeNil)
So(err, ShouldBeNil)
So(k.Session, ShouldNotBeNil)
So(k.Session.Config, ShouldNotBeNil)
r, err := NewKinesisReader(k.Session.Config, stream, shards[0],
//KinesisReaderBatchSize(),
//KinesisReaderShardIterator(),
KinesisReaderResponseReadTimeout(time.Second),
//KinesisReaderLogLevel(),
//KinesisReaderStats(),
)
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 writer", func() {
w := p.writer.(*KinesisWriter)
Convey("check that the writer was initialized with the correct stream name", func() {
So(w.stream, ShouldEqual, stream)
})
Convey("check that the writer was initialized correctly", func() {
So(w.client, ShouldNotBeNil)
})
})
Convey("check that we can send and receive a single message", func() {
start := time.Now()
data := "hello"
p.Send(&Message{
PartitionKey: aws.String("key"),
Data: []byte(data),
})
msg, err := l.RetrieveWithContext(context.TODO())
elapsed := time.Since(start)
So(err, ShouldBeNil)
So(string(msg.Data), ShouldEqual, data)
So(elapsed.Seconds(), ShouldBeGreaterThan, 1)
})
Convey("check that we can send a single message after batch timeout elapses", func() {
start := time.Now()
data := "hello"
p.Send(&Message{
PartitionKey: aws.String("key"),
Data: []byte(data),
})
msg, err := l.Retrieve()
elapsed := time.Since(start)
Printf("(send took %f seconds)\n", elapsed.Seconds())
So(err, ShouldBeNil)
So(string(msg.Data), ShouldEqual, data)
So(elapsed.Seconds(), ShouldBeGreaterThan, 1)
})
Convey("check that we can send a batch of messages after batch size is reached", func(c C) {
start := time.Now()
var elapsed time.Duration
data := []string{"hello1", "hello2", "hello3", "hello4", "hello5", "hello6"}
wg := sync.WaitGroup{}
wg.Add(1)
go func() {
defer wg.Done()
for i := 0; i < 5; i++ {
msg, err := l.Retrieve()
c.So(err, ShouldBeNil)
c.So(string(msg.Data), ShouldEqual, data[i])
}
elapsed = time.Since(start)
}()
wg.Add(1)
go func() {
defer wg.Done()
for _, datum := range data {
p.Send(&Message{
PartitionKey: aws.String("key"),
Data: []byte(datum),
})
}
}()
wg.Wait()
//So(elapsed.Seconds(), ShouldBeLessThan, 1)
Printf("(first 5 took %f seconds)\n", elapsed.Seconds())
msg, err := l.Retrieve()
So(err, ShouldBeNil)
So(string(msg.Data), ShouldEqual, data[5])
elapsed = time.Since(start)
So(elapsed.Seconds(), ShouldBeGreaterThan, 1)
Printf("(last took %f seconds)\n", elapsed.Seconds())
})
Reset(func() {
k.DeleteStream(stream)
k.WaitUntilStreamDeleted(context.TODO(), stream, request.WithWaiterDelay(request.ConstantWaiterDelay(1*time.Second)))
})
})
}