mirror of
https://github.com/v2fly/v2ray-core.git
synced 2025-01-21 16:56:27 -05:00
more test cases for pipe
This commit is contained in:
parent
845606d86c
commit
2e6985a98b
@ -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()
|
||||
|
@ -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)
|
||||
}
|
||||
|
@ -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,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…
Reference in New Issue
Block a user