Skip to content

Commit

Permalink
fix(rpc): avoid sleep with pending txs tests(#1098)
Browse files Browse the repository at this point in the history
* avoid pending tx get confirmed when sleep

* fix install-tparse which need go >= 1.18

* fix strings.Cut in https://github.com/tharsis/ethermint/runs/6611646254?check_suite_focus=true
* for more info, https://dev.to/hgsgtk/go-118-new-function-cut-added-to-stringsbytes-package-5c2f

* Revert "fix install-tparse which need go >= 1.18"

This reverts commit 5e39c2d7351ebbd6b789df214f2396e6c5dfb28e.

Co-authored-by: Federico Kunze Küllmer <[email protected]>
  • Loading branch information
niceDeve and fedekunze committed May 31, 2022
1 parent 4eb41b1 commit 9012e1e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
10 changes: 6 additions & 4 deletions tests/rpc/rpc_pending_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"fmt"
"math/big"
"testing"
"time"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
Expand Down Expand Up @@ -269,8 +270,9 @@ func TestEth_Pending_GetTransactionByBlockNumberAndIndex(t *testing.T) {
}

func TestEth_Pending_GetTransactionByHash(t *testing.T) {
sleep := 0 * time.Second
// negative case, check that it returns empty.
rpcRes := Call(t, "eth_getTransactionByHash", []interface{}{"0xec5fa15e1368d6ac314f9f64118c5794f076f63c02e66f97ea5fe1de761a8973"})
rpcRes := CallWithSleep(t, "eth_getTransactionByHash", []interface{}{"0xec5fa15e1368d6ac314f9f64118c5794f076f63c02e66f97ea5fe1de761a8973"}, sleep)
var tx map[string]interface{}
err := json.Unmarshal(rpcRes.Result, &tx)
require.NoError(t, err)
Expand All @@ -281,17 +283,17 @@ func TestEth_Pending_GetTransactionByHash(t *testing.T) {
param := makePendingTxParams(t)
param[0]["data"] = data

txRes := Call(t, "eth_sendTransaction", param)
txRes := CallWithSleep(t, "eth_sendTransaction", param, sleep)
var txHash common.Hash
err = txHash.UnmarshalJSON(txRes.Result)
require.NoError(t, err)

rpcRes = Call(t, "eth_getTransactionByHash", []interface{}{txHash})
rpcRes = CallWithSleep(t, "eth_getTransactionByHash", []interface{}{txHash}, sleep)
var pendingTx map[string]interface{}
err = json.Unmarshal(rpcRes.Result, &pendingTx)
require.NoError(t, err)

txsRes := Call(t, "eth_getPendingTransactions", []interface{}{})
txsRes := CallWithSleep(t, "eth_getPendingTransactions", []interface{}{}, sleep)
var pendingTxs []map[string]interface{}
err = json.Unmarshal(txsRes.Result, &pendingTxs)
require.NoError(t, err)
Expand Down
10 changes: 8 additions & 2 deletions tests/rpc/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,14 @@ func CreateRequest(method string, params interface{}) Request {
}
}

func Call(t *testing.T, method string, params interface{}) *Response {
func CallWithSleep(t *testing.T, method string, params interface{}, sleep time.Duration) *Response {
req, err := json.Marshal(CreateRequest(method, params))
require.NoError(t, err)

var rpcRes *Response
time.Sleep(1 * time.Second)
if sleep > 0 {
time.Sleep(sleep)
}

httpReq, err := http.NewRequestWithContext(context.Background(), "POST", HOST, bytes.NewBuffer(req))
if err != nil {
Expand All @@ -94,6 +96,10 @@ func Call(t *testing.T, method string, params interface{}) *Response {
return rpcRes
}

func Call(t *testing.T, method string, params interface{}) *Response {
return CallWithSleep(t, method, params, time.Second)
}

func CallWithError(method string, params interface{}) (*Response, error) {
req, err := json.Marshal(CreateRequest(method, params))
if err != nil {
Expand Down

0 comments on commit 9012e1e

Please sign in to comment.