forked from git-lfs/git-lfs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcopycallback_test.go
More file actions
100 lines (75 loc) · 1.94 KB
/
copycallback_test.go
File metadata and controls
100 lines (75 loc) · 1.94 KB
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
package tools
import (
"io"
"sync/atomic"
"testing"
"github.com/stretchr/testify/assert"
)
func TestCopyCallbackReaderCallsCallbackUnderfilledBuffer(t *testing.T) {
var (
calls uint32
actualTotalSize int64
actualReadSoFar int64
actualReadSinceLast int
)
cb := func(totalSize int64, readSoFar int64, readSinceLast int) error {
atomic.AddUint32(&calls, 1)
actualTotalSize = totalSize
actualReadSoFar = readSoFar
actualReadSinceLast = readSinceLast
return nil
}
buf := []byte{0x1}
r := &CallbackReader{
C: cb,
TotalSize: 3,
ReadSize: 2,
Reader: &EOFReader{b: buf},
}
p := make([]byte, len(buf)+1)
n, err := r.Read(p)
assert.Equal(t, 1, n)
assert.Nil(t, err)
assert.EqualValues(t, 1, calls, "expected 1 call(s) to callback, got %d", calls)
assert.EqualValues(t, 3, actualTotalSize)
assert.EqualValues(t, 2+1, actualReadSoFar)
assert.EqualValues(t, 1, actualReadSinceLast)
}
type EOFReader struct {
b []byte
i int
}
var _ io.Reader = (*EOFReader)(nil)
func (r *EOFReader) Read(p []byte) (n int, err error) {
n = copy(p, r.b[r.i:])
r.i += n
if r.i == len(r.b) {
err = io.EOF
}
return
}
func TestEOFReaderReturnsEOFs(t *testing.T) {
r := EOFReader{[]byte{0x1}, 0}
p := make([]byte, 2)
n, err := r.Read(p)
assert.Equal(t, 1, n)
assert.Equal(t, io.EOF, err)
}
func TestBodyCallbackReaderCountsReads(t *testing.T) {
br := NewByteBodyWithCallback([]byte{0x1, 0x2, 0x3, 0x4}, 4, nil)
assert.EqualValues(t, 0, br.readSize)
p := make([]byte, 8)
n, err := br.Read(p)
assert.Equal(t, 4, n)
assert.Nil(t, err)
assert.EqualValues(t, 4, br.readSize)
}
func TestBodyCallbackReaderUpdatesOffsetOnSeek(t *testing.T) {
br := NewByteBodyWithCallback([]byte{0x1, 0x2, 0x3, 0x4}, 4, nil)
br.Seek(1, io.SeekStart)
assert.EqualValues(t, 1, br.readSize)
br.Seek(1, io.SeekCurrent)
assert.EqualValues(t, 2, br.readSize)
br.Seek(-1, io.SeekEnd)
assert.EqualValues(t, 3, br.readSize)
}