Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: fix flaky tests + new method RetryForBlocks #14609

Merged
merged 19 commits into from
Jan 17, 2023
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion client/rpc/rpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package rpc_test
import (
"context"
"fmt"
"strconv"
"testing"

"github.com/stretchr/testify/suite"
Expand Down Expand Up @@ -61,7 +62,9 @@ func (s *IntegrationTestSuite) TestCLIQueryConn() {
s.NoError(err)

blockHeight := header.Get(grpctypes.GRPCBlockHeightHeader)
s.Require().Equal([]string{"1"}, blockHeight)
height, err := strconv.Atoi(blockHeight[0])
s.Require().NoError(err)
s.Require().GreaterOrEqual(height, 1) // at least the 1st block

s.Equal("hello", res.Message)
}
Expand Down
31 changes: 23 additions & 8 deletions tests/e2e/auth/suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,6 @@ func (s *E2ETestSuite) TestCLISignBatch() {
)
s.Require().NoError(err)
s.Require().NoError(s.network.WaitForNextBlock())
s.Require().NoError(s.network.WaitForNextBlock())

// fetch the sequence after a tx, should be incremented.
_, seq1, err := val.ClientCtx.AccountRetriever.GetAccountNumberSequence(val.ClientCtx, val.Address)
Expand Down Expand Up @@ -530,9 +529,15 @@ func (s *E2ETestSuite) TestCLIQueryTxCmdByHash() {
s.Run(tc.name, func() {
cmd := authcli.QueryTxCmd()
clientCtx := val.ClientCtx
var (
out testutil.BufferWriter
err error
)

out, err := clitestutil.ExecTestCLICmd(clientCtx, cmd, tc.args)

err = s.network.RetryForBlocks(func() error {
out, err = clitestutil.ExecTestCLICmd(clientCtx, cmd, tc.args)
return err
}, 2)
if tc.expectErr {
s.Require().Error(err)
s.Require().NotEqual("internal", err.Error())
Expand Down Expand Up @@ -568,7 +573,10 @@ func (s *E2ETestSuite) TestCLIQueryTxCmdByEvents() {
s.Require().NoError(s.network.WaitForNextBlock())

// Query the tx by hash to get the inner tx.
out, err = clitestutil.ExecTestCLICmd(val.ClientCtx, authcli.QueryTxCmd(), []string{txRes.TxHash, fmt.Sprintf("--%s=json", flags.FlagOutput)})
err = s.network.RetryForBlocks(func() error {
out, err = clitestutil.ExecTestCLICmd(val.ClientCtx, authcli.QueryTxCmd(), []string{txRes.TxHash, fmt.Sprintf("--%s=json", flags.FlagOutput)})
return err
}, 3)
s.Require().NoError(err)
s.Require().NoError(val.ClientCtx.Codec.UnmarshalJSON(out.Bytes(), &txRes))
protoTx := txRes.GetTx().(*tx.Tx)
Expand Down Expand Up @@ -685,7 +693,10 @@ func (s *E2ETestSuite) TestCLIQueryTxsCmdByEvents() {
s.Require().NoError(s.network.WaitForNextBlock())

// Query the tx by hash to get the inner tx.
out, err = clitestutil.ExecTestCLICmd(val.ClientCtx, authcli.QueryTxCmd(), []string{txRes.TxHash, fmt.Sprintf("--%s=json", flags.FlagOutput)})
err = s.network.RetryForBlocks(func() error {
out, err = clitestutil.ExecTestCLICmd(val.ClientCtx, authcli.QueryTxCmd(), []string{txRes.TxHash, fmt.Sprintf("--%s=json", flags.FlagOutput)})
return err
}, 3)
s.Require().NoError(err)
s.Require().NoError(val.ClientCtx.Codec.UnmarshalJSON(out.Bytes(), &txRes))

Expand Down Expand Up @@ -865,7 +876,10 @@ func (s *E2ETestSuite) TestCLISendGenerateSignAndBroadcast() {
s.Require().NoError(s.network.WaitForNextBlock())

// Ensure destiny account state
resp, err = clitestutil.QueryBalancesExec(val1.ClientCtx, addr)
err = s.network.RetryForBlocks(func() error {
resp, err = clitestutil.QueryBalancesExec(val1.ClientCtx, addr)
return err
}, 3)
s.Require().NoError(err)

err = val1.ClientCtx.Codec.UnmarshalJSON(resp.Bytes(), &balRes)
Expand Down Expand Up @@ -1154,7 +1168,9 @@ func (s *E2ETestSuite) TestCLIMultisign() {
s.Require().NoError(err)

var balRes banktypes.QueryAllBalancesResponse
err = val1.ClientCtx.Codec.UnmarshalJSON(resp.Bytes(), &balRes)
err = s.network.RetryForBlocks(func() error {
return val1.ClientCtx.Codec.UnmarshalJSON(resp.Bytes(), &balRes)
}, 3)
s.Require().NoError(err)
s.Require().True(sendTokens.Amount.Equal(balRes.Balances.AmountOf(s.cfg.BondDenom)))

Expand Down Expand Up @@ -1683,7 +1699,6 @@ func (s *E2ETestSuite) TestSignWithMultiSignersAminoJSON() {
)
require.NoError(err)
require.NoError(s.network.WaitForNextBlock())
require.NoError(s.network.WaitForNextBlock())

var txRes sdk.TxResponse
require.NoError(val0.ClientCtx.Codec.UnmarshalJSON(res.Bytes(), &txRes))
Expand Down
1 change: 0 additions & 1 deletion tests/e2e/feegrant/suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ func (s *E2ETestSuite) createGrant(granter, grantee sdk.Address) {
_, err := clitestutil.ExecTestCLICmd(clientCtx, cmd, args)
s.Require().NoError(err)
s.Require().NoError(s.network.WaitForNextBlock())
s.Require().NoError(s.network.WaitForNextBlock())
}

func (s *E2ETestSuite) TearDownSuite() {
Expand Down
91 changes: 44 additions & 47 deletions tests/e2e/gov/deposits.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package gov

import (
"fmt"
"strconv"
"time"

"github.com/stretchr/testify/suite"
Expand All @@ -19,10 +20,8 @@ import (
type DepositTestSuite struct {
suite.Suite

cfg network.Config
network *network.Network
deposits sdk.Coins
proposalIDs []string
cfg network.Config
network *network.Network
}

func NewDepositTestSuite(cfg network.Config) *DepositTestSuite {
Expand All @@ -35,38 +34,9 @@ func (s *DepositTestSuite) SetupSuite() {
var err error
s.network, err = network.New(s.T(), s.T().TempDir(), s.cfg)
s.Require().NoError(err)

s.Require().NoError(s.network.WaitForNextBlock())

val := s.network.Validators[0]

deposits := sdk.Coins{
sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(0)),
sdk.NewCoin(s.cfg.BondDenom, v1.DefaultMinDepositTokens),
sdk.NewCoin(s.cfg.BondDenom, v1.DefaultMinDepositTokens.Sub(sdk.NewInt(50))),
}
s.deposits = deposits

// create 2 proposals for testing
for i := 0; i < len(deposits); i++ {
id := i + 1
deposit := deposits[i]

s.submitProposal(val, deposit, id)
s.proposalIDs = append(s.proposalIDs, fmt.Sprintf("%d", id))
}
}

func (s *DepositTestSuite) SetupNewSuite() {
s.T().Log("setting up new test suite")

var err error
s.network, err = network.New(s.T(), s.T().TempDir(), s.cfg)
s.Require().NoError(err)
s.Require().NoError(s.network.WaitForNextBlock())
}

func (s *DepositTestSuite) submitProposal(val *network.Validator, initialDeposit sdk.Coin, id int) {
func (s *DepositTestSuite) submitProposal(val *network.Validator, initialDeposit sdk.Coin, name string) uint64 {
var exactArgs []string

if !initialDeposit.IsZero() {
Expand All @@ -76,13 +46,25 @@ func (s *DepositTestSuite) submitProposal(val *network.Validator, initialDeposit
_, err := govclitestutil.MsgSubmitLegacyProposal(
val.ClientCtx,
val.Address.String(),
fmt.Sprintf("Text Proposal %d", id),
fmt.Sprintf("Text Proposal %s", name),
"Where is the title!?",
v1beta1.ProposalTypeText,
exactArgs...,
)
s.Require().NoError(err)
s.Require().NoError(s.network.WaitForNextBlock())

// query proposals, return the last's id
cmd := cli.GetCmdQueryProposals()
args := []string{fmt.Sprintf("--%s=json", flags.FlagOutput)}
res, err := clitestutil.ExecTestCLICmd(val.ClientCtx, cmd, args)
s.Require().NoError(err)

var proposals v1.QueryProposalsResponse
err = s.cfg.Codec.UnmarshalJSON(res.Bytes(), &proposals)
s.Require().NoError(err)

return proposals.Proposals[len(proposals.Proposals)-1].Id
}

func (s *DepositTestSuite) TearDownSuite() {
Expand All @@ -93,7 +75,10 @@ func (s *DepositTestSuite) TearDownSuite() {
func (s *DepositTestSuite) TestQueryDepositsWithoutInitialDeposit() {
val := s.network.Validators[0]
clientCtx := val.ClientCtx
proposalID := s.proposalIDs[0]

// submit proposal without initial deposit
id := s.submitProposal(val, sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(0)), "TestQueryDepositsWithoutInitialDeposit")
proposalID := strconv.FormatUint(id, 10)

// deposit amount
depositAmount := sdk.NewCoin(s.cfg.BondDenom, v1.DefaultMinDepositTokens.Add(sdk.NewInt(50))).String()
Expand All @@ -105,7 +90,6 @@ func (s *DepositTestSuite) TestQueryDepositsWithoutInitialDeposit() {
deposit := s.queryDeposit(val, proposalID, false, "")
s.Require().NotNil(deposit)
s.Require().Equal(sdk.Coins(deposit.Amount).String(), depositAmount)
s.Require().NoError(s.network.WaitForNextBlock())

// query deposits
deposits := s.queryDeposits(val, proposalID, false, "")
Expand All @@ -117,38 +101,51 @@ func (s *DepositTestSuite) TestQueryDepositsWithoutInitialDeposit() {

func (s *DepositTestSuite) TestQueryDepositsWithInitialDeposit() {
val := s.network.Validators[0]
proposalID := s.proposalIDs[1]
depositAmount := sdk.NewCoin(s.cfg.BondDenom, v1.DefaultMinDepositTokens)

// submit proposal with an initial deposit
id := s.submitProposal(val, depositAmount, "TestQueryDepositsWithInitialDeposit")
proposalID := strconv.FormatUint(id, 10)

// query deposit
deposit := s.queryDeposit(val, proposalID, false, "")
s.Require().NotNil(deposit)
s.Require().Equal(sdk.Coins(deposit.Amount).String(), s.deposits[1].String())
s.Require().Equal(sdk.Coins(deposit.Amount).String(), depositAmount.String())
s.Require().NoError(s.network.WaitForNextBlock())

// query deposits
deposits := s.queryDeposits(val, proposalID, false, "")
s.Require().NotNil(deposits)
s.Require().Len(deposits.Deposits, 1)
// verify initial deposit
s.Require().Equal(sdk.Coins(deposits.Deposits[0].Amount).String(), s.deposits[1].String())
s.Require().Equal(sdk.Coins(deposits.Deposits[0].Amount).String(), depositAmount.String())
}

func (s *DepositTestSuite) TestQueryProposalAfterVotingPeriod() {
val := s.network.Validators[0]
clientCtx := val.ClientCtx
proposalID := s.proposalIDs[2]
depositAmount := sdk.NewCoin(s.cfg.BondDenom, v1.DefaultMinDepositTokens.Sub(sdk.NewInt(50)))

// submit proposal with an initial deposit
id := s.submitProposal(val, depositAmount, "TestQueryProposalAfterVotingPeriod")
proposalID := strconv.FormatUint(id, 10)

args := []string{fmt.Sprintf("--%s=json", flags.FlagOutput)}
cmd := cli.GetCmdQueryProposals()
res, err := clitestutil.ExecTestCLICmd(val.ClientCtx, cmd, args)
fmt.Println(res.String())
facundomedica marked this conversation as resolved.
Show resolved Hide resolved
s.Require().NoError(err)

// query proposal
args := []string{proposalID, fmt.Sprintf("--%s=json", flags.FlagOutput)}
cmd := cli.GetCmdQueryProposal()
_, err := clitestutil.ExecTestCLICmd(clientCtx, cmd, args)
args = []string{proposalID, fmt.Sprintf("--%s=json", flags.FlagOutput)}
cmd = cli.GetCmdQueryProposal()
_, err = clitestutil.ExecTestCLICmd(val.ClientCtx, cmd, args)
s.Require().NoError(err)

// waiting for deposit and voting period to end
time.Sleep(20 * time.Second)
time.Sleep(25 * time.Second)

// query proposal
_, err = clitestutil.ExecTestCLICmd(clientCtx, cmd, args)
_, err = clitestutil.ExecTestCLICmd(val.ClientCtx, cmd, args)
s.Require().Error(err)
s.Require().Contains(err.Error(), fmt.Sprintf("proposal %s doesn't exist", proposalID))

Expand Down
62 changes: 42 additions & 20 deletions tests/e2e/staking/suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package testutil

import (
"context"
"errors"
"fmt"
"strings"
"testing"
Expand Down Expand Up @@ -47,7 +48,7 @@ func (s *E2ETestSuite) SetupSuite() {
var err error
s.network, err = network.New(s.T(), s.T().TempDir(), s.cfg)
s.Require().NoError(err)
s.Require().NoError(s.network.WaitForNextBlock())
// s.Require().NoError(s.network.WaitForNextBlock())
facundomedica marked this conversation as resolved.
Show resolved Hide resolved

unbond, err := sdk.ParseCoinNormalized("10stake")
s.Require().NoError(err)
Expand Down Expand Up @@ -1460,8 +1461,8 @@ func (s *E2ETestSuite) TestBlockResults() {
require.NoError(s.network.WaitForNextBlock())

// Use CLI to create a delegation from the new account to validator `val`.
delHeight, err := s.network.LatestHeight()
require.NoError(err)
// delHeight, err := s.network.LatestHeight()
// require.NoError(err)
cmd := cli.NewDelegateCmd()
_, err = clitestutil.ExecTestCLICmd(val.ClientCtx, cmd, []string{
val.ValAddress.String(),
Expand All @@ -1480,31 +1481,52 @@ func (s *E2ETestSuite) TestBlockResults() {

// Loop until we find a block result with the correct validator updates.
// By experience, it happens around 2 blocks after `delHeight`.
for {
s.network.RetryForBlocks(func() error {
latestHeight, err := s.network.LatestHeight()
require.NoError(err)
res, err := rpcClient.BlockResults(context.Background(), &latestHeight)
if err != nil {
return err
}

// Wait maximum 10 blocks, or else fail test.
if latestHeight > delHeight+10 {
s.Fail("timeout reached")
if len(res.ValidatorUpdates) == 0 {
return errors.New("validator update not found yet")
}

res, err := rpcClient.BlockResults(context.Background(), &latestHeight)
require.NoError(err)
valUpdate := res.ValidatorUpdates[0]
require.Equal(
valUpdate.GetPubKey().Sum.(*crypto.PublicKey_Ed25519).Ed25519,
val.PubKey.Bytes(),
)

if len(res.ValidatorUpdates) > 0 {
valUpdate := res.ValidatorUpdates[0]
require.Equal(
valUpdate.GetPubKey().Sum.(*crypto.PublicKey_Ed25519).Ed25519,
val.PubKey.Bytes(),
)
return nil

// We got our validator update, test passed.
break
}
}, 10)
// for {
// latestHeight, err := s.network.LatestHeight()
facundomedica marked this conversation as resolved.
Show resolved Hide resolved
// require.NoError(err)

s.network.WaitForNextBlock()
}
// // Wait maximum 10 blocks, or else fail test.
// if latestHeight > delHeight+10 {
// s.Fail("timeout reached")
// }

// res, err := rpcClient.BlockResults(context.Background(), &latestHeight)
// require.NoError(err)

// if len(res.ValidatorUpdates) > 0 {
// valUpdate := res.ValidatorUpdates[0]
// require.Equal(
// valUpdate.GetPubKey().Sum.(*crypto.PublicKey_Ed25519).Ed25519,
// val.PubKey.Bytes(),
// )

// // We got our validator update, test passed.
// break
// }

// require.NoError(s.network.WaitForNextBlock())
// }
}

// https://github.com/cosmos/cosmos-sdk/issues/10660
Expand Down
Loading