graph/db: add NewTestDB method

In this commit, we add a `test_kvdb.go` file with a single definition of
the `NewTestDB` function. A new version of `MakeTestGraph` (called
`MakeTestGraphNew` is added which makes use of this `NewTestDB` function
to create the backing `V1Store` passed to the `ChannelGraph` for tests.

Later on, we will add new implementations of this method backed by
sqlite and postgres.  When those are added, then build flags will
control which version of `NewTestDB` is called.

With this change, the only test call-site of `NewKVStore` is the new
`test_kvdb.go` file.
This commit is contained in:
Elle Mouton committed 2025-05-22 14:14:42 +02:00
1 parent dc353dc50e
commit b285546ce1
4 files changed
+55 -10

No files matched your search

+29
View File
@@ -5,6 +5,7 @@ import (
"fmt"
"sync"
"sync/atomic"
"testing"
"time"
"github.com/btcsuite/btcd/chaincfg/chainhash"
@@ -13,6 +14,7 @@ import (
"github.com/lightningnetwork/lnd/graph/db/models"
"github.com/lightningnetwork/lnd/lnwire"
"github.com/lightningnetwork/lnd/routing/route"
"github.com/stretchr/testify/require"
)
// ErrChanGraphShuttingDown indicates that the ChannelGraph has shutdown or is
@@ -580,3 +582,30 @@ func (c *ChannelGraph) UpdateEdgePolicy(edge *models.ChannelEdgePolicy,
return nil
}
// MakeTestGraphNew creates a new instance of the ChannelGraph for testing
// purposes. The backing V1Store implementation depends on the version of
// NewTestDB included in the current build.
//
// NOTE: this is currently unused, but is left here for future use to show how
// NewTestDB can be used. As the SQL implementation of the V1Store is
// implemented, unit tests will be switched to use this function instead of
// the existing MakeTestGraph helper. Once only this function is used, the
// existing MakeTestGraph function will be removed and this one will be renamed.
func MakeTestGraphNew(t testing.TB,
opts ...ChanGraphOption) *ChannelGraph {
t.Helper()
store := NewTestDB(t)
graph, err := NewChannelGraph(store, opts...)
require.NoError(t, err)
require.NoError(t, graph.Start())
t.Cleanup(func() {
require.NoError(t, graph.Stop())
})
return graph
}