Skip to content

Commit 360af61

Browse files
committed
loopdb: test error handling for bbolt.Open
1 parent 417e254 commit 360af61

2 files changed

Lines changed: 32 additions & 1 deletion

File tree

loopdb/store.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ import (
1818
)
1919

2020
var (
21+
// bboltOpen allows overriding the open function in tests.
22+
bboltOpen = bbolt.Open
23+
2124
// dbFileName is the default file name of the client-side loop sub-swap
2225
// database.
2326
dbFileName = "loop.db"
@@ -191,7 +194,7 @@ func NewBoltSwapStore(dbPath string, chainParams *chaincfg.Params) (
191194
// Now that we know that path exists, we'll open up bolt, which
192195
// implements our default swap store.
193196
path := filepath.Join(dbPath, dbFileName)
194-
bdb, err := bbolt.Open(path, 0600, &bbolt.Options{
197+
bdb, err := bboltOpen(path, 0600, &bbolt.Options{
195198
Timeout: DefaultLoopDBTimeout,
196199
})
197200
if errors.Is(err, bbolt.ErrTimeout) {

loopdb/store_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package loopdb
33
import (
44
"context"
55
"crypto/sha256"
6+
"fmt"
67
"io/ioutil"
78
"os"
89
"path/filepath"
@@ -50,6 +51,33 @@ var (
5051
testTime = time.Date(2018, time.January, 9, 14, 00, 00, 0, time.UTC)
5152
)
5253

54+
// TestNewBoltSwapStoreTimeout ensures a wrapped bbolt timeout is detected
55+
// correctly when opening the store.
56+
func TestNewBoltSwapStoreTimeout(t *testing.T) {
57+
tempDir := t.TempDir()
58+
59+
// Override the bbolt open function to return a wrapped timeout.
60+
origOpen := bboltOpen
61+
t.Cleanup(func() {
62+
bboltOpen = origOpen
63+
})
64+
65+
wrappedErr := fmt.Errorf("wrapped: %w", bbolt.ErrTimeout)
66+
bboltOpen = func(path string, mode os.FileMode,
67+
options *bbolt.Options) (*bbolt.DB, error) {
68+
69+
require.NotNil(t, options)
70+
require.Equal(t, filepath.Join(tempDir, dbFileName), path)
71+
72+
return nil, wrappedErr
73+
}
74+
75+
store, err := NewBoltSwapStore(tempDir, &chaincfg.MainNetParams)
76+
require.Nil(t, store)
77+
require.ErrorIs(t, err, bbolt.ErrTimeout)
78+
require.ErrorContains(t, err, "couldn't obtain exclusive lock")
79+
}
80+
5381
// TestLoopOutStore tests all the basic functionality of the current bbolt
5482
// swap store.
5583
func TestLoopOutStore(t *testing.T) {

0 commit comments

Comments
 (0)