chainntnfs: replace defer cleanup with t.Cleanup

Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
This commit is contained in:
Eng Zer Jun committed 2022-10-13 17:01:35 +08:00
1 parent b796b747ed
commit cac8f54a45
4 files changed
+38 -48

No files matched your search

+8 -13
View File
@@ -65,6 +65,9 @@ func setUpNotifier(t *testing.T, bitcoindConn *chain.BitcoindConn,
if err := notifier.Start(); err != nil { if err := notifier.Start(); err != nil {
t.Fatalf("unable to start notifier: %v", err) t.Fatalf("unable to start notifier: %v", err)
} }
t.Cleanup(func() {
require.NoError(t, notifier.Stop())
})
return notifier return notifier
} }
@@ -107,15 +110,13 @@ func TestHistoricalConfDetailsTxIndex(t *testing.T) {
} }
func testHistoricalConfDetailsTxIndex(t *testing.T, rpcPolling bool) { func testHistoricalConfDetailsTxIndex(t *testing.T, rpcPolling bool) {
miner, tearDown := chainntnfs.NewMiner( miner := chainntnfs.NewMiner(
t, []string{"--txindex"}, true, 25, t, []string{"--txindex"}, true, 25,
) )
defer tearDown()
bitcoindConn, cleanUp := chainntnfs.NewBitcoindBackend( bitcoindConn := chainntnfs.NewBitcoindBackend(
t, miner.P2PAddress(), true, rpcPolling, t, miner.P2PAddress(), true, rpcPolling,
) )
defer cleanUp()
hintCache := initHintCache(t) hintCache := initHintCache(t)
blockCache := blockcache.NewBlockCache(10000) blockCache := blockcache.NewBlockCache(10000)
@@ -123,7 +124,6 @@ func testHistoricalConfDetailsTxIndex(t *testing.T, rpcPolling bool) {
notifier := setUpNotifier( notifier := setUpNotifier(
t, bitcoindConn, hintCache, hintCache, blockCache, t, bitcoindConn, hintCache, hintCache, blockCache,
) )
defer notifier.Stop()
syncNotifierWithMiner(t, notifier, miner) syncNotifierWithMiner(t, notifier, miner)
@@ -198,21 +198,16 @@ func TestHistoricalConfDetailsNoTxIndex(t *testing.T) {
} }
func testHistoricalConfDetailsNoTxIndex(t *testing.T, rpcpolling bool) { func testHistoricalConfDetailsNoTxIndex(t *testing.T, rpcpolling bool) {
miner, tearDown := chainntnfs.NewMiner(t, nil, true, 25) miner := chainntnfs.NewMiner(t, nil, true, 25)
defer tearDown()
bitcoindConn, cleanUp := chainntnfs.NewBitcoindBackend( bitcoindConn := chainntnfs.NewBitcoindBackend(
t, miner.P2PAddress(), false, rpcpolling, t, miner.P2PAddress(), false, rpcpolling,
) )
defer cleanUp()
hintCache := initHintCache(t) hintCache := initHintCache(t)
blockCache := blockcache.NewBlockCache(10000) blockCache := blockcache.NewBlockCache(10000)
notifier := setUpNotifier( notifier := setUpNotifier(t, bitcoindConn, hintCache, hintCache, blockCache)
t, bitcoindConn, hintCache, hintCache, blockCache,
)
defer notifier.Stop()
// Since the node has its txindex disabled, we fall back to scanning the // Since the node has its txindex disabled, we fall back to scanning the
// chain manually. A transaction unknown to the network should not be // chain manually. A transaction unknown to the network should not be
+5 -6
View File
@@ -61,6 +61,9 @@ func setUpNotifier(t *testing.T, h *rpctest.Harness) *BtcdNotifier {
if err := notifier.Start(); err != nil { if err := notifier.Start(); err != nil {
t.Fatalf("unable to start notifier: %v", err) t.Fatalf("unable to start notifier: %v", err)
} }
t.Cleanup(func() {
require.NoError(t, notifier.Stop())
})
return notifier return notifier
} }
@@ -70,13 +73,11 @@ func setUpNotifier(t *testing.T, h *rpctest.Harness) *BtcdNotifier {
func TestHistoricalConfDetailsTxIndex(t *testing.T) { func TestHistoricalConfDetailsTxIndex(t *testing.T) {
t.Parallel() t.Parallel()
harness, tearDown := chainntnfs.NewMiner( harness := chainntnfs.NewMiner(
t, []string{"--txindex"}, true, 25, t, []string{"--txindex"}, true, 25,
) )
defer tearDown()
notifier := setUpNotifier(t, harness) notifier := setUpNotifier(t, harness)
defer notifier.Stop()
// A transaction unknown to the node should not be found within the // 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 // 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) { func TestHistoricalConfDetailsNoTxIndex(t *testing.T) {
t.Parallel() t.Parallel()
harness, tearDown := chainntnfs.NewMiner(t, nil, true, 25) harness := chainntnfs.NewMiner(t, nil, true, 25)
defer tearDown()
notifier := setUpNotifier(t, harness) notifier := setUpNotifier(t, harness)
defer notifier.Stop()
// Since the node has its txindex disabled, we fall back to scanning the // Since the node has its txindex disabled, we fall back to scanning the
// chain manually. A transaction unknown to the network should not be // chain manually. A transaction unknown to the network should not be
+4 -12
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 // 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 // this node with a chain length of 125, so we have plenty of BTC to
// play around with. // play around with.
miner, tearDown := chainntnfs.NewMiner(t, nil, true, 25) miner := chainntnfs.NewMiner(t, nil, true, 25)
defer tearDown()
rpcConfig := miner.RPCConfig() rpcConfig := miner.RPCConfig()
p2pAddr := miner.P2PAddress() p2pAddr := miner.P2PAddress()
@@ -1850,14 +1849,13 @@ func TestInterfaces(t *testing.T, targetBackEnd string) {
blockCache := blockcache.NewBlockCache(10000) blockCache := blockcache.NewBlockCache(10000)
var ( var (
cleanUp func()
newNotifier func() (chainntnfs.TestChainNotifier, error) newNotifier func() (chainntnfs.TestChainNotifier, error)
) )
switch notifierType { switch notifierType {
case "bitcoind": case "bitcoind":
var bitcoindConn *chain.BitcoindConn var bitcoindConn *chain.BitcoindConn
bitcoindConn, cleanUp = chainntnfs.NewBitcoindBackend( bitcoindConn = chainntnfs.NewBitcoindBackend(
t, p2pAddr, true, false, t, p2pAddr, true, false,
) )
newNotifier = func() (chainntnfs.TestChainNotifier, error) { newNotifier = func() (chainntnfs.TestChainNotifier, error) {
@@ -1869,7 +1867,7 @@ func TestInterfaces(t *testing.T, targetBackEnd string) {
case "bitcoind-rpc-polling": case "bitcoind-rpc-polling":
var bitcoindConn *chain.BitcoindConn var bitcoindConn *chain.BitcoindConn
bitcoindConn, cleanUp = chainntnfs.NewBitcoindBackend( bitcoindConn = chainntnfs.NewBitcoindBackend(
t, p2pAddr, true, true, t, p2pAddr, true, true,
) )
newNotifier = func() (chainntnfs.TestChainNotifier, error) { newNotifier = func() (chainntnfs.TestChainNotifier, error) {
@@ -1889,9 +1887,7 @@ func TestInterfaces(t *testing.T, targetBackEnd string) {
case "neutrino": case "neutrino":
var spvNode *neutrino.ChainService var spvNode *neutrino.ChainService
spvNode, cleanUp = chainntnfs.NewNeutrinoBackend( spvNode = chainntnfs.NewNeutrinoBackend(t, p2pAddr)
t, p2pAddr,
)
newNotifier = func() (chainntnfs.TestChainNotifier, error) { newNotifier = func() (chainntnfs.TestChainNotifier, error) {
return neutrinonotify.New( return neutrinonotify.New(
spvNode, hintCache, hintCache, spvNode, hintCache, hintCache,
@@ -1964,9 +1960,5 @@ func TestInterfaces(t *testing.T, targetBackEnd string) {
break break
} }
} }
if cleanUp != nil {
cleanUp()
}
} }
} }
+21 -17
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 // NewMiner spawns testing harness backed by a btcd node that can serve as a
// miner. // miner.
func NewMiner(t *testing.T, extraArgs []string, createChain bool, func NewMiner(t *testing.T, extraArgs []string, createChain bool,
spendableOutputs uint32) (*rpctest.Harness, func()) { spendableOutputs uint32) *rpctest.Harness {
t.Helper() t.Helper()
@@ -175,12 +175,15 @@ func NewMiner(t *testing.T, extraArgs []string, createChain bool,
node, err := rpctest.New(NetParams, nil, extraArgs, "") node, err := rpctest.New(NetParams, nil, extraArgs, "")
require.NoError(t, err, "unable to create backend node") 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 { if err := node.SetUp(createChain, spendableOutputs); err != nil {
node.TearDown()
t.Fatalf("unable to set up backend node: %v", err) 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 // 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. // used for block and tx notifications or if its ZMQ interface should be used.
// A connection to the newly spawned bitcoind node is returned. // A connection to the newly spawned bitcoind node is returned.
func NewBitcoindBackend(t *testing.T, minerAddr string, txindex, func NewBitcoindBackend(t *testing.T, minerAddr string, txindex,
rpcpolling bool) (*chain.BitcoindConn, func()) { rpcpolling bool) *chain.BitcoindConn {
t.Helper() t.Helper()
@@ -219,6 +222,10 @@ func NewBitcoindBackend(t *testing.T, minerAddr string, txindex,
if err := bitcoind.Start(); err != nil { if err := bitcoind.Start(); err != nil {
t.Fatalf("unable to start bitcoind: %v", err) t.Fatalf("unable to start bitcoind: %v", err)
} }
t.Cleanup(func() {
_ = bitcoind.Process.Kill()
_ = bitcoind.Wait()
})
// Wait for the bitcoind instance to start up. // Wait for the bitcoind instance to start up.
host := fmt.Sprintf("127.0.0.1:%d", rpcPort) host := fmt.Sprintf("127.0.0.1:%d", rpcPort)
@@ -257,21 +264,16 @@ func NewBitcoindBackend(t *testing.T, minerAddr string, txindex,
return conn.Start() return conn.Start()
}, 10*time.Second) }, 10*time.Second)
if err != nil { if err != nil {
bitcoind.Process.Kill()
bitcoind.Wait()
t.Fatalf("unable to establish connection to bitcoind: %v", err) t.Fatalf("unable to establish connection to bitcoind: %v", err)
} }
t.Cleanup(conn.Stop)
return conn, func() { return conn
conn.Stop()
bitcoind.Process.Kill()
bitcoind.Wait()
}
} }
// NewNeutrinoBackend spawns a new neutrino node that connects to a miner at // NewNeutrinoBackend spawns a new neutrino node that connects to a miner at
// the specified address. // the specified address.
func NewNeutrinoBackend(t *testing.T, minerAddr string) (*neutrino.ChainService, func()) { func NewNeutrinoBackend(t *testing.T, minerAddr string) *neutrino.ChainService {
t.Helper() t.Helper()
spvDir := t.TempDir() spvDir := t.TempDir()
@@ -283,6 +285,9 @@ func NewNeutrinoBackend(t *testing.T, minerAddr string) (*neutrino.ChainService,
if err != nil { if err != nil {
t.Fatalf("unable to create walletdb: %v", err) t.Fatalf("unable to create walletdb: %v", err)
} }
t.Cleanup(func() {
spvDatabase.Close()
})
// Create an instance of neutrino connected to the running btcd // Create an instance of neutrino connected to the running btcd
// instance. // instance.
@@ -294,7 +299,6 @@ func NewNeutrinoBackend(t *testing.T, minerAddr string) (*neutrino.ChainService,
} }
spvNode, err := neutrino.NewChainService(spvConfig) spvNode, err := neutrino.NewChainService(spvConfig)
if err != nil { if err != nil {
spvDatabase.Close()
t.Fatalf("unable to create neutrino: %v", err) t.Fatalf("unable to create neutrino: %v", err)
} }
@@ -304,9 +308,9 @@ func NewNeutrinoBackend(t *testing.T, minerAddr string) (*neutrino.ChainService,
for !spvNode.IsCurrent() { for !spvNode.IsCurrent() {
time.Sleep(time.Millisecond * 100) time.Sleep(time.Millisecond * 100)
} }
t.Cleanup(func() {
return spvNode, func() {
spvNode.Stop() spvNode.Stop()
spvDatabase.Close() })
}
return spvNode
} }