Skip to content

Commit dd4acca

Browse files
firewalldb: 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 0b70477 commit dd4acca

File tree

4 files changed

+19
-16
lines changed

4 files changed

+19
-16
lines changed

firewalldb/actions_test.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,6 @@ func TestActionStorage(t *testing.T) {
2828
sessDB := session.NewTestDBWithAccounts(t, clock, accountsDB)
2929

3030
db := NewTestDBWithSessionsAndAccounts(t, sessDB, accountsDB, clock)
31-
t.Cleanup(func() {
32-
_ = db.Close()
33-
})
3431

3532
// Assert that attempting to add an action for a session that does not
3633
// exist returns an error.
@@ -198,9 +195,6 @@ func TestListActions(t *testing.T) {
198195
sessDB := session.NewTestDB(t, clock)
199196

200197
db := NewTestDBWithSessions(t, sessDB, clock)
201-
t.Cleanup(func() {
202-
_ = db.Close()
203-
})
204198

205199
// Add 2 sessions that we can reference.
206200
sess1, err := sessDB.NewSession(
@@ -466,9 +460,6 @@ func TestListGroupActions(t *testing.T) {
466460
}
467461

468462
db := NewTestDBWithSessions(t, sessDB, clock)
469-
t.Cleanup(func() {
470-
_ = db.Close()
471-
})
472463

473464
// There should not be any actions in group 1 yet.
474465
al, _, _, err := db.ListActions(ctx, nil, WithActionGroupID(group1))

firewalldb/test_postgres.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ import (
1111

1212
// NewTestDB is a helper function that creates an BBolt database for testing.
1313
func NewTestDB(t *testing.T, clock clock.Clock) *SQLDB {
14-
return NewSQLDB(db.NewTestPostgresDB(t).BaseDB, clock)
14+
return createStore(t, db.NewTestPostgresDB(t).BaseDB, clock)
1515
}
1616

1717
// NewTestDBFromPath is a helper function that creates a new BoltStore with a
1818
// connection to an existing BBolt database for testing.
1919
func NewTestDBFromPath(t *testing.T, _ string, clock clock.Clock) *SQLDB {
20-
return NewSQLDB(db.NewTestPostgresDB(t).BaseDB, clock)
20+
return createStore(t, db.NewTestPostgresDB(t).BaseDB, clock)
2121
}

firewalldb/test_sql.go

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

99
"github.com/lightninglabs/lightning-terminal/accounts"
10+
"github.com/lightninglabs/lightning-terminal/db"
1011
"github.com/lightninglabs/lightning-terminal/session"
1112
"github.com/lightningnetwork/lnd/clock"
1213
"github.com/stretchr/testify/require"
@@ -20,7 +21,7 @@ func NewTestDBWithSessions(t *testing.T, sessionStore session.Store,
2021
sessions, ok := sessionStore.(*session.SQLStore)
2122
require.True(t, ok)
2223

23-
return NewSQLDB(sessions.BaseDB, clock)
24+
return createStore(t, sessions.BaseDB, clock)
2425
}
2526

2627
// NewTestDBWithSessionsAndAccounts creates a new test SQLDB Store with access
@@ -36,7 +37,7 @@ func NewTestDBWithSessionsAndAccounts(t *testing.T, sessionStore SessionDB,
3637

3738
require.Equal(t, accounts.BaseDB, sessions.BaseDB)
3839

39-
return NewSQLDB(sessions.BaseDB, clock)
40+
return createStore(t, sessions.BaseDB, clock)
4041
}
4142

4243
func assertEqualActions(t *testing.T, expected, got *Action) {
@@ -52,3 +53,14 @@ func assertEqualActions(t *testing.T, expected, got *Action) {
5253
expected.AttemptedAt = expectedAttemptedAt
5354
got.AttemptedAt = actualAttemptedAt
5455
}
56+
57+
// createStore is a helper function that creates a new SQLDB and ensure that
58+
// it is closed when during the test cleanup.
59+
func createStore(t *testing.T, sqlDB *db.BaseDB, clock clock.Clock) *SQLDB {
60+
store := NewSQLDB(sqlDB, clock)
61+
t.Cleanup(func() {
62+
require.NoError(t, store.Close())
63+
})
64+
65+
return store
66+
}

firewalldb/test_sqlite.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ import (
1111

1212
// NewTestDB is a helper function that creates an BBolt database for testing.
1313
func NewTestDB(t *testing.T, clock clock.Clock) *SQLDB {
14-
return NewSQLDB(db.NewTestSqliteDB(t).BaseDB, clock)
14+
return createStore(t, db.NewTestSqliteDB(t).BaseDB, clock)
1515
}
1616

1717
// NewTestDBFromPath is a helper function that creates a new BoltStore with a
1818
// connection to an existing BBolt database for testing.
1919
func NewTestDBFromPath(t *testing.T, dbPath string, clock clock.Clock) *SQLDB {
20-
return NewSQLDB(
21-
db.NewTestSqliteDbHandleFromPath(t, dbPath).BaseDB, clock,
20+
return createStore(
21+
t, db.NewTestSqliteDbHandleFromPath(t, dbPath).BaseDB, clock,
2222
)
2323
}

0 commit comments

Comments
 (0)