-
Notifications
You must be signed in to change notification settings - Fork 0
/
fanoutwriter_test.go
284 lines (212 loc) · 5.62 KB
/
fanoutwriter_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
package fanoutwriter
import (
"github.com/stretchr/testify/assert"
"io"
"runtime"
"sync"
"testing"
)
func validateWrite(t *testing.T, f io.Writer, b []byte) {
n, err := f.Write(b)
assert.NoError(t, err, "Write should not return with an error")
assert.Equal(t, len(b), n, "length written should match buffer length")
}
func validateRead(t *testing.T, r io.Reader, b []byte, l int) {
n, err := r.Read(b)
assert.NoError(t, err, "Read should not return with an error")
assert.Equal(t, l, n, "length read unexpected")
}
func vclose(t *testing.T, c io.Closer) {
e := c.Close()
assert.NoError(t, e, "Close should not return with an error")
}
func TestCreateReaderWriteThenRead(t *testing.T) {
fw := NewDefaultFanoutWriter()
r := fw.NewReader()
r2 := fw.NewReader()
defer vclose(t, fw)
defer vclose(t, r)
defer vclose(t, r2)
wb := []byte{1, 2, 3, 4, 5}
validateWrite(t, fw, wb)
rb := make([]byte, 5, 5)
validateRead(t, r, rb, 5)
assert.ElementsMatch(t, wb, rb)
validateRead(t, r2, rb, 5)
assert.ElementsMatch(t, wb, rb)
}
func TestWriteCreateReaderWriteThenRead(t *testing.T) {
fw := NewDefaultFanoutWriter()
defer vclose(t, fw)
wb := []byte{1, 2, 3, 4, 5}
validateWrite(t, fw, wb)
r := fw.NewReader()
defer vclose(t, r)
validateWrite(t, fw, wb)
rb := make([]byte, 10, 10)
validateRead(t, r, rb, 5)
assert.ElementsMatch(t, wb, rb[:5])
}
func TestWriteCreateReaderWriteThenReadWithReadFromStart(t *testing.T) {
fw := NewFanoutWriter(&FanoutWriterConfig{
ReadFromStart: true,
})
defer vclose(t, fw)
wb := []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
validateWrite(t, fw, wb[:5])
r := fw.NewReader()
defer vclose(t, r)
validateWrite(t, fw, wb[5:])
rb := make([]byte, 10, 10)
validateRead(t, r, rb, 10)
assert.ElementsMatch(t, wb, rb)
}
func TestRFSLimitDoubleWriteOverCreateReaderRead(t *testing.T) {
fw := NewFanoutWriter(&FanoutWriterConfig{
Limit: 6,
ReadFromStart: true,
})
defer vclose(t, fw)
wb := []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
validateWrite(t, fw, wb[:5])
validateWrite(t, fw, wb[5:])
rb := make([]byte, 10, 10)
r := fw.NewReader()
defer vclose(t, r)
validateRead(t, r, rb, 6)
// rb should contain the last 6 elements of wb
assert.ElementsMatch(t, wb[4:], rb[:6])
}
func TestRFSLimitWriteOverCreateReaderRead(t *testing.T) {
fw := NewFanoutWriter(&FanoutWriterConfig{
Limit: 6,
ReadFromStart: true,
})
defer vclose(t, fw)
wb := []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
validateWrite(t, fw, wb)
rb := make([]byte, 10, 10)
r := fw.NewReader()
defer vclose(t, r)
validateRead(t, r, rb, 6)
// rb should contain the last 6 elements of wb
assert.ElementsMatch(t, wb[4:], rb[:6])
}
func TestInitialBuffer(t *testing.T) {
b := []byte{1, 2, 3, 4, 5}
fw := NewFanoutWriter(&FanoutWriterConfig{
Buf: b,
ReadFromStart: true,
})
defer vclose(t, fw)
rb := make([]byte, 10, 10)
r := fw.NewReader()
defer vclose(t, r)
validateRead(t, r, rb, 5)
assert.ElementsMatch(t, b, rb[:5])
}
func TestInitialBufferCreateReaderWriteRead(t *testing.T) {
b := []byte{1, 2, 3, 4, 5}
fw := NewFanoutWriter(&FanoutWriterConfig{
Buf: b[:3],
ReadFromStart: true,
})
defer vclose(t, fw)
r := fw.NewReader()
defer vclose(t, r)
// so like make sure we actually copy things in i guess
b[3] = 6
b[4] = 7
validateWrite(t, fw, b[3:])
rb := make([]byte, 10, 10)
validateRead(t, r, rb, 5)
assert.ElementsMatch(t, b, rb[:5])
}
func TestZeroSizeWrite(t *testing.T) {
fw := NewDefaultFanoutWriter()
defer vclose(t, fw)
r := fw.NewReader()
defer vclose(t, r)
wb := []byte{1, 2, 3, 4, 5}
validateWrite(t, fw, wb[0:0])
validateWrite(t, fw, wb)
rb := make([]byte, 10, 10)
validateRead(t, r, rb, 5)
assert.ElementsMatch(t, wb, rb[:5])
}
func TestWriteAfterClose(t *testing.T) {
fw := NewDefaultFanoutWriter()
e := fw.Close()
assert.NoError(t, e)
_, e = fw.Write([]byte{1, 2, 3, 4, 5})
assert.Equal(t, io.ErrClosedPipe, e)
}
func TestCreateReaderAfterClose(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Fail() // we should have paniced!
}
}()
fw := NewDefaultFanoutWriter()
e := fw.Close()
assert.NoError(t, e)
fw.NewReader() // uh oh!
}
func TestLimitWriteWouldCauseFallBehind(t *testing.T) {
fw := NewFanoutWriter(&FanoutWriterConfig{
Limit: 6,
})
defer vclose(t, fw)
r := fw.NewReader()
defer vclose(t, r)
wb := []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
_, e := fw.Write(wb)
assert.Equal(t, ErrWriteWouldCauseFallBehind, e)
}
func TestLimitNoReaderWriteWouldCauseFallBehindNoErr(t *testing.T) {
fw := NewFanoutWriter(&FanoutWriterConfig{
Limit: 6,
})
defer vclose(t, fw)
wb := []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
validateWrite(t, fw, wb)
}
func TestLimitReaderFellBehind(t *testing.T) {
fw := NewFanoutWriter(&FanoutWriterConfig{
Limit: 6,
})
defer vclose(t, fw)
r := fw.NewReader()
defer vclose(t, r)
wb := []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
validateWrite(t, fw, wb[:5])
validateWrite(t, fw, wb[5:])
rb := make([]byte, 10, 10)
_, err := r.Read(rb)
assert.Equal(t, ErrFellBehind, err)
}
func TestDeferredWriteAndClose(t *testing.T) {
fw := NewDefaultFanoutWriter()
r := fw.NewReader()
defer vclose(t, r)
wb := []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
var wg sync.WaitGroup
wg.Add(1)
go func() {
rb := make([]byte, 15, 15)
validateRead(t, r, rb, 10)
assert.ElementsMatch(t, wb, rb[:10])
rb2 := make([]byte, 10, 10)
_, err := r.Read(rb2)
assert.Equal(t, io.EOF, err)
wg.Done()
}()
// we should yield to the other so it can start its read call
runtime.Gosched()
// so we write to the writer...
validateWrite(t, fw, wb[:10])
// ...then close it
e := fw.Close()
assert.NoError(t, e)
wg.Wait()
}