Skip to content

Commit

Permalink
Merge pull request #1182 from ExchangeUnion/test/sim-dynamic-ports
Browse files Browse the repository at this point in the history
test: use dynamic ports for simulation tests
  • Loading branch information
sangaman authored Aug 29, 2019
2 parents 381d71d + 5a4a5a7 commit c8af327
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 52 deletions.
1 change: 1 addition & 0 deletions test/simulation/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ require (
github.com/ltcsuite/ltcutil v0.0.0-20190507133322-23cdfa9fcc3d
github.com/onsi/ginkgo v1.8.0 // indirect
github.com/onsi/gomega v1.5.0 // indirect
github.com/phayes/freeport v0.0.0-20180830031419-95f893ade6f2
github.com/roasbeef/btcd v0.0.0-20180418012700-a03db407e40d
github.com/roasbeef/btcutil v0.0.0-20180406014609-dfb640c57141
github.com/stretchr/testify v1.3.0
Expand Down
2 changes: 2 additions & 0 deletions test/simulation/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,8 @@ github.com/onsi/gomega v1.4.1/go.mod h1:C1qb7wdrVGGVU+Z6iS04AVkA3Q65CEZX59MT0QO5
github.com/onsi/gomega v1.4.3/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
github.com/onsi/gomega v1.5.0 h1:izbySO9zDPmjJ8rDjLvkA2zJHIo+HkYXHnf7eN7SSyo=
github.com/onsi/gomega v1.5.0/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
github.com/phayes/freeport v0.0.0-20180830031419-95f893ade6f2 h1:JhzVVoYvbOACxoUmOs6V/G4D5nPVUW73rKvXxP4XUJc=
github.com/phayes/freeport v0.0.0-20180830031419-95f893ade6f2/go.mod h1:iIss55rKnNBTvrwdmkUpLnDpZoAHvWaiq5+iMmen4AE=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/roasbeef/btcd v0.0.0-20180418012700-a03db407e40d h1:3p7ZK0clyDVNQL3a5q4jTaTDv5YzW4AxkdftpBZxsrU=
Expand Down
57 changes: 14 additions & 43 deletions test/simulation/lntest/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/go-errors/errors"
"github.com/lightningnetwork/lnd/lnrpc"
"github.com/lightningnetwork/lnd/macaroons"
"github.com/phayes/freeport"
"github.com/roasbeef/btcd/chaincfg/chainhash"
"github.com/roasbeef/btcd/wire"
)
Expand All @@ -31,27 +32,6 @@ var (
// numActiveNodes is the number of active nodes within the test network.
numActiveNodes = 0

// defaultNodePort is the initial p2p port which will be used by the
// first created lightning node to listen on for incoming p2p
// connections. Subsequent allocated ports for future Lightning nodes
// instances will be monotonically increasing numbers calculated as
// such: defaultP2pPort + (3 * harness.nodeNum).
defaultNodePort = 19555

// defaultClientPort is the initial rpc port which will be used by the
// first created lightning node to listen on for incoming rpc
// connections. Subsequent allocated ports for future rpc harness
// instances will be monotonically increasing numbers calculated
// as such: defaultP2pPort + (3 * harness.nodeNum).
defaultClientPort = 19556

// defaultRestPort is the initial rest port which will be used by the
// first created lightning node to listen on for incoming rest
// connections. Subsequent allocated ports for future rpc harness
// instances will be monotonically increasing numbers calculated
// as such: defaultP2pPort + (3 * harness.nodeNum).
defaultRestPort = 19557

// logOutput is a flag that can be set to append the output from the
// seed nodes to log files.
logOutput = flag.Bool("logoutput", false,
Expand All @@ -68,26 +48,6 @@ var (
trickleDelay = 50
)

// generateListeningPorts returns three ints representing ports to listen on
// designated for the current lightning network test. If there haven't been any
// test instances created, the default ports are used. Otherwise, in order to
// support multiple test nodes running at once, the p2p, rpc, and rest ports
// are incremented after each initialization.
func generateListeningPorts() (int, int, int) {
var p2p, rpc, rest int
if numActiveNodes == 0 {
p2p = defaultNodePort
rpc = defaultClientPort
rest = defaultRestPort
} else {
p2p = defaultNodePort + (3 * numActiveNodes)
rpc = defaultClientPort + (3 * numActiveNodes)
rest = defaultRestPort + (3 * numActiveNodes)
}

return p2p, rpc, rest
}

type nodeConfig struct {
Name string
RPCConfig *ConnConfig
Expand Down Expand Up @@ -215,8 +175,8 @@ var _ lnrpc.WalletUnlockerClient = (*HarnessNode)(nil)

// newNode creates a new test lightning node instance from the passed config.
func newNode(cfg nodeConfig) (*HarnessNode, error) {
var err error
if cfg.BaseDir == "" {
var err error
cfg.BaseDir, err = ioutil.TempDir("", "lndtest-node")
if err != nil {
return nil, err
Expand All @@ -230,7 +190,18 @@ func newNode(cfg nodeConfig) (*HarnessNode, error) {
cfg.ReadMacPath = filepath.Join(cfg.DataDir, "readonly.macaroon")
cfg.InvoiceMacPath = filepath.Join(cfg.DataDir, "invoice.macaroon")

cfg.P2PPort, cfg.RPCPort, cfg.RESTPort = generateListeningPorts()
cfg.P2PPort, err = freeport.GetFreePort()
if err != nil {
return nil, err
}
cfg.RPCPort, err = freeport.GetFreePort()
if err != nil {
return nil, err
}
cfg.RESTPort, err = freeport.GetFreePort()
if err != nil {
return nil, err
}

nodeNum := numActiveNodes
numActiveNodes++
Expand Down
23 changes: 14 additions & 9 deletions test/simulation/xudtest/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,13 @@ import (
"github.com/ExchangeUnion/xud-simulation/lntest"
"github.com/ExchangeUnion/xud-simulation/xudrpc"
"github.com/go-errors/errors"
"github.com/phayes/freeport"
"golang.org/x/net/context"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
)

var (
numActiveNodes int32
baseP2PPort = 40000
baseRPCPort = 30000
baseHTTPPort = 35000
)
var numActiveNodes int32

type nodeConfig struct {
DataDir string
Expand Down Expand Up @@ -138,9 +134,18 @@ func newNode(name string, xudPath string, noBalanceChecks bool) (*HarnessNode, e
cfg.LogPath = fmt.Sprintf("./temp/logs/xud-%s-%d.log", name, epoch)

cfg.TLSCertPath = filepath.Join(cfg.DataDir, "tls.cert")
cfg.P2PPort = baseP2PPort + nodeNum
cfg.RPCPort = baseRPCPort + nodeNum
cfg.HTTPPort = baseHTTPPort + nodeNum
cfg.P2PPort, err = freeport.GetFreePort()
if err != nil {
return nil, err
}
cfg.RPCPort, err = freeport.GetFreePort()
if err != nil {
return nil, err
}
cfg.HTTPPort, err = freeport.GetFreePort()
if err != nil {
return nil, err
}

return &HarnessNode{
Cfg: &cfg,
Expand Down

0 comments on commit c8af327

Please sign in to comment.