Skip to content

Commit

Permalink
chore(e2e): remove maxTries in stress tests
Browse files Browse the repository at this point in the history
  • Loading branch information
qdm12 committed Apr 6, 2022
1 parent f02da5d commit 39ec48b
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 43 deletions.
13 changes: 9 additions & 4 deletions tests/stress/grandpa_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,9 @@ func TestStress_Grandpa_ThreeAuthorities(t *testing.T) {
numRounds := 5
for i := 1; i < numRounds+1; i++ {
const getFinalizedHeadByRoundTimeout = time.Second
const retryWait = time.Second
fin, err := compareFinalizedHeadsWithRetry(ctx,
nodes, uint64(i), getFinalizedHeadByRoundTimeout)
nodes, uint64(i), getFinalizedHeadByRoundTimeout, retryWait)
require.NoError(t, err)
t.Logf("finalised hash in round %d: %s", i, fin)
}
Expand All @@ -79,8 +80,9 @@ func TestStress_Grandpa_SixAuthorities(t *testing.T) {
numRounds := 10
for i := 1; i < numRounds+1; i++ {
const getFinalizedHeadByRoundTimeout = time.Second
const retryWait = time.Second
fin, err := compareFinalizedHeadsWithRetry(ctx, nodes,
uint64(i), getFinalizedHeadByRoundTimeout)
uint64(i), getFinalizedHeadByRoundTimeout, retryWait)
require.NoError(t, err)
t.Logf("finalised hash in round %d: %s", i, fin)
}
Expand All @@ -103,8 +105,9 @@ func TestStress_Grandpa_NineAuthorities(t *testing.T) {
numRounds := 3
for i := 1; i < numRounds+1; i++ {
const getFinalizedHeadByRoundTimeout = time.Second
const retryWait = time.Second
fin, err := compareFinalizedHeadsWithRetry(ctx, nodes,
uint64(i), getFinalizedHeadByRoundTimeout)
uint64(i), getFinalizedHeadByRoundTimeout, retryWait)
require.NoError(t, err)
t.Logf("finalised hash in round %d: %s", i, fin)
}
Expand Down Expand Up @@ -136,7 +139,9 @@ func TestStress_Grandpa_CatchUp(t *testing.T) {
numRounds := 10
for i := 1; i < numRounds+1; i++ {
const getFinalizedHeadByRoundTimeout = time.Second
fin, err := compareFinalizedHeadsWithRetry(ctx, nodes, uint64(i), getFinalizedHeadByRoundTimeout)
const retryWait = time.Second
fin, err := compareFinalizedHeadsWithRetry(ctx, nodes, uint64(i),
getFinalizedHeadByRoundTimeout, retryWait)
require.NoError(t, err)
t.Logf("finalised hash in round %d: %s", i, fin)
}
Expand Down
60 changes: 26 additions & 34 deletions tests/stress/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
)

var (
maxRetries = 32
testTimeout = time.Minute * 3
logger = log.NewFromGlobal(log.AddContext("pkg", "tests/stress"))
)
Expand Down Expand Up @@ -53,13 +52,10 @@ func compareChainHeads(ctx context.Context, nodes node.Nodes,
// retrying until the context gets canceled.
func compareChainHeadsWithRetry(ctx context.Context, nodes node.Nodes,
getChainHeadTimeout time.Duration) error {
var hashes map[common.Hash][]string
var err error

for i := 0; i < maxRetries; i++ {
hashes, err = compareChainHeads(ctx, nodes, getChainHeadTimeout)
for {
hashes, err := compareChainHeads(ctx, nodes, getChainHeadTimeout)
if err == nil {
break
return nil
}

timer := time.NewTimer(time.Second)
Expand All @@ -69,15 +65,9 @@ func compareChainHeadsWithRetry(ctx context.Context, nodes node.Nodes,
if !timer.Stop() {
<-timer.C
}
return err // last error
return fmt.Errorf("%w: hashes=%v", err, hashes) // last error
}
}

if err != nil {
err = fmt.Errorf("%w: hashes=%v", err, hashes)
}

return err
}

// compareBlocksByNumber calls getBlockByNumber for each node in the array
Expand Down Expand Up @@ -194,34 +184,36 @@ func compareFinalizedHeadsByRound(ctx context.Context, nodes node.Nodes,
return hashes, err
}

// compareFinalizedHeadsWithRetry calls compareFinalizedHeadsByRound, retrying up to maxRetries times if it errors.
// it returns the finalised hash if it succeeds
// compareFinalizedHeadsWithRetry calls compareFinalizedHeadsByRound,
// retrying until the context is canceled or times out.
// It returns the finalised hash if it succeeds
func compareFinalizedHeadsWithRetry(ctx context.Context, nodes node.Nodes, round uint64,
getFinalizedHeadByRoundTimeout time.Duration) (hash common.Hash, err error) {
var hashes map[common.Hash][]string

for i := 0; i < maxRetries; i++ {
hashes, err = compareFinalizedHeadsByRound(ctx, nodes, round, getFinalizedHeadByRoundTimeout)
getFinalizedHeadByRoundTimeout, retryWait time.Duration) (hashes []common.Hash, err error) {
for {
hashToKeys, err := compareFinalizedHeadsByRound(ctx, nodes, round, getFinalizedHeadByRoundTimeout)
if err == nil {
break
hashes = make([]common.Hash, 0, len(hashToKeys))
for hash := range hashToKeys {
hashes = append(hashes, hash)
}
return hashes, nil
}

if errors.Is(err, errFinalizedBlockMismatch) {
return common.Hash{}, fmt.Errorf("%w: round=%d hashes=%v", err, round, hashes)
return nil, fmt.Errorf("%w: round=%d hash-to-keys=%v", err, round, hashToKeys)
}

time.Sleep(3 * time.Second)
}

if err != nil {
return common.Hash{}, fmt.Errorf("%w: round=%d hashes=%v", err, round, hashes)
}

for h := range hashes {
return h, nil
timer := time.NewTimer(retryWait)
select {
case <-timer.C:
case <-ctx.Done():
if !timer.Stop() {
<-timer.C
}
return nil, fmt.Errorf("%w: (%s) round=%d hash-to-keys=%v",
err, ctx.Err(), round, hashToKeys)
}
}

return common.Hash{}, nil
}

func getPendingExtrinsics(ctx context.Context, t *testing.T, node node.Node) []string {
Expand Down
25 changes: 20 additions & 5 deletions tests/stress/stress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -579,16 +579,27 @@ func TestSync_SubmitExtrinsic(t *testing.T) {
time.Sleep(time.Second * 20)

// wait until there's no more pending extrinsics
for i := 0; i < maxRetries; i++ {
getPendingExtsCtx, getPendingExtsCancel := context.WithTimeout(ctx, time.Second)
const waitNoExtTimeout = 30 * time.Second
waitNoExtCtx, waitNoExtCancel := context.WithTimeout(ctx, waitNoExtTimeout)
for {
getPendingExtsCtx, getPendingExtsCancel := context.WithTimeout(waitNoExtCtx, time.Second)
exts := getPendingExtrinsics(getPendingExtsCtx, t, nodes[idx])
getPendingExtsCancel()

if len(exts) == 0 {
waitNoExtCancel()
break
}

time.Sleep(time.Second)
timer := time.NewTimer(time.Second)
select {
case <-timer.C:
case <-waitNoExtCtx.Done():
if !timer.Stop() {
<-timer.C
}
require.NoError(t, waitNoExtCtx.Err())
}
}

getChainHeadCtx, getChainHeadCancel = context.WithTimeout(ctx, time.Second)
Expand All @@ -602,10 +613,13 @@ func TestSync_SubmitExtrinsic(t *testing.T) {
extInBlock uint
)

for i := 0; i < maxRetries; i++ {
getBlockCtx, getBlockCancel := context.WithTimeout(ctx, time.Second)
const extrinsicSearchTimeout = 10 * time.Second
extrinsicSearchCtx, extrinsicSearchCancel := context.WithTimeout(ctx, extrinsicSearchTimeout)
for {
getBlockCtx, getBlockCancel := context.WithTimeout(extrinsicSearchCtx, time.Second)
block, err := rpc.GetBlock(getBlockCtx, nodes[idx].GetRPCPort(), header.ParentHash)
getBlockCancel()

require.NoError(t, err)

if block == nil {
Expand All @@ -622,6 +636,7 @@ func TestSync_SubmitExtrinsic(t *testing.T) {
logger.Debugf("extrinsics: %v", resExts)
if len(resExts) >= 2 {
extInBlock = block.Header.Number
extrinsicSearchCancel()
break
}
}
Expand Down

0 comments on commit 39ec48b

Please sign in to comment.