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 invokes CloseError() method if the object is either Reader or Writer.
func CloseError(v interface{}) {
if c, ok := v.(closeError); ok {
c.CloseError()

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

4
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()
}

4
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()
}

Loading…
Cancel
Save