Browse Source

test: fix non-deterministic backfill test (#6648)

pull/6654/head
Callum Waters 3 years ago
committed by GitHub
parent
commit
a1e1e6c290
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 7 deletions
  1. +6
    -1
      internal/statesync/reactor.go
  2. +7
    -6
      internal/statesync/reactor_test.go

+ 6
- 1
internal/statesync/reactor.go View File

@ -310,12 +310,17 @@ func (r *Reactor) backfill(
// waiting on blocks. If it takes 4s to retrieve a block and 1s to verify // waiting on blocks. If it takes 4s to retrieve a block and 1s to verify
// it, then steady state involves four workers. // it, then steady state involves four workers.
for i := 0; i < int(r.cfg.Fetchers); i++ { for i := 0; i < int(r.cfg.Fetchers); i++ {
ctxWithCancel, cancel := context.WithCancel(ctx)
defer cancel()
go func() { go func() {
for { for {
select { select {
case height := <-queue.nextHeight(): case height := <-queue.nextHeight():
r.Logger.Debug("fetching next block", "height", height) r.Logger.Debug("fetching next block", "height", height)
lb, peer, err := r.dispatcher.LightBlock(ctx, height)
lb, peer, err := r.dispatcher.LightBlock(ctxWithCancel, height)
if errors.Is(err, context.Canceled) {
return
}
if err != nil { if err != nil {
queue.retry(height) queue.retry(height)
if errors.Is(err, errNoConnectedPeers) { if errors.Is(err, errNoConnectedPeers) {


+ 7
- 6
internal/statesync/reactor_test.go View File

@ -3,12 +3,11 @@ package statesync
import ( import (
"context" "context"
"fmt" "fmt"
"math/rand"
"sync" "sync"
"testing" "testing"
"time" "time"
// "github.com/fortytw2/leaktest"
"github.com/fortytw2/leaktest"
"github.com/stretchr/testify/mock" "github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
dbm "github.com/tendermint/tm-db" dbm "github.com/tendermint/tm-db"
@ -421,11 +420,11 @@ func TestReactor_Dispatcher(t *testing.T) {
func TestReactor_Backfill(t *testing.T) { func TestReactor_Backfill(t *testing.T) {
// test backfill algorithm with varying failure rates [0, 10] // test backfill algorithm with varying failure rates [0, 10]
failureRates := []int{0, 3, 9}
failureRates := []int{0, 2, 9}
for _, failureRate := range failureRates { for _, failureRate := range failureRates {
failureRate := failureRate failureRate := failureRate
t.Run(fmt.Sprintf("failure rate: %d", failureRate), func(t *testing.T) { t.Run(fmt.Sprintf("failure rate: %d", failureRate), func(t *testing.T) {
// t.Cleanup(leaktest.Check(t))
t.Cleanup(leaktest.CheckTimeout(t, 1*time.Minute))
rts := setup(t, nil, nil, nil, 21) rts := setup(t, nil, nil, nil, 21)
var ( var (
@ -467,7 +466,7 @@ func TestReactor_Backfill(t *testing.T) {
factory.MakeBlockIDWithHash(chain[startHeight].Header.Hash()), factory.MakeBlockIDWithHash(chain[startHeight].Header.Hash()),
stopTime, stopTime,
) )
if failureRate > 5 {
if failureRate > 3 {
require.Error(t, err) require.Error(t, err)
} else { } else {
require.NoError(t, err) require.NoError(t, err)
@ -506,6 +505,7 @@ func handleLightBlockRequests(t *testing.T,
close chan struct{}, close chan struct{},
failureRate int) { failureRate int) {
requests := 0 requests := 0
errorCount := 0
for { for {
select { select {
case envelope := <-receiving: case envelope := <-receiving:
@ -520,7 +520,7 @@ func handleLightBlockRequests(t *testing.T,
}, },
} }
} else { } else {
switch rand.Intn(3) {
switch errorCount % 3 {
case 0: // send a different block case 0: // send a different block
differntLB, err := mockLB(t, int64(msg.Height), factory.DefaultTestTime, factory.MakeBlockID()).ToProto() differntLB, err := mockLB(t, int64(msg.Height), factory.DefaultTestTime, factory.MakeBlockID()).ToProto()
require.NoError(t, err) require.NoError(t, err)
@ -539,6 +539,7 @@ func handleLightBlockRequests(t *testing.T,
} }
case 2: // don't do anything case 2: // don't do anything
} }
errorCount++
} }
} }
case <-close: case <-close:


Loading…
Cancel
Save