2018-08-27 18:56:49 +00:00
|
|
|
// +build !wasm
|
|
|
|
|
2018-08-25 18:54:37 +00:00
|
|
|
package buf_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/rand"
|
|
|
|
"net"
|
|
|
|
"testing"
|
|
|
|
|
2018-12-06 09:22:14 +00:00
|
|
|
"golang.org/x/sync/errgroup"
|
2018-08-25 18:54:37 +00:00
|
|
|
"v2ray.com/core/common"
|
|
|
|
. "v2ray.com/core/common/buf"
|
|
|
|
"v2ray.com/core/common/compare"
|
|
|
|
"v2ray.com/core/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() // nolint: errcheck
|
|
|
|
|
|
|
|
conn, err := net.Dial("tcp", dest.NetAddr())
|
|
|
|
common.Must(err)
|
|
|
|
defer conn.Close() // nolint: errcheck
|
|
|
|
|
|
|
|
const size = 8192
|
|
|
|
data := make([]byte, 8192)
|
|
|
|
common.Must2(rand.Read(data))
|
|
|
|
|
2018-12-06 09:22:14 +00:00
|
|
|
var errg errgroup.Group
|
|
|
|
errg.Go(func() error {
|
2018-08-25 18:54:37 +00:00
|
|
|
writer := NewWriter(conn)
|
2018-11-18 18:36:36 +00:00
|
|
|
mb := MergeBytes(nil, data)
|
2018-08-25 18:54:37 +00:00
|
|
|
|
2018-12-06 09:22:14 +00:00
|
|
|
return writer.WriteMultiBuffer(mb)
|
|
|
|
})
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if err := errg.Wait(); err != nil {
|
|
|
|
t.Error(err)
|
2018-08-25 18:54:37 +00: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 08:12:20 +00:00
|
|
|
rmb, _ = MergeMulti(rmb, mb)
|
2018-08-25 18:54:37 +00:00
|
|
|
if rmb.Len() == size {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
rdata := make([]byte, size)
|
2018-11-18 18:44:32 +00:00
|
|
|
SplitBytes(rmb, rdata)
|
2018-08-25 18:54:37 +00:00
|
|
|
|
|
|
|
if err := compare.BytesEqualWithDetail(data, rdata); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|