Skip to content

Commit 44625c3

Browse files
committed
session: add DB constructor helpers for tests
In preparation for adding helpers with the same names but that will compile under different build flags, we add the helper DB constructors to use when testing the session store logic against a KVDB backend.
1 parent 5ed5fd2 commit 44625c3

File tree

2 files changed

+34
-22
lines changed

2 files changed

+34
-22
lines changed

session/store_test.go

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,10 @@ var testTime = time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC)
1616
func TestBasicSessionStore(t *testing.T) {
1717
// Set up a new DB.
1818
clock := clock.NewTestClock(testTime)
19-
db, err := NewDB(t.TempDir(), "test.db", clock)
20-
require.NoError(t, err)
21-
t.Cleanup(func() {
22-
_ = db.Close()
23-
})
19+
db := NewTestDB(t, clock)
2420

2521
// Try fetch a session that doesn't exist yet.
26-
_, err = db.GetSessionByID(ID{1, 3, 4, 4})
22+
_, err := db.GetSessionByID(ID{1, 3, 4, 4})
2723
require.ErrorIs(t, err, ErrSessionNotFound)
2824

2925
// Reserve a session. This should succeed.
@@ -201,11 +197,7 @@ func TestLinkingSessions(t *testing.T) {
201197

202198
// Set up a new DB.
203199
clock := clock.NewTestClock(testTime)
204-
db, err := NewDB(t.TempDir(), "test.db", clock)
205-
require.NoError(t, err)
206-
t.Cleanup(func() {
207-
_ = db.Close()
208-
})
200+
db := NewTestDB(t, clock)
209201

210202
groupID, err := IDFromBytes([]byte{1, 2, 3, 4})
211203
require.NoError(t, err)
@@ -242,11 +234,7 @@ func TestLinkedSessions(t *testing.T) {
242234

243235
// Set up a new DB.
244236
clock := clock.NewTestClock(testTime)
245-
db, err := NewDB(t.TempDir(), "test.db", clock)
246-
require.NoError(t, err)
247-
t.Cleanup(func() {
248-
_ = db.Close()
249-
})
237+
db := NewTestDB(t, clock)
250238

251239
// Create a few sessions. The first one is a new session and the two
252240
// after are all linked to the prior one. All these sessions belong to
@@ -298,18 +286,14 @@ func TestLinkedSessions(t *testing.T) {
298286
func TestStateShift(t *testing.T) {
299287
// Set up a new DB.
300288
clock := clock.NewTestClock(testTime)
301-
db, err := NewDB(t.TempDir(), "test.db", clock)
302-
require.NoError(t, err)
303-
t.Cleanup(func() {
304-
_ = db.Close()
305-
})
289+
db := NewTestDB(t, clock)
306290

307291
// Add a new session to the DB.
308292
s1 := createSession(t, db, "label 1")
309293

310294
// Check that the session is in the StateCreated state. Also check that
311295
// the "RevokedAt" time has not yet been set.
312-
s1, err = db.GetSession(s1.LocalPublicKey)
296+
s1, err := db.GetSession(s1.LocalPublicKey)
313297
require.NoError(t, err)
314298
require.Equal(t, StateCreated, s1.State)
315299
require.Equal(t, time.Time{}, s1.RevokedAt)

session/test_kvdb.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package session
2+
3+
import (
4+
"testing"
5+
6+
"github.com/lightningnetwork/lnd/clock"
7+
"github.com/stretchr/testify/require"
8+
)
9+
10+
// NewTestDB is a helper function that creates an BBolt database for testing.
11+
func NewTestDB(t *testing.T, clock clock.Clock) *BoltStore {
12+
return NewTestDBFromPath(t, t.TempDir(), clock)
13+
}
14+
15+
// NewTestDBFromPath is a helper function that creates a new BoltStore with a
16+
// connection to an existing BBolt database for testing.
17+
func NewTestDBFromPath(t *testing.T, dbPath string,
18+
clock clock.Clock) *BoltStore {
19+
20+
store, err := NewDB(dbPath, DBFilename, clock)
21+
require.NoError(t, err)
22+
23+
t.Cleanup(func() {
24+
require.NoError(t, store.DB.Close())
25+
})
26+
27+
return store
28+
}

0 commit comments

Comments
 (0)