Browse Source

more test cases for pipe

pull/1086/head
Darien Raymond 7 years ago
parent
commit
2e6985a98b
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169
  1. 1
      transport/pipe/pipe.go
  2. 35
      transport/pipe/pipe_test.go
  3. 4
      transport/pipe/reader.go
  4. 4
      transport/pipe/writer.go

1
transport/pipe/pipe.go

@ -42,6 +42,7 @@ type closeError interface {
CloseError() CloseError()
} }
// CloseError invokes CloseError() method if the object is either Reader or Writer.
func CloseError(v interface{}) { func CloseError(v interface{}) {
if c, ok := v.(closeError); ok { if c, ok := v.(closeError); ok {
c.CloseError() c.CloseError()

35
transport/pipe/pipe_test.go

@ -1,6 +1,7 @@
package pipe_test package pipe_test
import ( import (
"io"
"testing" "testing"
"v2ray.com/core/common/buf" "v2ray.com/core/common/buf"
@ -21,3 +22,37 @@ func TestPipeReadWrite(t *testing.T) {
assert(err, IsNil) assert(err, IsNil)
assert(rb.String(), Equals, b.String()) 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)
}

4
transport/pipe/reader.go

@ -6,18 +6,22 @@ import (
"v2ray.com/core/common/buf" "v2ray.com/core/common/buf"
) )
// Reader is a buf.Reader that reads content from a pipe.
type Reader struct { type Reader struct {
pipe *pipe pipe *pipe
} }
// ReadMultiBuffer implements buf.Reader.
func (r *Reader) ReadMultiBuffer() (buf.MultiBuffer, error) { func (r *Reader) ReadMultiBuffer() (buf.MultiBuffer, error) {
return r.pipe.ReadMultiBuffer() 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) { func (r *Reader) ReadMultiBufferWithTimeout(d time.Duration) (buf.MultiBuffer, error) {
return r.pipe.ReadMultiBufferWithTimeout(d) 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() { func (r *Reader) CloseError() {
r.pipe.CloseError() r.pipe.CloseError()
} }

4
transport/pipe/writer.go

@ -4,18 +4,22 @@ import (
"v2ray.com/core/common/buf" "v2ray.com/core/common/buf"
) )
// Writer is a buf.Writer that writes data into a pipe.
type Writer struct { type Writer struct {
pipe *pipe pipe *pipe
} }
// WriteMultiBuffer implements buf.Writer.
func (w *Writer) WriteMultiBuffer(mb buf.MultiBuffer) error { func (w *Writer) WriteMultiBuffer(mb buf.MultiBuffer) error {
return w.pipe.WriteMultiBuffer(mb) 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 { func (w *Writer) Close() error {
return w.pipe.Close() 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() { func (w *Writer) CloseError() {
w.pipe.CloseError() w.pipe.CloseError()
} }

Loading…
Cancel
Save