Add StdCopy happy path test: both reading and writing
Signed-off-by: Sergey Evstifeev <sergey.evstifeev@gmail.com>
This commit is contained in:
parent
9dc0973655
commit
6335c90d4a
2 changed files with 27 additions and 1 deletions
|
@ -12,6 +12,8 @@ const (
|
||||||
stdWriterPrefixLen = 8
|
stdWriterPrefixLen = 8
|
||||||
stdWriterFdIndex = 0
|
stdWriterFdIndex = 0
|
||||||
stdWriterSizeIndex = 4
|
stdWriterSizeIndex = 4
|
||||||
|
|
||||||
|
startingBufLen = 32*1024 + stdWriterPrefixLen + 1
|
||||||
)
|
)
|
||||||
|
|
||||||
// StdType prefixes type and length to standard stream.
|
// 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`.
|
// `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) {
|
func StdCopy(dstout, dsterr io.Writer, src io.Reader) (written int64, err error) {
|
||||||
var (
|
var (
|
||||||
buf = make([]byte, 32*1024+stdWriterPrefixLen+1)
|
buf = make([]byte, startingBufLen)
|
||||||
bufLen = len(buf)
|
bufLen = len(buf)
|
||||||
nr, nw int
|
nr, nw int
|
||||||
er, ew error
|
er, ew error
|
||||||
|
|
|
@ -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) {
|
func TestStdCopyWithInvalidInputHeader(t *testing.T) {
|
||||||
dstOut := NewStdWriter(ioutil.Discard, Stdout)
|
dstOut := NewStdWriter(ioutil.Discard, Stdout)
|
||||||
dstErr := NewStdWriter(ioutil.Discard, Stderr)
|
dstErr := NewStdWriter(ioutil.Discard, Stderr)
|
||||||
|
|
Loading…
Reference in a new issue