test: fix flaky tests + new method RetryForBlocks (#14609)

Co-authored-by: Marko <marbar3778@yahoo.com>
This commit is contained in:
Facundo Medica
2023-01-17 15:58:29 +00:00
committed by GitHub
co-authored by Marko
parent 781a4d6d77
commit 7505c18e2f
9 changed files with 158 additions and 105 deletions
+44 -8
View File
@@ -27,6 +27,7 @@ import (
"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/grpc/tmservice"
"github.com/cosmos/cosmos-sdk/client/tx"
"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
@@ -602,12 +603,27 @@ func (n *Network) LatestHeight() (int64, error) {
return 0, errors.New("no validators available")
}
status, err := n.Validators[0].RPCClient.Status(context.Background())
if err != nil {
return 0, err
}
ticker := time.NewTicker(time.Second)
defer ticker.Stop()
return status.SyncInfo.LatestBlockHeight, nil
timeout := time.NewTimer(time.Second * 5)
defer timeout.Stop()
var latestHeight int64
val := n.Validators[0]
queryClient := tmservice.NewServiceClient(val.ClientCtx)
for {
select {
case <-timeout.C:
return latestHeight, errors.New("timeout exceeded waiting for block")
case <-ticker.C:
res, err := queryClient.GetLatestBlock(context.Background(), &tmservice.GetLatestBlockRequest{})
if err == nil && res != nil {
return res.SdkBlock.Header.Height, nil
}
}
}
}
// WaitForHeight performs a blocking check where it waits for a block to be
@@ -632,15 +648,17 @@ func (n *Network) WaitForHeightWithTimeout(h int64, t time.Duration) (int64, err
var latestHeight int64
val := n.Validators[0]
queryClient := tmservice.NewServiceClient(val.ClientCtx)
for {
select {
case <-timeout.C:
return latestHeight, errors.New("timeout exceeded waiting for block")
case <-ticker.C:
status, err := val.RPCClient.Status(context.Background())
if err == nil && status != nil {
latestHeight = status.SyncInfo.LatestBlockHeight
res, err := queryClient.GetLatestBlock(context.Background(), &tmservice.GetLatestBlockRequest{})
if err == nil && res != nil {
latestHeight = res.GetSdkBlock().Header.Height
if latestHeight >= h {
return latestHeight, nil
}
@@ -649,6 +667,24 @@ func (n *Network) WaitForHeightWithTimeout(h int64, t time.Duration) (int64, err
}
}
// RetryForBlocks will wait for the next block and execute the function provided.
// It will do this until the function returns a nil error or until the number of
// blocks has been reached.
func (n *Network) RetryForBlocks(retryFunc func() error, blocks int) error {
for i := 0; i < blocks; i++ {
n.WaitForNextBlock()
err := retryFunc()
if err == nil {
return nil
}
// we've reached the last block to wait, return the error
if i == blocks-1 {
return err
}
}
return nil
}
// WaitForNextBlock waits for the next block to be committed, returning an error
// upon failure.
func (n *Network) WaitForNextBlock() error {