Skip to content

Commit

Permalink
refactor(systemtest): Add cli.RunAndWait for common operations (#21689)
Browse files Browse the repository at this point in the history
(cherry picked from commit 81c9172)
  • Loading branch information
alpe authored and mergify[bot] committed Sep 13, 2024
1 parent 18dd4cd commit 15cf849
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 10 deletions.
12 changes: 4 additions & 8 deletions tests/systemtests/account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,17 @@ func TestAccountCreation(t *testing.T) {
rsp := cli.CustomQuery("q", "auth", "account", account1Addr)
assert.Equal(t, account1Addr, gjson.Get(rsp, "account.value.address").String(), rsp)

rsp1 := cli.Run("tx", "bank", "send", account1Addr, account2Addr, "5000stake", "--from="+account1Addr, "--fees=1stake")
txResult, found := cli.AwaitTxCommitted(rsp1)
require.True(t, found)
RequireTxSuccess(t, txResult)
rsp1 := cli.RunAndWait("tx", "bank", "send", account1Addr, account2Addr, "5000stake", "--from="+account1Addr, "--fees=1stake")
RequireTxSuccess(t, rsp1)

// query account2
assertNotFound := func(t assert.TestingT, err error, msgAndArgs ...interface{}) (ok bool) {
return strings.Contains(err.Error(), "not found: key not found")
}
_ = cli.WithRunErrorMatcher(assertNotFound).CustomQuery("q", "auth", "account", account2Addr)

rsp3 := cli.Run("tx", "bank", "send", account2Addr, account1Addr, "1000stake", "--from="+account2Addr, "--fees=1stake")
txResult, found = cli.AwaitTxCommitted(rsp3)
require.True(t, found)
RequireTxSuccess(t, txResult)
rsp3 := cli.RunAndWait("tx", "bank", "send", account2Addr, account1Addr, "1000stake", "--from="+account2Addr, "--fees=1stake")
RequireTxSuccess(t, rsp3)

// query account2 to make sure its created
rsp4 := cli.CustomQuery("q", "auth", "account", account2Addr)
Expand Down
11 changes: 11 additions & 0 deletions tests/systemtests/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,18 @@ func (c CLIWrapper) Run(args ...string) string {
return rsp
}

// RunAndWait runs a cli command and waits for the server result when the TX is executed
// It returns the result of the transaction.
func (c CLIWrapper) RunAndWait(args ...string) string {
rsp := c.Run(args...)
RequireTxSuccess(c.t, rsp)
txResult, found := c.AwaitTxCommitted(rsp)
require.True(c.t, found)
return txResult
}

// AwaitTxCommitted wait for tx committed on chain
// returns the server execution result and true when found within 3 blocks.
func (c CLIWrapper) AwaitTxCommitted(submitResp string, timeout ...time.Duration) (string, bool) {
c.t.Helper()
RequireTxSuccess(c.t, submitResp)
Expand Down
4 changes: 2 additions & 2 deletions tests/systemtests/staking_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func TestStakeUnstake(t *testing.T) {
valAddr := gjson.Get(rsp, "validators.#.operator_address").Array()[0].String()

// stake tokens
rsp = cli.Run("tx", "staking", "delegate", valAddr, "10000stake", "--from="+account1Addr, "--fees=1stake")
rsp = cli.RunAndWait("tx", "staking", "delegate", valAddr, "10000stake", "--from="+account1Addr, "--fees=1stake")
RequireTxSuccess(t, rsp)

t.Log(cli.QueryBalance(account1Addr, "stake"))
Expand All @@ -42,7 +42,7 @@ func TestStakeUnstake(t *testing.T) {
assert.Equal(t, "stake", gjson.Get(rsp, "delegation_response.balance.denom").String(), rsp)

// unstake tokens
rsp = cli.Run("tx", "staking", "unbond", valAddr, "5000stake", "--from="+account1Addr, "--fees=1stake")
rsp = cli.RunAndWait("tx", "staking", "unbond", valAddr, "5000stake", "--from="+account1Addr, "--fees=1stake")
RequireTxSuccess(t, rsp)

rsp = cli.CustomQuery("q", "staking", "delegation", account1Addr, valAddr)
Expand Down

0 comments on commit 15cf849

Please sign in to comment.