Skip to content

Commit

Permalink
fix(tests): race condition from heavy Network tests (#1913)
Browse files Browse the repository at this point in the history
* fix(gosdk): tests parallel race condition

* chore: add test comands to justfile

* test(oracle): Fix missing tear down step for oracle integration test

* refactor: more consistent test names

* chore: changelog
  • Loading branch information
Unique-Divine authored Jun 6, 2024
1 parent bbcc6f8 commit ad173e9
Show file tree
Hide file tree
Showing 11 changed files with 119 additions and 101 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- [#1859](https://github.com/NibiruChain/nibiru/pull/1859) - refactor(oracle): add oracle slashing events
- [#1893](https://github.com/NibiruChain/nibiru/pull/1893) - feat(gosdk): migrate Go-sdk into the Nibiru blockchain repo.
- [#1899](https://github.com/NibiruChain/nibiru/pull/1899) - build(deps): cometbft v0.37.5, cosmos-sdk v0.47.11, proto-builder v0.14.0
- [#1913](https://github.com/NibiruChain/nibiru/pull/1913) - fix(tests): race condition from heavy Network tests

### Dependencies

Expand Down
16 changes: 9 additions & 7 deletions app/wasmext/wasm_cli_test/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,16 @@ var commonArgs = []string{
sdk.NewCoins(sdk.NewCoin(denoms.NIBI, math.NewInt(10_000_000))).String()),
}

type IntegrationTestSuite struct {
var _ suite.TearDownAllSuite = (*TestSuite)(nil)

type TestSuite struct {
suite.Suite

cfg testutilcli.Config
network *testutilcli.Network
}

func (s *IntegrationTestSuite) SetupSuite() {
func (s *TestSuite) SetupSuite() {
testutil.BeforeIntegrationSuite(s.T())
testapp.EnsureNibiruPrefix()

Expand All @@ -52,12 +54,12 @@ func (s *IntegrationTestSuite) SetupSuite() {
s.Require().NoError(s.network.WaitForNextBlock())
}

func (s *IntegrationTestSuite) TearDownSuite() {
func (s *TestSuite) TearDownSuite() {
s.T().Log("tearing down integration test suite")
s.network.Cleanup()
}

func (s *IntegrationTestSuite) TestWasmHappyPath() {
func (s *TestSuite) TestWasmHappyPath() {
s.requiredDeployedContractsLen(0)

_, err := s.deployWasmContract("testdata/cw_nameservice.wasm")
Expand All @@ -70,7 +72,7 @@ func (s *IntegrationTestSuite) TestWasmHappyPath() {
}

// deployWasmContract deploys a wasm contract located in path.
func (s *IntegrationTestSuite) deployWasmContract(path string) (uint64, error) {
func (s *TestSuite) deployWasmContract(path string) (uint64, error) {
val := s.network.Validators[0]
codec := val.ClientCtx.Codec

Expand Down Expand Up @@ -124,7 +126,7 @@ func (s *IntegrationTestSuite) deployWasmContract(path string) (uint64, error) {
}

// requiredDeployedContractsLen checks the number of deployed contracts.
func (s *IntegrationTestSuite) requiredDeployedContractsLen(total int) {
func (s *TestSuite) requiredDeployedContractsLen(total int) {
val := s.network.Validators[0]
var queryCodeResponse types.QueryCodesResponse
err := testutilcli.ExecQuery(
Expand All @@ -138,5 +140,5 @@ func (s *IntegrationTestSuite) requiredDeployedContractsLen(total int) {
}

func TestIntegrationTestSuite(t *testing.T) {
suite.Run(t, new(IntegrationTestSuite))
suite.Run(t, new(TestSuite))
}
38 changes: 20 additions & 18 deletions eth/rpc/rpcapi/eth_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ import (
"github.com/NibiruChain/nibiru/x/common/testutil/testapp"
)

type IntegrationSuite struct {
var _ suite.TearDownAllSuite = (*TestSuite)(nil)

type TestSuite struct {
suite.Suite
cfg testutilcli.Config
network *testutilcli.Network
Expand All @@ -43,12 +45,12 @@ type IntegrationSuite struct {
contractData evmtest.CompiledEvmContract
}

func TestSuite_IntegrationSuite_RunAll(t *testing.T) {
suite.Run(t, new(IntegrationSuite))
func TestSuite_RunAll(t *testing.T) {
suite.Run(t, new(TestSuite))
}

// SetupSuite initialize network
func (s *IntegrationSuite) SetupSuite() {
func (s *TestSuite) SetupSuite() {
testutil.BeforeIntegrationSuite(s.T())
testapp.EnsureNibiruPrefix()

Expand Down Expand Up @@ -76,14 +78,14 @@ func (s *IntegrationSuite) SetupSuite() {
}

// Test_ChainID EVM method: eth_chainId
func (s *IntegrationSuite) Test_ChainID() {
func (s *TestSuite) Test_ChainID() {
ethChainID, err := s.ethClient.ChainID(context.Background())
s.NoError(err)
s.Equal(appconst.ETH_CHAIN_ID_DEFAULT, ethChainID.Int64())
}

// Test_BlockNumber EVM method: eth_blockNumber
func (s *IntegrationSuite) Test_BlockNumber() {
func (s *TestSuite) Test_BlockNumber() {
networkBlockNumber, err := s.network.LatestHeight()
s.NoError(err)

Expand All @@ -93,7 +95,7 @@ func (s *IntegrationSuite) Test_BlockNumber() {
}

// Test_BlockByNumber EVM method: eth_getBlockByNumber
func (s *IntegrationSuite) Test_BlockByNumber() {
func (s *TestSuite) Test_BlockByNumber() {
networkBlockNumber, err := s.network.LatestHeight()
s.NoError(err)

Expand All @@ -105,7 +107,7 @@ func (s *IntegrationSuite) Test_BlockByNumber() {
}

// Test_BalanceAt EVM method: eth_getBalance
func (s *IntegrationSuite) Test_BalanceAt() {
func (s *TestSuite) Test_BalanceAt() {
testAccEthAddr := gethcommon.BytesToAddress(testutilcli.NewAccount(s.network, "new-user"))

// New user balance should be 0
Expand All @@ -122,7 +124,7 @@ func (s *IntegrationSuite) Test_BalanceAt() {
}

// Test_StorageAt EVM method: eth_getStorageAt
func (s *IntegrationSuite) Test_StorageAt() {
func (s *TestSuite) Test_StorageAt() {
storage, err := s.ethClient.StorageAt(
context.Background(), s.fundedAccEthAddr, gethcommon.Hash{}, nil,
)
Expand All @@ -132,7 +134,7 @@ func (s *IntegrationSuite) Test_StorageAt() {
}

// Test_PendingStorageAt EVM method: eth_getStorageAt | pending
func (s *IntegrationSuite) Test_PendingStorageAt() {
func (s *TestSuite) Test_PendingStorageAt() {
storage, err := s.ethClient.PendingStorageAt(
context.Background(), s.fundedAccEthAddr, gethcommon.Hash{},
)
Expand All @@ -143,7 +145,7 @@ func (s *IntegrationSuite) Test_PendingStorageAt() {
}

// Test_CodeAt EVM method: eth_getCode
func (s *IntegrationSuite) Test_CodeAt() {
func (s *TestSuite) Test_CodeAt() {
code, err := s.ethClient.CodeAt(context.Background(), s.fundedAccEthAddr, nil)
s.NoError(err)

Expand All @@ -152,7 +154,7 @@ func (s *IntegrationSuite) Test_CodeAt() {
}

// Test_PendingCodeAt EVM method: eth_getCode
func (s *IntegrationSuite) Test_PendingCodeAt() {
func (s *TestSuite) Test_PendingCodeAt() {
code, err := s.ethClient.PendingCodeAt(context.Background(), s.fundedAccEthAddr)
s.NoError(err)

Expand All @@ -161,7 +163,7 @@ func (s *IntegrationSuite) Test_PendingCodeAt() {
}

// Test_EstimateGas EVM method: eth_estimateGas
func (s *IntegrationSuite) Test_EstimateGas() {
func (s *TestSuite) Test_EstimateGas() {
testAccEthAddr := gethcommon.BytesToAddress(testutilcli.NewAccount(s.network, "new-user"))
gasLimit := uint64(21000)
msg := geth.CallMsg{
Expand All @@ -176,14 +178,14 @@ func (s *IntegrationSuite) Test_EstimateGas() {
}

// Test_SuggestGasPrice EVM method: eth_gasPrice
func (s *IntegrationSuite) Test_SuggestGasPrice() {
func (s *TestSuite) Test_SuggestGasPrice() {
// TODO: the backend method is stubbed to 0
_, err := s.ethClient.SuggestGasPrice(context.Background())
s.NoError(err)
}

// Test_SimpleTransferTransaction EVM method: eth_sendRawTransaction
func (s *IntegrationSuite) Test_SimpleTransferTransaction() {
func (s *TestSuite) Test_SimpleTransferTransaction() {
chainID, err := s.ethClient.ChainID(context.Background())
s.NoError(err)
nonce, err := s.ethClient.PendingNonceAt(context.Background(), s.fundedAccEthAddr)
Expand Down Expand Up @@ -230,7 +232,7 @@ func (s *IntegrationSuite) Test_SimpleTransferTransaction() {
}

// Test_SmartContract includes contract deployment, query, execution
func (s *IntegrationSuite) Test_SmartContract() {
func (s *TestSuite) Test_SmartContract() {
chainID, err := s.ethClient.ChainID(context.Background())
s.NoError(err)
nonce, err := s.ethClient.NonceAt(context.Background(), s.fundedAccEthAddr, nil)
Expand Down Expand Up @@ -295,12 +297,12 @@ func (s *IntegrationSuite) Test_SmartContract() {
s.assertERC20Balance(contractAddress, recipientAddr, recipientBalance)
}

func (s *IntegrationSuite) TearDownSuite() {
func (s *TestSuite) TearDownSuite() {
s.T().Log("tearing down integration test suite")
s.network.Cleanup()
}

func (s *IntegrationSuite) assertERC20Balance(
func (s *TestSuite) assertERC20Balance(
contractAddress gethcommon.Address,
userAddress gethcommon.Address,
expectedBalance *big.Int,
Expand Down
21 changes: 6 additions & 15 deletions gosdk/gosdk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
// --------------------------------------------------

var _ suite.SetupAllSuite = (*TestSuite)(nil)
var _ suite.TearDownAllSuite = (*TestSuite)(nil)

type TestSuite struct {
suite.Suite
Expand All @@ -33,7 +34,7 @@ type TestSuite struct {
val *cli.Validator
}

func TestNibiruClientTestSuite_RunAll(t *testing.T) {
func TestSuite_RunAll(t *testing.T) {
suite.Run(t, new(TestSuite))
}

Expand All @@ -46,6 +47,8 @@ func (s *TestSuite) RPCEndpoint() string {
func (s *TestSuite) SetupSuite() {
testutil.BeforeIntegrationSuite(s.T())

s.Run("DoTestGetGrpcConnection_NoNetwork", s.DoTestGetGrpcConnection_NoNetwork)

nibiru, err := gosdk.CreateBlockchain(s.T())
s.NoError(err)
s.network = nibiru.Network
Expand Down Expand Up @@ -141,21 +144,9 @@ func (s *TestSuite) TearDownSuite() {
s.network.Cleanup()
}

// --------------------------------------------------
// NibiruClientSuite_NoNetwork
// --------------------------------------------------

type NibiruClientSuite_NoNetwork struct {
suite.Suite
}

func TestNibiruClientSuite_NoNetwork_RunAll(t *testing.T) {
suite.Run(t, new(NibiruClientSuite_NoNetwork))
}

func (s *NibiruClientSuite_NoNetwork) TestGetGrpcConnection_NoNetwork() {
func (s *TestSuite) DoTestGetGrpcConnection_NoNetwork() {
grpcConn, err := gosdk.GetGRPCConnection(
gosdk.DefaultNetworkInfo.GrpcEndpoint, true, 2,
gosdk.DefaultNetworkInfo.GrpcEndpoint+"notendpoint", true, 2,
)
s.Error(err)
s.Nil(grpcConn)
Expand Down
16 changes: 16 additions & 0 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ install-clean:
build:
make build

# Cleans the Go cache, modcache, and testcashe
clean-cache:
go clean -cache -testcache -modcache

alias b := build

# Generate protobuf code (Golang) for Nibiru
Expand Down Expand Up @@ -82,3 +86,15 @@ test-release:

release-publish:
make release

# Run Go tests (short mode)
test-unit:
go test ./... -short

# Run Go tests (short mode) + coverage
test-coverage-unit:
make test-coverage-unit

# Run Go tests, including live network tests + coverage
test-coverage-integration:
make test-coverage-integration
22 changes: 11 additions & 11 deletions x/common/testutil/cli/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ func New(logger Logger, baseDir string, cfg Config) (*Network, error) {
buf := bufio.NewReader(os.Stdin)

// generate private keys, node IDs, and initial transactions
for i := 0; i < cfg.NumValidators; i++ {
for valIdx := 0; valIdx < cfg.NumValidators; valIdx++ {
appCfg := serverconfig.DefaultConfig()
appCfg.Pruning = cfg.PruningStrategy
appCfg.MinGasPrices = cfg.MinGasPrices
Expand All @@ -243,7 +243,7 @@ func New(logger Logger, baseDir string, cfg Config) (*Network, error) {
appCfg.GRPC.Enable = false
appCfg.GRPCWeb.Enable = false
apiListenAddr := ""
if i == 0 {
if valIdx == 0 {
if cfg.APIAddress != "" {
apiListenAddr = cfg.APIAddress
} else {
Expand Down Expand Up @@ -309,7 +309,7 @@ func New(logger Logger, baseDir string, cfg Config) (*Network, error) {

ctx.Logger = loggerNoOp

nodeDirName := fmt.Sprintf("node%d", i)
nodeDirName := fmt.Sprintf("node%d", valIdx)
nodeDir := filepath.Join(network.BaseDir, nodeDirName, "simd")
clientDir := filepath.Join(network.BaseDir, nodeDirName, "simcli")
gentxsDir := filepath.Join(network.BaseDir, "gentxs")
Expand All @@ -326,7 +326,7 @@ func New(logger Logger, baseDir string, cfg Config) (*Network, error) {

tmCfg.SetRoot(nodeDir)
tmCfg.Moniker = nodeDirName
monikers[i] = nodeDirName
monikers[valIdx] = nodeDirName

proxyAddr, _, err := server.FreeTCPAddr()
if err != nil {
Expand All @@ -348,8 +348,8 @@ func New(logger Logger, baseDir string, cfg Config) (*Network, error) {
return nil, err
}

nodeIDs[i] = nodeID
valPubKeys[i] = pubKey
nodeIDs[valIdx] = nodeID
valPubKeys[valIdx] = pubKey

kb, err := keyring.New(sdk.KeyringServiceName(), keyring.BackendTest, clientDir, buf, cfg.Codec, cfg.KeyringOptions...)
if err != nil {
Expand All @@ -363,8 +363,8 @@ func New(logger Logger, baseDir string, cfg Config) (*Network, error) {
}

var mnemonic string
if i < len(cfg.Mnemonics) {
mnemonic = cfg.Mnemonics[i]
if valIdx < len(cfg.Mnemonics) {
mnemonic = cfg.Mnemonics[valIdx]
}

addr, secret, err := sdktestutil.GenerateSaveCoinKey(kb, nodeDirName, mnemonic, true, algo)
Expand Down Expand Up @@ -404,7 +404,7 @@ func New(logger Logger, baseDir string, cfg Config) (*Network, error) {

createValMsg, err := stakingtypes.NewMsgCreateValidator(
sdk.ValAddress(addr),
valPubKeys[i],
valPubKeys[valIdx],
sdk.NewCoin(cfg.BondDenom, cfg.BondedTokens),
stakingtypes.NewDescription(nodeDirName, "", "", "", ""),
stakingtypes.NewCommissionRates(commission, math.LegacyOneDec(), math.LegacyOneDec()),
Expand All @@ -419,7 +419,7 @@ func New(logger Logger, baseDir string, cfg Config) (*Network, error) {
return nil, err
}

memo := fmt.Sprintf("%s@%s:%s", nodeIDs[i], p2pURL.Hostname(), p2pURL.Port())
memo := fmt.Sprintf("%s@%s:%s", nodeIDs[valIdx], p2pURL.Hostname(), p2pURL.Port())
fee := sdk.NewCoins(sdk.NewCoin(fmt.Sprintf("%stoken", nodeDirName), math.ZeroInt()))
txBuilder := cfg.TxConfig.NewTxBuilder()
err = txBuilder.SetMsgs(createValMsg)
Expand Down Expand Up @@ -464,7 +464,7 @@ func New(logger Logger, baseDir string, cfg Config) (*Network, error) {
WithTxConfig(cfg.TxConfig).
WithAccountRetriever(cfg.AccountRetriever)

network.Validators[i] = &Validator{
network.Validators[valIdx] = &Validator{
AppConfig: appCfg,
ClientCtx: clientCtx,
Ctx: ctx,
Expand Down
Loading

0 comments on commit ad173e9

Please sign in to comment.