channeldb: use T.TempDir to create temporary test directory
Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
This commit is contained in:
9 files changed
+79
-166
No files matched your search
@@ -1,11 +1,9 @@
|
|||||||
package channeldb
|
package channeldb
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io/ioutil"
|
|
||||||
"math"
|
"math"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"net"
|
"net"
|
||||||
"os"
|
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"reflect"
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
@@ -32,9 +30,7 @@ func TestOpenWithCreate(t *testing.T) {
|
|||||||
|
|
||||||
// First, create a temporary directory to be used for the duration of
|
// First, create a temporary directory to be used for the duration of
|
||||||
// this test.
|
// this test.
|
||||||
tempDirName, err := ioutil.TempDir("", "channeldb")
|
tempDirName := t.TempDir()
|
||||||
require.NoError(t, err, "unable to create temp dir")
|
|
||||||
defer os.RemoveAll(tempDirName)
|
|
||||||
|
|
||||||
// Next, open thereby creating channeldb for the first time.
|
// Next, open thereby creating channeldb for the first time.
|
||||||
dbPath := filepath.Join(tempDirName, "cdb")
|
dbPath := filepath.Join(tempDirName, "cdb")
|
||||||
@@ -70,9 +66,7 @@ func TestWipe(t *testing.T) {
|
|||||||
|
|
||||||
// First, create a temporary directory to be used for the duration of
|
// First, create a temporary directory to be used for the duration of
|
||||||
// this test.
|
// this test.
|
||||||
tempDirName, err := ioutil.TempDir("", "channeldb")
|
tempDirName := t.TempDir()
|
||||||
require.NoError(t, err, "unable to create temp dir")
|
|
||||||
defer os.RemoveAll(tempDirName)
|
|
||||||
|
|
||||||
// Next, open thereby creating channeldb for the first time.
|
// Next, open thereby creating channeldb for the first time.
|
||||||
dbPath := filepath.Join(tempDirName, "cdb")
|
dbPath := filepath.Join(tempDirName, "cdb")
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ package channeldb_test
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"io/ioutil"
|
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"runtime"
|
"runtime"
|
||||||
"testing"
|
"testing"
|
||||||
@@ -843,13 +842,7 @@ func loadFwdPkgs(t *testing.T, db kvdb.Backend,
|
|||||||
// provided path is an empty, it will create a temp dir/file to use.
|
// provided path is an empty, it will create a temp dir/file to use.
|
||||||
func makeFwdPkgDB(t *testing.T, path string) kvdb.Backend { // nolint:unparam
|
func makeFwdPkgDB(t *testing.T, path string) kvdb.Backend { // nolint:unparam
|
||||||
if path == "" {
|
if path == "" {
|
||||||
var err error
|
path = filepath.Join(t.TempDir(), "fwdpkg.db")
|
||||||
path, err = ioutil.TempDir("", "fwdpkgdb")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("unable to create temp path: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
path = filepath.Join(path, "fwdpkg.db")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bdb, err := kvdb.Create(
|
bdb, err := kvdb.Create(
|
||||||
|
|||||||
+42
-90
@@ -7,11 +7,9 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"image/color"
|
"image/color"
|
||||||
"io/ioutil"
|
|
||||||
"math"
|
"math"
|
||||||
prand "math/rand"
|
prand "math/rand"
|
||||||
"net"
|
"net"
|
||||||
"os"
|
|
||||||
"reflect"
|
"reflect"
|
||||||
"runtime"
|
"runtime"
|
||||||
"sync"
|
"sync"
|
||||||
@@ -53,27 +51,18 @@ var (
|
|||||||
testPub = route.Vertex{2, 202, 4}
|
testPub = route.Vertex{2, 202, 4}
|
||||||
)
|
)
|
||||||
|
|
||||||
// MakeTestGraph creates a new instance of the ChannelGraph for testing
|
// MakeTestGraph creates a new instance of the ChannelGraph for testing purposes.
|
||||||
// purposes. A callback which cleans up the created temporary directories is
|
func MakeTestGraph(t testing.TB, modifiers ...OptionModifier) (*ChannelGraph, error) {
|
||||||
// also returned and intended to be executed after the test completes.
|
|
||||||
func MakeTestGraph(modifiers ...OptionModifier) (*ChannelGraph, func(), error) {
|
|
||||||
// First, create a temporary directory to be used for the duration of
|
|
||||||
// this test.
|
|
||||||
tempDirName, err := ioutil.TempDir("", "channelgraph")
|
|
||||||
if err != nil {
|
|
||||||
return nil, nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
opts := DefaultOptions()
|
opts := DefaultOptions()
|
||||||
for _, modifier := range modifiers {
|
for _, modifier := range modifiers {
|
||||||
modifier(&opts)
|
modifier(&opts)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Next, create channelgraph for the first time.
|
// Next, create channelgraph for the first time.
|
||||||
backend, backendCleanup, err := kvdb.GetTestBackend(tempDirName, "cgr")
|
backend, backendCleanup, err := kvdb.GetTestBackend(t.TempDir(), "cgr")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
backendCleanup()
|
backendCleanup()
|
||||||
return nil, nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
graph, err := NewChannelGraph(
|
graph, err := NewChannelGraph(
|
||||||
@@ -83,17 +72,15 @@ func MakeTestGraph(modifiers ...OptionModifier) (*ChannelGraph, func(), error) {
|
|||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
backendCleanup()
|
backendCleanup()
|
||||||
_ = os.RemoveAll(tempDirName)
|
return nil, err
|
||||||
return nil, nil, err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
cleanUp := func() {
|
t.Cleanup(func() {
|
||||||
_ = backend.Close()
|
_ = backend.Close()
|
||||||
backendCleanup()
|
backendCleanup()
|
||||||
_ = os.RemoveAll(tempDirName)
|
})
|
||||||
}
|
|
||||||
|
|
||||||
return graph, cleanUp, nil
|
return graph, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func createLightningNode(db kvdb.Backend, priv *btcec.PrivateKey) (*LightningNode, error) {
|
func createLightningNode(db kvdb.Backend, priv *btcec.PrivateKey) (*LightningNode, error) {
|
||||||
@@ -127,8 +114,7 @@ func createTestVertex(db kvdb.Backend) (*LightningNode, error) {
|
|||||||
func TestNodeInsertionAndDeletion(t *testing.T) {
|
func TestNodeInsertionAndDeletion(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
graph, cleanUp, err := MakeTestGraph()
|
graph, err := MakeTestGraph(t)
|
||||||
defer cleanUp()
|
|
||||||
require.NoError(t, err, "unable to make test database")
|
require.NoError(t, err, "unable to make test database")
|
||||||
|
|
||||||
// We'd like to test basic insertion/deletion for vertexes from the
|
// We'd like to test basic insertion/deletion for vertexes from the
|
||||||
@@ -189,8 +175,7 @@ func TestNodeInsertionAndDeletion(t *testing.T) {
|
|||||||
func TestPartialNode(t *testing.T) {
|
func TestPartialNode(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
graph, cleanUp, err := MakeTestGraph()
|
graph, err := MakeTestGraph(t)
|
||||||
defer cleanUp()
|
|
||||||
require.NoError(t, err, "unable to make test database")
|
require.NoError(t, err, "unable to make test database")
|
||||||
|
|
||||||
// We want to be able to insert nodes into the graph that only has the
|
// We want to be able to insert nodes into the graph that only has the
|
||||||
@@ -247,8 +232,7 @@ func TestPartialNode(t *testing.T) {
|
|||||||
func TestAliasLookup(t *testing.T) {
|
func TestAliasLookup(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
graph, cleanUp, err := MakeTestGraph()
|
graph, err := MakeTestGraph(t)
|
||||||
defer cleanUp()
|
|
||||||
require.NoError(t, err, "unable to make test database")
|
require.NoError(t, err, "unable to make test database")
|
||||||
|
|
||||||
// We'd like to test the alias index within the database, so first
|
// We'd like to test the alias index within the database, so first
|
||||||
@@ -287,9 +271,8 @@ func TestAliasLookup(t *testing.T) {
|
|||||||
func TestSourceNode(t *testing.T) {
|
func TestSourceNode(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
graph, cleanUp, err := MakeTestGraph()
|
graph, err := MakeTestGraph(t)
|
||||||
require.NoError(t, err, "unable to make test database")
|
require.NoError(t, err, "unable to make test database")
|
||||||
defer cleanUp()
|
|
||||||
|
|
||||||
// We'd like to test the setting/getting of the source node, so we
|
// We'd like to test the setting/getting of the source node, so we
|
||||||
// first create a fake node to use within the test.
|
// first create a fake node to use within the test.
|
||||||
@@ -320,8 +303,7 @@ func TestSourceNode(t *testing.T) {
|
|||||||
func TestEdgeInsertionDeletion(t *testing.T) {
|
func TestEdgeInsertionDeletion(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
graph, cleanUp, err := MakeTestGraph()
|
graph, err := MakeTestGraph(t)
|
||||||
defer cleanUp()
|
|
||||||
require.NoError(t, err, "unable to make test database")
|
require.NoError(t, err, "unable to make test database")
|
||||||
|
|
||||||
// We'd like to test the insertion/deletion of edges, so we create two
|
// We'd like to test the insertion/deletion of edges, so we create two
|
||||||
@@ -445,8 +427,7 @@ func createEdge(height, txIndex uint32, txPosition uint16, outPointIndex uint32,
|
|||||||
func TestDisconnectBlockAtHeight(t *testing.T) {
|
func TestDisconnectBlockAtHeight(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
graph, cleanUp, err := MakeTestGraph()
|
graph, err := MakeTestGraph(t)
|
||||||
defer cleanUp()
|
|
||||||
require.NoError(t, err, "unable to make test database")
|
require.NoError(t, err, "unable to make test database")
|
||||||
|
|
||||||
sourceNode, err := createTestVertex(graph.db)
|
sourceNode, err := createTestVertex(graph.db)
|
||||||
@@ -717,8 +698,7 @@ func createChannelEdge(db kvdb.Backend, node1, node2 *LightningNode) (*ChannelEd
|
|||||||
func TestEdgeInfoUpdates(t *testing.T) {
|
func TestEdgeInfoUpdates(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
graph, cleanUp, err := MakeTestGraph()
|
graph, err := MakeTestGraph(t)
|
||||||
defer cleanUp()
|
|
||||||
require.NoError(t, err, "unable to make test database")
|
require.NoError(t, err, "unable to make test database")
|
||||||
|
|
||||||
// We'd like to test the update of edges inserted into the database, so
|
// We'd like to test the update of edges inserted into the database, so
|
||||||
@@ -1022,8 +1002,7 @@ func newEdgePolicy(chanID uint64, db kvdb.Backend,
|
|||||||
func TestGraphTraversal(t *testing.T) {
|
func TestGraphTraversal(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
graph, cleanUp, err := MakeTestGraph()
|
graph, err := MakeTestGraph(t)
|
||||||
defer cleanUp()
|
|
||||||
require.NoError(t, err, "unable to make test database")
|
require.NoError(t, err, "unable to make test database")
|
||||||
|
|
||||||
// We'd like to test some of the graph traversal capabilities within
|
// We'd like to test some of the graph traversal capabilities within
|
||||||
@@ -1110,8 +1089,7 @@ func TestGraphTraversal(t *testing.T) {
|
|||||||
func TestGraphTraversalCacheable(t *testing.T) {
|
func TestGraphTraversalCacheable(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
graph, cleanUp, err := MakeTestGraph()
|
graph, err := MakeTestGraph(t)
|
||||||
defer cleanUp()
|
|
||||||
require.NoError(t, err, "unable to make test database")
|
require.NoError(t, err, "unable to make test database")
|
||||||
|
|
||||||
// We'd like to test some of the graph traversal capabilities within
|
// We'd like to test some of the graph traversal capabilities within
|
||||||
@@ -1174,8 +1152,7 @@ func TestGraphTraversalCacheable(t *testing.T) {
|
|||||||
func TestGraphCacheTraversal(t *testing.T) {
|
func TestGraphCacheTraversal(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
graph, cleanUp, err := MakeTestGraph()
|
graph, err := MakeTestGraph(t)
|
||||||
defer cleanUp()
|
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
// We'd like to test some of the graph traversal capabilities within
|
// We'd like to test some of the graph traversal capabilities within
|
||||||
@@ -1412,8 +1389,7 @@ func assertChanViewEqualChanPoints(t *testing.T, a []EdgePoint, b []*wire.OutPoi
|
|||||||
func TestGraphPruning(t *testing.T) {
|
func TestGraphPruning(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
graph, cleanUp, err := MakeTestGraph()
|
graph, err := MakeTestGraph(t)
|
||||||
defer cleanUp()
|
|
||||||
require.NoError(t, err, "unable to make test database")
|
require.NoError(t, err, "unable to make test database")
|
||||||
|
|
||||||
sourceNode, err := createTestVertex(graph.db)
|
sourceNode, err := createTestVertex(graph.db)
|
||||||
@@ -1603,8 +1579,7 @@ func TestGraphPruning(t *testing.T) {
|
|||||||
func TestHighestChanID(t *testing.T) {
|
func TestHighestChanID(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
graph, cleanUp, err := MakeTestGraph()
|
graph, err := MakeTestGraph(t)
|
||||||
defer cleanUp()
|
|
||||||
require.NoError(t, err, "unable to make test database")
|
require.NoError(t, err, "unable to make test database")
|
||||||
|
|
||||||
// If we don't yet have any channels in the database, then we should
|
// If we don't yet have any channels in the database, then we should
|
||||||
@@ -1666,8 +1641,7 @@ func TestHighestChanID(t *testing.T) {
|
|||||||
func TestChanUpdatesInHorizon(t *testing.T) {
|
func TestChanUpdatesInHorizon(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
graph, cleanUp, err := MakeTestGraph()
|
graph, err := MakeTestGraph(t)
|
||||||
defer cleanUp()
|
|
||||||
require.NoError(t, err, "unable to make test database")
|
require.NoError(t, err, "unable to make test database")
|
||||||
|
|
||||||
// If we issue an arbitrary query before any channel updates are
|
// If we issue an arbitrary query before any channel updates are
|
||||||
@@ -1826,8 +1800,7 @@ func TestChanUpdatesInHorizon(t *testing.T) {
|
|||||||
func TestNodeUpdatesInHorizon(t *testing.T) {
|
func TestNodeUpdatesInHorizon(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
graph, cleanUp, err := MakeTestGraph()
|
graph, err := MakeTestGraph(t)
|
||||||
defer cleanUp()
|
|
||||||
require.NoError(t, err, "unable to make test database")
|
require.NoError(t, err, "unable to make test database")
|
||||||
|
|
||||||
startTime := time.Unix(1234, 0)
|
startTime := time.Unix(1234, 0)
|
||||||
@@ -1943,8 +1916,7 @@ func TestNodeUpdatesInHorizon(t *testing.T) {
|
|||||||
func TestFilterKnownChanIDs(t *testing.T) {
|
func TestFilterKnownChanIDs(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
graph, cleanUp, err := MakeTestGraph()
|
graph, err := MakeTestGraph(t)
|
||||||
defer cleanUp()
|
|
||||||
require.NoError(t, err, "unable to make test database")
|
require.NoError(t, err, "unable to make test database")
|
||||||
|
|
||||||
// If we try to filter out a set of channel ID's before we even know of
|
// If we try to filter out a set of channel ID's before we even know of
|
||||||
@@ -2050,8 +2022,7 @@ func TestFilterKnownChanIDs(t *testing.T) {
|
|||||||
func TestFilterChannelRange(t *testing.T) {
|
func TestFilterChannelRange(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
graph, cleanUp, err := MakeTestGraph()
|
graph, err := MakeTestGraph(t)
|
||||||
defer cleanUp()
|
|
||||||
require.NoError(t, err, "unable to make test database")
|
require.NoError(t, err, "unable to make test database")
|
||||||
|
|
||||||
// We'll first populate our graph with two nodes. All channels created
|
// We'll first populate our graph with two nodes. All channels created
|
||||||
@@ -2174,8 +2145,7 @@ func TestFilterChannelRange(t *testing.T) {
|
|||||||
func TestFetchChanInfos(t *testing.T) {
|
func TestFetchChanInfos(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
graph, cleanUp, err := MakeTestGraph()
|
graph, err := MakeTestGraph(t)
|
||||||
defer cleanUp()
|
|
||||||
require.NoError(t, err, "unable to make test database")
|
require.NoError(t, err, "unable to make test database")
|
||||||
|
|
||||||
// We'll first populate our graph with two nodes. All channels created
|
// We'll first populate our graph with two nodes. All channels created
|
||||||
@@ -2284,8 +2254,7 @@ func TestFetchChanInfos(t *testing.T) {
|
|||||||
func TestIncompleteChannelPolicies(t *testing.T) {
|
func TestIncompleteChannelPolicies(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
graph, cleanUp, err := MakeTestGraph()
|
graph, err := MakeTestGraph(t)
|
||||||
defer cleanUp()
|
|
||||||
require.NoError(t, err, "unable to make test database")
|
require.NoError(t, err, "unable to make test database")
|
||||||
|
|
||||||
// Create two nodes.
|
// Create two nodes.
|
||||||
@@ -2384,8 +2353,7 @@ func TestIncompleteChannelPolicies(t *testing.T) {
|
|||||||
func TestChannelEdgePruningUpdateIndexDeletion(t *testing.T) {
|
func TestChannelEdgePruningUpdateIndexDeletion(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
graph, cleanUp, err := MakeTestGraph()
|
graph, err := MakeTestGraph(t)
|
||||||
defer cleanUp()
|
|
||||||
require.NoError(t, err, "unable to make test database")
|
require.NoError(t, err, "unable to make test database")
|
||||||
|
|
||||||
sourceNode, err := createTestVertex(graph.db)
|
sourceNode, err := createTestVertex(graph.db)
|
||||||
@@ -2528,8 +2496,7 @@ func TestChannelEdgePruningUpdateIndexDeletion(t *testing.T) {
|
|||||||
func TestPruneGraphNodes(t *testing.T) {
|
func TestPruneGraphNodes(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
graph, cleanUp, err := MakeTestGraph()
|
graph, err := MakeTestGraph(t)
|
||||||
defer cleanUp()
|
|
||||||
require.NoError(t, err, "unable to make test database")
|
require.NoError(t, err, "unable to make test database")
|
||||||
|
|
||||||
// We'll start off by inserting our source node, to ensure that it's
|
// We'll start off by inserting our source node, to ensure that it's
|
||||||
@@ -2601,8 +2568,7 @@ func TestPruneGraphNodes(t *testing.T) {
|
|||||||
func TestAddChannelEdgeShellNodes(t *testing.T) {
|
func TestAddChannelEdgeShellNodes(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
graph, cleanUp, err := MakeTestGraph()
|
graph, err := MakeTestGraph(t)
|
||||||
defer cleanUp()
|
|
||||||
require.NoError(t, err, "unable to make test database")
|
require.NoError(t, err, "unable to make test database")
|
||||||
|
|
||||||
// To start, we'll create two nodes, and only add one of them to the
|
// To start, we'll create two nodes, and only add one of them to the
|
||||||
@@ -2643,8 +2609,7 @@ func TestAddChannelEdgeShellNodes(t *testing.T) {
|
|||||||
func TestNodePruningUpdateIndexDeletion(t *testing.T) {
|
func TestNodePruningUpdateIndexDeletion(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
graph, cleanUp, err := MakeTestGraph()
|
graph, err := MakeTestGraph(t)
|
||||||
defer cleanUp()
|
|
||||||
require.NoError(t, err, "unable to make test database")
|
require.NoError(t, err, "unable to make test database")
|
||||||
|
|
||||||
// We'll first populate our graph with a single node that will be
|
// We'll first populate our graph with a single node that will be
|
||||||
@@ -2703,8 +2668,7 @@ func TestNodeIsPublic(t *testing.T) {
|
|||||||
// We'll need to create a separate database and channel graph for each
|
// We'll need to create a separate database and channel graph for each
|
||||||
// participant to replicate real-world scenarios (private edges being in
|
// participant to replicate real-world scenarios (private edges being in
|
||||||
// some graphs but not others, etc.).
|
// some graphs but not others, etc.).
|
||||||
aliceGraph, cleanUp, err := MakeTestGraph()
|
aliceGraph, err := MakeTestGraph(t)
|
||||||
defer cleanUp()
|
|
||||||
require.NoError(t, err, "unable to make test database")
|
require.NoError(t, err, "unable to make test database")
|
||||||
aliceNode, err := createTestVertex(aliceGraph.db)
|
aliceNode, err := createTestVertex(aliceGraph.db)
|
||||||
require.NoError(t, err, "unable to create test node")
|
require.NoError(t, err, "unable to create test node")
|
||||||
@@ -2712,8 +2676,7 @@ func TestNodeIsPublic(t *testing.T) {
|
|||||||
t.Fatalf("unable to set source node: %v", err)
|
t.Fatalf("unable to set source node: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
bobGraph, cleanUp, err := MakeTestGraph()
|
bobGraph, err := MakeTestGraph(t)
|
||||||
defer cleanUp()
|
|
||||||
require.NoError(t, err, "unable to make test database")
|
require.NoError(t, err, "unable to make test database")
|
||||||
bobNode, err := createTestVertex(bobGraph.db)
|
bobNode, err := createTestVertex(bobGraph.db)
|
||||||
require.NoError(t, err, "unable to create test node")
|
require.NoError(t, err, "unable to create test node")
|
||||||
@@ -2721,8 +2684,7 @@ func TestNodeIsPublic(t *testing.T) {
|
|||||||
t.Fatalf("unable to set source node: %v", err)
|
t.Fatalf("unable to set source node: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
carolGraph, cleanUp, err := MakeTestGraph()
|
carolGraph, err := MakeTestGraph(t)
|
||||||
defer cleanUp()
|
|
||||||
require.NoError(t, err, "unable to make test database")
|
require.NoError(t, err, "unable to make test database")
|
||||||
carolNode, err := createTestVertex(carolGraph.db)
|
carolNode, err := createTestVertex(carolGraph.db)
|
||||||
require.NoError(t, err, "unable to create test node")
|
require.NoError(t, err, "unable to create test node")
|
||||||
@@ -2841,9 +2803,8 @@ func TestNodeIsPublic(t *testing.T) {
|
|||||||
func TestDisabledChannelIDs(t *testing.T) {
|
func TestDisabledChannelIDs(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
graph, cleanUp, err := MakeTestGraph()
|
graph, err := MakeTestGraph(t)
|
||||||
require.NoError(t, err, "unable to make test database")
|
require.NoError(t, err, "unable to make test database")
|
||||||
defer cleanUp()
|
|
||||||
|
|
||||||
// Create first node and add it to the graph.
|
// Create first node and add it to the graph.
|
||||||
node1, err := createTestVertex(graph.db)
|
node1, err := createTestVertex(graph.db)
|
||||||
@@ -2925,8 +2886,7 @@ func TestDisabledChannelIDs(t *testing.T) {
|
|||||||
func TestEdgePolicyMissingMaxHtcl(t *testing.T) {
|
func TestEdgePolicyMissingMaxHtcl(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
graph, cleanUp, err := MakeTestGraph()
|
graph, err := MakeTestGraph(t)
|
||||||
defer cleanUp()
|
|
||||||
require.NoError(t, err, "unable to make test database")
|
require.NoError(t, err, "unable to make test database")
|
||||||
|
|
||||||
// We'd like to test the update of edges inserted into the database, so
|
// We'd like to test the update of edges inserted into the database, so
|
||||||
@@ -3087,8 +3047,7 @@ func TestGraphZombieIndex(t *testing.T) {
|
|||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
// We'll start by creating our test graph along with a test edge.
|
// We'll start by creating our test graph along with a test edge.
|
||||||
graph, cleanUp, err := MakeTestGraph()
|
graph, err := MakeTestGraph(t)
|
||||||
defer cleanUp()
|
|
||||||
require.NoError(t, err, "unable to create test database")
|
require.NoError(t, err, "unable to create test database")
|
||||||
|
|
||||||
node1, err := createTestVertex(graph.db)
|
node1, err := createTestVertex(graph.db)
|
||||||
@@ -3274,9 +3233,8 @@ func TestLightningNodeSigVerification(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Create a LightningNode from the same private key.
|
// Create a LightningNode from the same private key.
|
||||||
graph, cleanUp, err := MakeTestGraph()
|
graph, err := MakeTestGraph(t)
|
||||||
require.NoError(t, err, "unable to make test database")
|
require.NoError(t, err, "unable to make test database")
|
||||||
defer cleanUp()
|
|
||||||
|
|
||||||
node, err := createLightningNode(graph.db, priv)
|
node, err := createLightningNode(graph.db, priv)
|
||||||
require.NoError(t, err, "unable to create node")
|
require.NoError(t, err, "unable to create node")
|
||||||
@@ -3318,9 +3276,8 @@ func TestComputeFee(t *testing.T) {
|
|||||||
func TestBatchedAddChannelEdge(t *testing.T) {
|
func TestBatchedAddChannelEdge(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
graph, cleanUp, err := MakeTestGraph()
|
graph, err := MakeTestGraph(t)
|
||||||
require.Nil(t, err)
|
require.Nil(t, err)
|
||||||
defer cleanUp()
|
|
||||||
|
|
||||||
sourceNode, err := createTestVertex(graph.db)
|
sourceNode, err := createTestVertex(graph.db)
|
||||||
require.Nil(t, err)
|
require.Nil(t, err)
|
||||||
@@ -3400,9 +3357,8 @@ func TestBatchedAddChannelEdge(t *testing.T) {
|
|||||||
func TestBatchedUpdateEdgePolicy(t *testing.T) {
|
func TestBatchedUpdateEdgePolicy(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
graph, cleanUp, err := MakeTestGraph()
|
graph, err := MakeTestGraph(t)
|
||||||
require.Nil(t, err)
|
require.Nil(t, err)
|
||||||
defer cleanUp()
|
|
||||||
|
|
||||||
// We'd like to test the update of edges inserted into the database, so
|
// We'd like to test the update of edges inserted into the database, so
|
||||||
// we create two vertexes to connect.
|
// we create two vertexes to connect.
|
||||||
@@ -3458,9 +3414,8 @@ func TestBatchedUpdateEdgePolicy(t *testing.T) {
|
|||||||
// BenchmarkForEachChannel is a benchmark test that measures the number of
|
// BenchmarkForEachChannel is a benchmark test that measures the number of
|
||||||
// allocations and the total memory consumed by the full graph traversal.
|
// allocations and the total memory consumed by the full graph traversal.
|
||||||
func BenchmarkForEachChannel(b *testing.B) {
|
func BenchmarkForEachChannel(b *testing.B) {
|
||||||
graph, cleanUp, err := MakeTestGraph()
|
graph, err := MakeTestGraph(b)
|
||||||
require.Nil(b, err)
|
require.Nil(b, err)
|
||||||
defer cleanUp()
|
|
||||||
|
|
||||||
const numNodes = 100
|
const numNodes = 100
|
||||||
const numChannels = 4
|
const numChannels = 4
|
||||||
@@ -3518,8 +3473,7 @@ func BenchmarkForEachChannel(b *testing.B) {
|
|||||||
// TestGraphCacheForEachNodeChannel tests that the ForEachNodeChannel method
|
// TestGraphCacheForEachNodeChannel tests that the ForEachNodeChannel method
|
||||||
// works as expected, and is able to handle nil self edges.
|
// works as expected, and is able to handle nil self edges.
|
||||||
func TestGraphCacheForEachNodeChannel(t *testing.T) {
|
func TestGraphCacheForEachNodeChannel(t *testing.T) {
|
||||||
graph, cleanUp, err := MakeTestGraph()
|
graph, err := MakeTestGraph(t)
|
||||||
defer cleanUp()
|
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
// Unset the channel graph cache to simulate the user running with the
|
// Unset the channel graph cache to simulate the user running with the
|
||||||
@@ -3559,9 +3513,7 @@ func TestGraphCacheForEachNodeChannel(t *testing.T) {
|
|||||||
func TestGraphLoading(t *testing.T) {
|
func TestGraphLoading(t *testing.T) {
|
||||||
// First, create a temporary directory to be used for the duration of
|
// First, create a temporary directory to be used for the duration of
|
||||||
// this test.
|
// this test.
|
||||||
tempDirName, err := ioutil.TempDir("", "channelgraph")
|
tempDirName := t.TempDir()
|
||||||
require.NoError(t, err)
|
|
||||||
defer os.RemoveAll(tempDirName)
|
|
||||||
|
|
||||||
// Next, create the graph for the first time.
|
// Next, create the graph for the first time.
|
||||||
backend, backendCleanup, err := kvdb.GetTestBackend(tempDirName, "cgr")
|
backend, backendCleanup, err := kvdb.GetTestBackend(tempDirName, "cgr")
|
||||||
|
|||||||
@@ -2,8 +2,6 @@ package channeldb
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"io/ioutil"
|
|
||||||
"os"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/go-errors/errors"
|
"github.com/go-errors/errors"
|
||||||
@@ -422,11 +420,7 @@ func TestMigrationWithoutErrors(t *testing.T) {
|
|||||||
func TestMigrationReversion(t *testing.T) {
|
func TestMigrationReversion(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
tempDirName, err := ioutil.TempDir("", "channeldb")
|
tempDirName := t.TempDir()
|
||||||
defer func() {
|
|
||||||
os.RemoveAll(tempDirName)
|
|
||||||
}()
|
|
||||||
require.NoError(t, err, "unable to create temp dir")
|
|
||||||
|
|
||||||
backend, cleanup, err := kvdb.GetTestBackend(tempDirName, "cdb")
|
backend, cleanup, err := kvdb.GetTestBackend(tempDirName, "cdb")
|
||||||
require.NoError(t, err, "unable to get test db backend")
|
require.NoError(t, err, "unable to get test db backend")
|
||||||
|
|||||||
@@ -52,8 +52,7 @@ func TestLocateChanBucket(t *testing.T) {
|
|||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
// Create test database.
|
// Create test database.
|
||||||
cdb, cleanUp, err := migtest.MakeDB()
|
cdb, err := migtest.MakeDB(t)
|
||||||
defer cleanUp()
|
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
// Create a test channel.
|
// Create a test channel.
|
||||||
@@ -138,8 +137,7 @@ func TestFindNextMigrateHeight(t *testing.T) {
|
|||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
// Create test database.
|
// Create test database.
|
||||||
cdb, cleanUp, err := migtest.MakeDB()
|
cdb, err := migtest.MakeDB(t)
|
||||||
defer cleanUp()
|
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
// tester is a helper closure that finds the next migration height.
|
// tester is a helper closure that finds the next migration height.
|
||||||
@@ -307,8 +305,7 @@ func TestIterator(t *testing.T) {
|
|||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
// Create test database.
|
// Create test database.
|
||||||
cdb, cleanUp, err := migtest.MakeDB()
|
cdb, err := migtest.MakeDB(t)
|
||||||
defer cleanUp()
|
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
// exitKey is used to signal exit when hitting this key.
|
// exitKey is used to signal exit when hitting this key.
|
||||||
@@ -383,8 +380,7 @@ func TestIterateBuckets(t *testing.T) {
|
|||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
// Create test database.
|
// Create test database.
|
||||||
cdb, cleanUp, err := migtest.MakeDB()
|
cdb, err := migtest.MakeDB(t)
|
||||||
defer cleanUp()
|
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
// Create three test channels.
|
// Create three test channels.
|
||||||
@@ -659,8 +655,7 @@ func TestLocalNextUpdateNum(t *testing.T) {
|
|||||||
|
|
||||||
for _, tc := range testCases {
|
for _, tc := range testCases {
|
||||||
// Create a test database.
|
// Create a test database.
|
||||||
cdb, cleanUp, err := migtest.MakeDB()
|
cdb, err := migtest.MakeDB(t)
|
||||||
defer cleanUp()
|
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
tc := tc
|
tc := tc
|
||||||
|
|||||||
@@ -164,8 +164,7 @@ func TestValidateMigration(t *testing.T) {
|
|||||||
tc := tc
|
tc := tc
|
||||||
|
|
||||||
// Create a test db.
|
// Create a test db.
|
||||||
cdb, cleanUp, err := migtest.MakeDB()
|
cdb, err := migtest.MakeDB(t)
|
||||||
defer cleanUp()
|
|
||||||
require.NoError(t, err, "failed to create test db")
|
require.NoError(t, err, "failed to create test db")
|
||||||
|
|
||||||
t.Run(tc.name, func(t *testing.T) {
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
|
|||||||
@@ -2,9 +2,8 @@ package migration_01_to_11
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"io/ioutil"
|
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"os"
|
"testing"
|
||||||
|
|
||||||
"github.com/btcsuite/btcd/btcec/v2"
|
"github.com/btcsuite/btcd/btcec/v2"
|
||||||
"github.com/btcsuite/btcd/btcutil"
|
"github.com/btcsuite/btcd/btcutil"
|
||||||
@@ -63,29 +62,18 @@ var (
|
|||||||
privKey, pubKey = btcec.PrivKeyFromBytes(key[:])
|
privKey, pubKey = btcec.PrivKeyFromBytes(key[:])
|
||||||
)
|
)
|
||||||
|
|
||||||
// makeTestDB creates a new instance of the ChannelDB for testing purposes. A
|
// makeTestDB creates a new instance of the ChannelDB for testing purposes.
|
||||||
// callback which cleans up the created temporary directories is also returned
|
func makeTestDB(t *testing.T) (*DB, error) {
|
||||||
// and intended to be executed after the test completes.
|
// Create channeldb for the first time.
|
||||||
func makeTestDB() (*DB, func(), error) {
|
cdb, err := Open(t.TempDir())
|
||||||
// First, create a temporary directory to be used for the duration of
|
|
||||||
// this test.
|
|
||||||
tempDirName, err := ioutil.TempDir("", "channeldb")
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
t.Cleanup(func() {
|
||||||
// Next, create channeldb for the first time.
|
|
||||||
cdb, err := Open(tempDirName)
|
|
||||||
if err != nil {
|
|
||||||
return nil, nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
cleanUp := func() {
|
|
||||||
cdb.Close()
|
cdb.Close()
|
||||||
os.RemoveAll(tempDirName)
|
})
|
||||||
}
|
|
||||||
|
|
||||||
return cdb, cleanUp, nil
|
return cdb, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func createTestChannelState(cdb *DB) (*OpenChannel, error) {
|
func createTestChannelState(cdb *DB) (*OpenChannel, error) {
|
||||||
|
|||||||
@@ -12,8 +12,7 @@ import (
|
|||||||
func applyMigration(t *testing.T, beforeMigration, afterMigration func(d *DB),
|
func applyMigration(t *testing.T, beforeMigration, afterMigration func(d *DB),
|
||||||
migrationFunc migration, shouldFail bool) {
|
migrationFunc migration, shouldFail bool) {
|
||||||
|
|
||||||
cdb, cleanUp, err := makeTestDB()
|
cdb, err := makeTestDB(t)
|
||||||
defer cleanUp()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,37 +2,38 @@ package migtest
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
|
||||||
"os"
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/lightningnetwork/lnd/kvdb"
|
"github.com/lightningnetwork/lnd/kvdb"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
// MakeDB creates a new instance of the ChannelDB for testing purposes. A
|
// MakeDB creates a new instance of the ChannelDB for testing purposes.
|
||||||
// callback which cleans up the created temporary directories is also returned
|
func MakeDB(t testing.TB) (kvdb.Backend, error) {
|
||||||
// and intended to be executed after the test completes.
|
|
||||||
func MakeDB() (kvdb.Backend, func(), error) {
|
|
||||||
// Create temporary database for mission control.
|
// Create temporary database for mission control.
|
||||||
file, err := ioutil.TempFile("", "*.db")
|
file, err := os.CreateTemp("", "*.db")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
dbPath := file.Name()
|
dbPath := file.Name()
|
||||||
|
t.Cleanup(func() {
|
||||||
|
require.NoError(t, file.Close())
|
||||||
|
require.NoError(t, os.Remove(dbPath))
|
||||||
|
})
|
||||||
|
|
||||||
db, err := kvdb.Open(
|
db, err := kvdb.Open(
|
||||||
kvdb.BoltBackendName, dbPath, true, kvdb.DefaultDBTimeout,
|
kvdb.BoltBackendName, dbPath, true, kvdb.DefaultDBTimeout,
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
t.Cleanup(func() {
|
||||||
|
require.NoError(t, db.Close())
|
||||||
|
})
|
||||||
|
|
||||||
cleanUp := func() {
|
return db, nil
|
||||||
db.Close()
|
|
||||||
os.RemoveAll(dbPath)
|
|
||||||
}
|
|
||||||
|
|
||||||
return db, cleanUp, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ApplyMigration is a helper test function that encapsulates the general steps
|
// ApplyMigration is a helper test function that encapsulates the general steps
|
||||||
@@ -43,8 +44,7 @@ func ApplyMigration(t *testing.T,
|
|||||||
|
|
||||||
t.Helper()
|
t.Helper()
|
||||||
|
|
||||||
cdb, cleanUp, err := MakeDB()
|
cdb, err := MakeDB(t)
|
||||||
defer cleanUp()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
@@ -94,8 +94,7 @@ func ApplyMigrationWithDb(t testing.TB, beforeMigration, afterMigration,
|
|||||||
|
|
||||||
t.Helper()
|
t.Helper()
|
||||||
|
|
||||||
cdb, cleanUp, err := MakeDB()
|
cdb, err := MakeDB(t)
|
||||||
defer cleanUp()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user