Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
qdm12 committed Apr 6, 2022
1 parent 88a83d1 commit 23c4806
Show file tree
Hide file tree
Showing 15 changed files with 82 additions and 53 deletions.
5 changes: 3 additions & 2 deletions tests/polkadotjs_test/start_polkadotjs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,15 @@ func TestStartGossamerAndPolkadotAPI(t *testing.T) {
config := config.CreateDefault(t)

genesisPath := libutils.GetDevGenesisSpecPathTest(t)
n := node.New(t, node.SetGenesis(genesisPath), node.SetConfig(config))
n := node.New(t, node.SetBabeLead(true), node.SetWebsocket(true),
node.SetGenesis(genesisPath), node.SetConfig(config))

ctx, cancel := context.WithCancel(context.Background())
n.InitAndStartTest(ctx, t, cancel)

command := "npx mocha ./test --timeout 30000"
parts := strings.Fields(command)
data, err := exec.CommandContext(ctx, parts[0], parts[1:]...).Output()
data, err := exec.CommandContext(ctx, parts[0], parts[1:]...).CombinedOutput()
assert.NoError(t, err, string(data))

//uncomment this to see log results from javascript tests
Expand Down
27 changes: 15 additions & 12 deletions tests/rpc/rpc_01-system_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,14 @@ func TestSystemRPC(t *testing.T) {
}

var response modules.SystemHealthResponse
err := retry.UntilOK(ctx, time.Second, func() (ok bool) {
err := retry.UntilOK(ctx, time.Second, func() (ok bool, err error) {
getResponseCtx, getResponseCancel := context.WithTimeout(ctx, time.Second)
err := getResponse(getResponseCtx, method, params, &response)
err = getResponse(getResponseCtx, method, params, &response)
getResponseCancel()

require.NoError(t, err)
return response.Peers == expected.Peers
if err != nil {
return false, err
}
return response.Peers == expected.Peers, nil
})
require.NoError(t, err)

Expand All @@ -69,24 +70,26 @@ func TestSystemRPC(t *testing.T) {
const params = "{}"

var response modules.SystemPeersResponse
retry.UntilOK(ctx, time.Second, func() (ok bool) {
err := retry.UntilOK(ctx, time.Second, func() (ok bool, err error) {
getResponseCtx, getResponseCancel := context.WithTimeout(ctx, time.Second)
err := getResponse(getResponseCtx, method, params, &response)
err = getResponse(getResponseCtx, method, params, &response)
getResponseCancel()

require.NoError(t, err)
if err != nil {
return false, err // error and stop retrying
}

if len(response) != numberOfNodes-1 {
return false
return false, nil // retry
}

for _, peer := range response {
if peer.PeerID == "" {
return false
return false, nil // retry
}
}
return true
return true, nil // success, stop retrying
})
require.NoError(t, err)

// Check randomly generated peer IDs and clear this field
for i := range response {
Expand Down
18 changes: 14 additions & 4 deletions tests/rpc/rpc_02-author_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/ChainSafe/gossamer/tests/utils"
"github.com/ChainSafe/gossamer/tests/utils/config"
"github.com/ChainSafe/gossamer/tests/utils/node"
"github.com/ChainSafe/gossamer/tests/utils/retry"
gsrpc "github.com/centrifuge/go-substrate-rpc-client/v3"
"github.com/centrifuge/go-substrate-rpc-client/v3/signature"
"github.com/centrifuge/go-substrate-rpc-client/v3/types"
Expand All @@ -32,16 +33,25 @@ func TestAuthorSubmitExtrinsic(t *testing.T) {
genesisPath := libutils.GetDevGenesisSpecPathTest(t)
config := config.CreateDefault(t)

node := node.New(t, node.SetIndex(1),
node := node.New(t, node.SetBabeLead(true),
node.SetGenesis(genesisPath), node.SetConfig(config))
ctx, cancel := context.WithCancel(context.Background())
node.InitAndStartTest(ctx, t, cancel)

time.Sleep(30 * time.Second) // wait for server to start and block 1 to be produced

api, err := gsrpc.NewSubstrateAPI(fmt.Sprintf("http://localhost:%s", node.GetRPCPort()))
require.NoError(t, err)

// Wait for the first block to be produced.
const retryWait = time.Second
err = retry.UntilOK(ctx, retryWait, func() (ok bool, err error) {
block, err := api.RPC.Chain.GetBlockLatest()
if err != nil {
return false, err
}
return block.Block.Header.Number > 0, nil
})
require.NoError(t, err)

meta, err := api.RPC.State.GetMetadataLatest()
require.NoError(t, err)

Expand Down Expand Up @@ -135,7 +145,7 @@ func TestAuthorRPC(t *testing.T) {

genesisPath := libutils.GetGssmrGenesisRawPathTest(t)
config := config.CreateDefault(t)
node := node.New(t, node.SetIndex(1),
node := node.New(t, node.SetBabeLead(true),
node.SetGenesis(genesisPath), node.SetConfig(config))
ctx, cancel := context.WithCancel(context.Background())
node.InitAndStartTest(ctx, t, cancel)
Expand Down
3 changes: 2 additions & 1 deletion tests/rpc/rpc_03-chain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ func TestChainRPC(t *testing.T) {

genesisPath := libutils.GetDevGenesisSpecPathTest(t)
config := config.CreateDefault(t)
node := node.New(t, node.SetIndex(1), node.SetGenesis(genesisPath), node.SetConfig(config))
node := node.New(t, node.SetBabeLead(true),
node.SetGenesis(genesisPath), node.SetConfig(config))
ctx, cancel := context.WithCancel(context.Background())
node.InitAndStartTest(ctx, t, cancel)

Expand Down
2 changes: 1 addition & 1 deletion tests/rpc/rpc_04-offchain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func TestOffchainRPC(t *testing.T) {

genesisPath := libutils.GetGssmrGenesisRawPathTest(t)
config := config.CreateDefault(t)
node := node.New(t, node.SetIndex(1),
node := node.New(t, node.SetBabeLead(true),
node.SetGenesis(genesisPath), node.SetConfig(config))
ctx, cancel := context.WithCancel(context.Background())
node.InitAndStartTest(ctx, t, cancel)
Expand Down
6 changes: 3 additions & 3 deletions tests/rpc/rpc_05-state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func TestStateRPCResponseValidation(t *testing.T) {

genesisPath := libutils.GetGssmrGenesisRawPathTest(t)
config := config.CreateDefault(t)
node := node.New(t, node.SetIndex(1),
node := node.New(t, node.SetBabeLead(true),
node.SetGenesis(genesisPath), node.SetConfig(config))
ctx, cancel := context.WithCancel(context.Background())
node.InitAndStartTest(ctx, t, cancel)
Expand Down Expand Up @@ -145,7 +145,7 @@ func TestStateRPCAPI(t *testing.T) {

genesisPath := libutils.GetGssmrGenesisRawPathTest(t)
config := config.CreateDefault(t)
node := node.New(t, node.SetIndex(1),
node := node.New(t, node.SetBabeLead(true),
node.SetGenesis(genesisPath), node.SetConfig(config))
ctx, cancel := context.WithCancel(context.Background())
node.InitAndStartTest(ctx, t, cancel)
Expand Down Expand Up @@ -350,7 +350,7 @@ func TestRPCStructParamUnmarshal(t *testing.T) {

genesisPath := libutils.GetDevGenesisSpecPathTest(t)
config := config.CreateDefault(t)
node := node.New(t, node.SetIndex(1),
node := node.New(t, node.SetBabeLead(true),
node.SetGenesis(genesisPath), node.SetConfig(config))
ctx, cancel := context.WithCancel(context.Background())
node.InitAndStartTest(ctx, t, cancel)
Expand Down
2 changes: 1 addition & 1 deletion tests/rpc/rpc_06-engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func TestEngineRPC(t *testing.T) {

genesisPath := libutils.GetGssmrGenesisRawPathTest(t)
config := config.CreateDefault(t)
node := node.New(t, node.SetIndex(1),
node := node.New(t, node.SetBabeLead(true),
node.SetGenesis(genesisPath), node.SetConfig(config))
ctx, cancel := context.WithCancel(context.Background())
node.InitAndStartTest(ctx, t, cancel)
Expand Down
2 changes: 1 addition & 1 deletion tests/rpc/rpc_07-payment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func TestPaymentRPC(t *testing.T) {

genesisPath := libutils.GetGssmrGenesisRawPathTest(t)
config := config.CreateDefault(t)
node := node.New(t, node.SetIndex(1),
node := node.New(t, node.SetBabeLead(true),
node.SetGenesis(genesisPath), node.SetConfig(config))
ctx, cancel := context.WithCancel(context.Background())
node.InitAndStartTest(ctx, t, cancel)
Expand Down
2 changes: 1 addition & 1 deletion tests/rpc/rpc_08-contracts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func TestContractsRPC(t *testing.T) {

genesisPath := libutils.GetGssmrGenesisRawPathTest(t)
config := config.CreateDefault(t)
node := node.New(t, node.SetIndex(1),
node := node.New(t, node.SetBabeLead(true),
node.SetGenesis(genesisPath), node.SetConfig(config))
ctx, cancel := context.WithCancel(context.Background())
node.InitAndStartTest(ctx, t, cancel)
Expand Down
2 changes: 1 addition & 1 deletion tests/rpc/rpc_09-babe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func TestBabeRPC(t *testing.T) {

genesisPath := libutils.GetGssmrGenesisRawPathTest(t)
config := config.CreateDefault(t)
node := node.New(t, node.SetIndex(1),
node := node.New(t, node.SetBabeLead(true),
node.SetGenesis(genesisPath), node.SetConfig(config))
ctx, cancel := context.WithCancel(context.Background())
node.InitAndStartTest(ctx, t, cancel)
Expand Down
29 changes: 16 additions & 13 deletions tests/stress/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,37 +71,40 @@ func compareChainHeadsWithRetry(ctx context.Context, nodes node.Nodes,
}
}

var errBlockHashNotOne = errors.New("expected 1 block hash")

// compareBlocksByNumber calls getBlockByNumber for each node in the array
// it returns a map of block hashes to node key names, and an error if the hashes don't all match
func compareBlocksByNumber(ctx context.Context, t *testing.T, nodes node.Nodes,
num string) (nodeKeys []string) {
t.Helper()

func compareBlocksByNumber(ctx context.Context, nodes node.Nodes,
num string) (nodeKeys []string, err error) {
blockHashes := make(map[common.Hash]struct{}, 1)
for _, n := range nodes {
const retryWait = time.Second
err := retry.UntilOK(ctx, retryWait, func() (ok bool) {
err := retry.UntilOK(ctx, retryWait, func() (ok bool, err error) {
hash, err := rpc.GetBlockHash(ctx, n.GetRPCPort(), num)
if err != nil {
const blockDoesNotExistString = "cannot find node with number greater than highest in blocktree"
if strings.Contains(err.Error(), blockDoesNotExistString) {
return false // retry after retryWait has elapsed.
return false, nil // retry after retryWait has elapsed.
}
require.NoError(t, err) // fail the test now.
return false, err // stop retrying
}

blockHashes[hash] = struct{}{}
nodeKeys = append(nodeKeys, n.GetKey())
return true
return true, nil
})
require.NoError(t, err)
if err != nil {
return nil, fmt.Errorf("for node %s and block number %s: %w", n, num, err)
}
}

require.Lenf(t, blockHashes, 1,
"expected 1 block found for number %s but got %d block(s)",
num, len(blockHashes))
if len(blockHashes) != 1 {
return nil, fmt.Errorf("%w: but got %d block hashes for block number %s",
errBlockHashNotOne, len(blockHashes), num)
}

return nodeKeys
return nodeKeys, nil
}

// compareFinalizedHeads calls getFinalizedHeadByRound for each node in the array
Expand Down
20 changes: 13 additions & 7 deletions tests/stress/stress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,15 +121,16 @@ func TestSync_SingleBlockProducer(t *testing.T) {
nodes = append(nodes, babeLeadNode)
nodes = append(nodes, noAuthorityNodes...)

ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)

nodes.InitAndStartTest(ctx, t, cancel)

const blockNumbers = 10
for blockNumber := 0; blockNumber < blockNumbers; blockNumber++ {
t.Logf("comparing block number %d...", blockNumber)

nodeKeys := compareBlocksByNumber(ctx, t, nodes, fmt.Sprint(blockNumber))
nodeKeys, err := compareBlocksByNumber(ctx, nodes, fmt.Sprint(blockNumber))
require.NoError(t, err)
require.Equal(t, len(nodeKeys), numNodes)
}
}
Expand Down Expand Up @@ -193,7 +194,8 @@ func TestSync_MultipleEpoch(t *testing.T) {
const compareTimeout = 5 * time.Second
compareCtx, cancel := context.WithTimeout(ctx, compareTimeout)

_ = compareBlocksByNumber(compareCtx, t, nodes, fmt.Sprint(i))
_, err := compareBlocksByNumber(compareCtx, nodes, fmt.Sprint(i))
require.NoError(t, err)

cancel()
}
Expand Down Expand Up @@ -241,7 +243,8 @@ func TestSync_SingleSyncingNode(t *testing.T) {
const compareTimeout = 5 * time.Second
compareCtx, cancel := context.WithTimeout(ctx, compareTimeout)

_ = compareBlocksByNumber(compareCtx, t, nodes, fmt.Sprint(i))
_, err := compareBlocksByNumber(compareCtx, nodes, fmt.Sprint(i))
require.NoError(t, err)

cancel()
}
Expand Down Expand Up @@ -341,7 +344,8 @@ func TestSync_Bench(t *testing.T) {
const compareTimeout = 5 * time.Second
compareCtx, pauseBabeCancel := context.WithTimeout(ctx, compareTimeout)

_ = compareBlocksByNumber(compareCtx, t, nodes, fmt.Sprint(numBlocks))
_, err = compareBlocksByNumber(compareCtx, nodes, fmt.Sprint(numBlocks))
require.NoError(t, err)

pauseBabeCancel()

Expand Down Expand Up @@ -448,7 +452,8 @@ func TestSync_Restart(t *testing.T) {
const compareTimeout = 5 * time.Second
compareCtx, cancel := context.WithTimeout(mainCtx, compareTimeout)

_ = compareBlocksByNumber(compareCtx, t, nodes, fmt.Sprint(i))
_, err := compareBlocksByNumber(compareCtx, nodes, fmt.Sprint(i))
require.NoError(t, err)

cancel()

Expand Down Expand Up @@ -640,7 +645,8 @@ func TestSync_SubmitExtrinsic(t *testing.T) {
const compareTimeout = 5 * time.Second
compareCtx, cancel := context.WithTimeout(ctx, compareTimeout)

_ = compareBlocksByNumber(compareCtx, t, nodes, fmt.Sprint(extInBlock))
_, err = compareBlocksByNumber(compareCtx, nodes, fmt.Sprint(extInBlock))
require.NoError(t, err)

cancel()
}
Expand Down
4 changes: 3 additions & 1 deletion tests/utils/node/node_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build endtoend

// Copyright 2022 ChainSafe Systems (ON)
// SPDX-License-Identifier: LGPL-3.0-only

Expand All @@ -13,7 +15,7 @@ func Test_Node_InitAndStartTest(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
t.Cleanup(cancel)

n := New(t)
n := New(t, SetBabeLead(true))

n.InitAndStartTest(ctx, t, cancel)

Expand Down
4 changes: 2 additions & 2 deletions tests/utils/node/writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ func Test_prefixedWriter(t *testing.T) {
message := []byte("message\n")
n, err := prefixWriter.Write(message)
require.NoError(t, err)
expectedBytesWrittenCount := 16
expectedBytesWrittenCount := 8
assert.Equal(t, expectedBytesWrittenCount, n)
expectedWritten := "prefix: message\n"
assert.Equal(t, expectedWritten, writer.String())

message = []byte("message two\n")
n, err = prefixWriter.Write(message)
require.NoError(t, err)
expectedBytesWrittenCount = 20
expectedBytesWrittenCount = 12
assert.Equal(t, expectedBytesWrittenCount, n)
expectedWritten = "prefix: message\nprefix: message two\n"
assert.Equal(t, expectedWritten, writer.String())
Expand Down
9 changes: 6 additions & 3 deletions tests/utils/retry/untilok.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,21 @@ import (
"time"
)

// UntilOK retries the function `f` until it succeeds.
// UntilOK retries the function `f` until it returns a true
// value for `ok` or a non nil error.
// It waits `retryWait` after each failed call to `f`.
// If the context `ctx` is canceled, the function returns
// immediately an error stating the number of failed tries,
// for how long it retried and the context error.
func UntilOK(ctx context.Context, retryWait time.Duration,
f func() (ok bool)) (err error) {
f func() (ok bool, err error)) (err error) {
failedTries := 0
for ctx.Err() == nil {
ok := f()
ok, err := f()
if ok {
return nil
} else if err != nil {
return fmt.Errorf("stop retrying function: %w", err)
}

failedTries++
Expand Down

0 comments on commit 23c4806

Please sign in to comment.