Skip to content

Commit 00d476f

Browse files
committed
test(hare): add db cleanup (#6221)
## Motivation Adding a cleanup for the in-memory db we are using in hare.
1 parent dcda406 commit 00d476f

File tree

5 files changed

+25
-12
lines changed

5 files changed

+25
-12
lines changed

hare3/hare_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,8 @@ func (n *node) reuseSigner(signer *signing.EdSigner) *node {
148148
return n
149149
}
150150

151-
func (n *node) withDb() *node {
152-
n.db = sql.InMemory()
151+
func (n *node) withDb(tb testing.TB) *node {
152+
n.db = sql.InMemoryTest(tb)
153153
n.atxsdata = atxsdata.New()
154154
n.proposals = store.New()
155155
return n
@@ -342,7 +342,7 @@ func (cl *lockstepCluster) addActive(n int) *lockstepCluster {
342342
for i := last; i < last+n; i++ {
343343
cl.addNode((&node{t: cl.t, i: i}).
344344
withController().withSyncer().withPublisher().
345-
withClock().withDb().withSigner().withAtx(cl.units.min, cl.units.max).
345+
withClock().withDb(cl.t).withSigner().withAtx(cl.units.min, cl.units.max).
346346
withOracle().withHare())
347347
}
348348
return cl
@@ -353,7 +353,7 @@ func (cl *lockstepCluster) addInactive(n int) *lockstepCluster {
353353
for i := last; i < last+n; i++ {
354354
cl.addNode((&node{t: cl.t, i: i}).
355355
withController().withSyncer().withPublisher().
356-
withClock().withDb().withSigner().
356+
withClock().withDb(cl.t).withSigner().
357357
withOracle().withHare())
358358
}
359359
return cl
@@ -366,7 +366,7 @@ func (cl *lockstepCluster) addEquivocators(n int) *lockstepCluster {
366366
cl.addNode((&node{t: cl.t, i: i}).
367367
reuseSigner(cl.nodes[i-last].signer).
368368
withController().withSyncer().withPublisher().
369-
withClock().withDb().withAtx(cl.units.min, cl.units.max).
369+
withClock().withDb(cl.t).withAtx(cl.units.min, cl.units.max).
370370
withOracle().withHare())
371371
}
372372
return cl

hare3/malfeasance_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ type testMalfeasanceHandler struct {
2626
}
2727

2828
func newTestMalfeasanceHandler(tb testing.TB) *testMalfeasanceHandler {
29-
db := sql.InMemory()
29+
db := sql.InMemoryTest(tb)
3030
observer, observedLogs := observer.New(zapcore.WarnLevel)
3131
logger := zaptest.NewLogger(tb, zaptest.WrapOptions(zap.WrapCore(
3232
func(core zapcore.Core) zapcore.Core {

hare4/eligibility/oracle_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ type testOracle struct {
5353
}
5454

5555
func defaultOracle(tb testing.TB) *testOracle {
56-
db := sql.InMemory()
56+
db := sql.InMemoryTest(tb)
5757
atxsdata := atxsdata.New()
5858

5959
ctrl := gomock.NewController(tb)

hare4/hare_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,8 @@ func (n *node) reuseSigner(signer *signing.EdSigner) *node {
159159
return n
160160
}
161161

162-
func (n *node) withDb() *node {
163-
n.db = sql.InMemory()
162+
func (n *node) withDb(tb testing.TB) *node {
163+
n.db = sql.InMemoryTest(tb)
164164
n.atxsdata = atxsdata.New()
165165
n.proposals = store.New()
166166
return n
@@ -391,7 +391,7 @@ func (cl *lockstepCluster) addActive(n int) *lockstepCluster {
391391
for i := last; i < last+n; i++ {
392392
nn := (&node{t: cl.t, i: i}).
393393
withController().withSyncer().withPublisher().
394-
withClock().withDb().withSigner().withAtx(cl.units.min, cl.units.max).
394+
withClock().withDb(cl.t).withSigner().withAtx(cl.units.min, cl.units.max).
395395
withStreamRequester().withOracle().withHare()
396396
if cl.mockVerify {
397397
nn = nn.withVerifier()
@@ -406,7 +406,7 @@ func (cl *lockstepCluster) addInactive(n int) *lockstepCluster {
406406
for i := last; i < last+n; i++ {
407407
cl.addNode((&node{t: cl.t, i: i}).
408408
withController().withSyncer().withPublisher().
409-
withClock().withDb().withSigner().
409+
withClock().withDb(cl.t).withSigner().
410410
withStreamRequester().withOracle().withHare())
411411
}
412412
return cl
@@ -419,7 +419,7 @@ func (cl *lockstepCluster) addEquivocators(n int) *lockstepCluster {
419419
cl.addNode((&node{t: cl.t, i: i}).
420420
reuseSigner(cl.nodes[i-last].signer).
421421
withController().withSyncer().withPublisher().
422-
withClock().withDb().withAtx(cl.units.min, cl.units.max).
422+
withClock().withDb(cl.t).withAtx(cl.units.min, cl.units.max).
423423
withStreamRequester().withOracle().withHare())
424424
}
425425
return cl

sql/database.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"strings"
1212
"sync"
1313
"sync/atomic"
14+
"testing"
1415
"time"
1516

1617
sqlite "github.com/go-llsqlite/crawshaw"
@@ -176,6 +177,7 @@ func WithQueryCacheSizes(sizes map[QueryCacheKind]int) Opt {
176177
type Opt func(c *conf)
177178

178179
// InMemory database for testing.
180+
// Please use InMemoryTest for automatic closing of the returned db during `tb.Cleanup`.
179181
func InMemory(opts ...Opt) *Database {
180182
opts = append(opts, WithConnections(1))
181183
db, err := Open("file::memory:?mode=memory", opts...)
@@ -185,6 +187,17 @@ func InMemory(opts ...Opt) *Database {
185187
return db
186188
}
187189

190+
// InMemoryTest returns an in-mem database for testing and ensures database is closed during `tb.Cleanup`.
191+
func InMemoryTest(tb testing.TB, opts ...Opt) *Database {
192+
opts = append(opts, WithConnections(1))
193+
db, err := Open("file::memory:?mode=memory", opts...)
194+
if err != nil {
195+
panic(err)
196+
}
197+
tb.Cleanup(func() { db.Close() })
198+
return db
199+
}
200+
188201
// Open database with options.
189202
//
190203
// Database is opened in WAL mode and pragma synchronous=normal.

0 commit comments

Comments
 (0)