| 1 | package tools |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "io" |
| 6 | "testing" |
| 7 | |
| 8 | "github.com/git-lfs/git-lfs/v3/errors" |
| 9 | "github.com/stretchr/testify/assert" |
| 10 | ) |
| 11 | |
| 12 | func TestCopyWithCallback(t *testing.T) { |
| 13 | var calls int |
| 14 | allReadSoFar := make([]int64, 0, 2) |
| 15 | |
| 16 | buf := bytes.NewBufferString("BOOYA") |
| 17 | bufSize := buf.Len() |
| 18 | |
| 19 | cb := func(totalSize int64, readSoFar int64, readSinceLast int) error { |
| 20 | calls++ |
| 21 | allReadSoFar = append(allReadSoFar, readSoFar) |
| 22 | |
| 23 | assert.EqualValues(t, bufSize, totalSize) |
| 24 | |
| 25 | return nil |
| 26 | } |
| 27 | |
| 28 | n, err := CopyWithCallback(io.Discard, buf, int64(bufSize), cb) |
| 29 | |
| 30 | // The underlying bytes.Buffer should always return a nil error |
| 31 | // when the last byte is read. |
| 32 | assert.Nil(t, err) |
| 33 | assert.EqualValues(t, bufSize, n) |
| 34 | |
| 35 | assert.Equal(t, 1, calls) |
| 36 | assert.Len(t, allReadSoFar, 1) |
| 37 | assert.EqualValues(t, bufSize, allReadSoFar[0]) |
| 38 | } |
| 39 | |
| 40 | func TestClosingByteReaderNopClose(t *testing.T) { |
| 41 | buf := []byte{0x1} |
| 42 | bufSize := len(buf) |
| 43 | |
| 44 | r := NewClosingByteReader(buf) |
| 45 | |
| 46 | // The underlying bytes.Reader has no Close() method and so reads |
| 47 | // should still proceed. |
| 48 | err := r.Close() |
| 49 | |
| 50 | assert.Nil(t, err) |
| 51 | |
| 52 | p := make([]byte, bufSize+1) |
| 53 | |
| 54 | n, err := r.Read(p) |
| 55 | |
| 56 | // The underlying bytes.Reader should always return |
| 57 | // a nil error when the last byte is read. |
| 58 | assert.Equal(t, bufSize, n) |
| 59 | assert.Nil(t, err) |
| 60 | } |
| 61 | |
| 62 | func TestRetriableReaderReturnsSuccessfulReads(t *testing.T) { |
| 63 | r := NewRetriableReader(bytes.NewBuffer([]byte{0x1, 0x2, 0x3, 0x4})) |
| 64 | |
| 65 | var buf [4]byte |
| 66 | n, err := r.Read(buf[:]) |
| 67 | |
| 68 | assert.Nil(t, err) |
| 69 | assert.Equal(t, 4, n) |
| 70 | assert.Equal(t, []byte{0x1, 0x2, 0x3, 0x4}, buf[:]) |
| 71 | } |
| 72 | |
| 73 | func TestRetriableReaderReturnsEOFs(t *testing.T) { |
| 74 | r := NewRetriableReader(bytes.NewBuffer([]byte{ /* empty */ })) |
| 75 | |
| 76 | var buf [1]byte |
| 77 | n, err := r.Read(buf[:]) |
| 78 | |
| 79 | assert.Equal(t, io.EOF, err) |
| 80 | assert.Equal(t, 0, n) |
| 81 | } |
| 82 | |
| 83 | func TestRetriableReaderMakesErrorsRetriable(t *testing.T) { |
| 84 | expected := errors.New("example error") |
| 85 | |
| 86 | r := NewRetriableReader(&ErrReader{expected}) |
| 87 | |
| 88 | var buf [1]byte |
| 89 | n, err := r.Read(buf[:]) |
| 90 | |
| 91 | assert.Equal(t, 0, n) |
| 92 | assert.EqualError(t, err, "LFS: "+expected.Error()) |
| 93 | assert.True(t, errors.IsRetriableError(err)) |
| 94 | |
| 95 | } |
| 96 | |
| 97 | func TestRetriableReaderDoesNotRewrap(t *testing.T) { |
| 98 | // expected is already "retriable", as would be the case if the |
| 99 | // underlying reader was a *RetriableReader itself. |
| 100 | expected := errors.NewRetriableError(errors.New("example error")) |
| 101 | |
| 102 | r := NewRetriableReader(&ErrReader{expected}) |
| 103 | |
| 104 | var buf [1]byte |
| 105 | n, err := r.Read(buf[:]) |
| 106 | |
| 107 | assert.Equal(t, 0, n) |
| 108 | // errors.NewRetriableError wraps the given error with the prefix |
| 109 | // message "LFS", so these two errors should be equal, indicating that |
| 110 | // the RetriableReader did not re-wrap the error it received. |
| 111 | assert.EqualError(t, err, expected.Error()) |
| 112 | assert.True(t, errors.IsRetriableError(err)) |
| 113 | |
| 114 | } |
| 115 | |
| 116 | // ErrReader implements io.Reader and only returns errors. |
| 117 | type ErrReader struct { |
| 118 | // err is the error that this reader will return. |
| 119 | err error |
| 120 | } |
| 121 | |
| 122 | // Read implements io.Reader#Read, and returns (0, e.err). |
| 123 | func (e *ErrReader) Read(p []byte) (n int, err error) { |
| 124 | return 0, e.err |
| 125 | } |