ethclient/gethclient: fix flaky TestSubscribePendingTxHashes test

Co-authored-by: s1na <1591639+s1na@users.noreply.github.com>
This commit is contained in:
copilot-swe-agent[bot] 2026-03-11 15:53:07 +00:00
parent b346c85e19
commit 3e0889571b

View file

@ -23,6 +23,7 @@ import (
"math/big" "math/big"
"strings" "strings"
"testing" "testing"
"time"
"github.com/ethereum/go-ethereum" "github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
@ -419,11 +420,11 @@ func testSubscribePendingTransactions(t *testing.T, client *rpc.Client) {
ethcl := ethclient.NewClient(client) ethcl := ethclient.NewClient(client)
// Subscribe to Transactions // Subscribe to Transactions
ch1 := make(chan common.Hash) ch1 := make(chan common.Hash, 128)
ec.SubscribePendingTransactions(context.Background(), ch1) ec.SubscribePendingTransactions(context.Background(), ch1)
// Subscribe to Transactions // Subscribe to Transactions
ch2 := make(chan *types.Transaction) ch2 := make(chan *types.Transaction, 128)
ec.SubscribeFullPendingTransactions(context.Background(), ch2) ec.SubscribeFullPendingTransactions(context.Background(), ch2)
// Send a transaction // Send a transaction
@ -452,15 +453,24 @@ func testSubscribePendingTransactions(t *testing.T, client *rpc.Client) {
t.Fatal(err) t.Fatal(err)
} }
// Check that the transaction was sent over the channel // Check that the transaction was sent over the channel
hash := <-ch1 timeout := 10 * time.Second
select {
case hash := <-ch1:
if hash != signedTx.Hash() { if hash != signedTx.Hash() {
t.Fatalf("Invalid tx hash received, got %v, want %v", hash, signedTx.Hash()) t.Fatalf("Invalid tx hash received, got %v, want %v", hash, signedTx.Hash())
} }
case <-time.After(timeout):
t.Fatal("Timed out waiting for pending tx hash notification")
}
// Check that the transaction was sent over the channel // Check that the transaction was sent over the channel
tx = <-ch2 select {
case tx := <-ch2:
if tx.Hash() != signedTx.Hash() { if tx.Hash() != signedTx.Hash() {
t.Fatalf("Invalid tx hash received, got %v, want %v", tx.Hash(), signedTx.Hash()) t.Fatalf("Invalid tx hash received, got %v, want %v", tx.Hash(), signedTx.Hash())
} }
case <-time.After(timeout):
t.Fatal("Timed out waiting for pending full tx notification")
}
} }
func testCallContract(t *testing.T, client *rpc.Client) { func testCallContract(t *testing.T, client *rpc.Client) {