From 8c3658b23bd481ee8d96c3a1c85baca312d67b31 Mon Sep 17 00:00:00 2001 From: Hyunchel Kim Date: Sun, 8 Oct 2023 11:25:02 -0400 Subject: [PATCH 1/4] ethclient: add tests on TxInBlock function This commit also asserts context.Cancel error upon canceling, and removes unnecessary newlines in logs messages. --- ethclient/ethclient_test.go | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/ethclient/ethclient_test.go b/ethclient/ethclient_test.go index 0f87ad5f5cd3..50802783b87c 100644 --- a/ethclient/ethclient_test.go +++ b/ethclient/ethclient_test.go @@ -264,7 +264,7 @@ func TestEthClient(t *testing.T) { func(t *testing.T) { testBalanceAt(t, client) }, }, "TxInBlockInterrupted": { - func(t *testing.T) { testTransactionInBlockInterrupted(t, client) }, + func(t *testing.T) { testTransactionInBlock(t, client) }, }, "ChainID": { func(t *testing.T) { testChainID(t, client) }, @@ -329,7 +329,7 @@ func testHeader(t *testing.T, chain []*types.Block, client *rpc.Client) { got.Number = big.NewInt(0) // hack to make DeepEqual work } if !reflect.DeepEqual(got, tt.want) { - t.Fatalf("HeaderByNumber(%v)\n = %v\nwant %v", tt.block, got, tt.want) + t.Fatalf("HeaderByNumber(%v) got = %v, want %v", tt.block, got, tt.want) } }) } @@ -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. @@ -398,14 +398,32 @@ func testTransactionInBlockInterrupted(t *testing.T, client *rpc.Client) { if tx != nil { t.Fatal("transaction should be nil") } - if err == nil || err == ethereum.NotFound { - t.Fatal("error should not be nil/notfound") + if !errors.Is(err, context.Canceled) { + t.Fatalf("error should be context.Canceled, got %v", err) } // Test tx in block not found. if _, err := ec.TransactionInBlock(context.Background(), block.Hash(), 20); err != ethereum.NotFound { t.Fatal("error should be ethereum.NotFound") } + + // Test tx in block found. + tx, err = ec.TransactionInBlock(context.Background(), block.Hash(), 0) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + if tx.Hash() != testTx1.Hash() { + t.Fatalf("unexpected transaction: %v", tx) + } + + tx, err = ec.TransactionInBlock(context.Background(), block.Hash(), 1) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + if tx.Hash() != testTx2.Hash() { + t.Fatalf("unexpected transaction: %v", tx) + } + t.Error("test") } func testChainID(t *testing.T, client *rpc.Client) { From 8922df8c92efcf2a3b8f4cafa8165d2c5474ed68 Mon Sep 17 00:00:00 2001 From: Hyunchel Kim Date: Sun, 8 Oct 2023 11:31:42 -0400 Subject: [PATCH 2/4] Remove t.Error used during inspection --- ethclient/ethclient_test.go | 1 - 1 file changed, 1 deletion(-) diff --git a/ethclient/ethclient_test.go b/ethclient/ethclient_test.go index 50802783b87c..12ac5ba0fd80 100644 --- a/ethclient/ethclient_test.go +++ b/ethclient/ethclient_test.go @@ -423,7 +423,6 @@ func testTransactionInBlock(t *testing.T, client *rpc.Client) { if tx.Hash() != testTx2.Hash() { t.Fatalf("unexpected transaction: %v", tx) } - t.Error("test") } func testChainID(t *testing.T, client *rpc.Client) { From cf7c7606149f0256b8478f5c73ba293ea52e7998 Mon Sep 17 00:00:00 2001 From: Felix Lange Date: Wed, 22 Nov 2023 12:25:41 +0100 Subject: [PATCH 3/4] Update ethclient_test.go --- ethclient/ethclient_test.go | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/ethclient/ethclient_test.go b/ethclient/ethclient_test.go index 12ac5ba0fd80..5eda752e00e8 100644 --- a/ethclient/ethclient_test.go +++ b/ethclient/ethclient_test.go @@ -390,18 +390,6 @@ func testTransactionInBlock(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") - } - if !errors.Is(err, context.Canceled) { - t.Fatalf("error should be context.Canceled, got %v", err) - } - // Test tx in block not found. if _, err := ec.TransactionInBlock(context.Background(), block.Hash(), 20); err != ethereum.NotFound { t.Fatal("error should be ethereum.NotFound") From 11c09876c9fc97a4ce28f689b1e2bdca1f9be450 Mon Sep 17 00:00:00 2001 From: Felix Lange Date: Wed, 22 Nov 2023 15:52:27 +0100 Subject: [PATCH 4/4] Update ethclient_test.go --- ethclient/ethclient_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ethclient/ethclient_test.go b/ethclient/ethclient_test.go index 5eda752e00e8..2ef68337c6d4 100644 --- a/ethclient/ethclient_test.go +++ b/ethclient/ethclient_test.go @@ -396,7 +396,7 @@ func testTransactionInBlock(t *testing.T, client *rpc.Client) { } // Test tx in block found. - tx, err = ec.TransactionInBlock(context.Background(), block.Hash(), 0) + tx, err := ec.TransactionInBlock(context.Background(), block.Hash(), 0) if err != nil { t.Fatalf("unexpected error: %v", err) }