Skip to content

Commit 0b70477

Browse files
session: ensure that test SQL store is closed
We add a helper function to the functions that creates the test SQL stores, in order to ensure that the store is properly closed when the test is cleaned up.
1 parent a7ecf0a commit 0b70477

File tree

3 files changed

+18
-6
lines changed

3 files changed

+18
-6
lines changed

session/test_postgres.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ var ErrDBClosed = errors.New("database is closed")
1616

1717
// NewTestDB is a helper function that creates an SQLStore database for testing.
1818
func NewTestDB(t *testing.T, clock clock.Clock) Store {
19-
return NewSQLStore(db.NewTestPostgresDB(t).BaseDB, clock)
19+
return createStore(t, db.NewTestPostgresDB(t).BaseDB, clock)
2020
}
2121

2222
// NewTestDBFromPath is a helper function that creates a new SQLStore with a
2323
// connection to an existing postgres database for testing.
2424
func NewTestDBFromPath(t *testing.T, dbPath string,
2525
clock clock.Clock) Store {
2626

27-
return NewSQLStore(db.NewTestPostgresDB(t).BaseDB, clock)
27+
return createStore(t, db.NewTestPostgresDB(t).BaseDB, clock)
2828
}

session/test_sql.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"testing"
77

88
"github.com/lightninglabs/lightning-terminal/accounts"
9+
"github.com/lightninglabs/lightning-terminal/db"
910
"github.com/lightningnetwork/lnd/clock"
1011
"github.com/stretchr/testify/require"
1112
)
@@ -16,5 +17,16 @@ func NewTestDBWithAccounts(t *testing.T, clock clock.Clock,
1617
accounts, ok := acctStore.(*accounts.SQLStore)
1718
require.True(t, ok)
1819

19-
return NewSQLStore(accounts.BaseDB, clock)
20+
return createStore(t, accounts.BaseDB, clock)
21+
}
22+
23+
// createStore is a helper function that creates a new SQLStore and ensure that
24+
// it is closed when during the test cleanup.
25+
func createStore(t *testing.T, sqlDB *db.BaseDB, clock clock.Clock) *SQLStore {
26+
store := NewSQLStore(sqlDB, clock)
27+
t.Cleanup(func() {
28+
require.NoError(t, store.Close())
29+
})
30+
31+
return store
2032
}

session/test_sqlite.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@ var ErrDBClosed = errors.New("database is closed")
1616

1717
// NewTestDB is a helper function that creates an SQLStore database for testing.
1818
func NewTestDB(t *testing.T, clock clock.Clock) Store {
19-
return NewSQLStore(db.NewTestSqliteDB(t).BaseDB, clock)
19+
return createStore(t, db.NewTestSqliteDB(t).BaseDB, clock)
2020
}
2121

2222
// NewTestDBFromPath is a helper function that creates a new SQLStore with a
2323
// connection to an existing sqlite database for testing.
2424
func NewTestDBFromPath(t *testing.T, dbPath string,
2525
clock clock.Clock) Store {
2626

27-
return NewSQLStore(
28-
db.NewTestSqliteDbHandleFromPath(t, dbPath).BaseDB, clock,
27+
return createStore(
28+
t, db.NewTestSqliteDbHandleFromPath(t, dbPath).BaseDB, clock,
2929
)
3030
}

0 commit comments

Comments
 (0)