Skip to content

Commit

Permalink
test: use a queue of open ports for tests (#14893)
Browse files Browse the repository at this point in the history
Co-authored-by: Marko <[email protected]>
(cherry picked from commit 5e57be0)

# Conflicts:
#	testutil/network/network.go
#	testutil/network/util.go
#	x/genutil/client/cli/init_test.go
  • Loading branch information
facundomedica authored and mergify[bot] committed Feb 6, 2023
1 parent 0c0c6a9 commit 178e654
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 4 deletions.
63 changes: 59 additions & 4 deletions testutil/network/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,30 @@ import (
)

// package-wide network lock to only allow one test network at a time
var lock = new(sync.Mutex)
var (
lock = new(sync.Mutex)
portPool = make(chan string, 200)
)

func init() {
closeFns := []func() error{}
for i := 0; i < 200; i++ {
_, port, closeFn, err := FreeTCPAddr()
if err != nil {
panic(err)
}

portPool <- port
closeFns = append(closeFns, closeFn)
}

for _, closeFn := range closeFns {
err := closeFn()
if err != nil {
panic(err)
}
}
}

// AppConstructor defines a function which accepts a network configuration and
// creates an ABCI Application to provide to Tendermint.
Expand Down Expand Up @@ -340,11 +363,18 @@ func New(l Logger, baseDir string, cfg Config) (*Network, error) {
if cfg.APIAddress != "" {
apiListenAddr = cfg.APIAddress
} else {
<<<<<<< HEAD
var err error
apiListenAddr, _, err = server.FreeTCPAddr()
if err != nil {
return nil, err
=======
if len(portPool) == 0 {
return nil, fmt.Errorf("failed to get port for API server")
>>>>>>> 5e57be076 (test: use a queue of open ports for tests (#14893))
}
port := <-portPool
apiListenAddr = fmt.Sprintf("tcp://0.0.0.0:%s", port)
}

appCfg.API.Address = apiListenAddr
Expand All @@ -357,21 +387,33 @@ func New(l Logger, baseDir string, cfg Config) (*Network, error) {
if cfg.RPCAddress != "" {
tmCfg.RPC.ListenAddress = cfg.RPCAddress
} else {
<<<<<<< HEAD
rpcAddr, _, err := server.FreeTCPAddr()
if err != nil {
return nil, err
=======
if len(portPool) == 0 {
return nil, fmt.Errorf("failed to get port for RPC server")
>>>>>>> 5e57be076 (test: use a queue of open ports for tests (#14893))
}
tmCfg.RPC.ListenAddress = rpcAddr
port := <-portPool
tmCfg.RPC.ListenAddress = fmt.Sprintf("tcp://0.0.0.0:%s", port)
}

if cfg.GRPCAddress != "" {
appCfg.GRPC.Address = cfg.GRPCAddress
} else {
<<<<<<< HEAD
_, grpcPort, err := server.FreeTCPAddr()
if err != nil {
return nil, err
=======
if len(portPool) == 0 {
return nil, fmt.Errorf("failed to get port for GRPC server")
>>>>>>> 5e57be076 (test: use a queue of open ports for tests (#14893))
}
appCfg.GRPC.Address = fmt.Sprintf("0.0.0.0:%s", grpcPort)
port := <-portPool
appCfg.GRPC.Address = fmt.Sprintf("0.0.0.0:%s", port)
}
appCfg.GRPC.Enable = true

Expand Down Expand Up @@ -409,17 +451,30 @@ func New(l Logger, baseDir string, cfg Config) (*Network, error) {
tmCfg.Moniker = nodeDirName
monikers[i] = nodeDirName

<<<<<<< HEAD
proxyAddr, _, err := server.FreeTCPAddr()
if err != nil {
return nil, err
=======
if len(portPool) == 0 {
return nil, fmt.Errorf("failed to get port for Proxy server")
>>>>>>> 5e57be076 (test: use a queue of open ports for tests (#14893))
}
port := <-portPool
proxyAddr := fmt.Sprintf("tcp://0.0.0.0:%s", port)
tmCfg.ProxyApp = proxyAddr

<<<<<<< HEAD
p2pAddr, _, err := server.FreeTCPAddr()
if err != nil {
return nil, err
=======
if len(portPool) == 0 {
return nil, fmt.Errorf("failed to get port for Proxy server")
>>>>>>> 5e57be076 (test: use a queue of open ports for tests (#14893))
}

port = <-portPool
p2pAddr := fmt.Sprintf("tcp://0.0.0.0:%s", port)
tmCfg.P2P.ListenAddress = p2pAddr
tmCfg.P2P.AddrBookStrict = false
tmCfg.P2P.AllowDuplicateIP = true
Expand Down
31 changes: 31 additions & 0 deletions testutil/network/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,23 @@ import (
"path/filepath"
"time"

<<<<<<< HEAD
"github.com/tendermint/tendermint/node"
"github.com/tendermint/tendermint/p2p"
pvm "github.com/tendermint/tendermint/privval"
"github.com/tendermint/tendermint/proxy"
"github.com/tendermint/tendermint/rpc/client/local"
"github.com/tendermint/tendermint/types"
tmtime "github.com/tendermint/tendermint/types/time"
=======
"github.com/cometbft/cometbft/node"
"github.com/cometbft/cometbft/p2p"
pvm "github.com/cometbft/cometbft/privval"
"github.com/cometbft/cometbft/proxy"
"github.com/cometbft/cometbft/rpc/client/local"
"github.com/cometbft/cometbft/types"
tmtime "github.com/cometbft/cometbft/types/time"
>>>>>>> 5e57be076 (test: use a queue of open ports for tests (#14893))

"github.com/cosmos/cosmos-sdk/server/api"
servergrpc "github.com/cosmos/cosmos-sdk/server/grpc"
Expand Down Expand Up @@ -202,3 +212,24 @@ func writeFile(name string, dir string, contents []byte) error {

return nil
}
<<<<<<< HEAD
=======

// Get a free address for a test tendermint server
// protocol is either tcp, http, etc
func FreeTCPAddr() (addr, port string, closeFn func() error, err error) {
l, err := net.Listen("tcp", "localhost:0")
if err != nil {
return "", "", nil, err
}

closeFn = func() error {
return l.Close()
}

portI := l.Addr().(*net.TCPAddr).Port
port = fmt.Sprintf("%d", portI)
addr = fmt.Sprintf("tcp://0.0.0.0:%s", port)
return
}
>>>>>>> 5e57be076 (test: use a queue of open ports for tests (#14893))
5 changes: 5 additions & 0 deletions x/genutil/client/cli/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,13 @@ func TestStartStandAlone(t *testing.T) {
app, err := mock.NewApp(home, logger)
require.NoError(t, err)

<<<<<<< HEAD
svrAddr, _, err := server.FreeTCPAddr()
=======
svrAddr, _, closeFn, err := network.FreeTCPAddr()
>>>>>>> 5e57be076 (test: use a queue of open ports for tests (#14893))
require.NoError(t, err)
require.NoError(t, closeFn())

svr, err := abci_server.NewServer(svrAddr, "socket", app)
require.NoError(t, err, "error creating listener")
Expand Down

0 comments on commit 178e654

Please sign in to comment.