mirror of
https://github.com/v2fly/v2ray-core.git
synced 2024-12-22 01:57:12 -05:00
74 lines
1.3 KiB
Go
74 lines
1.3 KiB
Go
//go:build !wasm
|
|
// +build !wasm
|
|
|
|
package buf_test
|
|
|
|
import (
|
|
"crypto/rand"
|
|
"net"
|
|
"testing"
|
|
|
|
"github.com/google/go-cmp/cmp"
|
|
"golang.org/x/sync/errgroup"
|
|
|
|
"github.com/v2fly/v2ray-core/v5/common"
|
|
. "github.com/v2fly/v2ray-core/v5/common/buf"
|
|
"github.com/v2fly/v2ray-core/v5/testing/servers/tcp"
|
|
)
|
|
|
|
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()
|
|
|
|
conn, err := net.Dial("tcp", dest.NetAddr())
|
|
common.Must(err)
|
|
defer conn.Close()
|
|
|
|
const size = 8192
|
|
data := make([]byte, 8192)
|
|
common.Must2(rand.Read(data))
|
|
|
|
var errg errgroup.Group
|
|
errg.Go(func() error {
|
|
writer := NewWriter(conn)
|
|
mb := MergeBytes(nil, data)
|
|
|
|
return writer.WriteMultiBuffer(mb)
|
|
})
|
|
|
|
defer func() {
|
|
if err := errg.Wait(); err != nil {
|
|
t.Error(err)
|
|
}
|
|
}()
|
|
|
|
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)
|
|
}
|
|
rmb, _ = MergeMulti(rmb, mb)
|
|
if rmb.Len() == size {
|
|
break
|
|
}
|
|
}
|
|
|
|
rdata := make([]byte, size)
|
|
SplitBytes(rmb, rdata)
|
|
|
|
if r := cmp.Diff(data, rdata); r != "" {
|
|
t.Fatal(r)
|
|
}
|
|
}
|