chainntnfs: use T.TempDir to create temporary test directory

Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
This commit is contained in:
Eng Zer Jun 2022-08-15 21:05:01 +08:00
parent 994f4d67a1
commit 712177ee03
No known key found for this signature in database
GPG key ID: DAEBBD2E34C111E6
5 changed files with 20 additions and 30 deletions

View file

@ -5,7 +5,6 @@ package bitcoindnotify
import (
"bytes"
"io/ioutil"
"testing"
"time"
@ -35,10 +34,12 @@ var (
func initHintCache(t *testing.T) *chainntnfs.HeightHintCache {
t.Helper()
tempDir, err := ioutil.TempDir("", "kek")
require.NoError(t, err, "unable to create temp dir")
db, err := channeldb.Open(tempDir)
db, err := channeldb.Open(t.TempDir())
require.NoError(t, err, "unable to create db")
t.Cleanup(func() {
require.NoError(t, db.Close())
})
testCfg := chainntnfs.CacheConfig{
QueryDisable: false,
}

View file

@ -5,7 +5,6 @@ package btcdnotify
import (
"bytes"
"io/ioutil"
"testing"
"github.com/btcsuite/btcd/chaincfg/chainhash"
@ -33,10 +32,12 @@ var (
func initHintCache(t *testing.T) *chainntnfs.HeightHintCache {
t.Helper()
tempDir, err := ioutil.TempDir("", "kek")
require.NoError(t, err, "unable to create temp dir")
db, err := channeldb.Open(tempDir)
db, err := channeldb.Open(t.TempDir())
require.NoError(t, err, "unable to create db")
t.Cleanup(func() {
require.NoError(t, db.Close())
})
testCfg := chainntnfs.CacheConfig{
QueryDisable: false,
}

View file

@ -2,7 +2,6 @@ package chainntnfs
import (
"bytes"
"io/ioutil"
"testing"
"github.com/btcsuite/btcd/chaincfg/chainhash"
@ -24,13 +23,15 @@ func initHintCache(t *testing.T) *HeightHintCache {
func initHintCacheWithConfig(t *testing.T, cfg CacheConfig) *HeightHintCache {
t.Helper()
tempDir, err := ioutil.TempDir("", "kek")
require.NoError(t, err, "unable to create temp dir")
db, err := channeldb.Open(tempDir)
db, err := channeldb.Open(t.TempDir())
require.NoError(t, err, "unable to create db")
hintCache, err := NewHeightHintCache(cfg, db.Backend)
require.NoError(t, err, "unable to create hint cache")
t.Cleanup(func() {
require.NoError(t, db.Close())
})
return hintCache
}

View file

@ -6,7 +6,6 @@ package chainntnfstest
import (
"bytes"
"fmt"
"io/ioutil"
"log"
"sync"
"testing"
@ -1833,10 +1832,7 @@ func TestInterfaces(t *testing.T, targetBackEnd string) {
}
// Initialize a height hint cache for each notifier.
tempDir, err := ioutil.TempDir("", "channeldb")
if err != nil {
t.Fatalf("unable to create temp dir: %v", err)
}
tempDir := t.TempDir()
db, err := channeldb.Open(tempDir)
if err != nil {
t.Fatalf("unable to create db: %v", err)

View file

@ -6,9 +6,7 @@ package chainntnfs
import (
"errors"
"fmt"
"io/ioutil"
"math/rand"
"os"
"os/exec"
"path/filepath"
"testing"
@ -196,8 +194,7 @@ func NewBitcoindBackend(t *testing.T, minerAddr string, txindex,
t.Helper()
tempBitcoindDir, err := ioutil.TempDir("", "bitcoind")
require.NoError(t, err, "unable to create temp dir")
tempBitcoindDir := t.TempDir()
rpcPort := rand.Intn(65536-1024) + 1024
zmqBlockHost := "ipc:///" + tempBitcoindDir + "/blocks.socket"
@ -220,7 +217,6 @@ func NewBitcoindBackend(t *testing.T, minerAddr string, txindex,
bitcoind := exec.Command("bitcoind", args...)
if err := bitcoind.Start(); err != nil {
os.RemoveAll(tempBitcoindDir)
t.Fatalf("unable to start bitcoind: %v", err)
}
@ -251,7 +247,8 @@ func NewBitcoindBackend(t *testing.T, minerAddr string, txindex,
}
var conn *chain.BitcoindConn
err = wait.NoError(func() error {
err := wait.NoError(func() error {
var err error
conn, err = chain.NewBitcoindConn(cfg)
if err != nil {
return err
@ -262,7 +259,6 @@ func NewBitcoindBackend(t *testing.T, minerAddr string, txindex,
if err != nil {
bitcoind.Process.Kill()
bitcoind.Wait()
os.RemoveAll(tempBitcoindDir)
t.Fatalf("unable to establish connection to bitcoind: %v", err)
}
@ -270,7 +266,6 @@ func NewBitcoindBackend(t *testing.T, minerAddr string, txindex,
conn.Stop()
bitcoind.Process.Kill()
bitcoind.Wait()
os.RemoveAll(tempBitcoindDir)
}
}
@ -279,15 +274,13 @@ func NewBitcoindBackend(t *testing.T, minerAddr string, txindex,
func NewNeutrinoBackend(t *testing.T, minerAddr string) (*neutrino.ChainService, func()) {
t.Helper()
spvDir, err := ioutil.TempDir("", "neutrino")
require.NoError(t, err, "unable to create temp dir")
spvDir := t.TempDir()
dbName := filepath.Join(spvDir, "neutrino.db")
spvDatabase, err := walletdb.Create(
"bdb", dbName, true, kvdb.DefaultDBTimeout,
)
if err != nil {
os.RemoveAll(spvDir)
t.Fatalf("unable to create walletdb: %v", err)
}
@ -301,7 +294,6 @@ func NewNeutrinoBackend(t *testing.T, minerAddr string) (*neutrino.ChainService,
}
spvNode, err := neutrino.NewChainService(spvConfig)
if err != nil {
os.RemoveAll(spvDir)
spvDatabase.Close()
t.Fatalf("unable to create neutrino: %v", err)
}
@ -316,6 +308,5 @@ func NewNeutrinoBackend(t *testing.T, minerAddr string) (*neutrino.ChainService,
return spvNode, func() {
spvNode.Stop()
spvDatabase.Close()
os.RemoveAll(spvDir)
}
}