-
Notifications
You must be signed in to change notification settings - Fork 6
/
buffer_test.go
347 lines (289 loc) · 6.72 KB
/
buffer_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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
package buffer_test
import (
"time"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/globocom/go-buffer/v2"
)
var _ = Describe("Buffer", func() {
var flusher *MockFlusher
BeforeEach(func() {
flusher = NewMockFlusher()
})
Context("Constructor", func() {
It("creates a new Buffer instance", func() {
// act
sut := buffer.New(
buffer.WithSize(10),
buffer.WithFlusher(flusher),
)
// assert
Expect(sut).NotTo(BeNil())
})
Context("invalid options", func() {
It("panics when provided an invalid size", func() {
Expect(func() {
buffer.New(
buffer.WithSize(0),
)
}).To(Panic())
})
It("panics when provided an invalid flusher", func() {
Expect(func() {
buffer.New(
buffer.WithSize(1),
buffer.WithFlusher(nil),
)
}).To(Panic())
})
It("panics when provided an invalid flush interval", func() {
Expect(func() {
buffer.New(
buffer.WithSize(1),
buffer.WithFlusher(flusher),
buffer.WithFlushInterval(-1),
)
}).To(Panic())
})
It("panics when provided an invalid push timeout", func() {
Expect(func() {
buffer.New(
buffer.WithSize(1),
buffer.WithFlusher(flusher),
buffer.WithPushTimeout(-1),
)
}).To(Panic())
})
It("panics when provided an invalid flush timeout", func() {
Expect(func() {
buffer.New(
buffer.WithSize(1),
buffer.WithFlusher(flusher),
buffer.WithFlushTimeout(-1),
)
}).To(Panic())
})
It("panics when provided an invalid close timeout", func() {
Expect(func() {
buffer.New(
buffer.WithSize(1),
buffer.WithFlusher(flusher),
buffer.WithCloseTimeout(-1),
)
}).To(Panic())
})
})
})
Context("Pushing", func() {
It("pushes items into the buffer when Push is called", func() {
// arrange
sut := buffer.New(
buffer.WithSize(3),
buffer.WithFlusher(flusher),
)
// act
err1 := sut.Push(1)
err2 := sut.Push(2)
err3 := sut.Push(3)
// assert
Expect(err1).To(Succeed())
Expect(err2).To(Succeed())
Expect(err3).To(Succeed())
})
It("fails when Push cannot execute in a timely fashion", func() {
// arrange
flusher.Func = func() { select {} }
sut := buffer.New(
buffer.WithSize(2),
buffer.WithFlusher(flusher),
buffer.WithPushTimeout(time.Second),
)
// act
err1 := sut.Push(1)
err2 := sut.Push(2)
err3 := sut.Push(3)
// assert
Expect(err1).To(Succeed())
Expect(err2).To(Succeed())
Expect(err3).To(MatchError(buffer.ErrTimeout))
})
It("fails when the buffer is closed", func() {
// arrange
sut := buffer.New(
buffer.WithSize(2),
buffer.WithFlusher(flusher),
)
_ = sut.Close()
// act
err := sut.Push(1)
// assert
Expect(err).To(MatchError(buffer.ErrClosed))
})
})
Context("Flushing", func() {
It("flushes the buffer when it fills up", func(done Done) {
// arrange
sut := buffer.New(
buffer.WithSize(5),
buffer.WithFlusher(flusher),
)
// act
_ = sut.Push(1)
_ = sut.Push(2)
_ = sut.Push(3)
_ = sut.Push(4)
_ = sut.Push(5)
// assert
result := <-flusher.Done
Expect(result.Items).To(ConsistOf(1, 2, 3, 4, 5))
close(done)
})
It("flushes the buffer when the provided interval has elapsed", func(done Done) {
// arrange
interval := 3 * time.Second
start := time.Now()
sut := buffer.New(
buffer.WithSize(5),
buffer.WithFlusher(flusher),
buffer.WithFlushInterval(interval),
)
// act
_ = sut.Push(1)
// assert
result := <-flusher.Done
Expect(result.Items).To(ConsistOf(1))
Expect(result.Time).To(BeTemporally("~", start, interval+time.Second))
close(done)
}, 5)
It("flushes the buffer when Flush is called", func(done Done) {
// arrange
sut := buffer.New(
buffer.WithSize(3),
buffer.WithFlusher(flusher),
)
_ = sut.Push(1)
_ = sut.Push(2)
// act
err := sut.Flush()
// assert
result := <-flusher.Done
Expect(err).To(Succeed())
Expect(result.Items).To(ConsistOf(1, 2))
close(done)
})
It("fails when Flush cannot execute in a timely fashion", func() {
// arrange
flusher.Func = func() { time.Sleep(3 * time.Second) }
sut := buffer.New(
buffer.WithSize(1),
buffer.WithFlusher(flusher),
buffer.WithFlushTimeout(time.Second),
)
_ = sut.Push(1)
// act
err := sut.Flush()
// assert
Expect(err).To(MatchError(buffer.ErrTimeout))
})
It("fails when the buffer is closed", func() {
// arrange
sut := buffer.New(
buffer.WithSize(2),
buffer.WithFlusher(flusher),
)
_ = sut.Close()
// act
err := sut.Flush()
// assert
Expect(err).To(MatchError(buffer.ErrClosed))
})
})
Context("Closing", func() {
It("flushes the buffer and closes it when Close is called", func(done Done) {
// arrange
sut := buffer.New(
buffer.WithSize(3),
buffer.WithFlusher(flusher),
)
_ = sut.Push(1)
_ = sut.Push(2)
// act
err := sut.Close()
// assert
result := <-flusher.Done
Expect(err).To(Succeed())
Expect(result.Items).To(ConsistOf(1, 2))
close(done)
})
It("fails when Close cannot execute in a timely fashion", func() {
// arrange
flusher.Func = func() { time.Sleep(2 * time.Second) }
sut := buffer.New(
buffer.WithSize(1),
buffer.WithFlusher(flusher),
buffer.WithCloseTimeout(time.Second),
)
_ = sut.Push(1)
// act
err := sut.Close()
// assert
Expect(err).To(MatchError(buffer.ErrTimeout))
})
It("fails when the buffer is closed", func() {
// arrange
flusher.Func = func() { time.Sleep(2 * time.Second) }
sut := buffer.New(
buffer.WithSize(1),
buffer.WithFlusher(flusher),
buffer.WithCloseTimeout(time.Second),
)
_ = sut.Close()
// act
err := sut.Close()
// assert
Expect(err).To(MatchError(buffer.ErrClosed))
})
It("allows Close to be called again if it fails", func() {
// arrange
flusher.Func = func() { time.Sleep(2 * time.Second) }
sut := buffer.New(
buffer.WithSize(1),
buffer.WithFlusher(flusher),
buffer.WithCloseTimeout(time.Second),
)
_ = sut.Push(1)
// act
err1 := sut.Close()
time.Sleep(time.Second)
err2 := sut.Close()
// assert
Expect(err1).To(MatchError(buffer.ErrTimeout))
Expect(err2).To(Succeed())
})
})
})
type (
MockFlusher struct {
Done chan *WriteCall
Func func()
}
WriteCall struct {
Time time.Time
Items []interface{}
}
)
func (flusher *MockFlusher) Write(items []interface{}) {
call := &WriteCall{
Time: time.Now(),
Items: items,
}
if flusher.Func != nil {
flusher.Func()
}
flusher.Done <- call
}
func NewMockFlusher() *MockFlusher {
return &MockFlusher{
Done: make(chan *WriteCall, 1),
}
}