Skip to content

Commit

Permalink
Create DB only for the node being tested
Browse files Browse the repository at this point in the history
  • Loading branch information
charithabandi authored and jchappelow committed Dec 4, 2024
1 parent 1fcf5dc commit 3f74b6a
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 40 deletions.
73 changes: 37 additions & 36 deletions node/consensus/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"fmt"
"os"
"path/filepath"
"slices"
"sync"
"testing"
"time"
Expand All @@ -34,7 +33,10 @@ import (
"github.com/stretchr/testify/require"
)

func generateTestCEConfig(t *testing.T, nodes int, noDBs ...int) []*Config {
// leaderDB is set assigns DB to the leader, else DB is assigned to the follower
// Most of these tests expect only one working node instance either a leader or the
// first validator and all other nodes interactions are mocked out.
func generateTestCEConfig(t *testing.T, nodes int, leaderDB bool) []*Config {
ceConfigs := make([]*Config, nodes)
tempDir := t.TempDir()

Expand Down Expand Up @@ -75,28 +77,25 @@ func generateTestCEConfig(t *testing.T, nodes int, noDBs ...int) []*Config {
// assert.NoError(t, err)
txapp := newDummyTxApp(valSet)

for i := range nodes {
var db *pg.DB
if !slices.Contains(noDBs, i) {
db = dbtest.NewTestDBNamed(t, "kwil_test_db", 5432+i, func(db *pg.DB) {
db.AutoCommit(true)
ctx := context.Background()
db.Execute(ctx, `DROP SCHEMA IF EXISTS kwild_chain CASCADE;`)
db.Execute(ctx, `DROP SCHEMA IF EXISTS kwild_internal CASCADE;`)
})
db := dbtest.NewTestDB(t, func(db *pg.DB) {
db.AutoCommit(true)
ctx := context.Background()
db.Execute(ctx, `DROP SCHEMA IF EXISTS kwild_chain CASCADE;`)
db.Execute(ctx, `DROP SCHEMA IF EXISTS kwild_internal CASCADE;`)
})

func() {
tx, err := db.BeginTx(ctx)
require.NoError(t, err)
defer tx.Rollback(ctx)
func() {
tx, err := db.BeginTx(ctx)
require.NoError(t, err)
defer tx.Rollback(ctx)

err = meta.InitializeMetaStore(ctx, tx)
require.NoError(t, err)
err = meta.InitializeMetaStore(ctx, tx)
require.NoError(t, err)

require.NoError(t, tx.Commit(ctx))
}()
}
require.NoError(t, tx.Commit(ctx))
}()

for i := range nodes {
nodeStr := fmt.Sprintf("NODE%d", i)
nodeDir := filepath.Join(tempDir, nodeStr)

Expand All @@ -107,7 +106,6 @@ func generateTestCEConfig(t *testing.T, nodes int, noDBs ...int) []*Config {
assert.NoError(t, err)

ceConfigs[i] = &Config{
DB: db,
PrivateKey: privKeys[i],
Leader: pubKeys[0],
Mempool: mempool.New(),
Expand All @@ -119,15 +117,18 @@ func generateTestCEConfig(t *testing.T, nodes int, noDBs ...int) []*Config {
Logger: logger,
ProposeTimeout: 1 * time.Second,
}
// if i == 0 {
// ceConfigs[i].DB = db
// } // only ce 0 (leader) has a valid DB, others are for identity of simulated peer

closers = append(closers, func() {
bs.Close()
})
}

if leaderDB {
ceConfigs[0].DB = db
} else {
ceConfigs[1].DB = db
}

t.Cleanup(func() {
for _, closerFn := range closers {
closerFn()
Expand Down Expand Up @@ -187,7 +188,7 @@ func TestValidatorStateMachine(t *testing.T) {
{
name: "BlkPropAndCommit",
setup: func(t *testing.T) []*Config {
return generateTestCEConfig(t, 2)
return generateTestCEConfig(t, 2, false)
},
actions: []action{
{
Expand Down Expand Up @@ -215,7 +216,7 @@ func TestValidatorStateMachine(t *testing.T) {
{
name: "InvalidAppHash",
setup: func(t *testing.T) []*Config {
return generateTestCEConfig(t, 2)
return generateTestCEConfig(t, 2, false)
},
actions: []action{
{
Expand Down Expand Up @@ -249,7 +250,7 @@ func TestValidatorStateMachine(t *testing.T) {
{
name: "MultipleBlockProposals",
setup: func(t *testing.T) []*Config {
return generateTestCEConfig(t, 2)
return generateTestCEConfig(t, 2, false)
},
actions: []action{
{
Expand Down Expand Up @@ -285,7 +286,7 @@ func TestValidatorStateMachine(t *testing.T) {
{
name: "StaleBlockProposals",
setup: func(t *testing.T) []*Config {
return generateTestCEConfig(t, 2)
return generateTestCEConfig(t, 2, false)
},
actions: []action{
{
Expand Down Expand Up @@ -322,7 +323,7 @@ func TestValidatorStateMachine(t *testing.T) {
{
name: "BlkAnnounceBeforeBlockProp",
setup: func(t *testing.T) []*Config {
return generateTestCEConfig(t, 2)
return generateTestCEConfig(t, 2, false)
},
actions: []action{
{
Expand Down Expand Up @@ -359,7 +360,7 @@ func TestValidatorStateMachine(t *testing.T) {
{
name: "ValidResetFlow",
setup: func(t *testing.T) []*Config {
return generateTestCEConfig(t, 2)
return generateTestCEConfig(t, 2, false)
},
actions: []action{
{
Expand Down Expand Up @@ -405,7 +406,7 @@ func TestValidatorStateMachine(t *testing.T) {
{
name: "ResetAfterCommit(Ignored)",
setup: func(t *testing.T) []*Config {
return generateTestCEConfig(t, 2)
return generateTestCEConfig(t, 2, false)
},
actions: []action{
{
Expand Down Expand Up @@ -442,7 +443,7 @@ func TestValidatorStateMachine(t *testing.T) {
{
name: "DuplicateReset",
setup: func(t *testing.T) []*Config {
return generateTestCEConfig(t, 2)
return generateTestCEConfig(t, 2, false)
},
actions: []action{
{
Expand Down Expand Up @@ -488,7 +489,7 @@ func TestValidatorStateMachine(t *testing.T) {
{
name: "InvalidFutureResetHeight",
setup: func(t *testing.T) []*Config {
return generateTestCEConfig(t, 2)
return generateTestCEConfig(t, 2, false)
},
actions: []action{
{
Expand Down Expand Up @@ -575,7 +576,7 @@ func TestValidatorStateMachine(t *testing.T) {

func TestCELeaderSingleNode(t *testing.T) {
// t.Parallel()
ceConfigs := generateTestCEConfig(t, 1)
ceConfigs := generateTestCEConfig(t, 1, true)

// bring up the node
leader := New(ceConfigs[0])
Expand All @@ -600,7 +601,7 @@ func TestCELeaderSingleNode(t *testing.T) {

func TestCELeaderTwoNodesMajorityAcks(t *testing.T) {
// Majority > n/2 -> 2
ceConfigs := generateTestCEConfig(t, 2)
ceConfigs := generateTestCEConfig(t, 2, true)

// bring up the nodes
n1 := New(ceConfigs[0])
Expand Down Expand Up @@ -656,7 +657,7 @@ func TestCELeaderTwoNodesMajorityAcks(t *testing.T) {

func TestCELeaderTwoNodesMajorityNacks(t *testing.T) {
// Majority > n/2 -> 2
ceConfigs := generateTestCEConfig(t, 3)
ceConfigs := generateTestCEConfig(t, 3, true)

// bring up the nodes
n1 := New(ceConfigs[0])
Expand Down
3 changes: 1 addition & 2 deletions node/versioning/versioning_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"context"
"testing"

"github.com/kwilteam/kwil-db/node/pg"
"github.com/kwilteam/kwil-db/node/types/sql"

dbtest "github.com/kwilteam/kwil-db/node/pg/test"
Expand All @@ -25,7 +24,7 @@ func Test_Versioning(t *testing.T) {
1: upgradeSchemaV0ToV1,
2: upgradeSchemaV1ToV2,
}
db := dbtest.NewTestDB(t, func(db *pg.DB) {})
db := dbtest.NewTestDB(t, nil)

tx, err := db.BeginTx(ctx)
require.NoError(t, err)
Expand Down
3 changes: 1 addition & 2 deletions node/voting/vote_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (

"github.com/kwilteam/kwil-db/core/types"
"github.com/kwilteam/kwil-db/extensions/resolutions"
"github.com/kwilteam/kwil-db/node/pg"
dbtest "github.com/kwilteam/kwil-db/node/pg/test"
"github.com/kwilteam/kwil-db/node/types/sql"

Expand Down Expand Up @@ -311,7 +310,7 @@ func Test_Voting(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
ctx := context.Background()

db := dbtest.NewTestDB(t, func(db *pg.DB) {})
db := dbtest.NewTestDB(t, nil)

dbTx, err := db.BeginTx(ctx)
require.NoError(t, err)
Expand Down

0 comments on commit 3f74b6a

Please sign in to comment.