mirror of
https://github.com/zrepl/zrepl.git
synced 2025-08-19 03:06:02 +02:00
move implementation to internal/
directory (#828)
This commit is contained in:
committed by
GitHub
parent
b9b9ad10cf
commit
908807bd59
44
internal/util/limitio/limitio_readcloser_test.go
Normal file
44
internal/util/limitio/limitio_readcloser_test.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package limitio
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
type mockRC struct {
|
||||
r io.Reader
|
||||
closed bool
|
||||
}
|
||||
|
||||
func newMockRC(r io.Reader) *mockRC { return &mockRC{r, false} }
|
||||
|
||||
func (m mockRC) Read(b []byte) (int, error) {
|
||||
if m.closed {
|
||||
return 0, io.ErrClosedPipe
|
||||
}
|
||||
return m.r.Read(b)
|
||||
}
|
||||
|
||||
func (m *mockRC) Close() error {
|
||||
m.closed = true
|
||||
return nil
|
||||
}
|
||||
|
||||
func TestReadCloser(t *testing.T) {
|
||||
foobarReader := bytes.NewReader([]byte("foobar2342"))
|
||||
mock := newMockRC(foobarReader)
|
||||
limited := ReadCloser(mock, 6)
|
||||
var buf [20]byte
|
||||
n, err := limited.Read(buf[:])
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, 6, n)
|
||||
assert.Equal(t, buf[:n], []byte("foobar"))
|
||||
n, err = limited.Read(buf[:])
|
||||
assert.Equal(t, 0, n)
|
||||
assert.Equal(t, io.EOF, err)
|
||||
limited.Close()
|
||||
assert.True(t, mock.closed)
|
||||
}
|
Reference in New Issue
Block a user