Skip to content

Commit

Permalink
multi: rename chan DB Open method to OpenForTesting
Browse files Browse the repository at this point in the history
  • Loading branch information
ellemouton committed Nov 28, 2024
1 parent 4089fbc commit 439a6c7
Show file tree
Hide file tree
Showing 24 changed files with 101 additions and 331 deletions.
6 changes: 1 addition & 5 deletions chainntnfs/bitcoindnotify/bitcoind_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,7 @@ var (
func initHintCache(t *testing.T) *channeldb.HeightHintCache {
t.Helper()

db, err := channeldb.Open(t.TempDir())
require.NoError(t, err, "unable to create db")
t.Cleanup(func() {
require.NoError(t, db.Close())
})
db := channeldb.OpenForTesting(t, t.TempDir())

testCfg := channeldb.CacheConfig{
QueryDisable: false,
Expand Down
6 changes: 1 addition & 5 deletions chainntnfs/btcdnotify/btcd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,7 @@ var (
func initHintCache(t *testing.T) *channeldb.HeightHintCache {
t.Helper()

db, err := channeldb.Open(t.TempDir())
require.NoError(t, err, "unable to create db")
t.Cleanup(func() {
require.NoError(t, db.Close())
})
db := channeldb.OpenForTesting(t, t.TempDir())

testCfg := channeldb.CacheConfig{
QueryDisable: false,
Expand Down
6 changes: 2 additions & 4 deletions chainntnfs/test/test_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -1906,10 +1906,8 @@ func TestInterfaces(t *testing.T, targetBackEnd string) {

// Initialize a height hint cache for each notifier.
tempDir := t.TempDir()
db, err := channeldb.Open(tempDir)
if err != nil {
t.Fatalf("unable to create db: %v", err)
}
db := channeldb.OpenForTesting(t, tempDir)

testCfg := channeldb.CacheConfig{
QueryDisable: false,
}
Expand Down
26 changes: 15 additions & 11 deletions channeldb/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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,
Expand All @@ -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)

db.dbPath = dbPath

t.Cleanup(func() {
require.NoError(t, db.Close())
})

return db, err
return db
}

// CreateWithBackend creates channeldb instance using the passed kvdb.Backend.
Expand Down
6 changes: 1 addition & 5 deletions channeldb/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 2 additions & 6 deletions channeldb/height_hint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
42 changes: 7 additions & 35 deletions contractcourt/breach_arbitrator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -635,15 +635,6 @@ func TestMockRetributionStore(t *testing.T) {
}
}

func makeTestChannelDB(t *testing.T) (*channeldb.DB, error) {
db, err := channeldb.Open(t.TempDir())
if err != nil {
return nil, err
}

return db, nil
}

// TestChannelDBRetributionStore instantiates a retributionStore backed by a
// channeldb.DB, and tests its behavior using the general RetributionStore test
// suite.
Expand All @@ -654,25 +645,19 @@ func TestChannelDBRetributionStore(t *testing.T) {
t.Run(
"channeldbDBRetributionStore."+test.name,
func(tt *testing.T) {
db, err := makeTestChannelDB(t)
if err != nil {
t.Fatalf("unable to open channeldb: %v", err)
}
defer db.Close()
db := channeldb.OpenForTesting(t, t.TempDir())

restartDb := func() RetributionStorer {
// Close and reopen channeldb
if err = db.Close(); err != nil {
if err := db.Close(); err != nil {
t.Fatalf("unable to close "+
"channeldb during "+
"restart: %v",
err)
}
db, err = channeldb.Open(db.Path())
if err != nil {
t.Fatalf("unable to open "+
"channeldb: %v", err)
}
db = channeldb.OpenForTesting(
t, db.Path(),
)

return NewRetributionStore(db)
}
Expand Down Expand Up @@ -2279,21 +2264,8 @@ func createInitChannels(t *testing.T) (
return nil, nil, err
}

dbAlice, err := channeldb.Open(t.TempDir())
if err != nil {
return nil, nil, err
}
t.Cleanup(func() {
require.NoError(t, dbAlice.Close())
})

dbBob, err := channeldb.Open(t.TempDir())
if err != nil {
return nil, nil, err
}
t.Cleanup(func() {
require.NoError(t, dbBob.Close())
})
dbAlice := channeldb.OpenForTesting(t, t.TempDir())
dbBob := channeldb.OpenForTesting(t, t.TempDir())

estimator := chainfee.NewStaticEstimator(12500, 0)
feePerKw, err := estimator.EstimateFeePerKW(1)
Expand Down
14 changes: 2 additions & 12 deletions contractcourt/chain_arbitrator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,7 @@ import (
func TestChainArbitratorRepublishCloses(t *testing.T) {
t.Parallel()

db, err := channeldb.Open(t.TempDir())
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() {
require.NoError(t, db.Close())
})
db := channeldb.OpenForTesting(t, t.TempDir())

// Create 10 test channels and sync them to the database.
const numChans = 10
Expand Down Expand Up @@ -139,11 +133,7 @@ func TestChainArbitratorRepublishCloses(t *testing.T) {
func TestResolveContract(t *testing.T) {
t.Parallel()

db, err := channeldb.Open(t.TempDir())
require.NoError(t, err, "unable to open db")
t.Cleanup(func() {
require.NoError(t, db.Close())
})
db := channeldb.OpenForTesting(t, t.TempDir())

// With the DB created, we'll make a new channel, and mark it as
// pending open within the database.
Expand Down
7 changes: 2 additions & 5 deletions contractcourt/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,9 @@ func copyChannelState(t *testing.T, state *channeldb.OpenChannel) (
return nil, err
}

newDb, err := channeldb.Open(tempDbPath)
if err != nil {
return nil, err
}
newDB := channeldb.OpenForTesting(t, tempDbPath)

chans, err := newDb.ChannelStateDB().FetchAllChannels()
chans, err := newDB.ChannelStateDB().FetchAllChannels()
if err != nil {
return nil, err
}
Expand Down
20 changes: 1 addition & 19 deletions discovery/gossiper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,21 +73,6 @@ var (
rebroadcastInterval = time.Hour * 1000000
)

// makeTestDB creates a new instance of the ChannelDB for testing purposes.
func makeTestDB(t *testing.T) (*channeldb.DB, error) {
// Create channeldb for the first time.
cdb, err := channeldb.Open(t.TempDir())
if err != nil {
return nil, err
}

t.Cleanup(func() {
cdb.Close()
})

return cdb, nil
}

type mockGraphSource struct {
bestHeight uint32

Expand Down Expand Up @@ -734,10 +719,7 @@ func createTestCtx(t *testing.T, startHeight uint32, isChanPeer bool) (
notifier := newMockNotifier()
router := newMockRouter(startHeight)

db, err := makeTestDB(t)
if err != nil {
return nil, err
}
db := channeldb.OpenForTesting(t, t.TempDir())

waitingProofStore, err := channeldb.NewWaitingProofStore(db)
if err != nil {
Expand Down
13 changes: 2 additions & 11 deletions discovery/message_store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,10 @@ import (
func createTestMessageStore(t *testing.T) *MessageStore {
t.Helper()

db, err := channeldb.Open(t.TempDir())
if err != nil {
t.Fatalf("unable to open db: %v", err)
}

t.Cleanup(func() {
db.Close()
})
db := channeldb.OpenForTesting(t, t.TempDir())

store, err := NewMessageStore(db)
if err != nil {
t.Fatalf("unable to initialize message store: %v", err)
}
require.NoError(t, err)

return store
}
Expand Down
5 changes: 1 addition & 4 deletions funding/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -427,10 +427,7 @@ func createTestFundingManager(t *testing.T, privKey *btcec.PrivateKey,
}

dbDir := filepath.Join(tempTestDir, "cdb")
fullDB, err := channeldb.Open(dbDir)
if err != nil {
return nil, err
}
fullDB := channeldb.OpenForTesting(t, dbDir)

cdb := fullDB.ChannelStateDB()

Expand Down
4 changes: 1 addition & 3 deletions htlcswitch/circuit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -625,9 +625,7 @@ func makeCircuitDB(t *testing.T, path string) *channeldb.DB {
path = t.TempDir()
}

db, err := channeldb.Open(path)
require.NoError(t, err, "unable to open channel db")
t.Cleanup(func() { db.Close() })
db := channeldb.OpenForTesting(t, path)

return db
}
Expand Down
3 changes: 1 addition & 2 deletions htlcswitch/link_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2169,7 +2169,7 @@ func newSingleLinkTestHarness(t *testing.T, chanAmt,
BaseFee: lnwire.NewMSatFromSatoshis(1),
TimeLockDelta: 6,
}
invoiceRegistry = newMockRegistry(globalPolicy.TimeLockDelta)
invoiceRegistry = newMockRegistry(t)
)

pCache := newMockPreimageCache()
Expand Down Expand Up @@ -2267,7 +2267,6 @@ func newSingleLinkTestHarness(t *testing.T, chanAmt,

t.Cleanup(func() {
close(alicePeer.quit)
invoiceRegistry.cleanup()
})

harness := singleLinkTestHarness{
Expand Down
Loading

0 comments on commit 439a6c7

Please sign in to comment.