chainntnfs: replace defer cleanup with t.Cleanup
Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
This commit is contained in:
4 files changed
+38
-48
No files matched your search
+21
-17
@@ -165,7 +165,7 @@ func CreateSpendTx(t *testing.T, prevOutPoint *wire.OutPoint,
|
||||
// NewMiner spawns testing harness backed by a btcd node that can serve as a
|
||||
// miner.
|
||||
func NewMiner(t *testing.T, extraArgs []string, createChain bool,
|
||||
spendableOutputs uint32) (*rpctest.Harness, func()) {
|
||||
spendableOutputs uint32) *rpctest.Harness {
|
||||
|
||||
t.Helper()
|
||||
|
||||
@@ -175,12 +175,15 @@ func NewMiner(t *testing.T, extraArgs []string, createChain bool,
|
||||
|
||||
node, err := rpctest.New(NetParams, nil, extraArgs, "")
|
||||
require.NoError(t, err, "unable to create backend node")
|
||||
t.Cleanup(func() {
|
||||
require.NoError(t, node.TearDown())
|
||||
})
|
||||
|
||||
if err := node.SetUp(createChain, spendableOutputs); err != nil {
|
||||
node.TearDown()
|
||||
t.Fatalf("unable to set up backend node: %v", err)
|
||||
}
|
||||
|
||||
return node, func() { node.TearDown() }
|
||||
return node
|
||||
}
|
||||
|
||||
// NewBitcoindBackend spawns a new bitcoind node that connects to a miner at the
|
||||
@@ -190,7 +193,7 @@ func NewMiner(t *testing.T, extraArgs []string, createChain bool,
|
||||
// used for block and tx notifications or if its ZMQ interface should be used.
|
||||
// A connection to the newly spawned bitcoind node is returned.
|
||||
func NewBitcoindBackend(t *testing.T, minerAddr string, txindex,
|
||||
rpcpolling bool) (*chain.BitcoindConn, func()) {
|
||||
rpcpolling bool) *chain.BitcoindConn {
|
||||
|
||||
t.Helper()
|
||||
|
||||
@@ -219,6 +222,10 @@ func NewBitcoindBackend(t *testing.T, minerAddr string, txindex,
|
||||
if err := bitcoind.Start(); err != nil {
|
||||
t.Fatalf("unable to start bitcoind: %v", err)
|
||||
}
|
||||
t.Cleanup(func() {
|
||||
_ = bitcoind.Process.Kill()
|
||||
_ = bitcoind.Wait()
|
||||
})
|
||||
|
||||
// Wait for the bitcoind instance to start up.
|
||||
host := fmt.Sprintf("127.0.0.1:%d", rpcPort)
|
||||
@@ -257,21 +264,16 @@ func NewBitcoindBackend(t *testing.T, minerAddr string, txindex,
|
||||
return conn.Start()
|
||||
}, 10*time.Second)
|
||||
if err != nil {
|
||||
bitcoind.Process.Kill()
|
||||
bitcoind.Wait()
|
||||
t.Fatalf("unable to establish connection to bitcoind: %v", err)
|
||||
}
|
||||
t.Cleanup(conn.Stop)
|
||||
|
||||
return conn, func() {
|
||||
conn.Stop()
|
||||
bitcoind.Process.Kill()
|
||||
bitcoind.Wait()
|
||||
}
|
||||
return conn
|
||||
}
|
||||
|
||||
// NewNeutrinoBackend spawns a new neutrino node that connects to a miner at
|
||||
// the specified address.
|
||||
func NewNeutrinoBackend(t *testing.T, minerAddr string) (*neutrino.ChainService, func()) {
|
||||
func NewNeutrinoBackend(t *testing.T, minerAddr string) *neutrino.ChainService {
|
||||
t.Helper()
|
||||
|
||||
spvDir := t.TempDir()
|
||||
@@ -283,6 +285,9 @@ func NewNeutrinoBackend(t *testing.T, minerAddr string) (*neutrino.ChainService,
|
||||
if err != nil {
|
||||
t.Fatalf("unable to create walletdb: %v", err)
|
||||
}
|
||||
t.Cleanup(func() {
|
||||
spvDatabase.Close()
|
||||
})
|
||||
|
||||
// Create an instance of neutrino connected to the running btcd
|
||||
// instance.
|
||||
@@ -294,7 +299,6 @@ func NewNeutrinoBackend(t *testing.T, minerAddr string) (*neutrino.ChainService,
|
||||
}
|
||||
spvNode, err := neutrino.NewChainService(spvConfig)
|
||||
if err != nil {
|
||||
spvDatabase.Close()
|
||||
t.Fatalf("unable to create neutrino: %v", err)
|
||||
}
|
||||
|
||||
@@ -304,9 +308,9 @@ func NewNeutrinoBackend(t *testing.T, minerAddr string) (*neutrino.ChainService,
|
||||
for !spvNode.IsCurrent() {
|
||||
time.Sleep(time.Millisecond * 100)
|
||||
}
|
||||
|
||||
return spvNode, func() {
|
||||
t.Cleanup(func() {
|
||||
spvNode.Stop()
|
||||
spvDatabase.Close()
|
||||
}
|
||||
})
|
||||
|
||||
return spvNode
|
||||
}
|
||||
Reference in new issue
Block a user