-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathstreams_test.go
298 lines (235 loc) · 7.68 KB
/
streams_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
package nakadi
import (
"context"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
)
func TestStreamAPI_startStreamLoop(t *testing.T) {
errorCh := make(chan error, 1)
okCh := make(chan struct{})
blockCh := make(chan time.Time, 1)
stream := &mockStreamer{}
streamAPI, opener, _ := setupMockStream(errorCh, okCh)
opener.On("openStream").Once().Return(stream, nil)
opener.On("openStream").Once().Return(nil, assert.AnError)
stream.On("nextEvents").Return(Cursor{}, nil, assert.AnError).WaitUntil(blockCh)
stream.On("closeStream").Return(nil)
blockCh <- time.Now()
<-okCh
streamAPI.cancel()
<-time.After(time.Second)
//Wait for startStream to loop
// If startStream tries to open the stream again after streamAPI is closed,
// test will panic if openStream is called more than once.
}
func TestStreamAPI_startStream(t *testing.T) {
retryCh := make(chan error, 1)
okCh := make(chan struct{}, 1)
blockCh := make(chan time.Time, 1)
stream := &mockStreamer{}
_, opener, _ := setupMockStream(retryCh, okCh)
opener.On("openStream").Once().Return(nil, assert.AnError)
opener.On("openStream").Once().Return(stream, nil).WaitUntil(blockCh)
stream.On("nextEvents").Return(Cursor{}, nil, assert.AnError).WaitUntil(blockCh)
select {
case err := <-retryCh:
assert.EqualError(t, assert.AnError, err.Error())
case <-time.After(50 * time.Millisecond):
assert.Fail(t, "no retry error detected")
}
blockCh <- time.Now()
select {
case <-okCh:
// nothing
case <-time.After(50 * time.Millisecond):
assert.Fail(t, "no retry ok detected")
}
}
func TestStreamAPI_NextEvents(t *testing.T) {
expectedCursor := Cursor{NakadiStreamID: "stream-id"}
expectedEvents := []byte(`"events":[{"metadata":{"eid":"74450ab6-5461-11e7-9dd2-87c3afa8811f"})]`)
t.Run("fail with error", func(t *testing.T) {
stream := &mockStreamer{}
blockCh := make(chan time.Time, 1)
streamAPI, opener, _ := setupMockStream(nil, nil)
opener.On("openStream").Return(stream, nil).WaitUntil(blockCh)
blockCh <- time.Now()
stream.On("nextEvents").Return(Cursor{}, nil, assert.AnError).WaitUntil(blockCh)
stream.On("closeStream").Return(nil)
blockCh <- time.Now()
_, _, err := streamAPI.NextEvents()
assert.EqualError(t, assert.AnError, err.Error())
})
t.Run("successful read events", func(t *testing.T) {
stream := &mockStreamer{}
blockCh := make(chan time.Time, 1)
streamAPI, opener, _ := setupMockStream(nil, nil)
opener.On("openStream").Return(stream, nil).WaitUntil(blockCh)
blockCh <- time.Now()
stream.On("nextEvents").Return(expectedCursor, expectedEvents, nil).WaitUntil(blockCh)
stream.On("closeStream").Return(nil)
blockCh <- time.Now()
cursor, events, err := streamAPI.NextEvents()
require.NoError(t, err)
assert.Equal(t, expectedCursor, cursor)
assert.Equal(t, expectedEvents, events)
blockCh <- time.Now()
cursor, events, err = streamAPI.NextEvents()
require.NoError(t, err)
assert.Equal(t, expectedCursor, cursor)
assert.Equal(t, expectedEvents, events)
})
t.Run("fail with canceled", func(t *testing.T) {
stream := &mockStreamer{}
blockCh := make(chan time.Time, 1)
streamAPI, opener, _ := setupMockStream(nil, nil)
opener.On("openStream").Return(stream, nil).WaitUntil(blockCh)
blockCh <- time.Now()
stream.On("nextEvents").Return(Cursor{}, nil, context.Canceled).WaitUntil(blockCh)
stream.On("closeStream").Return(nil)
blockCh <- time.Now()
_, _, err := streamAPI.NextEvents()
assert.EqualError(t, context.Canceled, err.Error())
})
}
func TestStreamAPI_CommitCursor(t *testing.T) {
retryCh := make(chan error, 1)
okCh := make(chan struct{}, 1)
errorCh := make(chan error, 1)
blockCh := make(chan time.Time, 1)
blockStreamer := make(chan time.Time, 1)
expectedCursor := Cursor{NakadiStreamID: "stream-id"}
t.Run("fail with time out", func(t *testing.T) {
streamAPI, opener, committer := setupMockStream(retryCh, okCh)
opener.On("openStream").WaitUntil(blockStreamer)
committer.On("commitCursor", expectedCursor).WaitUntil(blockCh).Twice().Return(assert.AnError)
go func() {
err := streamAPI.CommitCursor(expectedCursor)
errorCh <- err
}()
blockCh <- time.Now()
err := <-retryCh
assert.EqualError(t, assert.AnError, err.Error())
time.Sleep(600 * time.Millisecond)
blockCh <- time.Now()
err = <-errorCh
assert.EqualError(t, assert.AnError, err.Error())
})
t.Run("fail retry succeed", func(t *testing.T) {
streamAPI, opener, committer := setupMockStream(retryCh, okCh)
opener.On("openStream").WaitUntil(blockStreamer)
committer.On("commitCursor", expectedCursor).WaitUntil(blockCh).Once().Return(assert.AnError)
go func() {
err := streamAPI.CommitCursor(expectedCursor)
errorCh <- err
}()
blockCh <- time.Now()
err := <-retryCh
assert.EqualError(t, assert.AnError, err.Error())
committer.On("commitCursor", expectedCursor).WaitUntil(blockCh).Once().Return(nil)
blockCh <- time.Now()
err = <-errorCh
assert.NoError(t, err)
select {
case <-okCh:
// nothing
default:
assert.Fail(t, "no retry ok detected")
}
})
t.Run("success", func(t *testing.T) {
streamAPI, opener, committer := setupMockStream(nil, nil)
opener.On("openStream").WaitUntil(blockStreamer)
committer.On("commitCursor", expectedCursor).Once().Return(nil).WaitUntil(blockCh)
blockCh <- time.Now()
err := streamAPI.CommitCursor(expectedCursor)
assert.NoError(t, err)
})
}
func TestStreamAPI_Close(t *testing.T) {
errorCh := make(chan error, 1)
blockCh := make(chan time.Time, 1)
streamAPI, opener, _ := setupMockStream(nil, nil)
stream := &mockStreamer{}
opener.On("openStream").WaitUntil(blockCh).Return(stream, nil)
stream.On("nextEvents").WaitUntil(blockCh).Once().Return(Cursor{}, nil, assert.AnError)
stream.On("closeStream").Return(nil)
go func() {
err := streamAPI.Close()
errorCh <- err
}()
blockCh <- time.Now()
time.Sleep(100 * time.Millisecond)
blockCh <- time.Now()
err := <-errorCh
assert.NoError(t, err)
<-streamAPI.eventCh
stream.AssertCalled(t, "closeStream")
}
func setupMockStream(errCh chan error, okCh chan struct{}) (*StreamAPI, *mockStreamOpener, *mockCommitter) {
ctx, cancel := context.WithCancel(context.Background())
opener := &mockStreamOpener{}
committer := &mockCommitter{}
stream := &StreamAPI{
opener: opener,
committer: committer,
eventCh: make(chan eventsOrError, 10),
ctx: ctx,
cancel: cancel,
streamBackOffConf: backOffConfiguration{
Retry: true,
InitialRetryInterval: 1 * time.Millisecond,
MaxRetryInterval: 100 * time.Millisecond,
},
commitBackOffConf: backOffConfiguration{
Retry: true,
InitialRetryInterval: 1 * time.Millisecond,
MaxRetryInterval: 100 * time.Millisecond,
MaxElapsedTime: 500 * time.Millisecond,
},
notifyErr: func(err error, _ time.Duration) {
if errCh != nil {
errCh <- err
}
},
notifyOK: func() {
if okCh != nil {
okCh <- struct{}{}
}
},
}
go stream.startStream()
return stream, opener, committer
}
type mockStreamOpener struct {
mock.Mock
}
func (so *mockStreamOpener) openStream() (streamer, error) {
args := so.Called()
if args.Error(1) != nil {
return nil, args.Error(1)
}
return args.Get(0).(streamer), nil
}
type mockStreamer struct {
mock.Mock
}
func (s *mockStreamer) nextEvents() (Cursor, []byte, error) {
args := s.Called()
if args.Error(2) != nil {
return Cursor{}, nil, args.Error(2)
}
return args.Get(0).(Cursor), args.Get(1).([]byte), nil
}
func (s *mockStreamer) closeStream() error {
return s.Called().Error(0)
}
type mockCommitter struct {
mock.Mock
}
func (c *mockCommitter) commitCursor(cursor Cursor) error {
return c.Called(cursor).Error(0)
}