From 2e6985a98b3f026d64349207a6fd5449ac606937 Mon Sep 17 00:00:00 2001 From: Darien Raymond Date: Wed, 18 Apr 2018 15:40:43 +0200 Subject: [PATCH] more test cases for pipe --- transport/pipe/pipe.go | 1 + transport/pipe/pipe_test.go | 35 +++++++++++++++++++++++++++++++++++ transport/pipe/reader.go | 4 ++++ transport/pipe/writer.go | 4 ++++ 4 files changed, 44 insertions(+) diff --git a/transport/pipe/pipe.go b/transport/pipe/pipe.go index 50c3c632..7ba3976c 100644 --- a/transport/pipe/pipe.go +++ b/transport/pipe/pipe.go @@ -42,6 +42,7 @@ type closeError interface { CloseError() } +// CloseError invokes CloseError() method if the object is either Reader or Writer. func CloseError(v interface{}) { if c, ok := v.(closeError); ok { c.CloseError() diff --git a/transport/pipe/pipe_test.go b/transport/pipe/pipe_test.go index 9020ea30..14fa2de9 100644 --- a/transport/pipe/pipe_test.go +++ b/transport/pipe/pipe_test.go @@ -1,6 +1,7 @@ package pipe_test import ( + "io" "testing" "v2ray.com/core/common/buf" @@ -21,3 +22,37 @@ func TestPipeReadWrite(t *testing.T) { assert(err, IsNil) assert(rb.String(), Equals, b.String()) } + +func TestPipeCloseError(t *testing.T) { + assert := With(t) + + pReader, pWriter := New() + payload := []byte{'a', 'b', 'c', 'd'} + b := buf.New() + b.Append(payload) + assert(pWriter.WriteMultiBuffer(buf.NewMultiBufferValue(b)), IsNil) + pWriter.CloseError() + + rb, err := pReader.ReadMultiBuffer() + assert(err, Equals, io.ErrClosedPipe) + assert(rb.IsEmpty(), IsTrue) +} + +func TestPipeClose(t *testing.T) { + assert := With(t) + + pReader, pWriter := New() + payload := []byte{'a', 'b', 'c', 'd'} + b := buf.New() + b.Append(payload) + assert(pWriter.WriteMultiBuffer(buf.NewMultiBufferValue(b)), IsNil) + assert(pWriter.Close(), IsNil) + + rb, err := pReader.ReadMultiBuffer() + assert(err, IsNil) + assert(rb.String(), Equals, b.String()) + + rb, err = pReader.ReadMultiBuffer() + assert(err, Equals, io.EOF) + assert(rb.IsEmpty(), IsTrue) +} diff --git a/transport/pipe/reader.go b/transport/pipe/reader.go index 369baeb9..d0c97fb7 100644 --- a/transport/pipe/reader.go +++ b/transport/pipe/reader.go @@ -6,18 +6,22 @@ import ( "v2ray.com/core/common/buf" ) +// Reader is a buf.Reader that reads content from a pipe. type Reader struct { pipe *pipe } +// ReadMultiBuffer implements buf.Reader. func (r *Reader) ReadMultiBuffer() (buf.MultiBuffer, error) { return r.pipe.ReadMultiBuffer() } +// ReadMultiBufferWithTimeout reads content from a pipe within the given duration, or returns buf.ErrTimeout otherwise. func (r *Reader) ReadMultiBufferWithTimeout(d time.Duration) (buf.MultiBuffer, error) { return r.pipe.ReadMultiBufferWithTimeout(d) } +// CloseError sets the pipe to error state. Both reading and writing from/to the pipe will return io.ErrClosedPipe. func (r *Reader) CloseError() { r.pipe.CloseError() } diff --git a/transport/pipe/writer.go b/transport/pipe/writer.go index c9b2838f..2372d45c 100644 --- a/transport/pipe/writer.go +++ b/transport/pipe/writer.go @@ -4,18 +4,22 @@ import ( "v2ray.com/core/common/buf" ) +// Writer is a buf.Writer that writes data into a pipe. type Writer struct { pipe *pipe } +// WriteMultiBuffer implements buf.Writer. func (w *Writer) WriteMultiBuffer(mb buf.MultiBuffer) error { return w.pipe.WriteMultiBuffer(mb) } +// Close implements io.Closer. After the pipe is closed, writing to the pipe will return io.ErrClosedPipe, while reading will return io.EOF. func (w *Writer) Close() error { return w.pipe.Close() } +// CloseError sets the pipe to error state. Both reading and writing from/to the pipe will return io.ErrClosedPipe. func (w *Writer) CloseError() { w.pipe.CloseError() }