Merge pull request #4589 from yyforyongyu/itest-proper-cleanup

Itest: proper connect/disconnect miner
This commit is contained in:
Johan T. Halseth authored and GitHub committed 2020-09-25 10:31:56 +02:00
commit 74e62e5c5b
6 files changed
+69 -31

No files matched your search

+23 -10
View File
@@ -4,6 +4,7 @@ package lntest
import (
"encoding/hex"
"errors"
"fmt"
"os"
@@ -16,11 +17,11 @@ import (
// logDir is the name of the temporary log directory.
const logDir = "./.backendlogs"
// perm is used to signal we want to establish a permanent connection using the
// temp is used to signal we want to establish a temporary connection using the
// btcd Node API.
//
// NOTE: Cannot be const, since the node API expects a reference.
var perm = "perm"
var temp = "temp"
// BtcdBackendConfig is an implementation of the BackendConfig interface
// backed by a btcd node.
@@ -55,12 +56,12 @@ func (b BtcdBackendConfig) GenArgs() []string {
// ConnectMiner is called to establish a connection to the test miner.
func (b BtcdBackendConfig) ConnectMiner() error {
return b.harness.Node.Node(btcjson.NConnect, b.minerAddr, &perm)
return b.harness.Node.Node(btcjson.NConnect, b.minerAddr, &temp)
}
// DisconnectMiner is called to disconnect the miner.
func (b BtcdBackendConfig) DisconnectMiner() error {
return b.harness.Node.Node(btcjson.NRemove, b.minerAddr, &perm)
return b.harness.Node.Node(btcjson.NDisconnect, b.minerAddr, &temp)
}
// Name returns the name of the backend type.
@@ -72,7 +73,7 @@ func (b BtcdBackendConfig) Name() string {
// that node. miner should be set to the P2P address of the miner to connect
// to.
func NewBackend(miner string, netParams *chaincfg.Params) (
*BtcdBackendConfig, func(), error) {
*BtcdBackendConfig, func() error, error) {
args := []string{
"--rejectnonstd",
@@ -80,8 +81,11 @@ func NewBackend(miner string, netParams *chaincfg.Params) (
"--trickleinterval=100ms",
"--debuglevel=debug",
"--logdir=" + logDir,
"--connect=" + miner,
"--nowinservice",
// The miner will get banned and disconnected from the node if
// its requested data are not found. We add a nobanning flag to
// make sure they stay connected if it happens.
"--nobanning",
}
chainBackend, err := rpctest.New(netParams, nil, args)
if err != nil {
@@ -98,19 +102,28 @@ func NewBackend(miner string, netParams *chaincfg.Params) (
minerAddr: miner,
}
cleanUp := func() {
chainBackend.TearDown()
cleanUp := func() error {
var errStr string
if err := chainBackend.TearDown(); err != nil {
errStr += err.Error() + "\n"
}
// After shutting down the chain backend, we'll make a copy of
// the log file before deleting the temporary log dir.
logFile := logDir + "/" + netParams.Name + "/btcd.log"
err := CopyFile("./output_btcd_chainbackend.log", logFile)
if err != nil {
fmt.Printf("unable to copy file: %v\n", err)
errStr += fmt.Sprintf("unable to copy file: %v\n", err)
}
if err = os.RemoveAll(logDir); err != nil {
fmt.Printf("Cannot remove dir %s: %v\n", logDir, err)
errStr += fmt.Sprintf(
"cannot remove dir %s: %v\n", logDir, err,
)
}
if errStr != "" {
return errors.New(errStr)
}
return nil
}
return bd, cleanUp, nil