Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: use a queue of open ports for tests #14893

Merged
merged 13 commits into from
Feb 6, 2023
3 changes: 3 additions & 0 deletions server/api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ type Server struct {
// this mutex to avoid data races.
mtx sync.Mutex
listener net.Listener

ListenerAddr string
facundomedica marked this conversation as resolved.
Show resolved Hide resolved
}

// CustomGRPCHeaderMatcher for mapping request headers to
Expand Down Expand Up @@ -105,6 +107,7 @@ func (s *Server) Start(cfg config.Config) error {
return err
}

s.ListenerAddr = strings.Replace(listener.Addr().Network()+"://"+listener.Addr().String(), "[::]", "0.0.0.0", 1)
s.listener = listener
s.mtx.Unlock()

Expand Down
12 changes: 7 additions & 5 deletions server/grpc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package grpc
import (
"fmt"
"net"
"strings"
"time"

"google.golang.org/grpc"
Expand All @@ -18,7 +19,7 @@ import (
)

// StartGRPCServer starts a gRPC server on the given address.
func StartGRPCServer(clientCtx client.Context, app types.Application, cfg config.GRPCConfig) (*grpc.Server, error) {
func StartGRPCServer(clientCtx client.Context, app types.Application, cfg config.GRPCConfig) (*grpc.Server, string, error) {
maxSendMsgSize := cfg.MaxSendMsgSize
if maxSendMsgSize == 0 {
maxSendMsgSize = config.DefaultGRPCMaxSendMsgSize
Expand Down Expand Up @@ -53,7 +54,7 @@ func StartGRPCServer(clientCtx client.Context, app types.Application, cfg config
InterfaceRegistry: clientCtx.InterfaceRegistry,
})
if err != nil {
return nil, err
return nil, "", err
}

// Reflection allows external clients to see what services and methods
Expand All @@ -62,7 +63,7 @@ func StartGRPCServer(clientCtx client.Context, app types.Application, cfg config

listener, err := net.Listen("tcp", cfg.Address)
if err != nil {
return nil, err
return nil, "", err
}

errCh := make(chan error)
Expand All @@ -75,10 +76,11 @@ func StartGRPCServer(clientCtx client.Context, app types.Application, cfg config

select {
case err := <-errCh:
return nil, err
return nil, "", err

case <-time.After(types.ServerStartTime):
// assume server started successfully
return grpcSrv, nil
addr := strings.Replace(listener.Addr().String(), "[::]", "0.0.0.0", 1)
return grpcSrv, addr, nil
}
}
2 changes: 1 addition & 1 deletion server/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ func startInProcess(ctx *Context, clientCtx client.Context, appCreator types.App
ctx.Logger.Debug("grpc client assigned to client context", "target", grpcAddress)

// start grpc server
grpcSrv, err = servergrpc.StartGRPCServer(clientCtx, app, config.GRPC)
grpcSrv, _, err = servergrpc.StartGRPCServer(clientCtx, app, config.GRPC)
if err != nil {
return err
}
Expand Down
3 changes: 2 additions & 1 deletion tests/e2e/gov/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ package gov
import (
"fmt"

"github.com/cosmos/gogoproto/proto"

"github.com/cosmos/cosmos-sdk/testutil"
sdk "github.com/cosmos/cosmos-sdk/types"
grpctypes "github.com/cosmos/cosmos-sdk/types/grpc"
v1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1"
"github.com/cosmos/gogoproto/proto"
)

func (s *E2ETestSuite) TestGetProposalGRPC() {
Expand Down
15 changes: 5 additions & 10 deletions testutil/network/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,11 +342,8 @@ func New(l Logger, baseDir string, cfg Config) (*Network, error) {
if cfg.APIAddress != "" {
apiListenAddr = cfg.APIAddress
} else {
var err error
apiListenAddr, _, err = FreeTCPAddr()
if err != nil {
return nil, err
}
// use a random free tcp port
apiListenAddr = "tcp://0.0.0.0:0"
}

appCfg.API.Address = apiListenAddr
Expand All @@ -364,16 +361,14 @@ func New(l Logger, baseDir string, cfg Config) (*Network, error) {
return nil, err
}
tmCfg.RPC.ListenAddress = rpcAddr
// tmCfg.RPC.ListenAddress = "tcp://0.0.0.0:0"
}

if cfg.GRPCAddress != "" {
appCfg.GRPC.Address = cfg.GRPCAddress
} else {
_, grpcPort, err := FreeTCPAddr()
if err != nil {
return nil, err
}
appCfg.GRPC.Address = fmt.Sprintf("0.0.0.0:%s", grpcPort)
// use a random free tcp port
appCfg.GRPC.Address = "0.0.0.0:0"
}
appCfg.GRPC.Enable = true
appCfg.GRPCWeb.Enable = true
Expand Down
12 changes: 11 additions & 1 deletion testutil/network/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"net"
"net/url"
"os"
"path/filepath"
"time"
Expand Down Expand Up @@ -77,12 +78,13 @@ func startInProcess(cfg Config, val *Validator) error {
}

if val.AppConfig.GRPC.Enable {
grpcSrv, err := servergrpc.StartGRPCServer(val.ClientCtx, app, val.AppConfig.GRPC)
grpcSrv, addr, err := servergrpc.StartGRPCServer(val.ClientCtx, app, val.AppConfig.GRPC)
if err != nil {
return err
}

val.grpc = grpcSrv
val.AppConfig.GRPC.Address = addr
}

if val.APIAddress != "" {
Expand All @@ -104,6 +106,14 @@ func startInProcess(cfg Config, val *Validator) error {
}

val.api = apiSrv

// Update the APIAddress in case the port was set to 0 (random port).
apiURL, err := url.Parse(apiSrv.ListenerAddr)
if err != nil {
return err
}
val.APIAddress = fmt.Sprintf("http://%s:%s", apiURL.Hostname(), apiURL.Port())
val.AppConfig.API.Address = apiSrv.ListenerAddr
}

return nil
Expand Down