Skip to content

Commit

Permalink
Only mine on timer for replication tests (#1750)
Browse files Browse the repository at this point in the history
  • Loading branch information
sergekh2 authored Dec 7, 2024
1 parent 95286dc commit d876d2f
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 10 deletions.
17 changes: 11 additions & 6 deletions core/node/crypto/testutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,12 @@ func (w *autoMiningClientWrapper) SendTransaction(ctx context.Context, tx *types
}

type TestParams struct {
NumKeys int
MineOnTx bool
AutoMine bool
NoDeployer bool
NoOnChainConfig bool
NumKeys int
MineOnTx bool
AutoMine bool
AutoMineInterval time.Duration
NoDeployer bool
NoOnChainConfig bool
}

type BlockchainTestContext struct {
Expand Down Expand Up @@ -260,11 +261,15 @@ func NewBlockchainTestContext(ctx context.Context, params TestParams) (*Blockcha

if params.AutoMine && btc.Backend != nil {
go func() {
interval := params.AutoMineInterval
if interval == 0 {
interval = 50 * time.Millisecond
}
for {
select {
case <-ctx.Done():
return
case <-time.After(50 * time.Millisecond):
case <-time.After(interval):
_ = btc.mineBlock(ctx)
}
}
Expand Down
23 changes: 21 additions & 2 deletions core/node/rpc/repl_multiclient_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,30 @@ package rpc
import (
"fmt"
"testing"
"time"

"github.com/river-build/river/core/node/crypto"
//. "github.com/river-build/river/core/node/shared"
)

func newServiceTesterForReplication(t *testing.T) *serviceTester {
return newServiceTester(
t,
serviceTesterOpts{
numNodes: 5,
replicationFactor: 5,
start: true,
btcParams: &crypto.TestParams{
AutoMine: true,
AutoMineInterval: 200 * time.Millisecond,
MineOnTx: false,
},
},
)
}

func TestReplMulticlientSimple(t *testing.T) {
tt := newServiceTester(t, serviceTesterOpts{numNodes: 5, replicationFactor: 5, start: true})
tt := newServiceTesterForReplication(t)

alice := tt.newTestClient(0)

Expand Down Expand Up @@ -44,7 +63,7 @@ func TestReplMulticlientSimple(t *testing.T) {
}

func TestReplSpeakUntilMbTrim(t *testing.T) {
tt := newServiceTester(t, serviceTesterOpts{numNodes: 5, replicationFactor: 5, start: true})
tt := newServiceTesterForReplication(t)
require := tt.require

alice := tt.newTestClient(0)
Expand Down
11 changes: 9 additions & 2 deletions core/node/rpc/tester_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ type serviceTesterOpts struct {
numNodes int
replicationFactor int
start bool
btcParams *crypto.TestParams
}

func makeTestListenerNoCleanup(t *testing.T) (net.Listener, string) {
Expand Down Expand Up @@ -113,9 +114,15 @@ func newServiceTester(t *testing.T, opts serviceTesterOpts) *serviceTester {
// Cleanup context on test completion even if no other cleanups are registered.
st.cleanup(func() {})

btcParams := opts.btcParams
if btcParams == nil {
btcParams = &crypto.TestParams{NumKeys: opts.numNodes, MineOnTx: true, AutoMine: true}
} else if btcParams.NumKeys == 0 {
btcParams.NumKeys = opts.numNodes
}
btc, err := crypto.NewBlockchainTestContext(
st.ctx,
crypto.TestParams{NumKeys: opts.numNodes, MineOnTx: true, AutoMine: true},
*btcParams,
)
require.NoError(err)
st.btc = btc
Expand Down Expand Up @@ -783,7 +790,7 @@ func (tcs testClients) say(channelId StreamId, messages ...string) {
}, messages...)
}

// parallel calls each function for client with the same index in parallel.
// parallel spreads params over clients calling provided function in parallel.
func parallel[Params any](tcs testClients, f func(*testClient, Params), params ...Params) {
tcs[0].require.LessOrEqual(len(params), len(tcs))
resultC := make(chan int, len(params))
Expand Down

0 comments on commit d876d2f

Please sign in to comment.