diff --git a/loopdb/store.go b/loopdb/store.go index 3dd5a6bd..fe9cc267 100644 --- a/loopdb/store.go +++ b/loopdb/store.go @@ -18,6 +18,9 @@ import ( ) var ( + // bboltOpen allows overriding the open function in tests. + bboltOpen = bbolt.Open + // dbFileName is the default file name of the client-side loop sub-swap // database. dbFileName = "loop.db" @@ -191,7 +194,7 @@ func NewBoltSwapStore(dbPath string, chainParams *chaincfg.Params) ( // Now that we know that path exists, we'll open up bolt, which // implements our default swap store. path := filepath.Join(dbPath, dbFileName) - bdb, err := bbolt.Open(path, 0600, &bbolt.Options{ + bdb, err := bboltOpen(path, 0600, &bbolt.Options{ Timeout: DefaultLoopDBTimeout, }) if errors.Is(err, bbolt.ErrTimeout) { diff --git a/loopdb/store_test.go b/loopdb/store_test.go index 43dd0e75..fc78a987 100644 --- a/loopdb/store_test.go +++ b/loopdb/store_test.go @@ -3,6 +3,7 @@ package loopdb import ( "context" "crypto/sha256" + "fmt" "io/ioutil" "os" "path/filepath" @@ -50,6 +51,33 @@ var ( testTime = time.Date(2018, time.January, 9, 14, 00, 00, 0, time.UTC) ) +// TestNewBoltSwapStoreTimeout ensures a wrapped bbolt timeout is detected +// correctly when opening the store. +func TestNewBoltSwapStoreTimeout(t *testing.T) { + tempDir := t.TempDir() + + // Override the bbolt open function to return a wrapped timeout. + origOpen := bboltOpen + t.Cleanup(func() { + bboltOpen = origOpen + }) + + wrappedErr := fmt.Errorf("wrapped: %w", bbolt.ErrTimeout) + bboltOpen = func(path string, mode os.FileMode, + options *bbolt.Options) (*bbolt.DB, error) { + + require.NotNil(t, options) + require.Equal(t, filepath.Join(tempDir, dbFileName), path) + + return nil, wrappedErr + } + + store, err := NewBoltSwapStore(tempDir, &chaincfg.MainNetParams) + require.Nil(t, store) + require.ErrorIs(t, err, bbolt.ErrTimeout) + require.ErrorContains(t, err, "couldn't obtain exclusive lock") +} + // TestLoopOutStore tests all the basic functionality of the current bbolt // swap store. func TestLoopOutStore(t *testing.T) {