mirror of
https://github.com/XTLS/Xray-core.git
synced 2024-11-22 23:13:01 +00:00
Fix some flaky tests related to pick port (#985)
* Fix some tests to PickPort correctly * Add retry logic for two socks tests Two socks tests listen for both TCP and UDP with same port number, in some cases the port is not available Add retry logic for server core, start server core and client core separately Extract a common method * Add retry logic for two dokodemo tests Two dokodemo tests listen for 5 ports, in some cases they are not available Add retry logic for client core, start server and client separately
This commit is contained in:
parent
c1a54ae58e
commit
94c249a8c8
@ -58,7 +58,7 @@ func TestXrayClose(t *testing.T) {
|
|||||||
Address: net.NewIPOrDomain(net.LocalHostIP),
|
Address: net.NewIPOrDomain(net.LocalHostIP),
|
||||||
Port: uint32(0),
|
Port: uint32(0),
|
||||||
NetworkList: &net.NetworkList{
|
NetworkList: &net.NetworkList{
|
||||||
Network: []net.Network{net.Network_TCP, net.Network_UDP},
|
Network: []net.Network{net.Network_TCP},
|
||||||
},
|
},
|
||||||
}),
|
}),
|
||||||
},
|
},
|
||||||
|
@ -11,6 +11,7 @@ import (
|
|||||||
"runtime"
|
"runtime"
|
||||||
"sync"
|
"sync"
|
||||||
"syscall"
|
"syscall"
|
||||||
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/golang/protobuf/proto"
|
"github.com/golang/protobuf/proto"
|
||||||
@ -235,3 +236,20 @@ func testTCPConn2(conn net.Conn, payloadSize int, timeout time.Duration) func()
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func WaitConnAvailableWithTest(t *testing.T, testFunc func() error) bool {
|
||||||
|
for i := 1; ; i++ {
|
||||||
|
if i > 10 {
|
||||||
|
t.Log("All attempts failed to test tcp conn")
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
time.Sleep(time.Millisecond * 10)
|
||||||
|
if err := testFunc(); err != nil {
|
||||||
|
t.Log("err ", err)
|
||||||
|
} else {
|
||||||
|
t.Log("success with", i, "attempts")
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
@ -64,9 +64,14 @@ func TestDokodemoTCP(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
server, err := InitializeServerConfig(serverConfig)
|
||||||
|
common.Must(err)
|
||||||
|
defer CloseServer(server)
|
||||||
|
|
||||||
clientPort := uint32(tcp.PickPort())
|
|
||||||
clientPortRange := uint32(5)
|
clientPortRange := uint32(5)
|
||||||
|
retry := 1
|
||||||
|
clientPort := uint32(tcp.PickPort())
|
||||||
|
for {
|
||||||
clientConfig := &core.Config{
|
clientConfig := &core.Config{
|
||||||
App: []*serial.TypedMessage{
|
App: []*serial.TypedMessage{
|
||||||
serial.ToTypedMessage(&log.Config{
|
serial.ToTypedMessage(&log.Config{
|
||||||
@ -110,9 +115,17 @@ func TestDokodemoTCP(t *testing.T) {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
servers, err := InitializeServerConfigs(serverConfig, clientConfig)
|
server, _ := InitializeServerConfig(clientConfig)
|
||||||
common.Must(err)
|
if server != nil && WaitConnAvailableWithTest(t, testTCPConn(net.Port(clientPort), 1024, time.Second*2)) {
|
||||||
defer CloseAllServers(servers)
|
defer CloseServer(server)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
retry++
|
||||||
|
if retry > 5 {
|
||||||
|
t.Fatal("All attempts failed to start client")
|
||||||
|
}
|
||||||
|
clientPort = uint32(tcp.PickPort())
|
||||||
|
}
|
||||||
|
|
||||||
for port := clientPort; port <= clientPort+clientPortRange; port++ {
|
for port := clientPort; port <= clientPort+clientPortRange; port++ {
|
||||||
if err := testTCPConn(net.Port(port), 1024, time.Second*2)(); err != nil {
|
if err := testTCPConn(net.Port(port), 1024, time.Second*2)(); err != nil {
|
||||||
@ -155,9 +168,14 @@ func TestDokodemoUDP(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
server, err := InitializeServerConfig(serverConfig)
|
||||||
|
common.Must(err)
|
||||||
|
defer CloseServer(server)
|
||||||
|
|
||||||
clientPort := uint32(udp.PickPort())
|
|
||||||
clientPortRange := uint32(5)
|
clientPortRange := uint32(5)
|
||||||
|
retry := 1
|
||||||
|
clientPort := uint32(udp.PickPort())
|
||||||
|
for {
|
||||||
clientConfig := &core.Config{
|
clientConfig := &core.Config{
|
||||||
Inbound: []*core.InboundHandlerConfig{
|
Inbound: []*core.InboundHandlerConfig{
|
||||||
{
|
{
|
||||||
@ -195,9 +213,17 @@ func TestDokodemoUDP(t *testing.T) {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
servers, err := InitializeServerConfigs(serverConfig, clientConfig)
|
server, _ := InitializeServerConfig(clientConfig)
|
||||||
common.Must(err)
|
if server != nil && WaitConnAvailableWithTest(t, testUDPConn(net.Port(clientPort), 1024, time.Second*2)) {
|
||||||
defer CloseAllServers(servers)
|
defer CloseServer(server)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
retry++
|
||||||
|
if retry > 5 {
|
||||||
|
t.Fatal("All attempts failed to start client")
|
||||||
|
}
|
||||||
|
clientPort = uint32(udp.PickPort())
|
||||||
|
}
|
||||||
|
|
||||||
var errg errgroup.Group
|
var errg errgroup.Group
|
||||||
for port := clientPort; port <= clientPort+clientPortRange; port++ {
|
for port := clientPort; port <= clientPort+clientPortRange; port++ {
|
||||||
|
@ -234,7 +234,7 @@ func TestProxyOverKCP(t *testing.T) {
|
|||||||
defer tcpServer.Close()
|
defer tcpServer.Close()
|
||||||
|
|
||||||
serverUserID := protocol.NewID(uuid.New())
|
serverUserID := protocol.NewID(uuid.New())
|
||||||
serverPort := tcp.PickPort()
|
serverPort := udp.PickPort()
|
||||||
serverConfig := &core.Config{
|
serverConfig := &core.Config{
|
||||||
Inbound: []*core.InboundHandlerConfig{
|
Inbound: []*core.InboundHandlerConfig{
|
||||||
{
|
{
|
||||||
|
@ -111,7 +111,9 @@ func TestSocksBridageUDP(t *testing.T) {
|
|||||||
common.Must(err)
|
common.Must(err)
|
||||||
defer udpServer.Close()
|
defer udpServer.Close()
|
||||||
|
|
||||||
|
retry := 1
|
||||||
serverPort := tcp.PickPort()
|
serverPort := tcp.PickPort()
|
||||||
|
for {
|
||||||
serverConfig := &core.Config{
|
serverConfig := &core.Config{
|
||||||
Inbound: []*core.InboundHandlerConfig{
|
Inbound: []*core.InboundHandlerConfig{
|
||||||
{
|
{
|
||||||
@ -128,6 +130,19 @@ func TestSocksBridageUDP(t *testing.T) {
|
|||||||
UdpEnabled: true,
|
UdpEnabled: true,
|
||||||
}),
|
}),
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
|
||||||
|
PortList: &net.PortList{Range: []*net.PortRange{net.SinglePortRange(serverPort + 1)}},
|
||||||
|
Listen: net.NewIPOrDomain(net.LocalHostIP),
|
||||||
|
}),
|
||||||
|
ProxySettings: serial.ToTypedMessage(&dokodemo.Config{
|
||||||
|
Address: net.NewIPOrDomain(dest.Address),
|
||||||
|
Port: uint32(dest.Port),
|
||||||
|
NetworkList: &net.NetworkList{
|
||||||
|
Network: []net.Network{net.Network_UDP},
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
},
|
||||||
},
|
},
|
||||||
Outbound: []*core.OutboundHandlerConfig{
|
Outbound: []*core.OutboundHandlerConfig{
|
||||||
{
|
{
|
||||||
@ -136,7 +151,19 @@ func TestSocksBridageUDP(t *testing.T) {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
clientPort := tcp.PickPort()
|
server, _ := InitializeServerConfig(serverConfig)
|
||||||
|
if server != nil && WaitConnAvailableWithTest(t, testUDPConn(serverPort+1, 1024, time.Second*2)) {
|
||||||
|
defer CloseServer(server)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
retry++
|
||||||
|
if retry > 5 {
|
||||||
|
t.Fatal("All attempts failed to start server")
|
||||||
|
}
|
||||||
|
serverPort = tcp.PickPort()
|
||||||
|
}
|
||||||
|
|
||||||
|
clientPort := udp.PickPort()
|
||||||
clientConfig := &core.Config{
|
clientConfig := &core.Config{
|
||||||
Inbound: []*core.InboundHandlerConfig{
|
Inbound: []*core.InboundHandlerConfig{
|
||||||
{
|
{
|
||||||
@ -148,7 +175,7 @@ func TestSocksBridageUDP(t *testing.T) {
|
|||||||
Address: net.NewIPOrDomain(dest.Address),
|
Address: net.NewIPOrDomain(dest.Address),
|
||||||
Port: uint32(dest.Port),
|
Port: uint32(dest.Port),
|
||||||
NetworkList: &net.NetworkList{
|
NetworkList: &net.NetworkList{
|
||||||
Network: []net.Network{net.Network_TCP, net.Network_UDP},
|
Network: []net.Network{net.Network_UDP},
|
||||||
},
|
},
|
||||||
}),
|
}),
|
||||||
},
|
},
|
||||||
@ -175,12 +202,12 @@ func TestSocksBridageUDP(t *testing.T) {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
servers, err := InitializeServerConfigs(serverConfig, clientConfig)
|
server, err := InitializeServerConfig(clientConfig)
|
||||||
common.Must(err)
|
common.Must(err)
|
||||||
defer CloseAllServers(servers)
|
defer CloseServer(server)
|
||||||
|
|
||||||
if err := testUDPConn(clientPort, 1024, time.Second*5)(); err != nil {
|
if !WaitConnAvailableWithTest(t, testUDPConn(clientPort, 1024, time.Second*2)) {
|
||||||
t.Error(err)
|
t.Fail()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -192,7 +219,9 @@ func TestSocksBridageUDPWithRouting(t *testing.T) {
|
|||||||
common.Must(err)
|
common.Must(err)
|
||||||
defer udpServer.Close()
|
defer udpServer.Close()
|
||||||
|
|
||||||
|
retry := 1
|
||||||
serverPort := tcp.PickPort()
|
serverPort := tcp.PickPort()
|
||||||
|
for {
|
||||||
serverConfig := &core.Config{
|
serverConfig := &core.Config{
|
||||||
App: []*serial.TypedMessage{
|
App: []*serial.TypedMessage{
|
||||||
serial.ToTypedMessage(&router.Config{
|
serial.ToTypedMessage(&router.Config{
|
||||||
@ -201,7 +230,7 @@ func TestSocksBridageUDPWithRouting(t *testing.T) {
|
|||||||
TargetTag: &router.RoutingRule_Tag{
|
TargetTag: &router.RoutingRule_Tag{
|
||||||
Tag: "out",
|
Tag: "out",
|
||||||
},
|
},
|
||||||
InboundTag: []string{"socks"},
|
InboundTag: []string{"socks", "dokodemo"},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}),
|
}),
|
||||||
@ -219,6 +248,20 @@ func TestSocksBridageUDPWithRouting(t *testing.T) {
|
|||||||
UdpEnabled: true,
|
UdpEnabled: true,
|
||||||
}),
|
}),
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
Tag: "dokodemo",
|
||||||
|
ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
|
||||||
|
PortList: &net.PortList{Range: []*net.PortRange{net.SinglePortRange(serverPort + 1)}},
|
||||||
|
Listen: net.NewIPOrDomain(net.LocalHostIP),
|
||||||
|
}),
|
||||||
|
ProxySettings: serial.ToTypedMessage(&dokodemo.Config{
|
||||||
|
Address: net.NewIPOrDomain(dest.Address),
|
||||||
|
Port: uint32(dest.Port),
|
||||||
|
NetworkList: &net.NetworkList{
|
||||||
|
Network: []net.Network{net.Network_UDP},
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
},
|
||||||
},
|
},
|
||||||
Outbound: []*core.OutboundHandlerConfig{
|
Outbound: []*core.OutboundHandlerConfig{
|
||||||
{
|
{
|
||||||
@ -231,7 +274,19 @@ func TestSocksBridageUDPWithRouting(t *testing.T) {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
clientPort := tcp.PickPort()
|
server, _ := InitializeServerConfig(serverConfig)
|
||||||
|
if server != nil && WaitConnAvailableWithTest(t, testUDPConn(serverPort+1, 1024, time.Second*2)) {
|
||||||
|
defer CloseServer(server)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
retry++
|
||||||
|
if retry > 5 {
|
||||||
|
t.Fatal("All attempts failed to start server")
|
||||||
|
}
|
||||||
|
serverPort = tcp.PickPort()
|
||||||
|
}
|
||||||
|
|
||||||
|
clientPort := udp.PickPort()
|
||||||
clientConfig := &core.Config{
|
clientConfig := &core.Config{
|
||||||
Inbound: []*core.InboundHandlerConfig{
|
Inbound: []*core.InboundHandlerConfig{
|
||||||
{
|
{
|
||||||
@ -243,7 +298,7 @@ func TestSocksBridageUDPWithRouting(t *testing.T) {
|
|||||||
Address: net.NewIPOrDomain(dest.Address),
|
Address: net.NewIPOrDomain(dest.Address),
|
||||||
Port: uint32(dest.Port),
|
Port: uint32(dest.Port),
|
||||||
NetworkList: &net.NetworkList{
|
NetworkList: &net.NetworkList{
|
||||||
Network: []net.Network{net.Network_TCP, net.Network_UDP},
|
Network: []net.Network{net.Network_UDP},
|
||||||
},
|
},
|
||||||
}),
|
}),
|
||||||
},
|
},
|
||||||
@ -262,12 +317,12 @@ func TestSocksBridageUDPWithRouting(t *testing.T) {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
servers, err := InitializeServerConfigs(serverConfig, clientConfig)
|
server, err := InitializeServerConfig(clientConfig)
|
||||||
common.Must(err)
|
common.Must(err)
|
||||||
defer CloseAllServers(servers)
|
defer CloseServer(server)
|
||||||
|
|
||||||
if err := testUDPConn(clientPort, 1024, time.Second*5)(); err != nil {
|
if !WaitConnAvailableWithTest(t, testUDPConn(clientPort, 1024, time.Second*2)) {
|
||||||
t.Error(err)
|
t.Fail()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -108,7 +108,7 @@ func TestVMessDynamicPort(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
server, _ := InitializeServerConfig(serverConfig)
|
server, _ := InitializeServerConfig(serverConfig)
|
||||||
if server != nil && tcpConnAvailableAtPort(t, serverPort+100) {
|
if server != nil && WaitConnAvailableWithTest(t, testTCPConn(serverPort+100, 1024, time.Second*2)) {
|
||||||
defer CloseServer(server)
|
defer CloseServer(server)
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
@ -167,28 +167,11 @@ func TestVMessDynamicPort(t *testing.T) {
|
|||||||
common.Must(err)
|
common.Must(err)
|
||||||
defer CloseServer(server)
|
defer CloseServer(server)
|
||||||
|
|
||||||
if !tcpConnAvailableAtPort(t, clientPort) {
|
if !WaitConnAvailableWithTest(t, testTCPConn(clientPort, 1024, time.Second*2)) {
|
||||||
t.Fail()
|
t.Fail()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func tcpConnAvailableAtPort(t *testing.T, port net.Port) bool {
|
|
||||||
for i := 1; ; i++ {
|
|
||||||
if i > 10 {
|
|
||||||
t.Log("All attempts failed to test tcp conn")
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
time.Sleep(time.Millisecond * 10)
|
|
||||||
if err := testTCPConn(port, 1024, time.Second*2)(); err != nil {
|
|
||||||
t.Log("err ", err)
|
|
||||||
} else {
|
|
||||||
t.Log("success with", i, "attempts")
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestVMessGCM(t *testing.T) {
|
func TestVMessGCM(t *testing.T) {
|
||||||
tcpServer := tcp.Server{
|
tcpServer := tcp.Server{
|
||||||
MsgProcessor: xor,
|
MsgProcessor: xor,
|
||||||
|
Loading…
Reference in New Issue
Block a user