multi: rename chan DB Open method to OpenForTesting

This commit is contained in:
Elle Mouton
2024-11-26 12:31:38 +02:00
parent 4089fbcb44
commit 439a6c7d6c
24 changed files with 101 additions and 331 deletions

View File

@@ -34,6 +34,7 @@ import (
"github.com/lightningnetwork/lnd/invoices"
"github.com/lightningnetwork/lnd/kvdb"
"github.com/lightningnetwork/lnd/lnwire"
"github.com/stretchr/testify/require"
)
const (
@@ -345,10 +346,11 @@ type DB struct {
noRevLogAmtData bool
}
// Open opens or creates channeldb. Any necessary schemas migrations due
// to updates will take place as necessary.
// TODO(bhandras): deprecate this function.
func Open(dbPath string, modifiers ...OptionModifier) (*DB, error) {
// OpenForTesting opens or creates a channeldb to be used for tests. Any
// necessary schemas migrations due to updates will take place as necessary.
func OpenForTesting(t testing.TB, dbPath string,
modifiers ...OptionModifier) *DB {
backend, err := kvdb.GetBoltBackend(&kvdb.BoltBackendConfig{
DBPath: dbPath,
DBFileName: dbName,
@@ -357,16 +359,18 @@ func Open(dbPath string, modifiers ...OptionModifier) (*DB, error) {
AutoCompactMinAge: kvdb.DefaultBoltAutoCompactMinAge,
DBTimeout: kvdb.DefaultDBTimeout,
})
if err != nil {
return nil, err
}
require.NoError(t, err)
db, err := CreateWithBackend(backend, modifiers...)
if err == nil {
db.dbPath = dbPath
}
require.NoError(t, err)
return db, err
db.dbPath = dbPath
t.Cleanup(func() {
require.NoError(t, db.Close())
})
return db
}
// CreateWithBackend creates channeldb instance using the passed kvdb.Backend.

View File

@@ -65,11 +65,7 @@ func TestOpenWithCreate(t *testing.T) {
// Now, reopen the same db in dry run migration mode. Since we have not
// applied any migrations, this should ignore the flag and not fail.
cdb, err = Open(dbPath, OptionDryRunMigration(true))
require.NoError(t, err, "unable to create channeldb")
if err := cdb.Close(); err != nil {
t.Fatalf("unable to close channeldb: %v", err)
}
OpenForTesting(t, dbPath, OptionDryRunMigration(true))
}
// TestWipe tests that the database wipe operation completes successfully

View File

@@ -23,15 +23,11 @@ func initHintCache(t *testing.T) *HeightHintCache {
func initHintCacheWithConfig(t *testing.T, cfg CacheConfig) *HeightHintCache {
t.Helper()
db, err := Open(t.TempDir())
require.NoError(t, err, "unable to create db")
db := OpenForTesting(t, t.TempDir())
hintCache, err := NewHeightHintCache(cfg, db.Backend)
require.NoError(t, err, "unable to create hint cache")
t.Cleanup(func() {
require.NoError(t, db.Close())
})
return hintCache
}