Skip to content

Commit 7596db5

Browse files
hyunchelfjl
andauthored
ethclient: add tests for TransactionInBlock (#28283)
Co-authored-by: Felix Lange <[email protected]>
1 parent 89ccc68 commit 7596db5

File tree

1 file changed

+20
-15
lines changed

1 file changed

+20
-15
lines changed

ethclient/ethclient_test.go

+20-15
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ func TestEthClient(t *testing.T) {
264264
func(t *testing.T) { testBalanceAt(t, client) },
265265
},
266266
"TxInBlockInterrupted": {
267-
func(t *testing.T) { testTransactionInBlockInterrupted(t, client) },
267+
func(t *testing.T) { testTransactionInBlock(t, client) },
268268
},
269269
"ChainID": {
270270
func(t *testing.T) { testChainID(t, client) },
@@ -329,7 +329,7 @@ func testHeader(t *testing.T, chain []*types.Block, client *rpc.Client) {
329329
got.Number = big.NewInt(0) // hack to make DeepEqual work
330330
}
331331
if !reflect.DeepEqual(got, tt.want) {
332-
t.Fatalf("HeaderByNumber(%v)\n = %v\nwant %v", tt.block, got, tt.want)
332+
t.Fatalf("HeaderByNumber(%v) got = %v, want %v", tt.block, got, tt.want)
333333
}
334334
})
335335
}
@@ -381,7 +381,7 @@ func testBalanceAt(t *testing.T, client *rpc.Client) {
381381
}
382382
}
383383

384-
func testTransactionInBlockInterrupted(t *testing.T, client *rpc.Client) {
384+
func testTransactionInBlock(t *testing.T, client *rpc.Client) {
385385
ec := NewClient(client)
386386

387387
// Get current block by number.
@@ -390,22 +390,27 @@ func testTransactionInBlockInterrupted(t *testing.T, client *rpc.Client) {
390390
t.Fatalf("unexpected error: %v", err)
391391
}
392392

393-
// Test tx in block interrupted.
394-
ctx, cancel := context.WithCancel(context.Background())
395-
cancel()
396-
<-ctx.Done() // Ensure the close of the Done channel
397-
tx, err := ec.TransactionInBlock(ctx, block.Hash(), 0)
398-
if tx != nil {
399-
t.Fatal("transaction should be nil")
400-
}
401-
if err == nil || err == ethereum.NotFound {
402-
t.Fatal("error should not be nil/notfound")
403-
}
404-
405393
// Test tx in block not found.
406394
if _, err := ec.TransactionInBlock(context.Background(), block.Hash(), 20); err != ethereum.NotFound {
407395
t.Fatal("error should be ethereum.NotFound")
408396
}
397+
398+
// Test tx in block found.
399+
tx, err := ec.TransactionInBlock(context.Background(), block.Hash(), 0)
400+
if err != nil {
401+
t.Fatalf("unexpected error: %v", err)
402+
}
403+
if tx.Hash() != testTx1.Hash() {
404+
t.Fatalf("unexpected transaction: %v", tx)
405+
}
406+
407+
tx, err = ec.TransactionInBlock(context.Background(), block.Hash(), 1)
408+
if err != nil {
409+
t.Fatalf("unexpected error: %v", err)
410+
}
411+
if tx.Hash() != testTx2.Hash() {
412+
t.Fatalf("unexpected transaction: %v", tx)
413+
}
409414
}
410415

411416
func testChainID(t *testing.T, client *rpc.Client) {

0 commit comments

Comments
 (0)