1
0
mirror of https://github.com/golang/go synced 2024-11-18 04:04:49 -07:00

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: Keith Randall <khr@golang.org>
This commit is contained in:
Brad Fitzpatrick 2016-09-05 21:13:24 +00:00
parent 9e040979bd
commit 614dfe9b02

View File

@ -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)
}
}