Skip to content

Commit 9b7f35a

Browse files
authored
chore(tests/stress): refactor compareBlocksByNumber (#2370)
- Takes in context - Retry as long as context is not canceled - Assert errors and common values
1 parent 9c30149 commit 9b7f35a

File tree

4 files changed

+96
-75
lines changed

4 files changed

+96
-75
lines changed

tests/stress/empty.go

Lines changed: 0 additions & 4 deletions
This file was deleted.

tests/stress/errors.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,5 @@ import (
1010
var (
1111
errFinalizedBlockMismatch = errors.New("node finalised head hashes don't match")
1212
errNoFinalizedBlock = errors.New("did not finalise block for round")
13-
errNoBlockAtNumber = errors.New("no blocks found for given number")
14-
errBlocksAtNumberMismatch = errors.New("different blocks found for given number")
1513
errChainHeadMismatch = errors.New("node chain head hashes don't match")
1614
)

tests/stress/helpers.go

Lines changed: 40 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
package stress
55

66
import (
7+
"context"
78
"errors"
89
"fmt"
9-
"sync"
1010
"testing"
1111
"time"
1212

@@ -65,74 +65,60 @@ func compareChainHeadsWithRetry(t *testing.T, nodes []*utils.Node) error {
6565

6666
// compareBlocksByNumber calls getBlockByNumber for each node in the array
6767
// it returns a map of block hashes to node key names, and an error if the hashes don't all match
68-
func compareBlocksByNumber(t *testing.T, nodes []*utils.Node, num string) (map[common.Hash][]string, error) {
69-
hashes := make(map[common.Hash][]string)
70-
var errs []error
71-
var mapMu sync.Mutex
72-
var wg sync.WaitGroup
73-
wg.Add(len(nodes))
68+
func compareBlocksByNumber(ctx context.Context, t *testing.T, nodes []*utils.Node,
69+
num string) (hashToKeys map[common.Hash][]string) {
70+
type resultContainer struct {
71+
hash common.Hash
72+
nodeKey string
73+
err error
74+
}
75+
results := make(chan resultContainer)
7476

7577
for _, node := range nodes {
7678
go func(node *utils.Node) {
77-
logger.Debugf("calling chain_getBlockHash for node index %d", node.Idx)
78-
hash, err := utils.GetBlockHash(t, node, num)
79-
mapMu.Lock()
80-
defer func() {
81-
mapMu.Unlock()
82-
wg.Done()
83-
}()
84-
if err != nil {
85-
errs = append(errs, err)
86-
return
79+
result := resultContainer{
80+
nodeKey: node.Key,
8781
}
88-
logger.Debugf("got hash %s from node with key %s", hash, node.Key)
8982

90-
hashes[hash] = append(hashes[hash], node.Key)
91-
}(node)
92-
}
83+
for { // retry until context gets canceled
84+
result.hash, result.err = utils.GetBlockHash(t, node, num)
9385

94-
wg.Wait()
95-
96-
var err error
97-
if len(errs) != 0 {
98-
err = fmt.Errorf("%v", errs)
99-
}
86+
if err := ctx.Err(); err != nil {
87+
result.err = err
88+
break
89+
}
10090

101-
if len(hashes) == 0 {
102-
err = errNoBlockAtNumber
103-
}
91+
if result.err == nil {
92+
break
93+
}
94+
}
10495

105-
if len(hashes) > 1 {
106-
err = errBlocksAtNumberMismatch
96+
results <- result
97+
}(node)
10798
}
10899

109-
return hashes, err
110-
}
111-
112-
// compareBlocksByNumberWithRetry calls compareChainHeads, retrying up to maxRetries times if it errors.
113-
func compareBlocksByNumberWithRetry(t *testing.T, nodes []*utils.Node, num string) (map[common.Hash][]string, error) {
114-
var hashes map[common.Hash][]string
115100
var err error
101+
hashToKeys = make(map[common.Hash][]string, len(nodes))
102+
for range nodes {
103+
result := <-results
104+
if err != nil {
105+
continue // one failed, we don't care anymore
106+
}
116107

117-
timeout := time.After(120 * time.Second)
118-
doneBlockProduction:
119-
for {
120-
time.Sleep(time.Second)
121-
select {
122-
case <-timeout:
123-
break doneBlockProduction
124-
default:
125-
hashes, err = compareBlocksByNumber(t, nodes, num)
126-
if err == nil {
127-
break doneBlockProduction
128-
}
108+
if result.err != nil {
109+
err = result.err
110+
continue
129111
}
130-
}
131112

132-
if err != nil {
133-
err = fmt.Errorf("%w: hashes=%v", err, hashes)
113+
hashToKeys[result.hash] = append(hashToKeys[result.hash], result.nodeKey)
134114
}
135-
return hashes, err
115+
116+
require.NoError(t, err)
117+
require.Lenf(t, hashToKeys, 1,
118+
"expected 1 block found for number %s but got %d block(s)",
119+
num, len(hashToKeys))
120+
121+
return hashToKeys
136122
}
137123

138124
// compareFinalizedHeads calls getFinalizedHeadByRound for each node in the array

tests/stress/stress_test.go

Lines changed: 56 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package stress
55

66
import (
7+
"context"
78
"fmt"
89
"math/big"
910
"math/rand"
@@ -111,14 +112,18 @@ func TestSync_SingleBlockProducer(t *testing.T) {
111112
}()
112113

113114
numCmps := 10
115+
ctx := context.Background()
116+
114117
for i := 0; i < numCmps; i++ {
115118
time.Sleep(3 * time.Second)
116119
t.Log("comparing...", i)
117-
hashes, err := compareBlocksByNumberWithRetry(t, nodes, strconv.Itoa(i))
118-
if len(hashes) > 1 || len(hashes) == 0 {
119-
require.NoError(t, err, i)
120-
continue
121-
}
120+
121+
const comparisonTimeout = 5 * time.Second
122+
compareCtx, cancel := context.WithTimeout(ctx, comparisonTimeout)
123+
124+
hashes := compareBlocksByNumber(compareCtx, t, nodes, strconv.Itoa(i))
125+
126+
cancel()
122127

123128
// there will only be one key in the mapping
124129
for _, nodesWithHash := range hashes {
@@ -163,13 +168,20 @@ func TestSync_MultipleEpoch(t *testing.T) {
163168
// Wait for epoch to pass
164169
time.Sleep(time.Duration(uint64(slotDuration.Nanoseconds()) * epochLength))
165170

171+
ctx := context.Background()
172+
166173
// Just checking that everythings operating as expected
167174
header := utils.GetChainHead(t, nodes[0])
168175
currentHeight := header.Number
169176
for i := uint(0); i < currentHeight; i++ {
170177
t.Log("comparing...", i)
171-
_, err = compareBlocksByNumberWithRetry(t, nodes, strconv.Itoa(int(i)))
172-
require.NoError(t, err, i)
178+
179+
const compareTimeout = 5 * time.Second
180+
compareCtx, cancel := context.WithTimeout(ctx, compareTimeout)
181+
182+
_ = compareBlocksByNumber(compareCtx, t, nodes, strconv.Itoa(int(i)))
183+
184+
cancel()
173185
}
174186
}
175187

@@ -197,11 +209,18 @@ func TestSync_SingleSyncingNode(t *testing.T) {
197209
require.Len(t, errList, 0)
198210
}()
199211

212+
ctx := context.Background()
213+
200214
numCmps := 100
201215
for i := 0; i < numCmps; i++ {
202216
t.Log("comparing...", i)
203-
_, err = compareBlocksByNumberWithRetry(t, nodes, strconv.Itoa(i))
204-
require.NoError(t, err, i)
217+
218+
const compareTimeout = 5 * time.Second
219+
compareCtx, cancel := context.WithTimeout(ctx, compareTimeout)
220+
221+
_ = compareBlocksByNumber(compareCtx, t, nodes, strconv.Itoa(i))
222+
223+
cancel()
205224
}
206225
}
207226

@@ -277,8 +296,16 @@ func TestSync_Bench(t *testing.T) {
277296

278297
// assert block is correct
279298
t.Log("comparing block...", numBlocks)
280-
_, err = compareBlocksByNumberWithRetry(t, nodes, fmt.Sprint(numBlocks))
281-
require.NoError(t, err, numBlocks)
299+
300+
ctx := context.Background()
301+
302+
const compareTimeout = 5 * time.Second
303+
compareCtx, cancel := context.WithTimeout(ctx, compareTimeout)
304+
305+
_ = compareBlocksByNumber(compareCtx, t, nodes, fmt.Sprint(numBlocks))
306+
307+
cancel()
308+
282309
time.Sleep(time.Second * 3)
283310
}
284311

@@ -328,11 +355,19 @@ func TestSync_Restart(t *testing.T) {
328355
}
329356
}()
330357

358+
ctx := context.Background()
359+
331360
numCmps := 12
332361
for i := 0; i < numCmps; i++ {
333362
t.Log("comparing...", i)
334-
_, err = compareBlocksByNumberWithRetry(t, nodes, strconv.Itoa(i))
335-
require.NoError(t, err, i)
363+
364+
const compareTimeout = 5 * time.Second
365+
compareCtx, cancel := context.WithTimeout(ctx, compareTimeout)
366+
367+
_ = compareBlocksByNumber(compareCtx, t, nodes, strconv.Itoa(i))
368+
369+
cancel()
370+
336371
time.Sleep(time.Second * 5)
337372
}
338373
close(done)
@@ -476,8 +511,14 @@ func TestSync_SubmitExtrinsic(t *testing.T) {
476511

477512
require.True(t, included)
478513

479-
hashes, err := compareBlocksByNumberWithRetry(t, nodes, fmt.Sprint(extInBlock))
480-
require.NoError(t, err, hashes)
514+
ctx := context.Background()
515+
516+
const compareTimeout = 5 * time.Second
517+
compareCtx, cancel := context.WithTimeout(ctx, compareTimeout)
518+
519+
_ = compareBlocksByNumber(compareCtx, t, nodes, fmt.Sprint(extInBlock))
520+
521+
cancel()
481522
}
482523

483524
func Test_SubmitAndWatchExtrinsic(t *testing.T) {

0 commit comments

Comments
 (0)