From 98d7e64dc1e85dc494a583101a8202cc3ad74177 Mon Sep 17 00:00:00 2001 From: yyforyongyu Date: Tue, 14 Sep 2021 14:15:04 +0800 Subject: [PATCH] itest: add test_common to hold commonly used methods A new file, test_common.go, is added to hold commonly used functions across lntest. --- lntest/harness.go | 8 --- lntest/node.go | 106 +++----------------------------------- lntest/test_common.go | 116 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 122 insertions(+), 108 deletions(-) create mode 100644 lntest/test_common.go diff --git a/lntest/harness.go b/lntest/harness.go index 5d96efac8..84f61d271 100644 --- a/lntest/harness.go +++ b/lntest/harness.go @@ -84,14 +84,6 @@ type NetworkHarness struct { mtx sync.Mutex } -type DatabaseBackend int - -const ( - BackendBbolt DatabaseBackend = iota - BackendEtcd - BackendPostgres -) - // NewNetworkHarness creates a new network test harness. // TODO(roasbeef): add option to use golang's build library to a binary of the // current repo. This will save developers from having to manually `go install` diff --git a/lntest/node.go b/lntest/node.go index f56fe1c91..20166df98 100644 --- a/lntest/node.go +++ b/lntest/node.go @@ -6,18 +6,15 @@ import ( "crypto/rand" "encoding/hex" "encoding/json" - "flag" "fmt" "io" "io/ioutil" - "net" "os" "os/exec" "path" "path/filepath" "strings" "sync" - "sync/atomic" "time" "github.com/btcsuite/btcd/chaincfg" @@ -44,11 +41,6 @@ import ( ) const ( - // defaultNodePort is the start of the range for listening ports of - // harness nodes. Ports are monotonically increasing starting from this - // number and are determined by the results of nextAvailablePort(). - defaultNodePort = 5555 - // logPubKeyBytes is the number of bytes of the node's PubKey that will // be appended to the log file name. The whole PubKey is too long and // not really necessary to quickly identify what node produced which @@ -59,110 +51,24 @@ const ( // release of announcements by AuthenticatedGossiper to the network. trickleDelay = 50 - // listenerFormat is the format string that is used to generate local - // listener addresses. - listenerFormat = "127.0.0.1:%d" - - // NeutrinoBackendName is the name of the neutrino backend. - NeutrinoBackendName = "neutrino" - postgresDsn = "postgres://postgres:postgres@localhost:6432/%s?sslmode=disable" + + // commitInterval specifies the maximum interval the graph database + // will wait between attempting to flush a batch of modifications to + // disk(db.batch-commit-interval). + commitInterval = 10 * time.Millisecond ) var ( // numActiveNodes is the number of active nodes within the test network. numActiveNodes = 0 numActiveNodesMtx sync.Mutex - - // lastPort is the last port determined to be free for use by a new - // node. It should be used atomically. - lastPort uint32 = defaultNodePort - - // logOutput is a flag that can be set to append the output from the - // seed nodes to log files. - logOutput = flag.Bool("logoutput", false, - "log output from node n to file output-n.log") - - // logSubDir is the default directory where the logs are written to if - // logOutput is true. - logSubDir = flag.String("logdir", ".", "default dir to write logs to") - - // goroutineDump is a flag that can be set to dump the active - // goroutines of test nodes on failure. - goroutineDump = flag.Bool("goroutinedump", false, - "write goroutine dump from node n to file pprof-n.log") - - // btcdExecutable is the full path to the btcd binary. - btcdExecutable = flag.String( - "btcdexec", "", "full path to btcd binary", - ) ) func postgresDatabaseDsn(dbName string) string { return fmt.Sprintf(postgresDsn, dbName) } -// NextAvailablePort returns the first port that is available for listening by -// a new node. It panics if no port is found and the maximum available TCP port -// is reached. -func NextAvailablePort() int { - port := atomic.AddUint32(&lastPort, 1) - for port < 65535 { - // If there are no errors while attempting to listen on this - // port, close the socket and return it as available. While it - // could be the case that some other process picks up this port - // between the time the socket is closed and it's reopened in - // the harness node, in practice in CI servers this seems much - // less likely than simply some other process already being - // bound at the start of the tests. - addr := fmt.Sprintf(listenerFormat, port) - l, err := net.Listen("tcp4", addr) - if err == nil { - err := l.Close() - if err == nil { - return int(port) - } - } - port = atomic.AddUint32(&lastPort, 1) - } - - // No ports available? Must be a mistake. - panic("no ports available for listening") -} - -// ApplyPortOffset adds the given offset to the lastPort variable, making it -// possible to run the tests in parallel without colliding on the same ports. -func ApplyPortOffset(offset uint32) { - _ = atomic.AddUint32(&lastPort, offset) -} - -// GetLogDir returns the passed --logdir flag or the default value if it wasn't -// set. -func GetLogDir() string { - if logSubDir != nil && *logSubDir != "" { - return *logSubDir - } - return "." -} - -// GetBtcdBinary returns the full path to the binary of the custom built btcd -// executable or an empty string if none is set. -func GetBtcdBinary() string { - if btcdExecutable != nil { - return *btcdExecutable - } - - return "" -} - -// GenerateBtcdListenerAddresses is a function that returns two listener -// addresses with unique ports and should be used to overwrite rpctest's default -// generator which is prone to use colliding ports. -func GenerateBtcdListenerAddresses() (string, string) { - return fmt.Sprintf(listenerFormat, NextAvailablePort()), - fmt.Sprintf(listenerFormat, NextAvailablePort()) -} - // generateListeningPorts returns four ints representing ports to listen on // designated for the current lightning network test. This returns the next // available ports for the p2p, rpc, rest and profiling services. @@ -286,7 +192,7 @@ func (cfg NodeConfig) genArgs() []string { args = append(args, "--nobootstrap") args = append(args, "--debuglevel=debug") args = append(args, "--bitcoin.defaultchanconfs=1") - args = append(args, fmt.Sprintf("--db.batch-commit-interval=%v", 10*time.Millisecond)) + args = append(args, fmt.Sprintf("--db.batch-commit-interval=%v", commitInterval)) args = append(args, fmt.Sprintf("--bitcoin.defaultremotedelay=%v", DefaultCSV)) args = append(args, fmt.Sprintf("--rpclisten=%v", cfg.RPCAddr())) args = append(args, fmt.Sprintf("--restlisten=%v", cfg.RESTAddr())) diff --git a/lntest/test_common.go b/lntest/test_common.go new file mode 100644 index 000000000..25d52e892 --- /dev/null +++ b/lntest/test_common.go @@ -0,0 +1,116 @@ +package lntest + +import ( + "flag" + "fmt" + "net" + "sync/atomic" +) + +const ( + // defaultNodePort is the start of the range for listening ports of + // harness nodes. Ports are monotonically increasing starting from this + // number and are determined by the results of nextAvailablePort(). + defaultNodePort = 5555 + + // listenerFormat is the format string that is used to generate local + // listener addresses. + listenerFormat = "127.0.0.1:%d" + + // NeutrinoBackendName is the name of the neutrino backend. + NeutrinoBackendName = "neutrino" +) + +type DatabaseBackend int + +const ( + BackendBbolt DatabaseBackend = iota + BackendEtcd + BackendPostgres +) + +var ( + // lastPort is the last port determined to be free for use by a new + // node. It should be used atomically. + lastPort uint32 = defaultNodePort + + // logOutput is a flag that can be set to append the output from the + // seed nodes to log files. + logOutput = flag.Bool("logoutput", false, + "log output from node n to file output-n.log") + + // logSubDir is the default directory where the logs are written to if + // logOutput is true. + logSubDir = flag.String("logdir", ".", "default dir to write logs to") + + // goroutineDump is a flag that can be set to dump the active + // goroutines of test nodes on failure. + goroutineDump = flag.Bool("goroutinedump", false, + "write goroutine dump from node n to file pprof-n.log") + + // btcdExecutable is the full path to the btcd binary. + btcdExecutable = flag.String( + "btcdexec", "", "full path to btcd binary", + ) +) + +// NextAvailablePort returns the first port that is available for listening by +// a new node. It panics if no port is found and the maximum available TCP port +// is reached. +func NextAvailablePort() int { + port := atomic.AddUint32(&lastPort, 1) + for port < 65535 { + // If there are no errors while attempting to listen on this + // port, close the socket and return it as available. While it + // could be the case that some other process picks up this port + // between the time the socket is closed and it's reopened in + // the harness node, in practice in CI servers this seems much + // less likely than simply some other process already being + // bound at the start of the tests. + addr := fmt.Sprintf(listenerFormat, port) + l, err := net.Listen("tcp4", addr) + if err == nil { + err := l.Close() + if err == nil { + return int(port) + } + } + port = atomic.AddUint32(&lastPort, 1) + } + + // No ports available? Must be a mistake. + panic("no ports available for listening") +} + +// ApplyPortOffset adds the given offset to the lastPort variable, making it +// possible to run the tests in parallel without colliding on the same ports. +func ApplyPortOffset(offset uint32) { + _ = atomic.AddUint32(&lastPort, offset) +} + +// GetLogDir returns the passed --logdir flag or the default value if it wasn't +// set. +func GetLogDir() string { + if logSubDir != nil && *logSubDir != "" { + return *logSubDir + } + return "." +} + +// GetBtcdBinary returns the full path to the binary of the custom built btcd +// executable or an empty string if none is set. +func GetBtcdBinary() string { + if btcdExecutable != nil { + return *btcdExecutable + } + + return "" +} + +// GenerateBtcdListenerAddresses is a function that returns two listener +// addresses with unique ports and should be used to overwrite rpctest's +// default generator which is prone to use colliding ports. +func GenerateBtcdListenerAddresses() (string, string) { + return fmt.Sprintf(listenerFormat, NextAvailablePort()), + fmt.Sprintf(listenerFormat, NextAvailablePort()) +}