Skip to content

Commit c495f89

Browse files
cectonbkchr
andauthored
Add async test helper to timeout and provide a task_executor automatically (paritytech#6651)
* Initial commit Forked at: 60e3a69 Parent branch: origin/master * Add async test helper to timeout and provide a task_executor automatically * simplify error message to avoid difference between CI and locally * forgot env var * Use runtime env var instead of build env var * Rename variable to SUBSTRATE_TEST_TIMEOUT * CLEANUP Forked at: 60e3a69 Parent branch: origin/master * Apply suggestions from code review Co-authored-by: Bastian Köcher <[email protected]> * Re-export from test-utils * Default value to 120 * fix wrong crate in ci * Revert "Default value to 120" This reverts commit 8e45871. * Fix version * WIP Forked at: 60e3a69 Parent branch: origin/master * WIP Forked at: 60e3a69 Parent branch: origin/master * WIP Forked at: 60e3a69 Parent branch: origin/master * remove feature flag * fix missing dependency * CLEANUP Forked at: 60e3a69 Parent branch: origin/master * fix test * Removed autotests=false * Some doc... * Apply suggestions from code review Co-authored-by: Bastian Köcher <[email protected]> * WIP Forked at: 60e3a69 Parent branch: origin/master * WIP Forked at: 60e3a69 Parent branch: origin/master * Update test-utils/src/lib.rs Co-authored-by: Bastian Köcher <[email protected]>
1 parent 0c3cdf1 commit c495f89

15 files changed

+375
-1
lines changed

.gitlab-ci.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,7 @@ test-linux-stable: &test-linux
263263
script:
264264
# this job runs all tests in former runtime-benchmarks, frame-staking and wasmtime tests
265265
- time cargo test --workspace --locked --release --verbose --features runtime-benchmarks --manifest-path bin/node/cli/Cargo.toml
266+
- WASM_BUILD_NO_COLOR=1 SUBSTRATE_TEST_TIMEOUT=1 time cargo test -p substrate-test-utils --release --verbose --locked -- --ignored timeout
266267
- sccache -s
267268

268269
unleash-check:
@@ -727,7 +728,7 @@ deploy-kubernetes-alerting-rules:
727728
RULES: .maintain/monitoring/alerting-rules/alerting-rules.yaml
728729
script:
729730
- echo "deploying prometheus alerting rules"
730-
- kubectl -n ${NAMESPACE} patch prometheusrule ${PROMETHEUSRULE}
731+
- kubectl -n ${NAMESPACE} patch prometheusrule ${PROMETHEUSRULE}
731732
--type=merge --patch "$(sed 's/^/ /;1s/^/spec:\n/' ${RULES})"
732733
only:
733734
refs:

Cargo.lock

Lines changed: 32 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,9 +171,11 @@ members = [
171171
"primitives/utils",
172172
"primitives/wasm-interface",
173173
"test-utils/client",
174+
"test-utils/derive",
174175
"test-utils/runtime",
175176
"test-utils/runtime/client",
176177
"test-utils/runtime/transaction-pool",
178+
"test-utils/test-crate",
177179
"utils/browser",
178180
"utils/build-script-utils",
179181
"utils/fork-tree",

test-utils/Cargo.toml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,12 @@ repository = "https://github.com/paritytech/substrate/"
99

1010
[package.metadata.docs.rs]
1111
targets = ["x86_64-unknown-linux-gnu"]
12+
13+
[dependencies]
14+
futures = { version = "0.3.1", features = ["compat"] }
15+
substrate-test-utils-derive = { path = "./derive" }
16+
tokio = { version = "0.2.13", features = ["macros"] }
17+
18+
[dev-dependencies]
19+
sc-service = { path = "../client/service" }
20+
trybuild = { version = "1.0", features = ["diff"] }

test-utils/derive/Cargo.toml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
[package]
2+
name = "substrate-test-utils-derive"
3+
version = "0.8.0-rc5"
4+
authors = ["Parity Technologies <[email protected]>"]
5+
edition = "2018"
6+
license = "Apache-2.0"
7+
homepage = "https://substrate.dev"
8+
repository = "https://github.com/paritytech/substrate/"
9+
10+
[dependencies]
11+
quote = "1.0.6"
12+
syn = { version = "1.0.33", features = ["full"] }
13+
proc-macro-crate = "0.1.4"
14+
15+
[lib]
16+
proc-macro = true

test-utils/derive/src/lib.rs

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
// This file is part of Substrate.
2+
3+
// Copyright (C) 2020 Parity Technologies (UK) Ltd.
4+
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
5+
6+
// This program is free software: you can redistribute it and/or modify
7+
// it under the terms of the GNU General Public License as published by
8+
// the Free Software Foundation, either version 3 of the License, or
9+
// (at your option) any later version.
10+
11+
// This program is distributed in the hope that it will be useful,
12+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
// GNU General Public License for more details.
15+
16+
// You should have received a copy of the GNU General Public License
17+
// along with this program. If not, see <https://www.gnu.org/licenses/>.
18+
19+
use proc_macro::{Span, TokenStream};
20+
use proc_macro_crate::crate_name;
21+
use quote::quote;
22+
use std::env;
23+
24+
#[proc_macro_attribute]
25+
pub fn test(args: TokenStream, item: TokenStream) -> TokenStream {
26+
impl_test(args, item)
27+
}
28+
29+
fn impl_test(args: TokenStream, item: TokenStream) -> TokenStream {
30+
let input = syn::parse_macro_input!(item as syn::ItemFn);
31+
let args = syn::parse_macro_input!(args as syn::AttributeArgs);
32+
33+
parse_knobs(input, args).unwrap_or_else(|e| e.to_compile_error().into())
34+
}
35+
36+
fn parse_knobs(
37+
mut input: syn::ItemFn,
38+
args: syn::AttributeArgs,
39+
) -> Result<TokenStream, syn::Error> {
40+
let sig = &mut input.sig;
41+
let body = &input.block;
42+
let attrs = &input.attrs;
43+
let vis = input.vis;
44+
45+
if sig.inputs.len() != 1 {
46+
let msg = "the test function accepts only one argument of type sc_service::TaskExecutor";
47+
return Err(syn::Error::new_spanned(&sig, msg));
48+
}
49+
let (task_executor_name, task_executor_type) = match sig.inputs.pop().map(|x| x.into_value()) {
50+
Some(syn::FnArg::Typed(x)) => (x.pat, x.ty),
51+
_ => {
52+
let msg =
53+
"the test function accepts only one argument of type sc_service::TaskExecutor";
54+
return Err(syn::Error::new_spanned(&sig, msg));
55+
}
56+
};
57+
58+
let crate_name = if env::var("CARGO_PKG_NAME").unwrap() == "substrate-test-utils" {
59+
syn::Ident::new("substrate_test_utils", Span::call_site().into())
60+
} else {
61+
let crate_name = crate_name("substrate-test-utils")
62+
.map_err(|e| syn::Error::new_spanned(&sig, e))?;
63+
64+
syn::Ident::new(&crate_name, Span::call_site().into())
65+
};
66+
67+
let header = {
68+
quote! {
69+
#[#crate_name::tokio::test(#(#args)*)]
70+
}
71+
};
72+
73+
let result = quote! {
74+
#header
75+
#(#attrs)*
76+
#vis #sig {
77+
use #crate_name::futures::future::FutureExt;
78+
79+
let #task_executor_name: #task_executor_type = (|fut, _| {
80+
#crate_name::tokio::spawn(fut).map(drop)
81+
})
82+
.into();
83+
let timeout_task = #crate_name::tokio::time::delay_for(
84+
std::time::Duration::from_secs(
85+
std::env::var("SUBSTRATE_TEST_TIMEOUT")
86+
.ok()
87+
.and_then(|x| x.parse().ok())
88+
.unwrap_or(600))
89+
).fuse();
90+
let actual_test_task = async move {
91+
#body
92+
}
93+
.fuse();
94+
95+
#crate_name::futures::pin_mut!(timeout_task, actual_test_task);
96+
97+
#crate_name::futures::select! {
98+
_ = timeout_task => {
99+
panic!("The test took too long!");
100+
},
101+
_ = actual_test_task => {},
102+
}
103+
}
104+
};
105+
106+
Ok(result.into())
107+
}

test-utils/src/lib.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,29 @@
1717

1818
//! Test utils
1919
20+
#[doc(hidden)]
21+
pub use futures;
22+
/// Marks async function to be executed by an async runtime and provide a `TaskExecutor`, suitable
23+
/// to test environment.
24+
///
25+
/// # Requirements
26+
///
27+
/// You must have tokio in the `[dev-dependencies]` of your crate to use this macro.
28+
///
29+
/// # Example
30+
///
31+
/// ```
32+
/// #[substrate_test_utils::test]
33+
/// async fn basic_test(task_executor: TaskExecutor) {
34+
/// assert!(true);
35+
/// // create your node in here and use task_executor
36+
/// // then don't forget to gracefully shutdown your node before exit
37+
/// }
38+
/// ```
39+
pub use substrate_test_utils_derive::test;
40+
#[doc(hidden)]
41+
pub use tokio;
42+
2043
/// Panic when the vectors are different, without taking the order into account.
2144
///
2245
/// # Examples

test-utils/test-crate/Cargo.toml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
[package]
2+
name = "substrate-test-utils-test-crate"
3+
version = "0.1.0"
4+
authors = ["Parity Technologies <[email protected]>"]
5+
edition = "2018"
6+
license = "Apache-2.0"
7+
homepage = "https://substrate.dev"
8+
repository = "https://github.com/paritytech/substrate/"
9+
10+
[package.metadata.docs.rs]
11+
targets = ["x86_64-unknown-linux-gnu"]
12+
13+
[dev-dependencies]
14+
tokio = { version = "0.2.13", features = ["macros"] }
15+
test-utils = { path = "..", package = "substrate-test-utils" }
16+
sc-service = { path = "../../client/service" }

test-utils/test-crate/src/main.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// This file is part of Substrate.
2+
3+
// Copyright (C) 2020 Parity Technologies (UK) Ltd.
4+
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
5+
6+
// This program is free software: you can redistribute it and/or modify
7+
// it under the terms of the GNU General Public License as published by
8+
// the Free Software Foundation, either version 3 of the License, or
9+
// (at your option) any later version.
10+
11+
// This program is distributed in the hope that it will be useful,
12+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
// GNU General Public License for more details.
15+
16+
// You should have received a copy of the GNU General Public License
17+
// along with this program. If not, see <https://www.gnu.org/licenses/>.
18+
19+
#[cfg(test)]
20+
#[test_utils::test]
21+
async fn basic_test(_: sc_service::TaskExecutor) {
22+
assert!(true);
23+
}
24+
25+
fn main() {}

test-utils/tests/basic.rs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// This file is part of Substrate.
2+
3+
// Copyright (C) 2020 Parity Technologies (UK) Ltd.
4+
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
5+
6+
// This program is free software: you can redistribute it and/or modify
7+
// it under the terms of the GNU General Public License as published by
8+
// the Free Software Foundation, either version 3 of the License, or
9+
// (at your option) any later version.
10+
11+
// This program is distributed in the hope that it will be useful,
12+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
// GNU General Public License for more details.
15+
16+
// You should have received a copy of the GNU General Public License
17+
// along with this program. If not, see <https://www.gnu.org/licenses/>.
18+
19+
use sc_service::{TaskExecutor, TaskType};
20+
21+
#[substrate_test_utils::test]
22+
async fn basic_test(_: TaskExecutor) {
23+
assert!(true);
24+
}
25+
26+
#[substrate_test_utils::test]
27+
#[should_panic(expected = "boo!")]
28+
async fn panicking_test(_: TaskExecutor) {
29+
panic!("boo!");
30+
}
31+
32+
#[substrate_test_utils::test(max_threads = 2)]
33+
async fn basic_test_with_args(_: TaskExecutor) {
34+
assert!(true);
35+
}
36+
37+
#[substrate_test_utils::test]
38+
async fn rename_argument(ex: TaskExecutor) {
39+
let ex2 = ex.clone();
40+
ex2.spawn(Box::pin(async { () }), TaskType::Blocking);
41+
assert!(true);
42+
}
43+
44+
#[substrate_test_utils::test]
45+
#[should_panic(expected = "test took too long")]
46+
// NOTE: enable this test only after setting SUBSTRATE_TEST_TIMEOUT to a smaller value
47+
//
48+
// SUBSTRATE_TEST_TIMEOUT=1 cargo test -- --ignored timeout
49+
#[ignore]
50+
async fn timeout(_: TaskExecutor) {
51+
tokio::time::delay_for(std::time::Duration::from_secs(
52+
std::env::var("SUBSTRATE_TEST_TIMEOUT")
53+
.expect("env var SUBSTRATE_TEST_TIMEOUT has been provided by the user")
54+
.parse::<u64>()
55+
.unwrap() + 1,
56+
))
57+
.await;
58+
}

0 commit comments

Comments
 (0)