Skip to content

Commit

Permalink
test(hare): add db cleanup (#6221)
Browse files Browse the repository at this point in the history
## Motivation

Adding a cleanup for the in-memory db we are using in hare.
  • Loading branch information
acud committed Aug 6, 2024
1 parent dcda406 commit 00d476f
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 12 deletions.
10 changes: 5 additions & 5 deletions hare3/hare_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,8 @@ func (n *node) reuseSigner(signer *signing.EdSigner) *node {
return n
}

func (n *node) withDb() *node {
n.db = sql.InMemory()
func (n *node) withDb(tb testing.TB) *node {
n.db = sql.InMemoryTest(tb)
n.atxsdata = atxsdata.New()
n.proposals = store.New()
return n
Expand Down Expand Up @@ -342,7 +342,7 @@ func (cl *lockstepCluster) addActive(n int) *lockstepCluster {
for i := last; i < last+n; i++ {
cl.addNode((&node{t: cl.t, i: i}).
withController().withSyncer().withPublisher().
withClock().withDb().withSigner().withAtx(cl.units.min, cl.units.max).
withClock().withDb(cl.t).withSigner().withAtx(cl.units.min, cl.units.max).
withOracle().withHare())
}
return cl
Expand All @@ -353,7 +353,7 @@ func (cl *lockstepCluster) addInactive(n int) *lockstepCluster {
for i := last; i < last+n; i++ {
cl.addNode((&node{t: cl.t, i: i}).
withController().withSyncer().withPublisher().
withClock().withDb().withSigner().
withClock().withDb(cl.t).withSigner().
withOracle().withHare())
}
return cl
Expand All @@ -366,7 +366,7 @@ func (cl *lockstepCluster) addEquivocators(n int) *lockstepCluster {
cl.addNode((&node{t: cl.t, i: i}).
reuseSigner(cl.nodes[i-last].signer).
withController().withSyncer().withPublisher().
withClock().withDb().withAtx(cl.units.min, cl.units.max).
withClock().withDb(cl.t).withAtx(cl.units.min, cl.units.max).
withOracle().withHare())
}
return cl
Expand Down
2 changes: 1 addition & 1 deletion hare3/malfeasance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type testMalfeasanceHandler struct {
}

func newTestMalfeasanceHandler(tb testing.TB) *testMalfeasanceHandler {
db := sql.InMemory()
db := sql.InMemoryTest(tb)
observer, observedLogs := observer.New(zapcore.WarnLevel)
logger := zaptest.NewLogger(tb, zaptest.WrapOptions(zap.WrapCore(
func(core zapcore.Core) zapcore.Core {
Expand Down
2 changes: 1 addition & 1 deletion hare4/eligibility/oracle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ type testOracle struct {
}

func defaultOracle(tb testing.TB) *testOracle {
db := sql.InMemory()
db := sql.InMemoryTest(tb)
atxsdata := atxsdata.New()

ctrl := gomock.NewController(tb)
Expand Down
10 changes: 5 additions & 5 deletions hare4/hare_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,8 @@ func (n *node) reuseSigner(signer *signing.EdSigner) *node {
return n
}

func (n *node) withDb() *node {
n.db = sql.InMemory()
func (n *node) withDb(tb testing.TB) *node {
n.db = sql.InMemoryTest(tb)
n.atxsdata = atxsdata.New()
n.proposals = store.New()
return n
Expand Down Expand Up @@ -391,7 +391,7 @@ func (cl *lockstepCluster) addActive(n int) *lockstepCluster {
for i := last; i < last+n; i++ {
nn := (&node{t: cl.t, i: i}).
withController().withSyncer().withPublisher().
withClock().withDb().withSigner().withAtx(cl.units.min, cl.units.max).
withClock().withDb(cl.t).withSigner().withAtx(cl.units.min, cl.units.max).
withStreamRequester().withOracle().withHare()
if cl.mockVerify {
nn = nn.withVerifier()
Expand All @@ -406,7 +406,7 @@ func (cl *lockstepCluster) addInactive(n int) *lockstepCluster {
for i := last; i < last+n; i++ {
cl.addNode((&node{t: cl.t, i: i}).
withController().withSyncer().withPublisher().
withClock().withDb().withSigner().
withClock().withDb(cl.t).withSigner().
withStreamRequester().withOracle().withHare())
}
return cl
Expand All @@ -419,7 +419,7 @@ func (cl *lockstepCluster) addEquivocators(n int) *lockstepCluster {
cl.addNode((&node{t: cl.t, i: i}).
reuseSigner(cl.nodes[i-last].signer).
withController().withSyncer().withPublisher().
withClock().withDb().withAtx(cl.units.min, cl.units.max).
withClock().withDb(cl.t).withAtx(cl.units.min, cl.units.max).
withStreamRequester().withOracle().withHare())
}
return cl
Expand Down
13 changes: 13 additions & 0 deletions sql/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"strings"
"sync"
"sync/atomic"
"testing"
"time"

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

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

// InMemoryTest returns an in-mem database for testing and ensures database is closed during `tb.Cleanup`.
func InMemoryTest(tb testing.TB, opts ...Opt) *Database {
opts = append(opts, WithConnections(1))
db, err := Open("file::memory:?mode=memory", opts...)
if err != nil {
panic(err)
}
tb.Cleanup(func() { db.Close() })
return db
}

// Open database with options.
//
// Database is opened in WAL mode and pragma synchronous=normal.
Expand Down

0 comments on commit 00d476f

Please sign in to comment.