Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

progress: support ReadCloser #14

Merged
merged 1 commit into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions progress/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package progress

import "io"

// Reader is an [io.Reader] that reports the number of bytes read from it via a
// callback. The callback is called at most every "updateInterval" bytes. The
// updateInterval can be set using the [Reader.WithUpdateInterval] method.
// Reader is an [io.ReadCloser] that reports the number of bytes read from it
// via a callback. The callback is called at most every "updateInterval" bytes.
// The updateInterval can be set using the [Reader.WithUpdateInterval] method.
//
// The following is an example of how to use [Reader] to report the progress of
// reading from a file:
Expand Down Expand Up @@ -44,4 +44,11 @@ func (r *Reader) Read(p []byte) (n int, err error) {
return n, nil
}

var _ io.Reader = (*Reader)(nil)
func (r *Reader) Close() error {
if closer, ok := r.inner.(io.Closer); ok {
return closer.Close()
}
return nil
}

var _ io.ReadCloser = (*Reader)(nil)
33 changes: 33 additions & 0 deletions progress/reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package progress_test

import (
"bytes"
"errors"
"io"
"testing"

Expand All @@ -24,3 +25,35 @@ func TestReader(t *testing.T) {
assert.Greater(t, len(progressUpdates), 1)
assert.IsIncreasing(t, progressUpdates)
}

type testReader struct {
*bytes.Reader
closed bool
}

func (r *testReader) Close() error {
if r.closed {
return errors.New("already closed")
}
r.closed = true
return nil
}

func TestReadCloser(t *testing.T) {
readCloser := &testReader{Reader: bytes.NewReader(bytes.Repeat([]byte{42}, 1024*1024))}

var progressUpdates []int
progressReader := progress.NewReader(readCloser, func(readBytes int) {
progressUpdates = append(progressUpdates, readBytes)
})

data, err := io.ReadAll(progressReader)
assert.NoError(t, err)
assert.Equal(t, data, bytes.Repeat([]byte{42}, 1024*1024))

assert.Greater(t, len(progressUpdates), 1)
assert.IsIncreasing(t, progressUpdates)

assert.NoError(t, progressReader.Close())
assert.ErrorContains(t, progressReader.Close(), "already closed")
}