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

View file

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

View file

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

View file

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

View file

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