From afc09f9d5982ee26ff6a281fb64bb61adcd7c835 Mon Sep 17 00:00:00 2001 From: mmsqe Date: Tue, 31 May 2022 13:04:14 +0800 Subject: [PATCH] fix(rpc): avoid sleep with pending txs tests(#1098) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 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 <31522760+fedekunze@users.noreply.github.com> --- tests/rpc/rpc_pending_test.go | 10 ++++++---- tests/rpc/utils.go | 10 ++++++++-- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/tests/rpc/rpc_pending_test.go b/tests/rpc/rpc_pending_test.go index e7444bff9c..282e854b76 100644 --- a/tests/rpc/rpc_pending_test.go +++ b/tests/rpc/rpc_pending_test.go @@ -10,6 +10,7 @@ import ( "fmt" "math/big" "testing" + "time" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common/hexutil" @@ -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) @@ -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) diff --git a/tests/rpc/utils.go b/tests/rpc/utils.go index 3fa2abebd9..cd681c2450 100644 --- a/tests/rpc/utils.go +++ b/tests/rpc/utils.go @@ -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 { @@ -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 {