From 614dfe9b02d69f96f4b222d818ec5ff47f26cb31 Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Mon, 5 Sep 2016 21:13:24 +0000 Subject: [PATCH] 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 TryBot-Result: Gobot Gobot Reviewed-by: Keith Randall --- src/io/multi_test.go | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/io/multi_test.go b/src/io/multi_test.go index 339362eb8c..16e351a879 100644 --- a/src/io/multi_test.go +++ b/src/io/multi_test.go @@ -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) + } +}