1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-17 18:06:15 -05:00
v2fly/transport/pipe/pipe_test.go

154 lines
3.4 KiB
Go
Raw Normal View History

2018-04-18 09:11:03 -04:00
package pipe_test
import (
2019-01-18 09:59:39 -05:00
"errors"
2018-04-18 09:40:43 -04:00
"io"
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
2019-01-06 17:30:38 -05:00
"github.com/google/go-cmp/cmp"
2019-01-18 09:59:39 -05:00
"golang.org/x/sync/errgroup"
2019-01-06 17:30:38 -05:00
2021-02-16 15:31:50 -05:00
"github.com/v2fly/v2ray-core/v4/common"
"github.com/v2fly/v2ray-core/v4/common/buf"
. "github.com/v2fly/v2ray-core/v4/transport/pipe"
2018-04-18 09:11:03 -04:00
)
func TestPipeReadWrite(t *testing.T) {
2018-05-25 06:50:33 -04:00
pReader, pWriter := New(WithSizeLimit(1024))
2018-04-18 09:11:03 -04:00
b := buf.New()
b.WriteString("abcd")
2019-01-06 17:30:38 -05:00
common.Must(pWriter.WriteMultiBuffer(buf.MultiBuffer{b}))
2018-04-18 09:11:03 -04:00
b2 := buf.New()
b2.WriteString("efg")
2019-01-06 17:30:38 -05:00
common.Must(pWriter.WriteMultiBuffer(buf.MultiBuffer{b2}))
2018-04-18 09:11:03 -04:00
rb, err := pReader.ReadMultiBuffer()
2019-01-06 17:30:38 -05:00
common.Must(err)
if r := cmp.Diff(rb.String(), "abcdefg"); r != "" {
t.Error(r)
}
2018-04-18 09:11:03 -04:00
}
2018-04-18 09:40:43 -04:00
2018-12-31 15:25:10 -05:00
func TestPipeInterrupt(t *testing.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)
2019-01-06 17:30:38 -05:00
common.Must(pWriter.WriteMultiBuffer(buf.MultiBuffer{b}))
2018-12-31 15:25:10 -05:00
pWriter.Interrupt()
2018-04-18 09:40:43 -04:00
rb, err := pReader.ReadMultiBuffer()
2019-01-06 17:30:38 -05:00
if err != io.ErrClosedPipe {
t.Fatal("expect io.ErrClosePipe, but got ", err)
}
if !rb.IsEmpty() {
t.Fatal("expect empty buffer, but got ", rb.Len())
}
2018-04-18 09:40:43 -04:00
}
func TestPipeClose(t *testing.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()
2019-01-18 09:59:39 -05:00
common.Must2(b.Write(payload))
common.Must(pWriter.WriteMultiBuffer(buf.MultiBuffer{b}))
common.Must(pWriter.Close())
2018-04-18 09:40:43 -04:00
rb, err := pReader.ReadMultiBuffer()
2019-01-18 09:59:39 -05:00
common.Must(err)
if rb.String() != string(payload) {
t.Fatal("expect content ", string(payload), " but actually ", rb.String())
}
2018-04-18 09:40:43 -04:00
rb, err = pReader.ReadMultiBuffer()
2019-01-18 09:59:39 -05:00
if err != io.EOF {
t.Fatal("expected EOF, but got ", err)
}
if !rb.IsEmpty() {
t.Fatal("expect empty buffer, but got ", rb.String())
}
2018-04-18 09:40:43 -04:00
}
2018-05-08 11:27:01 -04:00
func TestPipeLimitZero(t *testing.T) {
pReader, pWriter := New(WithSizeLimit(0))
bb := buf.New()
2019-01-18 09:59:39 -05:00
common.Must2(bb.Write([]byte{'a', 'b'}))
common.Must(pWriter.WriteMultiBuffer(buf.MultiBuffer{bb}))
2018-05-08 11:27:01 -04:00
2019-01-18 09:59:39 -05:00
var errg errgroup.Group
errg.Go(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})
2019-01-18 09:59:39 -05:00
})
errg.Go(func() error {
2018-05-08 11:27:01 -04:00
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
}
2019-01-18 09:59:39 -05:00
if r := cmp.Diff(container.String(), "abcd"); r != "" {
return errors.New(r)
}
2018-11-19 10:29:27 -05:00
return nil
2019-01-18 09:59:39 -05:00
})
errg.Go(func() error {
2018-11-19 10:29:27 -05:00
time.Sleep(time.Second * 2)
2019-01-18 09:59:39 -05:00
return pWriter.Close()
2018-12-06 08:44:24 -05:00
})
2019-01-18 09:59:39 -05:00
if err := errg.Wait(); err != nil {
t.Error(err)
}
2018-05-08 11:27:01 -04:00
}
2018-06-08 22:47:37 -04:00
func TestPipeWriteMultiThread(t *testing.T) {
pReader, pWriter := New(WithSizeLimit(0))
2019-01-18 09:59:39 -05:00
var errg errgroup.Group
2018-06-08 22:47:37 -04:00
for i := 0; i < 10; i++ {
2019-01-18 09:59:39 -05:00
errg.Go(func() error {
2018-06-08 22:47:37 -04:00
b := buf.New()
2018-11-14 16:55:20 -05:00
b.WriteString("abcd")
2019-01-18 09:59:39 -05:00
return pWriter.WriteMultiBuffer(buf.MultiBuffer{b})
})
2018-06-08 22:47:37 -04:00
}
2019-01-18 15:05:50 -05:00
time.Sleep(time.Millisecond * 100)
pWriter.Close()
2019-01-18 09:59:39 -05:00
errg.Wait()
2018-06-08 22:47:37 -04:00
b, err := pReader.ReadMultiBuffer()
2019-01-18 09:59:39 -05:00
common.Must(err)
if r := cmp.Diff(b[0].Bytes(), []byte{'a', 'b', 'c', 'd'}); r != "" {
t.Error(r)
}
2018-06-08 22:47:37 -04:00
}
func TestInterfaces(t *testing.T) {
2019-01-18 09:59:39 -05:00
_ = (buf.Reader)(new(Reader))
_ = (buf.TimeoutReader)(new(Reader))
2019-02-20 16:57:13 -05:00
_ = (common.Interruptible)(new(Reader))
_ = (common.Interruptible)(new(Writer))
_ = (common.Closable)(new(Writer))
}
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
}
}