integration/rpctest: randomizes port in rpctest.New to reduce collisions

This commit is contained in:
Liran Sharir 2020-11-10 16:27:55 -05:00 committed by John C. Vernaleo
parent f070f7f2be
commit 9fd26cf795

View file

@ -7,6 +7,7 @@ package rpctest
import ( import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"math/rand"
"net" "net"
"os" "os"
"path/filepath" "path/filepath"
@ -40,16 +41,6 @@ var (
// current number of active test nodes. // current number of active test nodes.
numTestInstances = 0 numTestInstances = 0
// processID is the process ID of the current running process. It is
// used to calculate ports based upon it when launching an rpc
// harnesses. The intent is to allow multiple process to run in
// parallel without port collisions.
//
// It should be noted however that there is still some small probability
// that there will be port collisions either due to other processes
// running or simply due to the stars aligning on the process IDs.
processID = os.Getpid()
// testInstances is a private package-level slice used to keep track of // testInstances is a private package-level slice used to keep track of
// all active test harnesses. This global can be used to perform // all active test harnesses. This global can be used to perform
// various "joins", shutdown several active harnesses after a test, // various "joins", shutdown several active harnesses after a test,
@ -477,13 +468,14 @@ func (h *Harness) GenerateAndSubmitBlockWithCustomCoinbaseOutputs(
// addresses designated for the current rpc test. If there haven't been any // addresses designated for the current rpc test. If there haven't been any
// test instances created, the default ports are used. Otherwise, in order to // test instances created, the default ports are used. Otherwise, in order to
// support multiple test nodes running at once, the p2p and rpc port are // support multiple test nodes running at once, the p2p and rpc port are
// incremented after each initialization. // picked at random between {min/max}PeerPort and {min/max}RPCPort respectively.
func generateListeningAddresses() (string, string) { func generateListeningAddresses() (string, string) {
localhost := "127.0.0.1" localhost := "127.0.0.1"
rand.Seed(time.Now().UnixNano())
portString := func(minPort, maxPort int) string { portString := func(minPort, maxPort int) string {
port := minPort + numTestInstances + ((20 * processID) % port := minPort + rand.Intn(maxPort-minPort)
(maxPort - minPort))
return strconv.Itoa(port) return strconv.Itoa(port)
} }