From 233656d13d09fe5cde6f14d9aa538739225e08eb Mon Sep 17 00:00:00 2001 From: Bui Quang Minh Date: Wed, 22 Nov 2023 18:12:20 +0700 Subject: [PATCH] ethclient: fix the transaction in block testcase We observe failure in TestEthClient/TxInBlockInterrupted which can be reproduced by go test -test.v -run=^TestEthClient/TxInBlockInterrupted --count=100 === RUN TestEthClient/TxInBlockInterrupted ethclient_test.go:399: transaction should be nil --- FAIL: TestEthClient (0.03s) This commit removes this part of testcase where the context is canceled before the client call and expects to get the context canceled error. It is because the client does not guarantee to prioritize context check before client call. This commit also adds some checks by use client.TransactionInBlock to query transactions in test block. --- ethclient/ethclient_test.go | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/ethclient/ethclient_test.go b/ethclient/ethclient_test.go index 0f87ad5f5c..209e12a347 100644 --- a/ethclient/ethclient_test.go +++ b/ethclient/ethclient_test.go @@ -263,8 +263,8 @@ func TestEthClient(t *testing.T) { "BalanceAt": { func(t *testing.T) { testBalanceAt(t, client) }, }, - "TxInBlockInterrupted": { - func(t *testing.T) { testTransactionInBlockInterrupted(t, client) }, + "TxInBlock": { + func(t *testing.T) { testTransactionInBlock(t, client) }, }, "ChainID": { func(t *testing.T) { testChainID(t, client) }, @@ -381,7 +381,7 @@ func testBalanceAt(t *testing.T, client *rpc.Client) { } } -func testTransactionInBlockInterrupted(t *testing.T, client *rpc.Client) { +func testTransactionInBlock(t *testing.T, client *rpc.Client) { ec := NewClient(client) // Get current block by number. @@ -390,16 +390,20 @@ func testTransactionInBlockInterrupted(t *testing.T, client *rpc.Client) { t.Fatalf("unexpected error: %v", err) } - // Test tx in block interrupted. - ctx, cancel := context.WithCancel(context.Background()) - cancel() - <-ctx.Done() // Ensure the close of the Done channel - tx, err := ec.TransactionInBlock(ctx, block.Hash(), 0) - if tx != nil { - t.Fatal("transaction should be nil") + tx1, err := ec.TransactionInBlock(context.Background(), block.Hash(), 0) + if err != nil { + t.Fatalf("unexpected error: %v", err) } - if err == nil || err == ethereum.NotFound { - t.Fatal("error should not be nil/notfound") + if tx1.Hash() != testTx1.Hash() { + t.Fatalf("wrong tx hash %v, want %v", tx1.Hash(), testTx1.Hash()) + } + + tx2, err := ec.TransactionInBlock(context.Background(), block.Hash(), 1) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + if tx2.Hash() != testTx2.Hash() { + t.Fatalf("wrong tx hash %v, want %v", tx2.Hash(), testTx2.Hash()) } // Test tx in block not found.