1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-22 10:08:15 -05:00
v2fly/transport/pipe/pipe_test.go

59 lines
1.3 KiB
Go
Raw Normal View History

2018-04-18 09:11:03 -04:00
package pipe_test
import (
2018-04-18 09:40:43 -04:00
"io"
2018-04-18 09:11:03 -04:00
"testing"
"v2ray.com/core/common/buf"
. "v2ray.com/core/transport/pipe"
. "v2ray.com/ext/assert"
)
func TestPipeReadWrite(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)
rb, err := pReader.ReadMultiBuffer()
assert(err, IsNil)
assert(rb.String(), Equals, b.String())
}
2018-04-18 09:40:43 -04:00
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)
}