Commit d5caea77 authored by Cuong Manh Le's avatar Cuong Manh Le Committed by Brad Fitzpatrick

bufio: save error from underlying ReadFrom

Updates #35194

Change-Id: Ib854bc6250ddeb606d6ff6240179e23b98e4ac62
Reviewed-on: https://go-review.googlesource.com/c/go/+/203999
Run-TryBot: Cuong Manh Le <cuong.manhle.vn@gmail.com>
Reviewed-by: default avatarIan Lance Taylor <iant@golang.org>
Reviewed-by: default avatarBrad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
parent 02a5502a
...@@ -708,7 +708,9 @@ func (b *Writer) WriteString(s string) (int, error) { ...@@ -708,7 +708,9 @@ func (b *Writer) WriteString(s string) (int, error) {
func (b *Writer) ReadFrom(r io.Reader) (n int64, err error) { func (b *Writer) ReadFrom(r io.Reader) (n int64, err error) {
if b.Buffered() == 0 { if b.Buffered() == 0 {
if w, ok := b.wr.(io.ReaderFrom); ok { if w, ok := b.wr.(io.ReaderFrom); ok {
return w.ReadFrom(r) n, err = w.ReadFrom(r)
b.err = err
return n, err
} }
} }
var m int var m int
......
...@@ -1535,6 +1535,26 @@ func TestPartialReadEOF(t *testing.T) { ...@@ -1535,6 +1535,26 @@ func TestPartialReadEOF(t *testing.T) {
} }
} }
type writerWithReadFromError struct{}
func (w writerWithReadFromError) ReadFrom(r io.Reader) (int64, error) {
return 0, errors.New("writerWithReadFromError error")
}
func (w writerWithReadFromError) Write(b []byte) (n int, err error) {
return 10, nil
}
func TestWriterReadFromMustSetUnderlyingError(t *testing.T) {
var wr = NewWriter(writerWithReadFromError{})
if _, err := wr.ReadFrom(strings.NewReader("test2")); err == nil {
t.Fatal("expected ReadFrom returns error, got nil")
}
if _, err := wr.Write([]byte("123")); err == nil {
t.Fatal("expected Write returns error, got nil")
}
}
func BenchmarkReaderCopyOptimal(b *testing.B) { func BenchmarkReaderCopyOptimal(b *testing.B) {
// Optimal case is where the underlying reader implements io.WriterTo // Optimal case is where the underlying reader implements io.WriterTo
srcBuf := bytes.NewBuffer(make([]byte, 8192)) srcBuf := bytes.NewBuffer(make([]byte, 8192))
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment