Commit 614dfe9b authored by Brad Fitzpatrick's avatar Brad Fitzpatrick

io: add test that MultiReader zeros exhausted Readers

Updates #16983
Updates #16996

Change-Id: I76390766385b2668632c95e172b2d243d7f66651
Reviewed-on: https://go-review.googlesource.com/28771
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: default avatarKeith Randall <khr@golang.org>
parent 9e040979
......@@ -14,6 +14,7 @@ import (
"runtime"
"strings"
"testing"
"time"
)
func TestMultiReader(t *testing.T) {
......@@ -234,3 +235,32 @@ func TestMultiReaderFinalEOF(t *testing.T) {
t.Errorf("got %v, %v; want 1, EOF", n, err)
}
}
func TestMultiReaderFreesExhaustedReaders(t *testing.T) {
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)
}
}
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