github.com/dannin/go@v0.0.0-20161031215817-d35dfd405eaa/src/io/multi_test.go (about)

     1  // Copyright 2010 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package io_test
     6  
     7  import (
     8  	"bytes"
     9  	"crypto/sha1"
    10  	"errors"
    11  	"fmt"
    12  	. "io"
    13  	"io/ioutil"
    14  	"runtime"
    15  	"strings"
    16  	"testing"
    17  	"time"
    18  )
    19  
    20  func TestMultiReader(t *testing.T) {
    21  	var mr Reader
    22  	var buf []byte
    23  	nread := 0
    24  	withFooBar := func(tests func()) {
    25  		r1 := strings.NewReader("foo ")
    26  		r2 := strings.NewReader("")
    27  		r3 := strings.NewReader("bar")
    28  		mr = MultiReader(r1, r2, r3)
    29  		buf = make([]byte, 20)
    30  		tests()
    31  	}
    32  	expectRead := func(size int, expected string, eerr error) {
    33  		nread++
    34  		n, gerr := mr.Read(buf[0:size])
    35  		if n != len(expected) {
    36  			t.Errorf("#%d, expected %d bytes; got %d",
    37  				nread, len(expected), n)
    38  		}
    39  		got := string(buf[0:n])
    40  		if got != expected {
    41  			t.Errorf("#%d, expected %q; got %q",
    42  				nread, expected, got)
    43  		}
    44  		if gerr != eerr {
    45  			t.Errorf("#%d, expected error %v; got %v",
    46  				nread, eerr, gerr)
    47  		}
    48  		buf = buf[n:]
    49  	}
    50  	withFooBar(func() {
    51  		expectRead(2, "fo", nil)
    52  		expectRead(5, "o ", nil)
    53  		expectRead(5, "bar", nil)
    54  		expectRead(5, "", EOF)
    55  	})
    56  	withFooBar(func() {
    57  		expectRead(4, "foo ", nil)
    58  		expectRead(1, "b", nil)
    59  		expectRead(3, "ar", nil)
    60  		expectRead(1, "", EOF)
    61  	})
    62  	withFooBar(func() {
    63  		expectRead(5, "foo ", nil)
    64  	})
    65  }
    66  
    67  func TestMultiWriter(t *testing.T) {
    68  	sink := new(bytes.Buffer)
    69  	// Hide bytes.Buffer's WriteString method:
    70  	testMultiWriter(t, struct {
    71  		Writer
    72  		fmt.Stringer
    73  	}{sink, sink})
    74  }
    75  
    76  func TestMultiWriter_String(t *testing.T) {
    77  	testMultiWriter(t, new(bytes.Buffer))
    78  }
    79  
    80  // test that a multiWriter.WriteString calls results in at most 1 allocation,
    81  // even if multiple targets don't support WriteString.
    82  func TestMultiWriter_WriteStringSingleAlloc(t *testing.T) {
    83  	var sink1, sink2 bytes.Buffer
    84  	type simpleWriter struct { // hide bytes.Buffer's WriteString
    85  		Writer
    86  	}
    87  	mw := MultiWriter(simpleWriter{&sink1}, simpleWriter{&sink2})
    88  	allocs := int(testing.AllocsPerRun(1000, func() {
    89  		WriteString(mw, "foo")
    90  	}))
    91  	if allocs != 1 {
    92  		t.Errorf("num allocations = %d; want 1", allocs)
    93  	}
    94  }
    95  
    96  type writeStringChecker struct{ called bool }
    97  
    98  func (c *writeStringChecker) WriteString(s string) (n int, err error) {
    99  	c.called = true
   100  	return len(s), nil
   101  }
   102  
   103  func (c *writeStringChecker) Write(p []byte) (n int, err error) {
   104  	return len(p), nil
   105  }
   106  
   107  func TestMultiWriter_StringCheckCall(t *testing.T) {
   108  	var c writeStringChecker
   109  	mw := MultiWriter(&c)
   110  	WriteString(mw, "foo")
   111  	if !c.called {
   112  		t.Error("did not see WriteString call to writeStringChecker")
   113  	}
   114  }
   115  
   116  func testMultiWriter(t *testing.T, sink interface {
   117  	Writer
   118  	fmt.Stringer
   119  }) {
   120  	sha1 := sha1.New()
   121  	mw := MultiWriter(sha1, sink)
   122  
   123  	sourceString := "My input text."
   124  	source := strings.NewReader(sourceString)
   125  	written, err := Copy(mw, source)
   126  
   127  	if written != int64(len(sourceString)) {
   128  		t.Errorf("short write of %d, not %d", written, len(sourceString))
   129  	}
   130  
   131  	if err != nil {
   132  		t.Errorf("unexpected error: %v", err)
   133  	}
   134  
   135  	sha1hex := fmt.Sprintf("%x", sha1.Sum(nil))
   136  	if sha1hex != "01cb303fa8c30a64123067c5aa6284ba7ec2d31b" {
   137  		t.Error("incorrect sha1 value")
   138  	}
   139  
   140  	if sink.String() != sourceString {
   141  		t.Errorf("expected %q; got %q", sourceString, sink.String())
   142  	}
   143  }
   144  
   145  // Test that MultiReader copies the input slice and is insulated from future modification.
   146  func TestMultiReaderCopy(t *testing.T) {
   147  	slice := []Reader{strings.NewReader("hello world")}
   148  	r := MultiReader(slice...)
   149  	slice[0] = nil
   150  	data, err := ioutil.ReadAll(r)
   151  	if err != nil || string(data) != "hello world" {
   152  		t.Errorf("ReadAll() = %q, %v, want %q, nil", data, err, "hello world")
   153  	}
   154  }
   155  
   156  // Test that MultiWriter copies the input slice and is insulated from future modification.
   157  func TestMultiWriterCopy(t *testing.T) {
   158  	var buf bytes.Buffer
   159  	slice := []Writer{&buf}
   160  	w := MultiWriter(slice...)
   161  	slice[0] = nil
   162  	n, err := w.Write([]byte("hello world"))
   163  	if err != nil || n != 11 {
   164  		t.Errorf("Write(`hello world`) = %d, %v, want 11, nil", n, err)
   165  	}
   166  	if buf.String() != "hello world" {
   167  		t.Errorf("buf.String() = %q, want %q", buf.String(), "hello world")
   168  	}
   169  }
   170  
   171  // readerFunc is an io.Reader implemented by the underlying func.
   172  type readerFunc func(p []byte) (int, error)
   173  
   174  func (f readerFunc) Read(p []byte) (int, error) {
   175  	return f(p)
   176  }
   177  
   178  // Test that MultiReader properly flattens chained multiReaders when Read is called
   179  func TestMultiReaderFlatten(t *testing.T) {
   180  	pc := make([]uintptr, 1000) // 1000 should fit the full stack
   181  	var myDepth = runtime.Callers(0, pc)
   182  	var readDepth int // will contain the depth from which fakeReader.Read was called
   183  	var r Reader = MultiReader(readerFunc(func(p []byte) (int, error) {
   184  		readDepth = runtime.Callers(1, pc)
   185  		return 0, errors.New("irrelevant")
   186  	}))
   187  
   188  	// chain a bunch of multiReaders
   189  	for i := 0; i < 100; i++ {
   190  		r = MultiReader(r)
   191  	}
   192  
   193  	r.Read(nil) // don't care about errors, just want to check the call-depth for Read
   194  
   195  	if readDepth != myDepth+2 { // 2 should be multiReader.Read and fakeReader.Read
   196  		t.Errorf("multiReader did not flatten chained multiReaders: expected readDepth %d, got %d",
   197  			myDepth+2, readDepth)
   198  	}
   199  }
   200  
   201  // byteAndEOFReader is a Reader which reads one byte (the underlying
   202  // byte) and io.EOF at once in its Read call.
   203  type byteAndEOFReader byte
   204  
   205  func (b byteAndEOFReader) Read(p []byte) (n int, err error) {
   206  	if len(p) == 0 {
   207  		// Read(0 bytes) is useless. We expect no such useless
   208  		// calls in this test.
   209  		panic("unexpected call")
   210  	}
   211  	p[0] = byte(b)
   212  	return 1, EOF
   213  }
   214  
   215  // This used to yield bytes forever; issue 16795.
   216  func TestMultiReaderSingleByteWithEOF(t *testing.T) {
   217  	got, err := ioutil.ReadAll(LimitReader(MultiReader(byteAndEOFReader('a'), byteAndEOFReader('b')), 10))
   218  	if err != nil {
   219  		t.Fatal(err)
   220  	}
   221  	const want = "ab"
   222  	if string(got) != want {
   223  		t.Errorf("got %q; want %q", got, want)
   224  	}
   225  }
   226  
   227  // Test that a reader returning (n, EOF) at the end of an MultiReader
   228  // chain continues to return EOF on its final read, rather than
   229  // yielding a (0, EOF).
   230  func TestMultiReaderFinalEOF(t *testing.T) {
   231  	r := MultiReader(bytes.NewReader(nil), byteAndEOFReader('a'))
   232  	buf := make([]byte, 2)
   233  	n, err := r.Read(buf)
   234  	if n != 1 || err != EOF {
   235  		t.Errorf("got %v, %v; want 1, EOF", n, err)
   236  	}
   237  }
   238  
   239  func TestMultiReaderFreesExhaustedReaders(t *testing.T) {
   240  	var mr Reader
   241  	closed := make(chan struct{})
   242  	{
   243  		buf1 := bytes.NewReader([]byte("foo"))
   244  		buf2 := bytes.NewReader([]byte("bar"))
   245  		mr = MultiReader(buf1, buf2)
   246  		runtime.SetFinalizer(buf1, func(*bytes.Reader) {
   247  			close(closed)
   248  		})
   249  	}
   250  
   251  	buf := make([]byte, 4)
   252  	if n, err := ReadFull(mr, buf); err != nil || string(buf) != "foob" {
   253  		t.Fatalf(`ReadFull = %d (%q), %v; want 3, "foo", nil`, n, buf[:n], err)
   254  	}
   255  
   256  	runtime.GC()
   257  	select {
   258  	case <-closed:
   259  	case <-time.After(5 * time.Second):
   260  		t.Fatal("timeout waiting for collection of buf1")
   261  	}
   262  
   263  	if n, err := ReadFull(mr, buf[:2]); err != nil || string(buf[:2]) != "ar" {
   264  		t.Fatalf(`ReadFull = %d (%q), %v; want 2, "ar", nil`, n, buf[:n], err)
   265  	}
   266  }