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

refactor(neard/tests): reduce boilerplate with intuitive structures #4229

Merged
merged 34 commits into from
Apr 17, 2021
Merged
Show file tree
Hide file tree
Changes from 28 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
0541f8c
add structure for rm node launch boilerplate in tests
miraclx Apr 14, 2021
66a83b5
add means to properly handle ctrl-c interrupts
miraclx Apr 14, 2021
f5269b9
test-utils: rm actix: -rt only is sufficient
miraclx Apr 15, 2021
54aaafa
reduce extra nesting by making heavy_test a config on NodeCluster
miraclx Apr 15, 2021
ab88d83
create cluster, then execute, reads better
miraclx Apr 15, 2021
4bafd54
don't forget to stop the system after the test
miraclx Apr 15, 2021
1f65d47
expand NodeCluster use to the rest of rpc_nodes
miraclx Apr 15, 2021
90a7a60
expand NodeCluster to aid its usefulness in run_nodes
miraclx Apr 15, 2021
af1512f
move .heavy() to a variant in the configvariant enum
miraclx Apr 15, 2021
199614a
remove unnecessary is_err line
miraclx Apr 15, 2021
49e8398
SIGINT causes a murderous psychopath to kill all kids
miraclx Apr 15, 2021
7079342
rename mkdir to something more appropriate: mkdirs_with
miraclx Apr 15, 2021
d13e4f4
remove extra spawn within spawned cluster task
miraclx Apr 15, 2021
7ad0294
appropriately rename run_nodes reflecting its implicit heaviness
miraclx Apr 15, 2021
dbdeb7a
rename exec to something more appropriate: exec_until_stop
miraclx Apr 15, 2021
a15e1f7
expand NodeCluster to aid its usefulness in track_shards
miraclx Apr 15, 2021
818de94
register signal handler on first exec call, not init
miraclx Apr 16, 2021
3b8e025
check parent before and after starting nodes
miraclx Apr 16, 2021
c2f09cf
move SIGINT checks to test-utils/near-actix
miraclx Apr 16, 2021
6dace3b
replace actix::spawn with spawn_interruptible in other tests
miraclx Apr 16, 2021
2079fc8
merge NodeCluster::{new,mkdirs_with}: no nodeless clusters
miraclx Apr 16, 2021
a47cd9f
be explicit, unalias spawn_interruptible
miraclx Apr 16, 2021
b9b8050
Vec gets the size_hint, skip the explicit reservation
miraclx Apr 16, 2021
e193250
panic immediately on exec with config issues
miraclx Apr 16, 2021
56fba0d
cluster executors are FnOnce
miraclx Apr 16, 2021
d96f2c9
defer to using uniq assoc methods instead of enum
miraclx Apr 16, 2021
84b967f
typo: received not recieved
miraclx Apr 17, 2021
75df4ea
fix ctrl-c elison on busy blocking threads
miraclx Apr 17, 2021
19d97cd
manually define assoc methods, skip the use of macro
miraclx Apr 17, 2021
4401057
implicitly treat every NodeCluster executed test as a heavy test
miraclx Apr 17, 2021
9b658dc
init NodeCluster with node_count, instead of capacity
miraclx Apr 17, 2021
8c9ed4a
rename assoc methods, use properly named setters
miraclx Apr 17, 2021
444d5c9
Merge branch 'master' into neard-tests-structured-boilerplate
frol Apr 17, 2021
1532070
fix last merge: update changes from master
miraclx Apr 17, 2021
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
3 changes: 2 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

104 changes: 104 additions & 0 deletions neard/tests/node_cluster.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
use futures::future;

use near_actix_test_utils::{run_actix_until_stop, spawn_interruptible};
use near_client::{ClientActor, ViewClientActor};
use near_primitives::types::{BlockHeight, BlockHeightDelta, NumSeats, NumShards};
use testlib::{start_nodes, test_helpers::heavy_test};

#[derive(Debug, Default)]
pub struct NodeCluster {
dirs: Vec<tempfile::TempDir>,
is_heavy: bool,
num_shards: Option<NumShards>,
num_validator_seats: Option<NumSeats>,
num_lightclient: Option<usize>,
epoch_length: Option<BlockHeightDelta>,
genesis_height: Option<BlockHeight>,
}

macro_rules! add_mut_helpers {
($($method:ident($self:ident $(,$arg:ident:$type:ty)?) => $expr:expr),+) => {
$(
pub fn $method(mut $self$(, $arg: $type)?) -> Self {
$expr;
$self
}
)+
}
}

impl NodeCluster {
pub fn new<F: Fn(usize) -> String>(capacity: usize, gen_dirname: F) -> Self {
miraclx marked this conversation as resolved.
Show resolved Hide resolved
Self {
dirs: (0..capacity)
.map(|index| {
tempfile::Builder::new().prefix(&gen_dirname(index)).tempdir().unwrap()
})
.collect(),
..Default::default()
}
}

add_mut_helpers! {
miraclx marked this conversation as resolved.
Show resolved Hide resolved
set_heavy_test(self) => self.is_heavy = true,
miraclx marked this conversation as resolved.
Show resolved Hide resolved
set_shards(self, n: NumShards) => self.num_shards = Some(n),
miraclx marked this conversation as resolved.
Show resolved Hide resolved
set_validator_seats(self, n: NumSeats) => self.num_validator_seats = Some(n),
set_lightclient(self, n: usize) => self.num_lightclient = Some(n),
miraclx marked this conversation as resolved.
Show resolved Hide resolved
set_epoch_length(self, l: BlockHeightDelta) => self.epoch_length = Some(l),
set_genesis_height(self, h: BlockHeight) => self.genesis_height = Some(h)
}

fn exec<F, R>(self, f: F)
where
R: future::Future<Output = ()> + 'static,
F: FnOnce(
near_chain_configs::Genesis,
Vec<String>,
Vec<(
actix::Addr<ClientActor>,
actix::Addr<ViewClientActor>,
Vec<actix_rt::ArbiterHandle>,
)>,
) -> R,
{
assert!(!self.dirs.is_empty(), "cluster config: expected a non-zero number of directories");
let (num_shards, num_validator_seats, num_lightclient, epoch_length, genesis_height) = (
self.num_shards.expect("cluster config: [num_shards] undefined"),
self.num_validator_seats.expect("cluster config: [num_validator_seats] undefined"),
self.num_lightclient.expect("cluster config: [num_lightclient] undefined"),
self.epoch_length.expect("cluster config: [epoch_length] undefined"),
self.genesis_height.expect("cluster config: [genesis_height] undefined"),
);
run_actix_until_stop(async {
let (genesis, rpc_addrs, clients) = start_nodes(
num_shards,
&self.dirs,
num_validator_seats,
num_lightclient,
epoch_length,
genesis_height,
);
spawn_interruptible(f(genesis, rpc_addrs, clients));
});
}

pub fn exec_until_stop<F, R>(self, f: F)
where
R: future::Future<Output = ()> + 'static,
F: FnOnce(
near_chain_configs::Genesis,
Vec<String>,
Vec<(
actix::Addr<ClientActor>,
actix::Addr<ViewClientActor>,
Vec<actix_rt::ArbiterHandle>,
)>,
) -> R,
{
if self.is_heavy {
heavy_test(|| self.exec(f))
} else {
self.exec(f)
}
}
}
Loading