diff options
Diffstat (limited to 'libgo/go/io/multi_test.go')
-rw-r--r-- | libgo/go/io/multi_test.go | 60 |
1 files changed, 59 insertions, 1 deletions
diff --git a/libgo/go/io/multi_test.go b/libgo/go/io/multi_test.go index 447e7f59635..70f2a48e9ea 100644 --- a/libgo/go/io/multi_test.go +++ b/libgo/go/io/multi_test.go @@ -14,6 +14,7 @@ import ( "runtime" "strings" "testing" + "time" ) func TestMultiReader(t *testing.T) { @@ -212,7 +213,7 @@ func (b byteAndEOFReader) Read(p []byte) (n int, err error) { return 1, EOF } -// In Go 1.7, this yielded bytes forever. +// This used to yield bytes forever; issue 16795. func TestMultiReaderSingleByteWithEOF(t *testing.T) { got, err := ioutil.ReadAll(LimitReader(MultiReader(byteAndEOFReader('a'), byteAndEOFReader('b')), 10)) if err != nil { @@ -235,3 +236,60 @@ func TestMultiReaderFinalEOF(t *testing.T) { t.Errorf("got %v, %v; want 1, EOF", n, err) } } + +func TestMultiReaderFreesExhaustedReaders(t *testing.T) { + if runtime.Compiler == "gccgo" { + t.Skip("skipping finalizer test on gccgo with conservative GC") + } + + var mr Reader + closed := make(chan struct{}) + { + buf1 := bytes.NewReader([]byte("foo")) + buf2 := bytes.NewReader([]byte("bar")) + mr = MultiReader(buf1, buf2) + runtime.SetFinalizer(buf1, func(*bytes.Reader) { + close(closed) + }) + } + + buf := make([]byte, 4) + if n, err := ReadFull(mr, buf); err != nil || string(buf) != "foob" { + t.Fatalf(`ReadFull = %d (%q), %v; want 3, "foo", nil`, n, buf[:n], err) + } + + runtime.GC() + select { + case <-closed: + case <-time.After(5 * time.Second): + t.Fatal("timeout waiting for collection of buf1") + } + + if n, err := ReadFull(mr, buf[:2]); err != nil || string(buf[:2]) != "ar" { + t.Fatalf(`ReadFull = %d (%q), %v; want 2, "ar", nil`, n, buf[:n], err) + } +} + +func TestInterleavedMultiReader(t *testing.T) { + r1 := strings.NewReader("123") + r2 := strings.NewReader("45678") + + mr1 := MultiReader(r1, r2) + mr2 := MultiReader(mr1) + + buf := make([]byte, 4) + + // Have mr2 use mr1's []Readers. + // Consume r1 (and clear it for GC to handle) and consume part of r2. + n, err := ReadFull(mr2, buf) + if got := string(buf[:n]); got != "1234" || err != nil { + t.Errorf(`ReadFull(mr2) = (%q, %v), want ("1234", nil)`, got, err) + } + + // Consume the rest of r2 via mr1. + // This should not panic even though mr2 cleared r1. + n, err = ReadFull(mr1, buf) + if got := string(buf[:n]); got != "5678" || err != nil { + t.Errorf(`ReadFull(mr1) = (%q, %v), want ("5678", nil)`, got, err) + } +} |