Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing flakey tests #1883

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 7 additions & 58 deletions ethclient/ethclient_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 := &ethconfig.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()

Expand All @@ -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) },
},
Expand Down
65 changes: 6 additions & 59 deletions ethclient/gethclient/gethclient_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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 := &ethconfig.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) },
},
Expand Down
65 changes: 65 additions & 0 deletions ethclient/testutils.go
Original file line number Diff line number Diff line change
@@ -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 := &ethconfig.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
}