Add StdCopy happy path test: both reading and writing

Signed-off-by: Sergey Evstifeev <sergey.evstifeev@gmail.com>
This commit is contained in:
Sergey Evstifeev 2015-09-23 00:30:06 +02:00
parent 9dc0973655
commit 6335c90d4a
2 changed files with 27 additions and 1 deletions

View file

@ -12,6 +12,8 @@ const (
stdWriterPrefixLen = 8
stdWriterFdIndex = 0
stdWriterSizeIndex = 4
startingBufLen = 32*1024 + stdWriterPrefixLen + 1
)
// StdType prefixes type and length to standard stream.
@ -80,7 +82,7 @@ var errInvalidStdHeader = errors.New("Unrecognized input header")
// `written` will hold the total number of bytes written to `dstout` and `dsterr`.
func StdCopy(dstout, dsterr io.Writer, src io.Reader) (written int64, err error) {
var (
buf = make([]byte, 32*1024+stdWriterPrefixLen+1)
buf = make([]byte, startingBufLen)
bufLen = len(buf)
nr, nw int
er, ew error

View file

@ -85,6 +85,30 @@ func TestWriteDoesNotReturnNegativeWrittenBytes(t *testing.T) {
}
}
func TestStdCopyWriteAndRead(t *testing.T) {
buffer := new(bytes.Buffer)
stdOutBytes := []byte(strings.Repeat("o", startingBufLen))
dstOut := NewStdWriter(buffer, Stdout)
_, err := dstOut.Write(stdOutBytes)
if err != nil {
t.Fatal(err)
}
stdErrBytes := []byte(strings.Repeat("e", startingBufLen))
dstErr := NewStdWriter(buffer, Stderr)
_, err = dstErr.Write(stdErrBytes)
if err != nil {
t.Fatal(err)
}
written, err := StdCopy(ioutil.Discard, ioutil.Discard, buffer)
if err != nil {
t.Fatal(err)
}
expectedTotalWritten := len(stdOutBytes) + len(stdErrBytes)
if written != int64(expectedTotalWritten) {
t.Fatalf("Expected to have total of %d bytes written, got %d", expectedTotalWritten, written)
}
}
func TestStdCopyWithInvalidInputHeader(t *testing.T) {
dstOut := NewStdWriter(ioutil.Discard, Stdout)
dstErr := NewStdWriter(ioutil.Discard, Stderr)