v2ray-core/transport/ray/direct_test.go

50 lines
902 B
Go
Raw Normal View History

2016-12-22 17:28:06 +01:00
package ray_test
import (
2017-04-13 22:17:58 +02:00
"context"
2016-12-22 17:28:06 +01:00
"io"
"testing"
"v2ray.com/core/common/buf"
2017-10-24 16:15:35 +02:00
. "v2ray.com/ext/assert"
2016-12-22 17:28:06 +01:00
. "v2ray.com/core/transport/ray"
)
func TestStreamIO(t *testing.T) {
2017-10-24 16:15:35 +02:00
assert := With(t)
2016-12-22 17:28:06 +01:00
stream := NewStream(context.Background())
2016-12-27 00:44:11 +01:00
b1 := buf.New()
b1.AppendBytes('a')
2017-10-24 16:15:35 +02:00
assert(stream.Write(buf.NewMultiBufferValue(b1)), IsNil)
2016-12-22 17:28:06 +01:00
_, err := stream.Read()
2017-10-24 16:15:35 +02:00
assert(err, IsNil)
2016-12-22 17:28:06 +01:00
stream.Close()
_, err = stream.Read()
2017-10-24 16:15:35 +02:00
assert(err, Equals, io.EOF)
2016-12-22 17:28:06 +01:00
2016-12-27 00:44:11 +01:00
b2 := buf.New()
b2.AppendBytes('b')
2017-04-15 21:07:23 +02:00
err = stream.Write(buf.NewMultiBufferValue(b2))
2017-10-24 16:15:35 +02:00
assert(err, Equals, io.ErrClosedPipe)
2016-12-22 17:28:06 +01:00
}
func TestStreamClose(t *testing.T) {
2017-10-24 16:15:35 +02:00
assert := With(t)
2016-12-22 17:28:06 +01:00
stream := NewStream(context.Background())
2016-12-27 00:44:11 +01:00
b1 := buf.New()
b1.AppendBytes('a')
2017-10-24 16:15:35 +02:00
assert(stream.Write(buf.NewMultiBufferValue(b1)), IsNil)
2016-12-22 17:28:06 +01:00
stream.Close()
_, err := stream.Read()
2017-10-24 16:15:35 +02:00
assert(err, IsNil)
2016-12-22 17:28:06 +01:00
_, err = stream.Read()
2017-10-24 16:15:35 +02:00
assert(err, Equals, io.EOF)
2016-12-22 17:28:06 +01:00
}