Skip to content

Commit

Permalink
refactor: improve compilation time for neard tests
Browse files Browse the repository at this point in the history
See #4027 for discussion why this layout is better. TL;DR is that we
don't re-link neard repeatedly.

As a cherry on top, we now get to proprely share NodeCluster code
between the tests, instead of it being compiled afresh for each test.
  • Loading branch information
matklad committed Apr 29, 2021
1 parent 1fd8e1a commit fcd9e7a
Show file tree
Hide file tree
Showing 10 changed files with 26 additions and 14 deletions.
3 changes: 3 additions & 0 deletions neard/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ mod migrations;
mod runtime;
mod shard_tracker;

#[cfg(test)]
mod tests;

const STORE_PATH: &str = "data";

pub fn store_path_exists<P: AsRef<Path>>(path: P) -> bool {
Expand Down
9 changes: 9 additions & 0 deletions neard/tests/node_cluster.rs → neard/src/tests.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
mod economics;
mod load_genesis;
mod rpc_nodes;
mod run_nodes;
mod stake_nodes;
mod sync_nodes;
mod sync_state_nodes;
mod track_shards;

use futures::future;

use near_actix_test_utils::{run_actix_until_stop, spawn_interruptible};
Expand Down
4 changes: 2 additions & 2 deletions neard/tests/economics.rs → neard/src/tests/economics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ use std::sync::Arc;

use num_rational::Rational;

use crate::config::GenesisExt;
use near_chain::{ChainGenesis, RuntimeAdapter};
use near_chain_configs::Genesis;
use near_client::test_utils::TestEnv;
use near_crypto::{InMemorySigner, KeyType};
use near_logger_utils::init_integration_logger;
use near_primitives::transaction::SignedTransaction;
use near_store::test_utils::create_test_store;
use neard::config::GenesisExt;
use testlib::fees_utils::FeeHelper;

#[cfg(feature = "protocol_feature_rectify_inflation")]
Expand All @@ -26,7 +26,7 @@ fn setup_env(f: &mut dyn FnMut(&mut Genesis) -> ()) -> (TestEnv, FeeHelper) {
genesis.config.runtime_config.transaction_costs.clone(),
genesis.config.min_gas_price,
);
let runtimes: Vec<Arc<dyn RuntimeAdapter>> = vec![Arc::new(neard::NightshadeRuntime::new(
let runtimes: Vec<Arc<dyn RuntimeAdapter>> = vec![Arc::new(crate::NightshadeRuntime::new(
Path::new("."),
store1,
&genesis,
Expand Down
File renamed without changes.
5 changes: 2 additions & 3 deletions neard/tests/rpc_nodes.rs → neard/src/tests/rpc_nodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,10 @@ use near_primitives::types::{BlockId, BlockReference, Finality, TransactionOrRec
use near_primitives::views::{
ExecutionOutcomeView, ExecutionStatusView, FinalExecutionOutcomeViewEnum, FinalExecutionStatus,
};
use neard::config::TESTING_INIT_BALANCE;
use testlib::genesis_block;

mod node_cluster;
use node_cluster::NodeCluster;
use crate::config::TESTING_INIT_BALANCE;
use crate::tests::NodeCluster;

macro_rules! panic_on_rpc_error {
($e:expr) => {
Expand Down
3 changes: 1 addition & 2 deletions neard/tests/run_nodes.rs → neard/src/tests/run_nodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ use near_network::test_utils::WaitOrTimeout;
use near_primitives::types::{BlockHeightDelta, NumSeats, NumShards};
use rand::{thread_rng, Rng};

mod node_cluster;
use node_cluster::NodeCluster;
use crate::tests::NodeCluster;

fn run_heavy_nodes(
num_shards: NumShards,
Expand Down
5 changes: 3 additions & 2 deletions neard/tests/stake_nodes.rs → neard/src/tests/stake_nodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@ use near_primitives::hash::CryptoHash;
use near_primitives::transaction::SignedTransaction;
use near_primitives::types::{AccountId, BlockHeightDelta, BlockReference, NumSeats};
use near_primitives::views::{QueryRequest, QueryResponseKind, ValidatorInfo};
use neard::config::{GenesisExt, TESTING_INIT_BALANCE, TESTING_INIT_STAKE};
use neard::{load_test_config, start_with_config, NearConfig, NEAR_BASE};
use testlib::{genesis_hash, test_helpers::heavy_test};

#[cfg(feature = "protocol_feature_rectify_inflation")]
use {near_primitives::types::BlockId, primitive_types::U256};

use crate::config::{GenesisExt, TESTING_INIT_BALANCE, TESTING_INIT_STAKE};
use crate::{load_test_config, start_with_config, NearConfig, NEAR_BASE};

#[derive(Clone)]
struct TestNode {
account_id: AccountId,
Expand Down
5 changes: 3 additions & 2 deletions neard/tests/sync_nodes.rs → neard/src/tests/sync_nodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ use near_primitives::types::validator_stake::ValidatorStake;
use near_primitives::types::{BlockHeightDelta, EpochId};
use near_primitives::validator_signer::{InMemoryValidatorSigner, ValidatorSigner};
use near_primitives::version::PROTOCOL_VERSION;
use neard::config::{GenesisExt, TESTING_INIT_STAKE};
use neard::{load_test_config, start_with_config, NearConfig};
use testlib::{genesis_block, test_helpers::heavy_test};

use crate::config::{GenesisExt, TESTING_INIT_STAKE};
use crate::{load_test_config, start_with_config, NearConfig};

// This assumes that there is no height skipped. Otherwise epoch hash calculation will be wrong.
fn add_blocks(
mut blocks: Vec<Block>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ use near_chain_configs::Genesis;
use near_client::GetBlock;
use near_logger_utils::init_integration_logger;
use near_network::test_utils::{convert_boot_nodes, open_port, WaitOrTimeout};
use neard::{config::GenesisExt, load_test_config, start_with_config};
use testlib::test_helpers::heavy_test;

use crate::{config::GenesisExt, load_test_config, start_with_config};

/// One client is in front, another must sync to it using state (fast) sync.
#[test]
fn sync_state_nodes() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ use near_logger_utils::init_integration_logger;
use near_network::test_utils::WaitOrTimeout;
use near_primitives::hash::CryptoHash;

mod node_cluster;
use node_cluster::NodeCluster;
use crate::tests::NodeCluster;

#[test]
fn track_shards() {
Expand Down

0 comments on commit fcd9e7a

Please sign in to comment.