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.
This commit is contained in:
Bui Quang Minh 2023-11-22 18:12:20 +07:00
parent e9f59b5d5e
commit 233656d13d
No known key found for this signature in database
GPG key ID: 9591AA13D0891B99

View file

@ -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.