Skip to content

Commit 90a0989

Browse files
ethclient/gethclient: remove race condition in tests (ethereum#32206)
alternative to ethereum#32200 The race condition is not happening yet, since there is only a single call to `newTestBackend`, but there might be more in the future
1 parent a9061cf commit 90a0989

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

ethclient/gethclient/gethclient_test.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,11 @@ var (
4848
testSlot = common.HexToHash("0xdeadbeef")
4949
testValue = crypto.Keccak256Hash(testSlot[:])
5050
testBalance = big.NewInt(2e15)
51-
testTxHashes []common.Hash
5251
)
5352

54-
func newTestBackend(t *testing.T) (*node.Node, []*types.Block) {
53+
func newTestBackend(t *testing.T) (*node.Node, []*types.Block, []common.Hash) {
5554
// Generate test chain.
56-
genesis, blocks := generateTestChain()
55+
genesis, blocks, txHashes := generateTestChain()
5756
// Create node
5857
n, err := node.New(&node.Config{
5958
HTTPModules: []string{"debug", "eth", "admin"},
@@ -82,10 +81,10 @@ func newTestBackend(t *testing.T) (*node.Node, []*types.Block) {
8281
if _, err := ethservice.BlockChain().InsertChain(blocks[1:]); err != nil {
8382
t.Fatalf("can't import test blocks: %v", err)
8483
}
85-
return n, blocks
84+
return n, blocks, txHashes
8685
}
8786

88-
func generateTestChain() (*core.Genesis, []*types.Block) {
87+
func generateTestChain() (*core.Genesis, []*types.Block, []common.Hash) {
8988
genesis := &core.Genesis{
9089
Config: params.AllEthashProtocolChanges,
9190
Alloc: types.GenesisAlloc{
@@ -96,6 +95,7 @@ func generateTestChain() (*core.Genesis, []*types.Block) {
9695
ExtraData: []byte("test genesis"),
9796
Timestamp: 9000,
9897
}
98+
txHashes := make([]common.Hash, 0)
9999
generate := func(i int, g *core.BlockGen) {
100100
g.OffsetTime(5)
101101
g.SetExtra([]byte("test"))
@@ -111,15 +111,15 @@ func generateTestChain() (*core.Genesis, []*types.Block) {
111111
})
112112
tx, _ = types.SignTx(tx, types.LatestSignerForChainID(genesis.Config.ChainID), testKey)
113113
g.AddTx(tx)
114-
testTxHashes = append(testTxHashes, tx.Hash())
114+
txHashes = append(txHashes, tx.Hash())
115115
}
116116
_, blocks, _ := core.GenerateChainWithGenesis(genesis, ethash.NewFaker(), 1, generate)
117117
blocks = append([]*types.Block{genesis.ToBlock()}, blocks...)
118-
return genesis, blocks
118+
return genesis, blocks, txHashes
119119
}
120120

121121
func TestGethClient(t *testing.T) {
122-
backend, _ := newTestBackend(t)
122+
backend, _, txHashes := newTestBackend(t)
123123
client := backend.Attach()
124124
defer backend.Close()
125125
defer client.Close()
@@ -172,7 +172,7 @@ func TestGethClient(t *testing.T) {
172172
},
173173
{
174174
"TestTraceTransaction",
175-
func(t *testing.T) { testTraceTransactions(t, client) },
175+
func(t *testing.T) { testTraceTransactions(t, client, txHashes) },
176176
},
177177
{
178178
"TestSetHead",
@@ -480,9 +480,9 @@ func testCallContract(t *testing.T, client *rpc.Client) {
480480
}
481481
}
482482

483-
func testTraceTransactions(t *testing.T, client *rpc.Client) {
483+
func testTraceTransactions(t *testing.T, client *rpc.Client, txHashes []common.Hash) {
484484
ec := New(client)
485-
for _, txHash := range testTxHashes {
485+
for _, txHash := range txHashes {
486486
// Struct logger
487487
_, err := ec.TraceTransaction(context.Background(), txHash, nil)
488488
if err != nil {

0 commit comments

Comments
 (0)