lntest/node: add numActiveNodesMtx

Fixes a slight race condition that could happen since Alice and Bob are
created in different goroutines, leading to using the same listening
ports.
This commit is contained in:
Johan T. Halseth 2019-11-21 13:56:48 +01:00
parent b1e6d9c5cf
commit ddbd7a68ac
No known key found for this signature in database
GPG Key ID: 15BAADA29DA20D26

@ -78,6 +78,7 @@ const (
var ( var (
// numActiveNodes is the number of active nodes within the test network. // numActiveNodes is the number of active nodes within the test network.
numActiveNodes = 0 numActiveNodes = 0
numActiveNodesMtx sync.Mutex
// logOutput is a flag that can be set to append the output from the // logOutput is a flag that can be set to append the output from the
// seed nodes to log files. // seed nodes to log files.
@ -96,6 +97,9 @@ var (
// support multiple test nodes running at once, the p2p, rpc, rest and // support multiple test nodes running at once, the p2p, rpc, rest and
// profiling ports are incremented after each initialization. // profiling ports are incremented after each initialization.
func generateListeningPorts() (int, int, int, int) { func generateListeningPorts() (int, int, int, int) {
numActiveNodesMtx.Lock()
defer numActiveNodesMtx.Unlock()
p2p := defaultNodePort + (4 * numActiveNodes) p2p := defaultNodePort + (4 * numActiveNodes)
rpc := defaultClientPort + (4 * numActiveNodes) rpc := defaultClientPort + (4 * numActiveNodes)
rest := defaultRestPort + (4 * numActiveNodes) rest := defaultRestPort + (4 * numActiveNodes)
@ -295,8 +299,10 @@ func newNode(cfg nodeConfig) (*HarnessNode, error) {
cfg.P2PPort, cfg.RPCPort, cfg.RESTPort, cfg.ProfilePort = generateListeningPorts() cfg.P2PPort, cfg.RPCPort, cfg.RESTPort, cfg.ProfilePort = generateListeningPorts()
numActiveNodesMtx.Lock()
nodeNum := numActiveNodes nodeNum := numActiveNodes
numActiveNodes++ numActiveNodes++
numActiveNodesMtx.Unlock()
return &HarnessNode{ return &HarnessNode{
cfg: &cfg, cfg: &cfg,