File: mwc_test.go

package info (click to toggle)
golang-github-kisom-goutils 0.0~git20161101.0.858c9cb-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 384 kB
  • ctags: 331
  • sloc: makefile: 6
file content (56 lines) | stat: -rw-r--r-- 1,414 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package mwc

import (
	"bytes"
	"testing"

	"github.com/kisom/goutils/testio"
	"github.com/kisom/goutils/assert"
)

func TestMWC(t *testing.T) {
	buf1 := testio.NewBufCloser(nil)
	buf2 := testio.NewBufCloser(nil)

	mwc := MultiWriteCloser(buf1, buf2)

	_, err := mwc.Write([]byte("hello, world"))
	assert.NoErrorT(t, err)

	assert.BoolT(t, bytes.Equal(buf1.Bytes(), buf2.Bytes()), "write failed")
	assert.BoolT(t, bytes.Equal(buf1.Bytes(), []byte("hello, world")), "write failed")

	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"))
	assert.ErrorT(t, err, "expected a short write error", "but no error occurred")			
}

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