From 4b4a2e960636d518c5c223f241d5780624740e2c Mon Sep 17 00:00:00 2001 From: Jacob Vosmaer Date: Wed, 12 Jan 2022 12:12:54 +0000 Subject: streamio.Reader: remember errors If there has been an error, subsequent calls to Read should keep returning that error. Changelog: fixed --- streamio/stream.go | 2 +- streamio/stream_test.go | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) (limited to 'streamio') diff --git a/streamio/stream.go b/streamio/stream.go index 0aab25315..68e6bd687 100644 --- a/streamio/stream.go +++ b/streamio/stream.go @@ -23,7 +23,7 @@ type receiveReader struct { } func (rr *receiveReader) Read(p []byte) (int, error) { - if len(rr.data) == 0 { + if len(rr.data) == 0 && rr.err == nil { rr.data, rr.err = rr.receiver() } diff --git a/streamio/stream_test.go b/streamio/stream_test.go index c91d5a58f..8f7c841fd 100644 --- a/streamio/stream_test.go +++ b/streamio/stream_test.go @@ -2,6 +2,7 @@ package streamio import ( "bytes" + "errors" "fmt" "io" "strings" @@ -60,6 +61,25 @@ func TestReadSizes(t *testing.T) { }) } +func TestRead_rememberError(t *testing.T) { + firstRead := true + myError := errors.New("hello world") + r := NewReader(func() ([]byte, error) { + if firstRead { + firstRead = false + return nil, myError + } + panic("should never be reached") + }) + + // Intentionally call Read more than once. We want the error to be + // sticky. + for i := 0; i < 10; i++ { + _, err := r.Read(nil) + require.Equal(t, err, myError) + } +} + func receiverFromReader(r io.Reader) func() ([]byte, error) { return func() ([]byte, error) { data := make([]byte, 10) -- cgit v1.2.3