loopdb: test error handling for bbolt.Open

This commit is contained in:
Boris Nagaev 2025-12-19 13:12:38 -03:00
parent 417e25463d
commit 360af61005
No known key found for this signature in database
2 changed files with 32 additions and 1 deletions

View file

@ -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) {

View file

@ -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) {