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