1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-01 16:57:46 -04:00
v2fly/common/buf/readv_test.go

73 lines
1.3 KiB
Go
Raw Normal View History

2018-08-27 14:56:49 -04:00
// +build !wasm
2018-08-25 14:54:37 -04:00
package buf_test
import (
"crypto/rand"
"net"
"testing"
2019-01-06 18:12:04 -05:00
"github.com/google/go-cmp/cmp"
2018-12-06 04:22:14 -05:00
"golang.org/x/sync/errgroup"
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/testing/servers/tcp"
2018-08-25 14:54:37 -04:00
)
func TestReadvReader(t *testing.T) {
tcpServer := &tcp.Server{
MsgProcessor: func(b []byte) []byte {
return b
},
}
dest, err := tcpServer.Start()
common.Must(err)
defer tcpServer.Close()
2018-08-25 14:54:37 -04:00
conn, err := net.Dial("tcp", dest.NetAddr())
common.Must(err)
defer conn.Close()
2018-08-25 14:54:37 -04:00
const size = 8192
data := make([]byte, 8192)
common.Must2(rand.Read(data))
2018-12-06 04:22:14 -05:00
var errg errgroup.Group
errg.Go(func() error {
2018-08-25 14:54:37 -04:00
writer := NewWriter(conn)
2018-11-18 13:36:36 -05:00
mb := MergeBytes(nil, data)
2018-08-25 14:54:37 -04:00
2018-12-06 04:22:14 -05:00
return writer.WriteMultiBuffer(mb)
})
defer func() {
if err := errg.Wait(); err != nil {
t.Error(err)
2018-08-25 14:54:37 -04:00
}
}()
rawConn, err := conn.(*net.TCPConn).SyscallConn()
common.Must(err)
reader := NewReadVReader(conn, rawConn)
var rmb MultiBuffer
for {
mb, err := reader.ReadMultiBuffer()
if err != nil {
t.Fatal("unexpected error: ", err)
}
2018-11-17 03:12:20 -05:00
rmb, _ = MergeMulti(rmb, mb)
2018-08-25 14:54:37 -04:00
if rmb.Len() == size {
break
}
}
rdata := make([]byte, size)
SplitBytes(rmb, rdata)
2018-08-25 14:54:37 -04:00
2019-01-06 18:12:04 -05:00
if r := cmp.Diff(data, rdata); r != "" {
t.Fatal(r)
2018-08-25 14:54:37 -04:00
}
}