-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathproducer_test.go
168 lines (137 loc) · 4.69 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
package kinetic
import (
"encoding/binary"
"errors"
"runtime"
"syscall"
"testing"
"time"
. "github.com/smartystreets/goconvey/convey"
)
func TestProducerStop(t *testing.T) {
producerInterface, _ := new(KinesisProducer).Init()
producerInterface.NewEndpoint(testEndpoint, "stream-name")
producer := producerInterface.(*KinesisProducer)
CreateAndWaitForStream(producer.client, "stream-name")
producerInterface.ReInit()
Convey("Given a running producer", t, func() {
go producer.produce()
Convey("It should stop producing if sent an interrupt signal", func() {
producer.interrupts <- syscall.SIGINT
runtime.Gosched()
// Wait for it to stop
for {
if !producer.IsProducing() {
break
}
}
So(producer.IsProducing(), ShouldEqual, false)
})
})
producer.Close()
}
func TestSyncStop(t *testing.T) {
producerInterface, _ := new(KinesisProducer).Init()
producerInterface.NewEndpoint(testEndpoint, "stream-name")
producer := producerInterface.(*KinesisProducer)
CreateAndWaitForStream(producer.client, "stream-name")
producerInterface.ReInit()
Convey("Given a running producer", t, func() {
go producer.produce()
runtime.Gosched()
Convey("It should stop producing if sent an interrupt signal", func() {
err := producer.CloseSync()
So(err, ShouldBeNil)
// Wait for it to stop
So(producer.IsProducing(), ShouldEqual, false)
})
})
producer.Close()
}
func TestProducerError(t *testing.T) {
producerInterface, _ := new(KinesisProducer).Init()
producerInterface.NewEndpoint(testEndpoint, "stream-name")
producer := producerInterface.(*KinesisProducer)
CreateAndWaitForStream(producer.client, "stream-name")
producerInterface.ReInit()
Convey("Given a running producer", t, func() {
go producer.produce()
Convey("It should handle errors successfully", func() {
producer.errors <- errors.New("All your base are belong to us")
// Let the error propagate
<-time.After(3 * time.Second)
So(producer.getErrCount(), ShouldEqual, 1)
So(producer.IsProducing(), ShouldEqual, true)
})
})
producer.Close()
}
func TestProducerMessage(t *testing.T) {
listener, _ := new(Listener).InitC("your-stream", "0", "LATEST", "accesskey", "secretkey", "us-east-1", 4)
producer, _ := new(KinesisProducer).InitC("your-stream", "0", "LATEST", "accesskey", "secretkey", "us-east-1", 4)
listener.NewEndpoint(testEndpoint, "your-stream")
producer.NewEndpoint(testEndpoint, "your-stream")
CreateAndWaitForStream(producer.(*KinesisProducer).client, "your-stream")
listener.ReInit()
producer.ReInit()
for _, c := range cases {
Convey("Given a valid message", t, func() {
producer.Send(new(Message).Init(c.message, "test"))
Convey("It should be passed on the queue without error", func() {
msg, err := listener.Retrieve()
if err != nil {
t.Fatalf(err.Error())
}
So(string(msg.Value()), ShouldResemble, string(c.message))
})
})
}
listener.Close()
producer.Close()
}
func TestProducerSendSyncMessage(t *testing.T) {
listener, _ := new(Listener).InitC("your-stream", "0", "LATEST", "accesskey", "secretkey", "us-east-1", 4)
producer, _ := new(KinesisProducer).InitC("your-stream", "0", "LATEST", "accesskey", "secretkey", "us-east-1", 4)
listener.NewEndpoint(testEndpoint, "your-stream")
producer.NewEndpoint(testEndpoint, "your-stream")
CreateAndWaitForStream(producer.(*KinesisProducer).client, "your-stream")
listener.ReInit()
producer.ReInit()
for _, c := range cases {
Convey("Given a valid message", t, func() {
producer.SendSync(new(Message).Init(c.message, "test"))
Convey("It should be passed on the queue without error", func() {
msg, err := listener.Retrieve()
if err != nil {
t.Fatalf(err.Error())
}
So(string(msg.Value()), ShouldResemble, string(c.message))
})
})
}
listener.Close()
producer.Close()
}
func TestProducerTryToSend(t *testing.T) {
producer, _ := new(KinesisProducer).InitC("your-stream", "0", "LATEST", "accesskey", "secretkey", "us-east-1", 4)
producer.NewEndpoint(testEndpoint, "your-stream")
CreateAndWaitForStream(producer.(*KinesisProducer).client, "stream-name")
producer.ReInit()
closeError := producer.CloseSync() // This is to make the test deterministic. It stops producer from sending messages.
runtime.Gosched()
var totDropped int
for i := 0; i < 5000; i++ {
b := make([]byte, 2)
binary.LittleEndian.PutUint16(b, uint16(i))
if err := producer.TryToSend(new(Message).Init(b, "foo")); nil != err {
totDropped++
}
}
Convey("Given a producer", t, func() {
Convey("TryToSend should drop messages when the queue is full", func() {
So(closeError, ShouldBeNil)
So(totDropped, ShouldEqual, 1000)
So(len(producer.(*KinesisProducer).messages), ShouldEqual, 4000)
})
})
}