| 1 |
package mwc |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"testing" |
| 6 |
|
| 7 |
"github.com/kisom/goutils/testio" |
| 8 |
"github.com/kisom/goutils/assert" |
| 9 |
) |
| 10 |
|
| 11 |
func TestMWC(t *testing.T) { |
| 12 |
buf1 := testio.NewBufCloser(nil) |
| 13 |
buf2 := testio.NewBufCloser(nil) |
| 14 |
|
| 15 |
mwc := MultiWriteCloser(buf1, buf2) |
| 16 |
|
| 17 |
_, err := mwc.Write([]byte("hello, world")) |
| 18 |
assert.NoErrorT(t, err) |
| 19 |
|
| 20 |
assert.BoolT(t, bytes.Equal(buf1.Bytes(), buf2.Bytes()), "write failed") |
| 21 |
assert.BoolT(t, bytes.Equal(buf1.Bytes(), []byte("hello, world")), "write failed") |
| 22 |
|
| 23 |
err = mwc.Close() |
| 24 |
assert.NoErrorT(t, err) |
| 25 |
} |
| 26 |
|
| 27 |
func TestMWCShort(t *testing.T) { |
| 28 |
buf1 := testio.NewBufCloser(nil) |
| 29 |
buf2 := testio.NewBufCloser(nil) |
| 30 |
buf3 := testio.NewBrokenWriter(5) |
| 31 |
buf4 := testio.NewSilentBrokenWriter(5) |
| 32 |
|
| 33 |
mwc := MultiWriteCloser(buf1, buf2, buf3) |
| 34 |
defer mwc.Close() |
| 35 |
|
| 36 |
_, err := mwc.Write([]byte("hello, world")) |
| 37 |
assert.ErrorT(t, err, "expected a short write error", "but no error occurred") |
| 38 |
mwc.Close() |
| 39 |
|
| 40 |
mwc = MultiWriteCloser(buf1, buf2, buf4) |
| 41 |
_, err = mwc.Write([]byte("hello, world")) |
| 42 |
assert.ErrorT(t, err, "expected a short write error", "but no error occurred") |
| 43 |
} |
| 44 |
|
| 45 |
func TestMWCClose(t *testing.T) { |
| 46 |
buf1 := testio.NewBufCloser(nil) |
| 47 |
buf2 := testio.NewBufCloser(nil) |
| 48 |
buf3 := testio.NewBrokenCloser(nil) |
| 49 |
|
| 50 |
mwc := MultiWriteCloser(buf1, buf2, buf3) |
| 51 |
_, err := mwc.Write([]byte("hello, world")) |
| 52 |
assert.NoErrorT(t, err) |
| 53 |
|
| 54 |
err = mwc.Close() |
| 55 |
assert.ErrorT(t, err, "expected broken closer to fail") |
| 56 |
} |