Skip to content

Commit cbb7e39

Browse files
committed
session: add a clock to the DB
1 parent 4010832 commit cbb7e39

File tree

3 files changed

+18
-7
lines changed

3 files changed

+18
-7
lines changed

session/db.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"path/filepath"
88
"time"
99

10+
"github.com/lightningnetwork/lnd/clock"
1011
"go.etcd.io/bbolt"
1112
)
1213

@@ -35,13 +36,15 @@ var (
3536
// DB is a bolt-backed persistent store.
3637
type DB struct {
3738
*bbolt.DB
39+
40+
clock clock.Clock
3841
}
3942

4043
// A compile-time check to ensure that DB implements the Store interface.
4144
var _ Store = (*DB)(nil)
4245

4346
// NewDB creates a new bolt database that can be found at the given directory.
44-
func NewDB(dir, fileName string) (*DB, error) {
47+
func NewDB(dir, fileName string, clock clock.Clock) (*DB, error) {
4548
firstInit := false
4649
path := filepath.Join(dir, fileName)
4750

@@ -64,7 +67,10 @@ func NewDB(dir, fileName string) (*DB, error) {
6467
return nil, err
6568
}
6669

67-
return &DB{DB: db}, nil
70+
return &DB{
71+
DB: db,
72+
clock: clock,
73+
}, nil
6874
}
6975

7076
// fileExists reports whether the named file or directory exists.

session/store_test.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,17 @@ import (
66
"time"
77

88
"github.com/btcsuite/btcd/btcec/v2"
9+
"github.com/lightningnetwork/lnd/clock"
910
"github.com/stretchr/testify/require"
1011
)
1112

13+
var testTime = time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC)
14+
1215
// TestBasicSessionStore tests the basic getters and setters of the session
1316
// store.
1417
func TestBasicSessionStore(t *testing.T) {
1518
// Set up a new DB.
16-
db, err := NewDB(t.TempDir(), "test.db")
19+
db, err := NewDB(t.TempDir(), "test.db", clock.NewTestClock(testTime))
1720
require.NoError(t, err)
1821
t.Cleanup(func() {
1922
_ = db.Close()
@@ -89,7 +92,7 @@ func TestBasicSessionStore(t *testing.T) {
8992
// TestLinkingSessions tests that session linking works as expected.
9093
func TestLinkingSessions(t *testing.T) {
9194
// Set up a new DB.
92-
db, err := NewDB(t.TempDir(), "test.db")
95+
db, err := NewDB(t.TempDir(), "test.db", clock.NewTestClock(testTime))
9396
require.NoError(t, err)
9497
t.Cleanup(func() {
9598
_ = db.Close()
@@ -125,7 +128,7 @@ func TestLinkingSessions(t *testing.T) {
125128
// of the GetGroupID and GetSessionIDs methods.
126129
func TestLinkedSessions(t *testing.T) {
127130
// Set up a new DB.
128-
db, err := NewDB(t.TempDir(), "test.db")
131+
db, err := NewDB(t.TempDir(), "test.db", clock.NewTestClock(testTime))
129132
require.NoError(t, err)
130133
t.Cleanup(func() {
131134
_ = db.Close()
@@ -192,7 +195,7 @@ func TestLinkedSessions(t *testing.T) {
192195
// method correctly checks if each session in a group passes a predicate.
193196
func TestCheckSessionGroupPredicate(t *testing.T) {
194197
// Set up a new DB.
195-
db, err := NewDB(t.TempDir(), "test.db")
198+
db, err := NewDB(t.TempDir(), "test.db", clock.NewTestClock(testTime))
196199
require.NoError(t, err)
197200
t.Cleanup(func() {
198201
_ = db.Close()

terminal.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,9 @@ func (g *LightningTerminal) start(ctx context.Context) error {
446446

447447
// Create an instance of the local Terminal Connect session store DB.
448448
networkDir := filepath.Join(g.cfg.LitDir, g.cfg.Network)
449-
g.sessionDB, err = session.NewDB(networkDir, session.DBFilename)
449+
g.sessionDB, err = session.NewDB(
450+
networkDir, session.DBFilename, clock.NewDefaultClock(),
451+
)
450452
if err != nil {
451453
return fmt.Errorf("error creating session DB: %v", err)
452454
}

0 commit comments

Comments
 (0)