goutils/mwc/mwc_test.go

57 lines
1.4 KiB
Go
Raw Permalink Normal View History

2015-09-22 17:39:27 +00:00
package mwc
import (
"bytes"
"testing"
2016-04-28 19:42:05 +00:00
"github.com/kisom/goutils/assert"
2017-11-16 16:26:27 +00:00
"github.com/kisom/goutils/testio"
2015-09-22 17:39:27 +00:00
)
func TestMWC(t *testing.T) {
buf1 := testio.NewBufCloser(nil)
buf2 := testio.NewBufCloser(nil)
mwc := MultiWriteCloser(buf1, buf2)
2016-04-28 19:42:05 +00:00
_, err := mwc.Write([]byte("hello, world"))
assert.NoErrorT(t, err)
2015-09-22 17:39:27 +00:00
2016-04-28 19:42:05 +00:00
assert.BoolT(t, bytes.Equal(buf1.Bytes(), buf2.Bytes()), "write failed")
assert.BoolT(t, bytes.Equal(buf1.Bytes(), []byte("hello, world")), "write failed")
2015-09-22 17:39:27 +00:00
2016-04-28 19:42:05 +00:00
err = mwc.Close()
assert.NoErrorT(t, err)
}
func TestMWCShort(t *testing.T) {
buf1 := testio.NewBufCloser(nil)
buf2 := testio.NewBufCloser(nil)
buf3 := testio.NewBrokenWriter(5)
buf4 := testio.NewSilentBrokenWriter(5)
mwc := MultiWriteCloser(buf1, buf2, buf3)
defer mwc.Close()
_, err := mwc.Write([]byte("hello, world"))
assert.ErrorT(t, err, "expected a short write error", "but no error occurred")
mwc.Close()
mwc = MultiWriteCloser(buf1, buf2, buf4)
_, err = mwc.Write([]byte("hello, world"))
2017-11-16 16:26:27 +00:00
assert.ErrorT(t, err, "expected a short write error", "but no error occurred")
2016-04-28 19:42:05 +00:00
}
func TestMWCClose(t *testing.T) {
buf1 := testio.NewBufCloser(nil)
buf2 := testio.NewBufCloser(nil)
buf3 := testio.NewBrokenCloser(nil)
mwc := MultiWriteCloser(buf1, buf2, buf3)
_, err := mwc.Write([]byte("hello, world"))
assert.NoErrorT(t, err)
err = mwc.Close()
assert.ErrorT(t, err, "expected broken closer to fail")
2015-09-22 17:39:27 +00:00
}