Skip to content

Commit c06036d

Browse files
committed
multi: use MakeTestGraphNew for all remaining unit tests
And delete the old MakeTestGraph function. Here we need to update some of our unit tests to ensure that any updates to existing nodes or channel policies have newer timestamps. This is so that we don't violate our SQL DB constraints that prevent updates to a node or channel policy record if the new update is not newer (has a newer LastUpdate time) than the currently presisted record).
1 parent e0801f5 commit c06036d

File tree

6 files changed

+50
-65
lines changed

6 files changed

+50
-65
lines changed

autopilot/prefattach_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ type testDBGraph struct {
3636
}
3737

3838
func newDiskChanGraph(t *testing.T) (testGraph, error) {
39-
graphDB := graphdb.MakeTestGraph(t)
39+
graphDB := graphdb.MakeTestGraphNew(t)
4040
require.NoError(t, graphDB.Start())
4141
t.Cleanup(func() {
4242
require.NoError(t, graphDB.Stop())

graph/builder_test.go

Lines changed: 44 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -892,15 +892,27 @@ func TestPruneChannelGraphDoubleDisabled(t *testing.T) {
892892
}
893893

894894
func testPruneChannelGraphDoubleDisabled(t *testing.T, assumeValid bool) {
895+
timestamp := time.Now()
896+
897+
// nextTimeStamp is a helper closure that will return a new
898+
// timestamp each time it's called, this helps us create channel updates
899+
// with new timestamps so that we don't run into our SQL DB constraint
900+
// which only allows an update to a channel edge if the last update
901+
// timestamp is greater than the previous one.
902+
nextTimeStamp := func() time.Time {
903+
timestamp = timestamp.Add(time.Second)
904+
905+
return timestamp
906+
}
907+
895908
// We'll create the following test graph so that only the last channel
896909
// is pruned. We'll use a fresh timestamp to ensure they're not pruned
897910
// according to that heuristic.
898-
timestamp := time.Now()
899911
testChannels := []*testChannel{
900912
// Channel from self shouldn't be pruned.
901913
symmetricTestChannel(
902914
"self", "a", 100000, &testChannelPolicy{
903-
LastUpdate: timestamp,
915+
LastUpdate: nextTimeStamp(),
904916
Disabled: true,
905917
}, 99,
906918
),
@@ -918,7 +930,7 @@ func testPruneChannelGraphDoubleDisabled(t *testing.T, assumeValid bool) {
918930
Node1: &testChannelEnd{
919931
Alias: "a",
920932
testChannelPolicy: &testChannelPolicy{
921-
LastUpdate: timestamp,
933+
LastUpdate: nextTimeStamp(),
922934
Disabled: true,
923935
},
924936
},
@@ -932,7 +944,7 @@ func testPruneChannelGraphDoubleDisabled(t *testing.T, assumeValid bool) {
932944
Node1: &testChannelEnd{
933945
Alias: "a",
934946
testChannelPolicy: &testChannelPolicy{
935-
LastUpdate: timestamp,
947+
LastUpdate: nextTimeStamp(),
936948
Disabled: false,
937949
},
938950
},
@@ -946,14 +958,14 @@ func testPruneChannelGraphDoubleDisabled(t *testing.T, assumeValid bool) {
946958
Node1: &testChannelEnd{
947959
Alias: "a",
948960
testChannelPolicy: &testChannelPolicy{
949-
LastUpdate: timestamp,
961+
LastUpdate: nextTimeStamp(),
950962
Disabled: true,
951963
},
952964
},
953965
Node2: &testChannelEnd{
954966
Alias: "b",
955967
testChannelPolicy: &testChannelPolicy{
956-
LastUpdate: timestamp,
968+
LastUpdate: nextTimeStamp(),
957969
Disabled: false,
958970
},
959971
},
@@ -963,13 +975,13 @@ func testPruneChannelGraphDoubleDisabled(t *testing.T, assumeValid bool) {
963975

964976
// Both edges enabled.
965977
symmetricTestChannel("c", "d", 100000, &testChannelPolicy{
966-
LastUpdate: timestamp,
978+
LastUpdate: nextTimeStamp(),
967979
Disabled: false,
968980
}, 2),
969981

970982
// Both edges disabled, only one pruned.
971983
symmetricTestChannel("e", "f", 100000, &testChannelPolicy{
972-
LastUpdate: timestamp,
984+
LastUpdate: nextTimeStamp(),
973985
Disabled: true,
974986
}, 3),
975987
}
@@ -1363,7 +1375,9 @@ func parseTestGraph(t *testing.T, useCache bool, path string) (
13631375
testAddrs = append(testAddrs, testAddr)
13641376

13651377
// Next, create a temporary graph database for usage within the test.
1366-
graph := graphdb.MakeTestGraph(t, graphdb.WithUseGraphCache(useCache))
1378+
graph := graphdb.MakeTestGraphNew(
1379+
t, graphdb.WithUseGraphCache(useCache),
1380+
)
13671381

13681382
aliasMap := make(map[string]route.Vertex)
13691383
privKeyMap := make(map[string]*btcec.PrivateKey)
@@ -1441,6 +1455,13 @@ func parseTestGraph(t *testing.T, useCache bool, path string) (
14411455
}
14421456

14431457
source = dbNode
1458+
1459+
// Set the selected source node.
1460+
if err := graph.SetSourceNode(ctx, source); err != nil {
1461+
return nil, err
1462+
}
1463+
1464+
continue
14441465
}
14451466

14461467
// With the node fully parsed, add it as a vertex within the
@@ -1450,13 +1471,6 @@ func parseTestGraph(t *testing.T, useCache bool, path string) (
14501471
}
14511472
}
14521473

1453-
if source != nil {
1454-
// Set the selected source node
1455-
if err := graph.SetSourceNode(ctx, source); err != nil {
1456-
return nil, err
1457-
}
1458-
}
1459-
14601474
// With all the vertexes inserted, we can now insert the edges into the
14611475
// test graph.
14621476
for _, edge := range g.Edges {
@@ -1739,14 +1753,16 @@ func createTestGraphFromChannels(t *testing.T, useCache bool,
17391753
testAddrs = append(testAddrs, testAddr)
17401754

17411755
// Next, create a temporary graph database for usage within the test.
1742-
graph := graphdb.MakeTestGraph(t, graphdb.WithUseGraphCache(useCache))
1756+
graph := graphdb.MakeTestGraphNew(
1757+
t, graphdb.WithUseGraphCache(useCache),
1758+
)
17431759

17441760
aliasMap := make(map[string]route.Vertex)
17451761
privKeyMap := make(map[string]*btcec.PrivateKey)
17461762

17471763
nodeIndex := byte(0)
1748-
addNodeWithAlias := func(alias string, features *lnwire.FeatureVector) (
1749-
*models.LightningNode, error) {
1764+
addNodeWithAlias := func(alias string,
1765+
features *lnwire.FeatureVector) error {
17501766

17511767
keyBytes := []byte{
17521768
0, 0, 0, 0, 0, 0, 0, 0,
@@ -1776,26 +1792,26 @@ func createTestGraphFromChannels(t *testing.T, useCache bool,
17761792

17771793
// With the node fully parsed, add it as a vertex within the
17781794
// graph.
1779-
if err := graph.AddLightningNode(ctx, dbNode); err != nil {
1780-
return nil, err
1795+
if alias == source {
1796+
err = graph.SetSourceNode(ctx, dbNode)
1797+
require.NoError(t, err)
1798+
} else {
1799+
err := graph.AddLightningNode(ctx, dbNode)
1800+
require.NoError(t, err)
17811801
}
17821802

17831803
aliasMap[alias] = dbNode.PubKeyBytes
17841804
nodeIndex++
17851805

1786-
return dbNode, nil
1806+
return nil
17871807
}
17881808

17891809
// Add the source node.
1790-
dbNode, err := addNodeWithAlias(source, lnwire.EmptyFeatureVector())
1810+
err = addNodeWithAlias(source, lnwire.EmptyFeatureVector())
17911811
if err != nil {
17921812
return nil, err
17931813
}
17941814

1795-
if err = graph.SetSourceNode(ctx, dbNode); err != nil {
1796-
return nil, err
1797-
}
1798-
17991815
// Initialize variable that keeps track of the next channel id to assign
18001816
// if none is specified.
18011817
nextUnassignedChannelID := uint64(100000)
@@ -1813,7 +1829,7 @@ func createTestGraphFromChannels(t *testing.T, useCache bool,
18131829
features =
18141830
node.testChannelPolicy.Features
18151831
}
1816-
_, err := addNodeWithAlias(
1832+
err := addNodeWithAlias(
18171833
node.Alias, features,
18181834
)
18191835
if err != nil {

graph/db/graph_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3147,7 +3147,7 @@ func TestChannelEdgePruningUpdateIndexDeletion(t *testing.T) {
31473147
t.Parallel()
31483148
ctx := context.Background()
31493149

3150-
graph := MakeTestGraph(t)
3150+
graph := MakeTestGraphNew(t)
31513151

31523152
// The update index only applies to the bbolt graph.
31533153
boltStore, ok := graph.V1Store.(*KVStore)
@@ -3681,7 +3681,7 @@ func TestEdgePolicyMissingMaxHtcl(t *testing.T) {
36813681
t.Parallel()
36823682
ctx := context.Background()
36833683

3684-
graph := MakeTestGraph(t)
3684+
graph := MakeTestGraphNew(t)
36853685

36863686
// This test currently directly edits the bytes stored in the bbolt DB.
36873687
boltStore, ok := graph.V1Store.(*KVStore)

graph/db/kv_store.go

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
"net"
1313
"sort"
1414
"sync"
15-
"testing"
1615
"time"
1716

1817
"github.com/btcsuite/btcd/btcec/v2"
@@ -28,7 +27,6 @@ import (
2827
"github.com/lightningnetwork/lnd/kvdb"
2928
"github.com/lightningnetwork/lnd/lnwire"
3029
"github.com/lightningnetwork/lnd/routing/route"
31-
"github.com/stretchr/testify/require"
3230
)
3331

3432
var (
@@ -4869,32 +4867,3 @@ func (c *chanGraphNodeTx) ForEachChannel(f func(*models.ChannelEdgeInfo,
48694867
},
48704868
)
48714869
}
4872-
4873-
// MakeTestGraph creates a new instance of the ChannelGraph for testing
4874-
// purposes.
4875-
//
4876-
// NOTE: this helper currently creates a ChannelGraph that is only ever backed
4877-
// by the `KVStore` of the `V1Store` interface.
4878-
func MakeTestGraph(t testing.TB, opts ...ChanGraphOption) *ChannelGraph {
4879-
t.Helper()
4880-
4881-
// Next, create KVStore for the first time.
4882-
backend, backendCleanup, err := kvdb.GetTestBackend(t.TempDir(), "cgr")
4883-
t.Cleanup(backendCleanup)
4884-
require.NoError(t, err)
4885-
t.Cleanup(func() {
4886-
require.NoError(t, backend.Close())
4887-
})
4888-
4889-
graphStore, err := NewKVStore(backend)
4890-
require.NoError(t, err)
4891-
4892-
graph, err := NewChannelGraph(graphStore, opts...)
4893-
require.NoError(t, err)
4894-
require.NoError(t, graph.Start())
4895-
t.Cleanup(func() {
4896-
require.NoError(t, graph.Stop())
4897-
})
4898-
4899-
return graph
4900-
}

graph/notifications_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -763,7 +763,7 @@ func TestNodeUpdateNotification(t *testing.T) {
763763
// then it should trigger a new notification.
764764
// TODO(roasbeef): assume monotonic time.
765765
nodeUpdateAnn := *node1
766-
nodeUpdateAnn.LastUpdate = node1.LastUpdate.Add(300 * time.Millisecond)
766+
nodeUpdateAnn.LastUpdate = node1.LastUpdate.Add(time.Second)
767767

768768
// Add new node topology update to the channel router.
769769
if err := ctx.builder.AddNode(ctxb, &nodeUpdateAnn); err != nil {
@@ -1059,7 +1059,7 @@ type testCtx struct {
10591059
func createTestCtxSingleNode(t *testing.T,
10601060
startingHeight uint32) *testCtx {
10611061

1062-
graph := graphdb.MakeTestGraph(t)
1062+
graph := graphdb.MakeTestGraphNew(t)
10631063
sourceNode := createTestNode(t)
10641064

10651065
require.NoError(t,

peer/test_utils.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -602,7 +602,7 @@ func createTestPeer(t *testing.T) *peerTestCtx {
602602

603603
const chanActiveTimeout = time.Minute
604604

605-
dbAliceGraph := graphdb.MakeTestGraph(t)
605+
dbAliceGraph := graphdb.MakeTestGraphNew(t)
606606
require.NoError(t, dbAliceGraph.Start())
607607
t.Cleanup(func() {
608608
require.NoError(t, dbAliceGraph.Stop())

0 commit comments

Comments
 (0)