This repository has been archived by the owner on Nov 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathio_test.go
306 lines (258 loc) · 7.25 KB
/
io_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
package xio
import (
"bytes"
"context"
"errors"
"io"
"reflect"
"testing"
"time"
)
func TestCopy(t *testing.T) {
t.Run("copy with context cancelation and do not wait", func(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
unblockWrite := make(chan struct{})
writeDone := make(chan struct{})
var writeTotal int
n, err := Copy(
ctx,
WriterFunc(func(b []byte) (int, error) {
defer close(writeDone)
cancel()
<-unblockWrite
writeTotal += len(b)
return len(b), nil
}),
ReaderFunc(func(b []byte) (int, error) {
return len(b), nil
}),
WaitForLastOp(false),
)
if !errors.Is(err, context.Canceled) {
t.Fatalf("expected error to be context canceled but got %v", err)
}
if n != 0 {
t.Fatalf("expected n to be 0 but got %d", n)
}
close(unblockWrite)
<-writeDone
if writeTotal != 32768 {
t.Fatalf("expected write total to be 32768 but got %d", writeTotal)
}
})
t.Run("Copy with wait for last write", func(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
unblockWrite := make(chan struct{})
time.AfterFunc(20*time.Millisecond, func() { close(unblockWrite) })
n, err := Copy(
ctx,
WriterFunc(func(b []byte) (int, error) {
cancel()
<-unblockWrite
return len(b), nil
}),
ReaderFunc(func(b []byte) (int, error) {
return len(b), nil
}),
WaitForLastOp(true),
)
if !errors.Is(err, context.Canceled) {
t.Fatalf("expected error to be context canceled but got %v", err)
}
if n != 32768 {
t.Fatalf("expected n to be 32768 but got %d", n)
}
})
t.Run("canceled write error", func(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
writeErr := errors.New("writer broke!")
n, err := Copy(
ctx,
WriterFunc(func(b []byte) (int, error) { return 42, writeErr }),
ReaderFunc(func(b []byte) (int, error) {
cancel()
time.Sleep(20 * time.Millisecond) // we give the time for the context.Done to have fired
return len(b), nil
}),
)
if err != writeErr {
t.Fatalf("expected write err to be %#q but got %#q", writeErr, err)
}
if n != 42 {
t.Fatalf("expected n to be 42 but got %d", n)
}
})
t.Run("canceled read error", func(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
readErr := errors.New("reader broke!")
n, err := Copy(
ctx,
WriterFunc(func(b []byte) (int, error) { return 42, nil }),
ReaderFunc(func(b []byte) (int, error) {
cancel()
time.Sleep(20 * time.Millisecond) // we give the time for the context.Done to have fired
return 0, readErr
}),
)
if err != readErr {
t.Fatalf("expected read err to be %#q but got %#q", readErr, err)
}
if n != 0 {
t.Fatalf("expected n to be 0 but got %d", n)
}
})
t.Run("calling with canceled context is a noop", func(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
cancel()
// will panic if Copy tries to read from src
n, err := Copy(ctx, nil, nil)
if err != context.Canceled {
t.Fatalf("expected err to be %#q but got %#q", context.Canceled, err)
}
if n != 0 {
t.Fatalf("expected n to be 0 but got %d", n)
}
})
t.Run("read error will write what it can then return error", func(t *testing.T) {
readErr := errors.New("reader encoutered invalid state!")
n, err := Copy(
context.Background(),
WriterFunc(func(b []byte) (int, error) { return len(b), nil }),
ReaderFunc(func(b []byte) (int, error) { return 42, readErr }),
)
if err != readErr {
t.Fatalf("expected err to be %#q but got %#q", readErr, err)
}
if n != 42 {
t.Fatalf("expected n to be 42 but got %d", n)
}
})
t.Run("invalid write err", func(t *testing.T) {
n, err := Copy(
context.Background(),
WriterFunc(func(b []byte) (int, error) { return 100, nil }),
ReaderFunc(func(b []byte) (int, error) { return 42, nil }),
)
if err != errInvalidWrite {
t.Fatalf("expected err to be %#q but got %#q", errInvalidWrite, err)
}
if n != 0 {
t.Fatalf("expected n to be 0 but got %d", n)
}
})
t.Run("can set buffer size", func(t *testing.T) {
var called bool
n, err := Copy(
context.Background(),
WriterFunc(func(b []byte) (int, error) {
return len(b), nil
}),
ReaderFunc(func(b []byte) (int, error) {
called = true
return len(b), io.EOF
}),
BufferSize(16),
)
if !called {
t.Fatal("Copy read func was not called")
}
if err != nil {
t.Fatalf("expected err to be nil but got %#q", err)
}
if n != 16 {
t.Fatalf("expected n to be buffersize %d but got %d", 16, n)
}
})
}
func TestCopyN(t *testing.T) {
t.Run("only copies N bytes", func(t *testing.T) {
var bytesRead []int
n, err := CopyN(
context.Background(),
WriterFunc(func(b []byte) (int, error) { return len(b), nil }),
ReaderFunc(func(b []byte) (int, error) {
bytesRead = append(bytesRead, len(b))
return len(b), nil
}),
100,
// we want multiple reads with one that overflows so we would have technically read 128 bytes
// but we will still get a n of 100
BufferSize(64),
)
if err != nil {
t.Fatalf("expected err to be nil but got %#q", err)
}
if n != 100 {
t.Fatalf("expected to total written to be 100 but got %d", n)
}
expectedBytesRead := []int{64, 36}
if !reflect.DeepEqual(bytesRead, expectedBytesRead) {
t.Fatalf("expected bytes to be read like so %v but got %v", expectedBytesRead, bytesRead)
}
})
t.Run("read less data than N", func(t *testing.T) {
n, err := CopyN(
context.Background(),
WriterFunc(func(b []byte) (int, error) { return len(b), nil }),
ReaderFunc(func(b []byte) (int, error) { return 50, io.EOF }),
100,
)
if err != io.EOF {
t.Fatalf("expected err to be %#q but got %#q", io.EOF, err)
}
if n != 50 {
t.Fatalf("expected n to be 50 but got %d", n)
}
})
t.Run("N of zero should not trigger any reads", func(t *testing.T) {
// Will panic if src Read is called
n, err := CopyN(context.Background(), nil, nil, 0)
if err != nil {
t.Fatalf("expected err to be nil but got %#q", err)
}
if n != 0 {
t.Fatalf("expected n to be 0 but got %d", n)
}
})
}
func TestCopyBuffer(t *testing.T) {
buffer := make([]byte, 15)
n, err := CopyBuffer(
context.Background(),
WriterFunc(func(b []byte) (int, error) {
return len(b), nil
}),
ReaderFunc(func(b []byte) (int, error) {
for i, v := range []byte("hello world") {
b[i] = v
}
return 11, io.EOF
}),
buffer,
)
if err != nil {
t.Fatalf("expected no error but got %v", err)
}
if n != 11 {
t.Fatalf("expected 11 bytes to be read but got: %d", n)
}
if substring := string(buffer[:11]); substring != "hello world" {
t.Fatalf("expected hello world but got: %s", substring)
}
}
func TestReadAll(t *testing.T) {
expected := []byte(`Hello world`)
actual, err := ReadAll(context.Background(), bytes.NewReader(expected))
if err != nil {
t.Fatalf("expectd err to be nil but got %#q", err)
}
if !reflect.DeepEqual(expected, actual) {
t.Fatalf("expect content to be %q but got %q", expected, actual)
}
}
type ReaderFunc func([]byte) (int, error)
func (fn ReaderFunc) Read(data []byte) (int, error) { return fn(data) }
type WriterFunc func([]byte) (int, error)
func (fn WriterFunc) Write(data []byte) (int, error) { return fn(data) }