Skip to content
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
6 changes: 6 additions & 0 deletions ring_buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,12 @@ func (r *RingBuffer) ReadFrom(rd io.Reader) (n int64, err error) {
continue
}
if rerr != nil && rerr != io.EOF {
// Keep the bytes that came with the error.
if nr > 0 {
r.w = (startW + nr) % r.size
r.isFull = r.r == r.w
n += int64(nr)
}
err = r.setErr(rerr, true)
r.mu.Unlock()
break
Expand Down
17 changes: 17 additions & 0 deletions ring_buffer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -973,6 +973,7 @@ func (e *errormock) Read(p []byte) (n int, err error) {

case len(p) > e.rleft:
n = e.rleft
err = e.rerr

default:
n = len(p)
Expand Down Expand Up @@ -1031,6 +1032,22 @@ func TestRingBuffer_ReadFrom_Error(t *testing.T) {
}
}

func TestRingBuffer_ReadFrom_ErrorKeepsData(t *testing.T) {
rb := New(16).SetBlocking(true)
tester := &errormock{rerr: io.ErrUnexpectedEOF, rleft: 5}

copied, err := rb.ReadFrom(tester)
if err != io.ErrUnexpectedEOF {
t.Errorf("expect io.ErrUnexpectedEOF but got %v", err)
}
if copied != 5 {
t.Errorf("expect 5 bytes copied but got %d", copied)
}
if got := rb.Length(); got != 5 {
t.Errorf("expect 5 bytes buffered but got %d", got)
}
}

func TestRingBuffer_WriteTo_Error(t *testing.T) {
const bufsize = 4 << 10

Expand Down
Loading