routing: use T.TempDir to create temporary test directory
Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
This commit is contained in:
8 files changed
+117
-197
No files matched your search
+24
-64
@@ -11,7 +11,6 @@ import (
|
||||
"io/ioutil"
|
||||
"math"
|
||||
"net"
|
||||
"os"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
@@ -150,28 +149,17 @@ type testChan struct {
|
||||
}
|
||||
|
||||
// makeTestGraph creates a new instance of a channeldb.ChannelGraph for testing
|
||||
// purposes. A callback which cleans up the created temporary directories is
|
||||
// also returned and intended to be executed after the test completes.
|
||||
func makeTestGraph(useCache bool) (*channeldb.ChannelGraph, kvdb.Backend,
|
||||
func(), error) {
|
||||
// purposes.
|
||||
func makeTestGraph(t *testing.T, useCache bool) (*channeldb.ChannelGraph,
|
||||
kvdb.Backend, error) {
|
||||
|
||||
// First, create a temporary directory to be used for the duration of
|
||||
// this test.
|
||||
tempDirName, err := ioutil.TempDir("", "channeldb")
|
||||
// Create channelgraph for the first time.
|
||||
backend, backendCleanup, err := kvdb.GetTestBackend(t.TempDir(), "cgr")
|
||||
if err != nil {
|
||||
return nil, nil, nil, err
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
// Next, create channelgraph for the first time.
|
||||
backend, backendCleanup, err := kvdb.GetTestBackend(tempDirName, "cgr")
|
||||
if err != nil {
|
||||
return nil, nil, nil, err
|
||||
}
|
||||
|
||||
cleanUp := func() {
|
||||
backendCleanup()
|
||||
_ = os.RemoveAll(tempDirName)
|
||||
}
|
||||
t.Cleanup(backendCleanup)
|
||||
|
||||
opts := channeldb.DefaultOptions()
|
||||
graph, err := channeldb.NewChannelGraph(
|
||||
@@ -180,16 +168,17 @@ func makeTestGraph(useCache bool) (*channeldb.ChannelGraph, kvdb.Backend,
|
||||
useCache, false,
|
||||
)
|
||||
if err != nil {
|
||||
cleanUp()
|
||||
return nil, nil, nil, err
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
return graph, backend, cleanUp, nil
|
||||
return graph, backend, nil
|
||||
}
|
||||
|
||||
// parseTestGraph returns a fully populated ChannelGraph given a path to a JSON
|
||||
// file which encodes a test graph.
|
||||
func parseTestGraph(useCache bool, path string) (*testGraphInstance, error) {
|
||||
func parseTestGraph(t *testing.T, useCache bool, path string) (
|
||||
*testGraphInstance, error) {
|
||||
|
||||
graphJSON, err := ioutil.ReadFile(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -214,7 +203,7 @@ func parseTestGraph(useCache bool, path string) (*testGraphInstance, error) {
|
||||
testAddrs = append(testAddrs, testAddr)
|
||||
|
||||
// Next, create a temporary graph database for usage within the test.
|
||||
graph, graphBackend, cleanUp, err := makeTestGraph(useCache)
|
||||
graph, graphBackend, err := makeTestGraph(t, useCache)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -429,7 +418,6 @@ func parseTestGraph(useCache bool, path string) (*testGraphInstance, error) {
|
||||
return &testGraphInstance{
|
||||
graph: graph,
|
||||
graphBackend: graphBackend,
|
||||
cleanUp: cleanUp,
|
||||
aliasMap: aliasMap,
|
||||
privKeyMap: privKeyMap,
|
||||
channelIDs: channelIDs,
|
||||
@@ -497,7 +485,6 @@ type testChannel struct {
|
||||
type testGraphInstance struct {
|
||||
graph *channeldb.ChannelGraph
|
||||
graphBackend kvdb.Backend
|
||||
cleanUp func()
|
||||
|
||||
// aliasMap is a map from a node's alias to its public key. This type is
|
||||
// provided in order to allow easily look up from the human memorable alias
|
||||
@@ -533,8 +520,8 @@ func (g *testGraphInstance) getLink(chanID lnwire.ShortChannelID) (
|
||||
// a deterministical way and added to the channel graph. A list of nodes is
|
||||
// not required and derived from the channel data. The goal is to keep
|
||||
// instantiating a test channel graph as light weight as possible.
|
||||
func createTestGraphFromChannels(useCache bool, testChannels []*testChannel,
|
||||
source string) (*testGraphInstance, error) {
|
||||
func createTestGraphFromChannels(t *testing.T, useCache bool,
|
||||
testChannels []*testChannel, source string) (*testGraphInstance, error) {
|
||||
|
||||
// We'll use this fake address for the IP address of all the nodes in
|
||||
// our tests. This value isn't needed for path finding so it doesn't
|
||||
@@ -547,7 +534,7 @@ func createTestGraphFromChannels(useCache bool, testChannels []*testChannel,
|
||||
testAddrs = append(testAddrs, testAddr)
|
||||
|
||||
// Next, create a temporary graph database for usage within the test.
|
||||
graph, graphBackend, cleanUp, err := makeTestGraph(useCache)
|
||||
graph, graphBackend, err := makeTestGraph(t, useCache)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -765,7 +752,6 @@ func createTestGraphFromChannels(useCache bool, testChannels []*testChannel,
|
||||
return &testGraphInstance{
|
||||
graph: graph,
|
||||
graphBackend: graphBackend,
|
||||
cleanUp: cleanUp,
|
||||
aliasMap: aliasMap,
|
||||
privKeyMap: privKeyMap,
|
||||
links: links,
|
||||
@@ -883,7 +869,6 @@ func runFindPathWithMetadata(t *testing.T, useCache bool) {
|
||||
}
|
||||
|
||||
ctx := newPathFindingTestContext(t, useCache, testChannels, "alice")
|
||||
defer ctx.cleanup()
|
||||
|
||||
paymentAmt := lnwire.NewMSatFromSatoshis(100)
|
||||
target := ctx.keyFromAlias("bob")
|
||||
@@ -952,7 +937,6 @@ func runFindLowestFeePath(t *testing.T, useCache bool) {
|
||||
}
|
||||
|
||||
ctx := newPathFindingTestContext(t, useCache, testChannels, "roasbeef")
|
||||
defer ctx.cleanup()
|
||||
|
||||
const (
|
||||
startingHeight = 100
|
||||
@@ -1053,9 +1037,8 @@ var basicGraphPathFindingTests = []basicGraphPathFindingTestCase{
|
||||
}}
|
||||
|
||||
func runBasicGraphPathFinding(t *testing.T, useCache bool) {
|
||||
testGraphInstance, err := parseTestGraph(useCache, basicGraphFilePath)
|
||||
testGraphInstance, err := parseTestGraph(t, useCache, basicGraphFilePath)
|
||||
require.NoError(t, err, "unable to create graph")
|
||||
defer testGraphInstance.cleanUp()
|
||||
|
||||
// With the test graph loaded, we'll test some basic path finding using
|
||||
// the pre-generated graph. Consult the testdata/basic_graph.json file
|
||||
@@ -1218,9 +1201,8 @@ func testBasicGraphPathFindingCase(t *testing.T, graphInstance *testGraphInstanc
|
||||
// the path can support custom TLV records for the receiver under the
|
||||
// appropriate circumstances.
|
||||
func runPathFindingWithAdditionalEdges(t *testing.T, useCache bool) {
|
||||
graph, err := parseTestGraph(useCache, basicGraphFilePath)
|
||||
graph, err := parseTestGraph(t, useCache, basicGraphFilePath)
|
||||
require.NoError(t, err, "unable to create graph")
|
||||
defer graph.cleanUp()
|
||||
|
||||
sourceNode, err := graph.graph.SourceNode()
|
||||
require.NoError(t, err, "unable to fetch source node")
|
||||
@@ -1641,7 +1623,6 @@ func runNewRoutePathTooLong(t *testing.T, useCache bool) {
|
||||
}
|
||||
|
||||
ctx := newPathFindingTestContext(t, useCache, testChannels, "start")
|
||||
defer ctx.cleanup()
|
||||
|
||||
// Assert that we can find 20 hop routes.
|
||||
node20 := ctx.keyFromAlias("node-20")
|
||||
@@ -1669,9 +1650,8 @@ func runNewRoutePathTooLong(t *testing.T, useCache bool) {
|
||||
}
|
||||
|
||||
func runPathNotAvailable(t *testing.T, useCache bool) {
|
||||
graph, err := parseTestGraph(useCache, basicGraphFilePath)
|
||||
graph, err := parseTestGraph(t, useCache, basicGraphFilePath)
|
||||
require.NoError(t, err, "unable to create graph")
|
||||
defer graph.cleanUp()
|
||||
|
||||
sourceNode, err := graph.graph.SourceNode()
|
||||
require.NoError(t, err, "unable to fetch source node")
|
||||
@@ -1728,7 +1708,6 @@ func runDestTLVGraphFallback(t *testing.T, useCache bool) {
|
||||
}
|
||||
|
||||
ctx := newPathFindingTestContext(t, useCache, testChannels, "roasbeef")
|
||||
defer ctx.cleanup()
|
||||
|
||||
sourceNode, err := ctx.graph.SourceNode()
|
||||
require.NoError(t, err, "unable to fetch source node")
|
||||
@@ -1826,7 +1805,6 @@ func runMissingFeatureDep(t *testing.T, useCache bool) {
|
||||
}
|
||||
|
||||
ctx := newPathFindingTestContext(t, useCache, testChannels, "roasbeef")
|
||||
defer ctx.cleanup()
|
||||
|
||||
// Conner's node in the graph has a broken feature vector, since it
|
||||
// signals payment addresses without signaling tlv onions. Pathfinding
|
||||
@@ -1899,7 +1877,6 @@ func runUnknownRequiredFeatures(t *testing.T, useCache bool) {
|
||||
}
|
||||
|
||||
ctx := newPathFindingTestContext(t, useCache, testChannels, "roasbeef")
|
||||
defer ctx.cleanup()
|
||||
|
||||
conner := ctx.keyFromAlias("conner")
|
||||
joost := ctx.keyFromAlias("joost")
|
||||
@@ -1941,7 +1918,6 @@ func runDestPaymentAddr(t *testing.T, useCache bool) {
|
||||
}
|
||||
|
||||
ctx := newPathFindingTestContext(t, useCache, testChannels, "roasbeef")
|
||||
defer ctx.cleanup()
|
||||
|
||||
luoji := ctx.keyFromAlias("luoji")
|
||||
|
||||
@@ -1967,9 +1943,8 @@ func runDestPaymentAddr(t *testing.T, useCache bool) {
|
||||
}
|
||||
|
||||
func runPathInsufficientCapacity(t *testing.T, useCache bool) {
|
||||
graph, err := parseTestGraph(useCache, basicGraphFilePath)
|
||||
graph, err := parseTestGraph(t, useCache, basicGraphFilePath)
|
||||
require.NoError(t, err, "unable to create graph")
|
||||
defer graph.cleanUp()
|
||||
|
||||
sourceNode, err := graph.graph.SourceNode()
|
||||
require.NoError(t, err, "unable to fetch source node")
|
||||
@@ -1998,9 +1973,8 @@ func runPathInsufficientCapacity(t *testing.T, useCache bool) {
|
||||
// runRouteFailMinHTLC tests that if we attempt to route an HTLC which is
|
||||
// smaller than the advertised minHTLC of an edge, then path finding fails.
|
||||
func runRouteFailMinHTLC(t *testing.T, useCache bool) {
|
||||
graph, err := parseTestGraph(useCache, basicGraphFilePath)
|
||||
graph, err := parseTestGraph(t, useCache, basicGraphFilePath)
|
||||
require.NoError(t, err, "unable to create graph")
|
||||
defer graph.cleanUp()
|
||||
|
||||
sourceNode, err := graph.graph.SourceNode()
|
||||
require.NoError(t, err, "unable to fetch source node")
|
||||
@@ -2050,7 +2024,6 @@ func runRouteFailMaxHTLC(t *testing.T, useCache bool) {
|
||||
}
|
||||
|
||||
ctx := newPathFindingTestContext(t, useCache, testChannels, "roasbeef")
|
||||
defer ctx.cleanup()
|
||||
|
||||
// First, attempt to send a payment greater than the max HTLC we are
|
||||
// about to set, which should succeed.
|
||||
@@ -2084,9 +2057,8 @@ func runRouteFailMaxHTLC(t *testing.T, useCache bool) {
|
||||
// ignore the disable flags, with the assumption that the correct bandwidth is
|
||||
// found among the bandwidth hints.
|
||||
func runRouteFailDisabledEdge(t *testing.T, useCache bool) {
|
||||
graph, err := parseTestGraph(useCache, basicGraphFilePath)
|
||||
graph, err := parseTestGraph(t, useCache, basicGraphFilePath)
|
||||
require.NoError(t, err, "unable to create graph")
|
||||
defer graph.cleanUp()
|
||||
|
||||
sourceNode, err := graph.graph.SourceNode()
|
||||
require.NoError(t, err, "unable to fetch source node")
|
||||
@@ -2150,9 +2122,8 @@ func runRouteFailDisabledEdge(t *testing.T, useCache bool) {
|
||||
// bandwidth hints is used by the path finding algorithm to consider whether to
|
||||
// use a local channel.
|
||||
func runPathSourceEdgesBandwidth(t *testing.T, useCache bool) {
|
||||
graph, err := parseTestGraph(useCache, basicGraphFilePath)
|
||||
graph, err := parseTestGraph(t, useCache, basicGraphFilePath)
|
||||
require.NoError(t, err, "unable to create graph")
|
||||
defer graph.cleanUp()
|
||||
|
||||
sourceNode, err := graph.graph.SourceNode()
|
||||
require.NoError(t, err, "unable to fetch source node")
|
||||
@@ -2462,7 +2433,6 @@ func runRestrictOutgoingChannel(t *testing.T, useCache bool) {
|
||||
}
|
||||
|
||||
ctx := newPathFindingTestContext(t, useCache, testChannels, "roasbeef")
|
||||
defer ctx.cleanup()
|
||||
|
||||
const (
|
||||
startingHeight = 100
|
||||
@@ -2523,7 +2493,6 @@ func runRestrictLastHop(t *testing.T, useCache bool) {
|
||||
}
|
||||
|
||||
ctx := newPathFindingTestContext(t, useCache, testChannels, "source")
|
||||
defer ctx.cleanup()
|
||||
|
||||
paymentAmt := lnwire.NewMSatFromSatoshis(100)
|
||||
target := ctx.keyFromAlias("target")
|
||||
@@ -2592,7 +2561,6 @@ func testCltvLimit(t *testing.T, useCache bool, limit uint32,
|
||||
}
|
||||
|
||||
ctx := newPathFindingTestContext(t, useCache, testChannels, "roasbeef")
|
||||
defer ctx.cleanup()
|
||||
|
||||
paymentAmt := lnwire.NewMSatFromSatoshis(100)
|
||||
target := ctx.keyFromAlias("target")
|
||||
@@ -2770,7 +2738,6 @@ func testProbabilityRouting(t *testing.T, useCache bool,
|
||||
}
|
||||
|
||||
ctx := newPathFindingTestContext(t, useCache, testChannels, "roasbeef")
|
||||
defer ctx.cleanup()
|
||||
|
||||
alias := ctx.testGraphInstance.aliasMap
|
||||
|
||||
@@ -2853,7 +2820,6 @@ func runEqualCostRouteSelection(t *testing.T, useCache bool) {
|
||||
}
|
||||
|
||||
ctx := newPathFindingTestContext(t, useCache, testChannels, "source")
|
||||
defer ctx.cleanup()
|
||||
|
||||
alias := ctx.testGraphInstance.aliasMap
|
||||
|
||||
@@ -2922,7 +2888,6 @@ func runNoCycle(t *testing.T, useCache bool) {
|
||||
}
|
||||
|
||||
ctx := newPathFindingTestContext(t, useCache, testChannels, "source")
|
||||
defer ctx.cleanup()
|
||||
|
||||
const (
|
||||
startingHeight = 100
|
||||
@@ -2975,7 +2940,6 @@ func runRouteToSelf(t *testing.T, useCache bool) {
|
||||
}
|
||||
|
||||
ctx := newPathFindingTestContext(t, useCache, testChannels, "source")
|
||||
defer ctx.cleanup()
|
||||
|
||||
paymentAmt := lnwire.NewMSatFromSatoshis(100)
|
||||
target := ctx.source
|
||||
@@ -3013,7 +2977,7 @@ func newPathFindingTestContext(t *testing.T, useCache bool,
|
||||
testChannels []*testChannel, source string) *pathFindingTestContext {
|
||||
|
||||
testGraphInstance, err := createTestGraphFromChannels(
|
||||
useCache, testChannels, source,
|
||||
t, useCache, testChannels, source,
|
||||
)
|
||||
require.NoError(t, err, "unable to create graph")
|
||||
|
||||
@@ -3046,10 +3010,6 @@ func (c *pathFindingTestContext) aliasFromKey(pubKey route.Vertex) string {
|
||||
return ""
|
||||
}
|
||||
|
||||
func (c *pathFindingTestContext) cleanup() {
|
||||
c.testGraphInstance.cleanUp()
|
||||
}
|
||||
|
||||
func (c *pathFindingTestContext) findPath(target route.Vertex,
|
||||
amt lnwire.MilliSatoshi) ([]*channeldb.CachedEdgePolicy,
|
||||
error) {
|
||||
|
||||
Reference in new issue
Block a user