htlcswitch: 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 committed 2022-08-24 09:03:01 +08:00
1 parent 4f1e4ff87a
commit 9294b53fcc
5 files changed
+43 -78

No files matched your search

+1 -6
View File
@@ -3,7 +3,6 @@ package htlcswitch_test
import (
"bytes"
"fmt"
"io/ioutil"
"reflect"
"testing"
@@ -624,11 +623,7 @@ func equalIgnoreLFD(c, c2 *htlcswitch.PaymentCircuit) bool {
// will be created.
func makeCircuitDB(t *testing.T, path string) *channeldb.DB {
if path == "" {
var err error
path, err = ioutil.TempDir("", "circuitdb")
if err != nil {
t.Fatalf("unable to create temp path: %v", err)
}
path = t.TempDir()
}
db, err := channeldb.Open(path)
+29 -31
View File
@@ -3,8 +3,6 @@ package htlcswitch
import (
"crypto/rand"
"fmt"
"io/ioutil"
"os"
"testing"
"time"
@@ -19,15 +17,6 @@ const (
cltv uint32 = 100000
)
// tempDecayedLogPath creates a new temporary database path to back a single
// decayed log instance.
func tempDecayedLogPath(t *testing.T) string {
dir, err := ioutil.TempDir("", "decayedlog")
require.NoError(t, err, "unable to create temporary decayed log dir")
return dir
}
// startup sets up the DecayedLog and possibly the garbage collector.
func startup(dbPath string, notifier bool) (sphinx.ReplayLog,
*mock.ChainNotifier, *sphinx.HashPrefix, func(), error) {
@@ -82,24 +71,19 @@ func startup(dbPath string, notifier bool) (sphinx.ReplayLog,
return log, chainNotifier, &hashedSecret, stop, nil
}
// shutdown deletes the temporary directory that the test database uses
// and handles closing the database.
func shutdown(dir string, d sphinx.ReplayLog) {
d.Stop()
os.RemoveAll(dir)
}
// TestDecayedLogGarbageCollector tests the ability of the garbage collector
// to delete expired cltv values every time a block is received. Expired cltv
// values are cltv values that are < current block height.
func TestDecayedLogGarbageCollector(t *testing.T) {
t.Parallel()
dbPath := tempDecayedLogPath(t)
dbPath := t.TempDir()
d, notifier, hashedSecret, _, err := startup(dbPath, true)
require.NoError(t, err, "Unable to start up DecayedLog")
defer shutdown(dbPath, d)
t.Cleanup(func() {
require.NoError(t, d.Stop())
})
// Store <hashedSecret, cltv> in the sharedHashBucket.
err = d.Put(hashedSecret, cltv)
@@ -150,11 +134,13 @@ func TestDecayedLogGarbageCollector(t *testing.T) {
func TestDecayedLogPersistentGarbageCollector(t *testing.T) {
t.Parallel()
dbPath := tempDecayedLogPath(t)
dbPath := t.TempDir()
d, _, hashedSecret, stop, err := startup(dbPath, true)
require.NoError(t, err, "Unable to start up DecayedLog")
defer shutdown(dbPath, d)
t.Cleanup(func() {
require.NoError(t, d.Stop())
})
// Store <hashedSecret, cltv> in the sharedHashBucket
if err = d.Put(hashedSecret, cltv); err != nil {
@@ -172,7 +158,9 @@ func TestDecayedLogPersistentGarbageCollector(t *testing.T) {
d2, notifier2, _, _, err := startup(dbPath, true)
require.NoError(t, err, "Unable to restart DecayedLog")
defer shutdown(dbPath, d2)
t.Cleanup(func() {
require.NoError(t, d2.Stop())
})
// Check that the hash prefix still exists in the new db instance.
_, err = d2.Get(hashedSecret)
@@ -202,11 +190,13 @@ func TestDecayedLogPersistentGarbageCollector(t *testing.T) {
func TestDecayedLogInsertionAndDeletion(t *testing.T) {
t.Parallel()
dbPath := tempDecayedLogPath(t)
dbPath := t.TempDir()
d, _, hashedSecret, _, err := startup(dbPath, false)
require.NoError(t, err, "Unable to start up DecayedLog")
defer shutdown(dbPath, d)
t.Cleanup(func() {
require.NoError(t, d.Stop())
})
// Store <hashedSecret, cltv> in the sharedHashBucket.
err = d.Put(hashedSecret, cltv)
@@ -234,11 +224,13 @@ func TestDecayedLogInsertionAndDeletion(t *testing.T) {
func TestDecayedLogStartAndStop(t *testing.T) {
t.Parallel()
dbPath := tempDecayedLogPath(t)
dbPath := t.TempDir()
d, _, hashedSecret, stop, err := startup(dbPath, false)
require.NoError(t, err, "Unable to start up DecayedLog")
defer shutdown(dbPath, d)
t.Cleanup(func() {
require.NoError(t, d.Stop())
})
// Store <hashedSecret, cltv> in the sharedHashBucket.
err = d.Put(hashedSecret, cltv)
@@ -249,7 +241,9 @@ func TestDecayedLogStartAndStop(t *testing.T) {
d2, _, hashedSecret2, stop, err := startup(dbPath, false)
require.NoError(t, err, "Unable to restart DecayedLog")
defer shutdown(dbPath, d2)
t.Cleanup(func() {
require.NoError(t, d2.Stop())
})
// Retrieve the stored cltv value given the hashedSecret key.
value, err := d2.Get(hashedSecret)
@@ -270,7 +264,9 @@ func TestDecayedLogStartAndStop(t *testing.T) {
d3, _, hashedSecret3, _, err := startup(dbPath, false)
require.NoError(t, err, "Unable to restart DecayedLog")
defer shutdown(dbPath, d3)
t.Cleanup(func() {
require.NoError(t, d3.Stop())
})
// Assert that hashedSecret is not in the sharedHashBucket
_, err = d3.Get(hashedSecret3)
@@ -288,11 +284,13 @@ func TestDecayedLogStartAndStop(t *testing.T) {
func TestDecayedLogStorageAndRetrieval(t *testing.T) {
t.Parallel()
dbPath := tempDecayedLogPath(t)
dbPath := t.TempDir()
d, _, hashedSecret, _, err := startup(dbPath, false)
require.NoError(t, err, "Unable to start up DecayedLog")
defer shutdown(dbPath, d)
t.Cleanup(func() {
require.NoError(t, d.Stop())
})
// Store <hashedSecret, cltv> in the sharedHashBucket
err = d.Put(hashedSecret, cltv)
+1 -7
View File
@@ -2,7 +2,6 @@ package htlcswitch
import (
"bytes"
"io/ioutil"
"math/rand"
"reflect"
"testing"
@@ -103,12 +102,7 @@ func TestNetworkResultStore(t *testing.T) {
const numResults = 4
tempDir, err := ioutil.TempDir("", "testdb")
if err != nil {
t.Fatal(err)
}
db, err := channeldb.Open(tempDir)
db, err := channeldb.Open(t.TempDir())
if err != nil {
t.Fatal(err)
}
+3 -11
View File
@@ -1,8 +1,6 @@
package htlcswitch
import (
"io/ioutil"
"os"
"path/filepath"
"testing"
@@ -40,20 +38,14 @@ func TestInsertAndDelete(t *testing.T) {
// Create the backend database and use it to create the resolution
// store.
dbDir, err := ioutil.TempDir("", "resolutionStore")
require.NoError(t, err)
dbPath := filepath.Join(dbDir, "testdb")
dbPath := filepath.Join(t.TempDir(), "testdb")
db, err := kvdb.Create(
kvdb.BoltBackendName, dbPath, true, kvdb.DefaultDBTimeout,
)
require.NoError(t, err)
cleanUp := func() {
t.Cleanup(func() {
db.Close()
os.RemoveAll(dbDir)
}
defer cleanUp()
})
resStore := newResolutionStore(db)
+9 -23
View File
@@ -6,9 +6,7 @@ import (
"crypto/sha256"
"fmt"
"io"
"io/ioutil"
mrand "math/rand"
"os"
"reflect"
"testing"
"time"
@@ -997,8 +995,7 @@ func TestSwitchForwardFailAfterFullAdd(t *testing.T) {
)
require.NoError(t, err, "unable to create bob server")
tempPath, err := ioutil.TempDir("", "circuitdb")
require.NoError(t, err, "unable to temporary path")
tempPath := t.TempDir()
cdb, err := channeldb.Open(tempPath)
require.NoError(t, err, "unable to open channeldb")
@@ -1186,8 +1183,7 @@ func TestSwitchForwardSettleAfterFullAdd(t *testing.T) {
)
require.NoError(t, err, "unable to create bob server")
tempPath, err := ioutil.TempDir("", "circuitdb")
require.NoError(t, err, "unable to temporary path")
tempPath := t.TempDir()
cdb, err := channeldb.Open(tempPath)
require.NoError(t, err, "unable to open channeldb")
@@ -1378,8 +1374,7 @@ func TestSwitchForwardDropAfterFullAdd(t *testing.T) {
)
require.NoError(t, err, "unable to create bob server")
tempPath, err := ioutil.TempDir("", "circuitdb")
require.NoError(t, err, "unable to temporary path")
tempPath := t.TempDir()
cdb, err := channeldb.Open(tempPath)
require.NoError(t, err, "unable to open channeldb")
@@ -1533,8 +1528,7 @@ func TestSwitchForwardFailAfterHalfAdd(t *testing.T) {
)
require.NoError(t, err, "unable to create bob server")
tempPath, err := ioutil.TempDir("", "circuitdb")
require.NoError(t, err, "unable to temporary path")
tempPath := t.TempDir()
cdb, err := channeldb.Open(tempPath)
require.NoError(t, err, "unable to open channeldb")
@@ -1689,8 +1683,7 @@ func TestSwitchForwardCircuitPersistence(t *testing.T) {
)
require.NoError(t, err, "unable to create bob server")
tempPath, err := ioutil.TempDir("", "circuitdb")
require.NoError(t, err, "unable to temporary path")
tempPath := t.TempDir()
cdb, err := channeldb.Open(tempPath)
require.NoError(t, err, "unable to open channeldb")
@@ -3771,8 +3764,7 @@ func TestSwitchHoldForward(t *testing.T) {
)
require.NoError(t, err, "unable to create bob server")
tempPath, err := ioutil.TempDir("", "circuitdb")
require.NoError(t, err, "unable to temporary path")
tempPath := t.TempDir()
cdb, err := channeldb.Open(tempPath)
require.NoError(t, err, "unable to open channeldb")
@@ -4706,8 +4698,7 @@ func testSwitchForwardFailAlias(t *testing.T, zeroConf bool) {
)
require.NoError(t, err)
tempPath, err := ioutil.TempDir("", "circuitdb")
require.NoError(t, err)
tempPath := t.TempDir()
cdb, err := channeldb.Open(tempPath)
require.NoError(t, err)
@@ -4795,7 +4786,6 @@ func testSwitchForwardFailAlias(t *testing.T, zeroConf bool) {
defer func() {
_ = s2.Stop()
_ = os.RemoveAll(tempPath)
}()
var aliceLink2 *mockChannelLink
@@ -4922,8 +4912,7 @@ func testSwitchAliasFailAdd(t *testing.T, zeroConf, private, useAlias bool) {
)
require.NoError(t, err)
tempPath, err := ioutil.TempDir("", "circuitdb")
require.NoError(t, err)
tempPath := t.TempDir()
cdb, err := channeldb.Open(tempPath)
require.NoError(t, err)
@@ -4939,7 +4928,6 @@ func testSwitchAliasFailAdd(t *testing.T, zeroConf, private, useAlias bool) {
defer func() {
_ = s.Stop()
_ = os.RemoveAll(tempPath)
}()
// Make Alice's channel zero-conf or option-scid-alias (feature bit).
@@ -5263,8 +5251,7 @@ func testSwitchAliasInterceptFail(t *testing.T, zeroConf bool) {
)
require.NoError(t, err)
tempPath, err := ioutil.TempDir("", "circuitdb")
require.NoError(t, err)
tempPath := t.TempDir()
cdb, err := channeldb.Open(tempPath)
require.NoError(t, err)
@@ -5277,7 +5264,6 @@ func testSwitchAliasInterceptFail(t *testing.T, zeroConf bool) {
defer func() {
_ = s.Stop()
_ = os.RemoveAll(tempPath)
}()
// Make Alice's alias here.