2018-04-18 09:11:03 -04:00
|
|
|
package pipe_test
|
|
|
|
|
|
|
|
import (
|
2018-04-18 09:40:43 -04:00
|
|
|
"io"
|
2018-06-08 22:47:37 -04:00
|
|
|
"sync"
|
2018-04-18 09:11:03 -04:00
|
|
|
"testing"
|
2018-05-08 11:27:01 -04:00
|
|
|
"time"
|
2018-04-18 09:11:03 -04:00
|
|
|
|
2018-11-17 13:47:30 -05:00
|
|
|
"v2ray.com/core/common"
|
2018-04-18 09:11:03 -04:00
|
|
|
"v2ray.com/core/common/buf"
|
2018-06-08 22:47:37 -04:00
|
|
|
"v2ray.com/core/common/task"
|
2018-04-18 09:11:03 -04:00
|
|
|
. "v2ray.com/core/transport/pipe"
|
|
|
|
. "v2ray.com/ext/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestPipeReadWrite(t *testing.T) {
|
|
|
|
assert := With(t)
|
|
|
|
|
2018-05-25 06:50:33 -04:00
|
|
|
pReader, pWriter := New(WithSizeLimit(1024))
|
2018-11-20 04:45:02 -05:00
|
|
|
|
2018-04-18 09:11:03 -04:00
|
|
|
b := buf.New()
|
2018-11-20 04:45:02 -05:00
|
|
|
b.WriteString("abcd")
|
2018-11-16 05:08:12 -05:00
|
|
|
assert(pWriter.WriteMultiBuffer(buf.MultiBuffer{b}), IsNil)
|
2018-04-18 09:11:03 -04:00
|
|
|
|
2018-11-20 04:45:02 -05:00
|
|
|
b2 := buf.New()
|
|
|
|
b2.WriteString("efg")
|
|
|
|
assert(pWriter.WriteMultiBuffer(buf.MultiBuffer{b2}), IsNil)
|
|
|
|
|
2018-04-18 09:11:03 -04:00
|
|
|
rb, err := pReader.ReadMultiBuffer()
|
|
|
|
assert(err, IsNil)
|
2018-11-20 04:45:02 -05:00
|
|
|
assert(rb.String(), Equals, "abcdefg")
|
2018-04-18 09:11:03 -04:00
|
|
|
}
|
2018-04-18 09:40:43 -04:00
|
|
|
|
|
|
|
func TestPipeCloseError(t *testing.T) {
|
|
|
|
assert := With(t)
|
|
|
|
|
2018-05-25 06:50:33 -04:00
|
|
|
pReader, pWriter := New(WithSizeLimit(1024))
|
2018-04-18 09:40:43 -04:00
|
|
|
payload := []byte{'a', 'b', 'c', 'd'}
|
|
|
|
b := buf.New()
|
2018-04-19 16:56:55 -04:00
|
|
|
b.Write(payload)
|
2018-11-16 05:08:12 -05:00
|
|
|
assert(pWriter.WriteMultiBuffer(buf.MultiBuffer{b}), IsNil)
|
2018-04-18 09:40:43 -04:00
|
|
|
pWriter.CloseError()
|
|
|
|
|
|
|
|
rb, err := pReader.ReadMultiBuffer()
|
|
|
|
assert(err, Equals, io.ErrClosedPipe)
|
|
|
|
assert(rb.IsEmpty(), IsTrue)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPipeClose(t *testing.T) {
|
|
|
|
assert := With(t)
|
|
|
|
|
2018-05-25 06:50:33 -04:00
|
|
|
pReader, pWriter := New(WithSizeLimit(1024))
|
2018-04-18 09:40:43 -04:00
|
|
|
payload := []byte{'a', 'b', 'c', 'd'}
|
|
|
|
b := buf.New()
|
2018-04-19 16:56:55 -04:00
|
|
|
b.Write(payload)
|
2018-11-16 05:08:12 -05:00
|
|
|
assert(pWriter.WriteMultiBuffer(buf.MultiBuffer{b}), IsNil)
|
2018-04-18 09:40:43 -04:00
|
|
|
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)
|
|
|
|
}
|
2018-05-08 11:27:01 -04:00
|
|
|
|
|
|
|
func TestPipeLimitZero(t *testing.T) {
|
|
|
|
assert := With(t)
|
|
|
|
|
|
|
|
pReader, pWriter := New(WithSizeLimit(0))
|
|
|
|
bb := buf.New()
|
|
|
|
bb.Write([]byte{'a', 'b'})
|
2018-11-16 05:08:12 -05:00
|
|
|
assert(pWriter.WriteMultiBuffer(buf.MultiBuffer{bb}), IsNil)
|
2018-05-08 11:27:01 -04:00
|
|
|
|
2018-05-27 08:42:53 -04:00
|
|
|
err := task.Run(task.Parallel(func() error {
|
2018-05-08 11:27:01 -04:00
|
|
|
b := buf.New()
|
|
|
|
b.Write([]byte{'c', 'd'})
|
2018-11-16 05:08:12 -05:00
|
|
|
return pWriter.WriteMultiBuffer(buf.MultiBuffer{b})
|
2018-05-08 11:27:01 -04:00
|
|
|
}, func() error {
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
|
2018-11-19 10:29:27 -05:00
|
|
|
var container buf.MultiBufferContainer
|
|
|
|
if err := buf.Copy(pReader, &container); err != nil {
|
2018-05-08 11:27:01 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-11-19 10:29:27 -05:00
|
|
|
assert(container.String(), Equals, "abcd")
|
|
|
|
return nil
|
|
|
|
}, func() error {
|
|
|
|
time.Sleep(time.Second * 2)
|
|
|
|
pWriter.Close()
|
2018-05-08 11:27:01 -04:00
|
|
|
return nil
|
2018-05-27 08:42:53 -04:00
|
|
|
}))()
|
2018-05-08 11:27:01 -04:00
|
|
|
|
|
|
|
assert(err, IsNil)
|
|
|
|
}
|
2018-06-08 22:47:37 -04:00
|
|
|
|
|
|
|
func TestPipeWriteMultiThread(t *testing.T) {
|
|
|
|
assert := With(t)
|
|
|
|
|
|
|
|
pReader, pWriter := New(WithSizeLimit(0))
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
b := buf.New()
|
2018-11-14 16:55:20 -05:00
|
|
|
b.WriteString("abcd")
|
2018-11-16 05:08:12 -05:00
|
|
|
pWriter.WriteMultiBuffer(buf.MultiBuffer{b})
|
2018-06-08 22:47:37 -04:00
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
time.Sleep(time.Millisecond * 100)
|
|
|
|
|
|
|
|
pWriter.Close()
|
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
b, err := pReader.ReadMultiBuffer()
|
|
|
|
assert(err, IsNil)
|
|
|
|
assert(b[0].Bytes(), Equals, []byte{'a', 'b', 'c', 'd'})
|
|
|
|
}
|
2018-06-27 06:21:22 -04:00
|
|
|
|
|
|
|
func TestInterfaces(t *testing.T) {
|
|
|
|
assert := With(t)
|
|
|
|
|
|
|
|
assert((*Reader)(nil), Implements, (*buf.Reader)(nil))
|
|
|
|
assert((*Reader)(nil), Implements, (*buf.TimeoutReader)(nil))
|
|
|
|
}
|
2018-11-17 13:47:30 -05:00
|
|
|
|
|
|
|
func BenchmarkPipeReadWrite(b *testing.B) {
|
|
|
|
reader, writer := New(WithoutSizeLimit())
|
|
|
|
a := buf.New()
|
|
|
|
a.Extend(buf.Size)
|
|
|
|
c := buf.MultiBuffer{a}
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
common.Must(writer.WriteMultiBuffer(c))
|
|
|
|
d, err := reader.ReadMultiBuffer()
|
|
|
|
common.Must(err)
|
|
|
|
c = d
|
|
|
|
}
|
|
|
|
}
|