From 08be3c20392ce0fac99202da3cbb6b70a8a11fed Mon Sep 17 00:00:00 2001 From: Andrei Smirnov Date: Thu, 14 Apr 2022 17:52:23 +0300 Subject: [PATCH] Fixing flakey tests TestEthClient and TestGethClient --- ethclient/ethclient_test.go | 65 +++---------------------- ethclient/gethclient/gethclient_test.go | 65 +++---------------------- ethclient/testutils.go | 65 +++++++++++++++++++++++++ 3 files changed, 78 insertions(+), 117 deletions(-) create mode 100644 ethclient/testutils.go diff --git a/ethclient/ethclient_test.go b/ethclient/ethclient_test.go index 63f82af707..6b94bd0489 100644 --- a/ethclient/ethclient_test.go +++ b/ethclient/ethclient_test.go @@ -28,16 +28,11 @@ import ( ethereum "github.com/celo-org/celo-blockchain" "github.com/celo-org/celo-blockchain/common" - mockEngine "github.com/celo-org/celo-blockchain/consensus/consensustest" - "github.com/celo-org/celo-blockchain/core" - "github.com/celo-org/celo-blockchain/core/rawdb" "github.com/celo-org/celo-blockchain/core/types" "github.com/celo-org/celo-blockchain/crypto" - "github.com/celo-org/celo-blockchain/eth" - "github.com/celo-org/celo-blockchain/eth/ethconfig" - "github.com/celo-org/celo-blockchain/node" "github.com/celo-org/celo-blockchain/params" "github.com/celo-org/celo-blockchain/rpc" + "github.com/stretchr/testify/require" ) // Verify that Client implements the ethereum interfaces. @@ -187,55 +182,10 @@ var ( testBalance = big.NewInt(2e15) ) -func newTestBackend(t *testing.T) (*node.Node, []*types.Block) { - // Generate test chain. - genesis, blocks := generateTestChain() - // Create node - n, err := node.New(&node.Config{}) - if err != nil { - t.Fatalf("can't create new node: %v", err) - } - // Create Ethereum Service - config := ðconfig.Config{Genesis: genesis} - ethservice, err := eth.New(n, config) - if err != nil { - t.Fatalf("can't create new ethereum service: %v", err) - } - // Import the test chain. - if err := n.Start(); err != nil { - t.Fatalf("can't start test node: %v", err) - } - if _, err := ethservice.BlockChain().InsertChain(blocks[1:]); err != nil { - t.Fatalf("can't import test blocks: %v", err) - } - return n, blocks -} - -func generateTestChain() (*core.Genesis, []*types.Block) { - db := rawdb.NewMemoryDatabase() - config := params.TestChainConfig - - engine := mockEngine.NewFaker() - - genesis := &core.Genesis{ - Config: config, - Alloc: core.GenesisAlloc{testAddr: {Balance: testBalance}}, - ExtraData: []byte("test genesis"), - Timestamp: 9000, - } - generate := func(i int, g *core.BlockGen) { - g.OffsetTime(5) - g.SetExtra(core.CreateEmptyIstanbulExtra([]byte("test"))) - } - gblock := genesis.ToBlock(db) - blocks, _ := core.GenerateChain(config, gblock, engine, db, 1, generate) - blocks = append([]*types.Block{gblock}, blocks...) - return genesis, blocks -} - func TestEthClient(t *testing.T) { - backend, chain := newTestBackend(t) - client, _ := backend.Attach() + backend, chain := NewTestBackend(t, testAddr, testBalance) + client, err := backend.Attach() + require.NoError(t, err) defer backend.Close() defer client.Close() @@ -260,10 +210,9 @@ func TestEthClient(t *testing.T) { "TestStatusFunctions": { func(t *testing.T) { testStatusFunctions(t, client) }, }, - // Flaky - // "TestCallContract": { - // func(t *testing.T) { testCallContract(t, client) }, - // }, + "TestCallContract": { + func(t *testing.T) { testCallContract(t, client) }, + }, "TestAtFunctions": { func(t *testing.T) { testAtFunctions(t, client) }, }, diff --git a/ethclient/gethclient/gethclient_test.go b/ethclient/gethclient/gethclient_test.go index aba375186a..aa9f4759fb 100644 --- a/ethclient/gethclient/gethclient_test.go +++ b/ethclient/gethclient/gethclient_test.go @@ -24,17 +24,11 @@ import ( ethereum "github.com/celo-org/celo-blockchain" "github.com/celo-org/celo-blockchain/common" - mockEngine "github.com/celo-org/celo-blockchain/consensus/consensustest" - "github.com/celo-org/celo-blockchain/core" - "github.com/celo-org/celo-blockchain/core/rawdb" "github.com/celo-org/celo-blockchain/core/types" "github.com/celo-org/celo-blockchain/crypto" - "github.com/celo-org/celo-blockchain/eth" - "github.com/celo-org/celo-blockchain/eth/ethconfig" "github.com/celo-org/celo-blockchain/ethclient" - "github.com/celo-org/celo-blockchain/node" - "github.com/celo-org/celo-blockchain/params" "github.com/celo-org/celo-blockchain/rpc" + "github.com/stretchr/testify/require" ) var ( @@ -43,66 +37,19 @@ var ( testBalance = big.NewInt(2e15) ) -func newTestBackend(t *testing.T) (*node.Node, []*types.Block) { - // Generate test chain. - genesis, blocks := generateTestChain() - // Create node - n, err := node.New(&node.Config{}) - if err != nil { - t.Fatalf("can't create new node: %v", err) - } - // Create Ethereum Service - config := ðconfig.Config{Genesis: genesis} - ethservice, err := eth.New(n, config) - if err != nil { - t.Fatalf("can't create new ethereum service: %v", err) - } - // Import the test chain. - if err := n.Start(); err != nil { - t.Fatalf("can't start test node: %v", err) - } - if _, err := ethservice.BlockChain().InsertChain(blocks[1:]); err != nil { - t.Fatalf("can't import test blocks: %v", err) - } - return n, blocks -} - -func generateTestChain() (*core.Genesis, []*types.Block) { - db := rawdb.NewMemoryDatabase() - config := params.TestChainConfig - genesis := &core.Genesis{ - Config: config, - Alloc: core.GenesisAlloc{testAddr: {Balance: testBalance}}, - ExtraData: []byte("test genesis"), - Timestamp: 9000, - } - generate := func(i int, g *core.BlockGen) { - g.OffsetTime(5) - g.SetExtra(core.CreateEmptyIstanbulExtra([]byte("test"))) - } - gblock := genesis.ToBlock(db) - engine := mockEngine.NewFaker() - blocks, _ := core.GenerateChain(config, gblock, engine, db, 1, generate) - blocks = append([]*types.Block{gblock}, blocks...) - return genesis, blocks -} - func TestGethClient(t *testing.T) { - backend, _ := newTestBackend(t) + backend, _ := ethclient.NewTestBackend(t, testAddr, testBalance) client, err := backend.Attach() - if err != nil { - t.Fatal(err) - } + require.NoError(t, err) defer backend.Close() defer client.Close() tests := map[string]struct { test func(t *testing.T) }{ - // Flaky - // "TestAccessList": { - // func(t *testing.T) { testAccessList(t, client) }, - // }, + "TestAccessList": { + func(t *testing.T) { testAccessList(t, client) }, + }, "TestGetProof": { func(t *testing.T) { testGetProof(t, client) }, }, diff --git a/ethclient/testutils.go b/ethclient/testutils.go new file mode 100644 index 0000000000..a49a393794 --- /dev/null +++ b/ethclient/testutils.go @@ -0,0 +1,65 @@ +package ethclient + +import ( + "math/big" + "testing" + "time" + + "github.com/celo-org/celo-blockchain/common" + mockEngine "github.com/celo-org/celo-blockchain/consensus/consensustest" + "github.com/celo-org/celo-blockchain/core" + "github.com/celo-org/celo-blockchain/core/rawdb" + "github.com/celo-org/celo-blockchain/core/types" + "github.com/celo-org/celo-blockchain/eth" + "github.com/celo-org/celo-blockchain/eth/ethconfig" + "github.com/celo-org/celo-blockchain/node" + "github.com/celo-org/celo-blockchain/params" + "github.com/stretchr/testify/require" +) + +func NewTestBackend(t *testing.T, testAddr common.Address, testBalance *big.Int) (*node.Node, []*types.Block) { + // Generate test chain. + genesis, blocks := generateTestChain(testAddr, testBalance) + + // Create node + n, err := node.New(&node.Config{}) + require.NoError(t, err, "can't create new node") + + // Create Ethereum Service + config := ðconfig.Config{Genesis: genesis} + ethservice, err := eth.New(n, config) + require.NoError(t, err, "can't create new ethereum service") + + // Import the test chain. + err = n.Start() + require.NoError(t, err, "can't start test node") + + _, err = ethservice.BlockChain().InsertChain(blocks[1:]) + require.NoError(t, err, "can't import test blocks") + + require.Eventually(t, ethservice.IsListening, 5*time.Second, 100*time.Millisecond) + + return n, blocks +} + +func generateTestChain(testAddr common.Address, testBalance *big.Int) (*core.Genesis, []*types.Block) { + db := rawdb.NewMemoryDatabase() + config := params.TestChainConfig + + engine := mockEngine.NewFaker() + + genesis := &core.Genesis{ + Config: config, + Alloc: core.GenesisAlloc{testAddr: {Balance: testBalance}}, + ExtraData: []byte("test genesis"), + Timestamp: 9000, + } + generate := func(i int, g *core.BlockGen) { + g.OffsetTime(5) + g.SetExtra(core.CreateEmptyIstanbulExtra([]byte("test"))) + } + gblock := genesis.ToBlock(db) + blocks, _ := core.GenerateChain(config, gblock, engine, db, 1, generate) + blocks = append([]*types.Block{gblock}, blocks...) + return genesis, blocks +}