Debian Codegraph @

tools/copycallback_test.go

raw symbols SCIP
1 package tools
2
3 import (
4 "bytes"
5 "errors"
6 "io"
7 "os"
8 "testing"
9
10 "github.com/git-lfs/git-lfs/v3/internal/testutil"
11 "github.com/stretchr/testify/assert"
12 "github.com/stretchr/testify/require"
13 )
14
15 func TestBothCallbackReadersInvokeCallbackOnEagerEOF(t *testing.T) {
16 var (
17 calls int
18 actualTotalSize int64
19 actualReadSoFar int64
20 actualReadSinceLast int
21 )
22
23 cb := func(totalSize int64, readSoFar int64, readSinceLast int) error {
24 calls++
25 actualTotalSize = totalSize
26 actualReadSoFar = readSoFar
27 actualReadSinceLast = readSinceLast
28
29 return nil
30 }
31
32 // We simulate a larger buffer which has not yet been fully read.
33 buf := []byte{0x1}
34 bufSize := len(buf)
35 initialTotalSize := 3 * int64(bufSize)
36 initialReadSize := initialTotalSize - int64(bufSize)
37
38 r := NewCallbackReader(testutil.NewEagerEOFByteReader(buf), initialTotalSize, cb)
39 br := NewBodyWithCallback(testutil.NewEagerEOFByteReader(buf), initialTotalSize, cb)
40 r.state.readSize = initialReadSize
41 br.state.readSize = initialReadSize
42
43 p := make([]byte, bufSize+1)
44
45 for _, reader := range []io.Reader{r, br} {
46 t.Logf("testing with reader: %T", reader)
47
48 n, err := reader.Read(p)
49
50 assert.Equal(t, bufSize, n)
51 assert.Equal(t, io.EOF, err)
52
53 assert.Equal(t, 1, calls, "expected 1 call to callback, got %d", calls)
54 assert.EqualValues(t, initialTotalSize, actualTotalSize)
55 assert.EqualValues(t, initialTotalSize, actualReadSoFar)
56 assert.Equal(t, bufSize, actualReadSinceLast)
57
58 // Read again and check that no callback is made after last
59 // byte has been read (since the simulated initial total
60 // matched the simulated number of bytes read).
61 calls = 0
62
63 n, err = reader.Read(p)
64
65 assert.Zero(t, n)
66 assert.Equal(t, io.EOF, err)
67
68 assert.Zero(t, calls, "expected no call to callback, got %d", calls)
69
70 calls = 0
71 actualTotalSize = 0
72 actualReadSoFar = 0
73 actualReadSinceLast = 0
74 }
75 }
76
77 func TestBothCallbackReadersCountReads(t *testing.T) {
78 var actualReadSoFar int64
79
80 cb := func(totalSize int64, readSoFar int64, readSinceLast int) error {
81 actualReadSoFar = readSoFar
82
83 return nil
84 }
85
86 buf := []byte{0x1, 0x2, 0x3, 0x4}
87 bufSize := len(buf)
88
89 r := NewCallbackReader(bytes.NewReader(buf), int64(bufSize), cb)
90 br := NewBodyWithCallback(NewClosingByteReader(buf), int64(bufSize), cb)
91
92 p := make([]byte, 1)
93
94 for _, reader := range []io.Reader{r, br} {
95 t.Logf("testing with reader: %T", reader)
96
97 for i := 1; i <= bufSize; i++ {
98 n, err := reader.Read(p)
99
100 // The underlying bytes.Reader should always return
101 // a nil error when the last byte is read.
102 assert.Equal(t, 1, n)
103 assert.Nil(t, err)
104
105 assert.EqualValues(t, i, actualReadSoFar)
106 }
107
108 actualReadSoFar = 0
109 }
110 }
111
112 func TestBothCallbackReadersPreferCallbackErrorOverEOF(t *testing.T) {
113 cbErr := errors.New("callback error")
114 cb := func(totalSize int64, readSoFar int64, readSinceLast int) error {
115 return cbErr
116 }
117
118 buf := []byte{0x1, 0x2}
119 bufSize := len(buf)
120
121 r := NewCallbackReader(testutil.NewEagerEOFByteReader(buf), int64(bufSize), cb)
122 br := NewBodyWithCallback(testutil.NewEagerEOFByteReader(buf), int64(bufSize), cb)
123
124 p := make([]byte, bufSize-1)
125
126 for _, reader := range []io.Reader{r, br} {
127 t.Logf("testing with reader: %T", reader)
128
129 n, err := reader.Read(p)
130
131 assert.Equal(t, bufSize-1, n)
132 assert.Equal(t, cbErr, err)
133
134 n, err = reader.Read(p)
135
136 // We expect the EOF from EagerEOFByteReader's Read() method
137 // to be replaced with the callback function's own error.
138 assert.Equal(t, 1, n)
139 assert.Equal(t, cbErr, err)
140
141 // We expect no callback to be performed when no bytes are
142 // available to be read, so no error should be returned.
143 n, err = reader.Read(p)
144
145 assert.Zero(t, n)
146 assert.Equal(t, io.EOF, err)
147 }
148 }
149
150 func TestBothCallbackReadersSkipCallbackAfterReadError(t *testing.T) {
151 var calls int
152
153 cb := func(totalSize int64, readSoFar int64, readSinceLast int) error {
154 calls++
155
156 return nil
157 }
158
159 buf := []byte{0x1, 0x2}
160 bufSize := len(buf)
161
162 r := NewCallbackReader(testutil.NewErrReader(io.ErrUnexpectedEOF), int64(bufSize), cb)
163 br := NewBodyWithCallback(testutil.NewErrReader(io.ErrUnexpectedEOF), int64(bufSize), cb)
164
165 p := make([]byte, bufSize-1)
166
167 for _, reader := range []io.Reader{r, br} {
168 t.Logf("testing with reader: %T", reader)
169
170 n, err := reader.Read(p)
171
172 assert.Zero(t, n)
173 assert.Equal(t, io.ErrUnexpectedEOF, err)
174
175 assert.Zero(t, calls, "expected no call to callback, got %d", calls)
176 }
177 }
178
179 func TestBothCallbackReadersInvokeCallbackOnEOFWhenTotalSizeIncorrect(t *testing.T) {
180 var (
181 calls int
182 actualTotalSize int64
183 actualReadSoFar int64
184 )
185
186 cb := func(totalSize int64, readSoFar int64, readSinceLast int) error {
187 calls++
188 actualTotalSize = totalSize
189 actualReadSoFar = readSoFar
190
191 return nil
192 }
193
194 buf := []byte{0x1, 0x2}
195 bufSize := len(buf)
196
197 p := make([]byte, bufSize-1)
198 readBufSize := len(p)
199
200 testCases := []struct {
201 description string
202 initialTotalSize int64
203 }{
204 {"initial total unknown", -1},
205 {"initial total overestimated", int64(bufSize) + 1},
206 {"initial total underestimated", int64(bufSize) - 1},
207 }
208
209 for _, tc := range testCases {
210 t.Logf("testing with %s", tc.description)
211
212 r := NewCallbackReader(testutil.NewEagerEOFByteReader(buf), tc.initialTotalSize, cb)
213 br := NewBodyWithCallback(testutil.NewEagerEOFByteReader(buf), tc.initialTotalSize, cb)
214
215 for _, reader := range []io.Reader{r, br} {
216 t.Logf("testing with reader: %T", reader)
217
218 n, err := reader.Read(p)
219
220 assert.Equal(t, readBufSize, n)
221 assert.Nil(t, err)
222
223 assert.Equal(t, 1, calls, "expected 1 call to callback, got %d", calls)
224 assert.EqualValues(t, tc.initialTotalSize, actualTotalSize)
225 assert.EqualValues(t, readBufSize, actualReadSoFar)
226
227 n, err = reader.Read(p)
228
229 // We expect an immediate EOF from the EagerEOFByteReader's
230 // Read() method once the final byte has been read.
231 assert.Equal(t, 1, n)
232 assert.Equal(t, io.EOF, err)
233
234 assert.Equal(t, 2, calls, "expected 2 calls to callback, got %d", calls)
235 assert.EqualValues(t, bufSize, actualTotalSize)
236 assert.EqualValues(t, bufSize, actualReadSoFar)
237
238 // Read again and check that no callback is made after last
239 // byte has been read (since the updated total matched the
240 // number of bytes read).
241 calls = 0
242
243 n, err = reader.Read(p)
244
245 assert.Zero(t, n)
246 assert.Equal(t, io.EOF, err)
247
248 assert.Zero(t, calls, "expected no call to callback, got %d", calls)
249
250 actualTotalSize = 0
251 actualReadSoFar = 0
252 }
253 }
254 }
255
256 func TestBodyCallbackReaderUpdatesOffsetOnSeek(t *testing.T) {
257 var calls int
258
259 cb := func(totalSize int64, readSoFar int64, readSinceLast int) error {
260 calls++
261
262 return nil
263 }
264
265 buf := []byte{0x1, 0x2, 0x3, 0x4}
266 bufSize := len(buf)
267
268 br := NewBodyWithCallback(NewClosingByteReader(buf), int64(bufSize), cb)
269
270 offset := 1
271 br.Seek(int64(offset), io.SeekStart)
272 assert.EqualValues(t, offset, br.state.readSize)
273
274 offset++
275 br.Seek(1, io.SeekCurrent)
276 assert.EqualValues(t, offset, br.state.readSize)
277
278 p := make([]byte, bufSize)
279
280 n, err := br.Read(p)
281
282 // The underlying bytes.Reader should always return
283 // a nil error when the last byte is read.
284 assert.Equal(t, bufSize-offset, n)
285 assert.Nil(t, err)
286 assert.Equal(t, buf[offset:], p[:bufSize-offset])
287
288 assert.Equal(t, 1, calls, "expected 1 call to callback, got %d", calls)
289
290 br.Seek(-1, io.SeekEnd)
291 assert.EqualValues(t, bufSize-1, br.state.readSize)
292
293 n, err = br.Read(p)
294
295 assert.Equal(t, 1, n)
296 assert.Nil(t, err)
297 assert.Equal(t, buf[bufSize-1], p[0])
298
299 assert.Equal(t, 2, calls, "expected 2 calls to callback, got %d", calls)
300 }
301
302 func TestBodyCallbackReaderResetsProgress(t *testing.T) {
303 var (
304 calls int
305 actualTotalSize int64
306 actualReadSoFar int64
307 actualReadSinceLast int
308 )
309
310 cb := func(totalSize int64, readSoFar int64, readSinceLast int) error {
311 calls++
312 actualTotalSize = totalSize
313 actualReadSoFar = readSoFar
314 actualReadSinceLast = readSinceLast
315
316 return nil
317 }
318
319 buf := []byte{0x1, 0x2, 0x3, 0x4}
320 bufSize := len(buf)
321
322 br := NewBodyWithCallback(NewClosingByteReader(buf), int64(bufSize), cb)
323
324 p := make([]byte, bufSize-1)
325 readBufSize := len(p)
326
327 n, err := br.Read(p)
328
329 assert.Equal(t, readBufSize, n)
330 assert.Nil(t, err)
331
332 assert.Equal(t, 1, calls, "expected 1 call to callback, got %d", calls)
333 assert.EqualValues(t, bufSize, actualTotalSize)
334 assert.EqualValues(t, readBufSize, actualReadSoFar)
335 assert.Equal(t, readBufSize, actualReadSinceLast)
336
337 // Note that ResetProgress() is only used to pass a decrement
338 // to the callback, as the caller is expected to create a new
339 // underlying buffer and retry the read operation.
340 br.ResetProgress()
341
342 assert.Equal(t, 2, calls, "expected 2 calls to callback, got %d", calls)
343 assert.EqualValues(t, bufSize, actualTotalSize)
344 assert.EqualValues(t, readBufSize, actualReadSoFar)
345 assert.Equal(t, -readBufSize, actualReadSinceLast)
346 }
347
348 func TestFileBodyCallbackReaderIgnoresClose(t *testing.T) {
349 buf := []byte{0x1, 0x2, 0x3, 0x4}
350 bufSize := len(buf)
351
352 filePath := t.TempDir() + "/testFileBodyCallback"
353
354 err := os.WriteFile(filePath, buf, 0644)
355 require.Nil(t, err)
356
357 f, err := os.Open(filePath)
358 require.Nil(t, err)
359 defer f.Close()
360
361 fbr := NewFileBodyWithCallback(f, int64(bufSize), nil)
362
363 // The underlying os.File should not be closed and reads should
364 // still proceed.
365 err = fbr.Close()
366
367 assert.Nil(t, err)
368
369 p := make([]byte, bufSize-1)
370
371 n, err := fbr.Read(p)
372
373 assert.Equal(t, bufSize-1, n)
374 assert.Nil(t, err)
375
376 // Closing the underlying os.File should result in a read error.
377 err = f.Close()
378
379 assert.Nil(t, err)
380
381 n, err = fbr.Read(p)
382
383 assert.Error(t, err)
384 }