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