chainntnfs: replace defer cleanup with t.Cleanup

Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
This commit is contained in:
Eng Zer Jun
2022-08-27 15:03:58 +08:00
parent b796b747ed
commit cac8f54a45
4 changed files with 38 additions and 48 deletions

View File

@@ -65,6 +65,9 @@ func setUpNotifier(t *testing.T, bitcoindConn *chain.BitcoindConn,
if err := notifier.Start(); err != nil {
t.Fatalf("unable to start notifier: %v", err)
}
t.Cleanup(func() {
require.NoError(t, notifier.Stop())
})
return notifier
}
@@ -107,15 +110,13 @@ func TestHistoricalConfDetailsTxIndex(t *testing.T) {
}
func testHistoricalConfDetailsTxIndex(t *testing.T, rpcPolling bool) {
miner, tearDown := chainntnfs.NewMiner(
miner := chainntnfs.NewMiner(
t, []string{"--txindex"}, true, 25,
)
defer tearDown()
bitcoindConn, cleanUp := chainntnfs.NewBitcoindBackend(
bitcoindConn := chainntnfs.NewBitcoindBackend(
t, miner.P2PAddress(), true, rpcPolling,
)
defer cleanUp()
hintCache := initHintCache(t)
blockCache := blockcache.NewBlockCache(10000)
@@ -123,7 +124,6 @@ func testHistoricalConfDetailsTxIndex(t *testing.T, rpcPolling bool) {
notifier := setUpNotifier(
t, bitcoindConn, hintCache, hintCache, blockCache,
)
defer notifier.Stop()
syncNotifierWithMiner(t, notifier, miner)
@@ -198,21 +198,16 @@ func TestHistoricalConfDetailsNoTxIndex(t *testing.T) {
}
func testHistoricalConfDetailsNoTxIndex(t *testing.T, rpcpolling bool) {
miner, tearDown := chainntnfs.NewMiner(t, nil, true, 25)
defer tearDown()
miner := chainntnfs.NewMiner(t, nil, true, 25)
bitcoindConn, cleanUp := chainntnfs.NewBitcoindBackend(
bitcoindConn := chainntnfs.NewBitcoindBackend(
t, miner.P2PAddress(), false, rpcpolling,
)
defer cleanUp()
hintCache := initHintCache(t)
blockCache := blockcache.NewBlockCache(10000)
notifier := setUpNotifier(
t, bitcoindConn, hintCache, hintCache, blockCache,
)
defer notifier.Stop()
notifier := setUpNotifier(t, bitcoindConn, hintCache, hintCache, blockCache)
// Since the node has its txindex disabled, we fall back to scanning the
// chain manually. A transaction unknown to the network should not be

View File

@@ -61,6 +61,9 @@ func setUpNotifier(t *testing.T, h *rpctest.Harness) *BtcdNotifier {
if err := notifier.Start(); err != nil {
t.Fatalf("unable to start notifier: %v", err)
}
t.Cleanup(func() {
require.NoError(t, notifier.Stop())
})
return notifier
}
@@ -70,13 +73,11 @@ func setUpNotifier(t *testing.T, h *rpctest.Harness) *BtcdNotifier {
func TestHistoricalConfDetailsTxIndex(t *testing.T) {
t.Parallel()
harness, tearDown := chainntnfs.NewMiner(
harness := chainntnfs.NewMiner(
t, []string{"--txindex"}, true, 25,
)
defer tearDown()
notifier := setUpNotifier(t, harness)
defer notifier.Stop()
// A transaction unknown to the node should not be found within the
// txindex even if it is enabled, so we should not proceed with any
@@ -144,11 +145,9 @@ func TestHistoricalConfDetailsTxIndex(t *testing.T) {
func TestHistoricalConfDetailsNoTxIndex(t *testing.T) {
t.Parallel()
harness, tearDown := chainntnfs.NewMiner(t, nil, true, 25)
defer tearDown()
harness := chainntnfs.NewMiner(t, nil, true, 25)
notifier := setUpNotifier(t, harness)
defer notifier.Stop()
// Since the node has its txindex disabled, we fall back to scanning the
// chain manually. A transaction unknown to the network should not be

View File

@@ -1816,8 +1816,7 @@ func TestInterfaces(t *testing.T, targetBackEnd string) {
// dedicated miner to generate blocks, cause re-orgs, etc. We'll set up
// this node with a chain length of 125, so we have plenty of BTC to
// play around with.
miner, tearDown := chainntnfs.NewMiner(t, nil, true, 25)
defer tearDown()
miner := chainntnfs.NewMiner(t, nil, true, 25)
rpcConfig := miner.RPCConfig()
p2pAddr := miner.P2PAddress()
@@ -1850,14 +1849,13 @@ func TestInterfaces(t *testing.T, targetBackEnd string) {
blockCache := blockcache.NewBlockCache(10000)
var (
cleanUp func()
newNotifier func() (chainntnfs.TestChainNotifier, error)
)
switch notifierType {
case "bitcoind":
var bitcoindConn *chain.BitcoindConn
bitcoindConn, cleanUp = chainntnfs.NewBitcoindBackend(
bitcoindConn = chainntnfs.NewBitcoindBackend(
t, p2pAddr, true, false,
)
newNotifier = func() (chainntnfs.TestChainNotifier, error) {
@@ -1869,7 +1867,7 @@ func TestInterfaces(t *testing.T, targetBackEnd string) {
case "bitcoind-rpc-polling":
var bitcoindConn *chain.BitcoindConn
bitcoindConn, cleanUp = chainntnfs.NewBitcoindBackend(
bitcoindConn = chainntnfs.NewBitcoindBackend(
t, p2pAddr, true, true,
)
newNotifier = func() (chainntnfs.TestChainNotifier, error) {
@@ -1889,9 +1887,7 @@ func TestInterfaces(t *testing.T, targetBackEnd string) {
case "neutrino":
var spvNode *neutrino.ChainService
spvNode, cleanUp = chainntnfs.NewNeutrinoBackend(
t, p2pAddr,
)
spvNode = chainntnfs.NewNeutrinoBackend(t, p2pAddr)
newNotifier = func() (chainntnfs.TestChainNotifier, error) {
return neutrinonotify.New(
spvNode, hintCache, hintCache,
@@ -1964,9 +1960,5 @@ func TestInterfaces(t *testing.T, targetBackEnd string) {
break
}
}
if cleanUp != nil {
cleanUp()
}
}
}

View File

@@ -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
}