This repository has been archived by the owner on Nov 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Add async test helper to timeout and provide a task_executor automatically #6651
Merged
Merged
Changes from 16 commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
476c881
Initial commit
cecton 8117934
Add async test helper to timeout and provide a task_executor automati…
cecton ce5ea42
Merge commit 056879f376c46154847927928511c6fd127bef28 (no conflict)
cecton 34c0f23
Merge commit aa36bf284178daaea56399fedf5fcae8b9e282bc (conflicts)
cecton 544c1d2
Merge commit 64d4a4da2a62b59bf4f0212174149c31292b62e7 (no conflict)
cecton 2796798
simplify error message to avoid difference between CI and locally
cecton a0535f7
forgot env var
cecton b86cc8e
Use runtime env var instead of build env var
cecton 6b2f66f
Rename variable to SUBSTRATE_TEST_TIMEOUT
cecton 2a47de6
CLEANUP
cecton 24a165b
Apply suggestions from code review
cecton 0507b2f
Merge commit edb48cfdd90e2658017e696a33ae566c7e0940a4 (no conflict)
cecton f173c1c
Re-export from test-utils
cecton 8e45871
Default value to 120
cecton b7e1b1d
fix wrong crate in ci
cecton a200f34
Revert "Default value to 120"
cecton c157c22
Fix version
cecton d074de0
WIP
cecton f0c0e7f
WIP
cecton 3a5d4f0
WIP
cecton 40d1804
remove feature flag
cecton a8c62ff
fix missing dependency
cecton 82ceab0
CLEANUP
cecton badbec3
fix test
cecton a9d03e1
Removed autotests=false
cecton c7c3e52
Some doc...
cecton 78037b8
Apply suggestions from code review
cecton 0587d76
WIP
cecton e4b9ff1
WIP
cecton 948c64c
Update test-utils/src/lib.rs
bkchr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
[package] | ||
name = "substrate-test-utils-derive" | ||
mihir-ghl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
version = "0.8.0-rc4" | ||
cecton marked this conversation as resolved.
Show resolved
Hide resolved
|
||
authors = ["Parity Technologies <admin@parity.io>"] | ||
edition = "2018" | ||
license = "Apache-2.0" | ||
homepage = "https://substrate.dev" | ||
repository = "https://github.com/paritytech/substrate/" | ||
|
||
[dependencies] | ||
quote = "1" | ||
gnunicorn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
syn = { version = "1", default-features = false } | ||
gnunicorn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
[lib] | ||
proc-macro = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
// This file is part of Substrate. | ||
|
||
// Copyright (C) 2020 Parity Technologies (UK) Ltd. | ||
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0 | ||
|
||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
|
||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
|
||
// You should have received a copy of the GNU General Public License | ||
// along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
|
||
use proc_macro::TokenStream; | ||
use quote::quote; | ||
|
||
#[proc_macro_attribute] | ||
pub fn test(args: TokenStream, item: TokenStream) -> TokenStream { | ||
impl_test(args, item) | ||
} | ||
|
||
fn impl_test(args: TokenStream, item: TokenStream) -> TokenStream { | ||
let input = syn::parse_macro_input!(item as syn::ItemFn); | ||
let args = syn::parse_macro_input!(args as syn::AttributeArgs); | ||
|
||
parse_knobs(input, args).unwrap_or_else(|e| e.to_compile_error().into()) | ||
} | ||
|
||
fn parse_knobs( | ||
mut input: syn::ItemFn, | ||
args: syn::AttributeArgs, | ||
) -> Result<TokenStream, syn::Error> { | ||
let sig = &mut input.sig; | ||
let body = &input.block; | ||
let attrs = &input.attrs; | ||
let vis = input.vis; | ||
|
||
if sig.inputs.len() != 1 { | ||
let msg = "the test function accepts only one argument of type sc_service::TaskExecutor"; | ||
return Err(syn::Error::new_spanned(&sig, msg)); | ||
} | ||
let (task_executor_name, task_executor_type) = match sig.inputs.pop().map(|x| x.into_value()) { | ||
Some(syn::FnArg::Typed(x)) => (x.pat, x.ty), | ||
_ => { | ||
let msg = | ||
"the test function accepts only one argument of type sc_service::TaskExecutor"; | ||
return Err(syn::Error::new_spanned(&sig, msg)); | ||
} | ||
}; | ||
|
||
let header = { | ||
quote! { | ||
#[tokio::test(#(#args)*)] | ||
} | ||
}; | ||
|
||
let result = quote! { | ||
#header | ||
#(#attrs)* | ||
#vis #sig { | ||
use substrate_test_utils::futures::future::FutureExt; | ||
gnunicorn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
let #task_executor_name: #task_executor_type = (|fut, _| { | ||
substrate_test_utils::tokio::spawn(fut).map(drop) | ||
}) | ||
.into(); | ||
let timeout_task = substrate_test_utils::tokio::time::delay_for( | ||
std::time::Duration::from_secs( | ||
std::env::var("SUBSTRATE_TEST_TIMEOUT") | ||
.ok() | ||
.and_then(|x| x.parse().ok()) | ||
.unwrap_or(600)) | ||
).fuse(); | ||
let actual_test_task = async move { | ||
#body | ||
} | ||
.fuse(); | ||
|
||
substrate_test_utils::futures::pin_mut!(timeout_task, actual_test_task); | ||
|
||
substrate_test_utils::futures::select! { | ||
_ = timeout_task => { | ||
panic!("the test took too long"); | ||
cecton marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}, | ||
_ = actual_test_task => {}, | ||
} | ||
} | ||
}; | ||
|
||
Ok(result.into()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// This file is part of Substrate. | ||
|
||
// Copyright (C) 2020 Parity Technologies (UK) Ltd. | ||
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0 | ||
|
||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
|
||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
|
||
// You should have received a copy of the GNU General Public License | ||
// along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
|
||
use sc_service::{TaskExecutor, TaskType}; | ||
|
||
#[substrate_test_utils::test] | ||
async fn basic_test(_: TaskExecutor) { | ||
assert!(true); | ||
} | ||
|
||
#[substrate_test_utils::test] | ||
#[should_panic(expected = "boo!")] | ||
async fn panicking_test(_: TaskExecutor) { | ||
panic!("boo!"); | ||
} | ||
|
||
#[substrate_test_utils::test(max_threads = 2)] | ||
async fn basic_test_with_args(_: TaskExecutor) { | ||
assert!(true); | ||
} | ||
|
||
#[substrate_test_utils::test] | ||
async fn rename_argument(ex: TaskExecutor) { | ||
let ex2 = ex.clone(); | ||
ex2.spawn(Box::pin(async { () }), TaskType::Blocking); | ||
assert!(true); | ||
} | ||
|
||
#[substrate_test_utils::test] | ||
#[should_panic(expected = "test took too long")] | ||
// NOTE: enable this test only after setting SUBSTRATE_TEST_TIMEOUT to a smaller value | ||
// | ||
// SUBSTRATE_TEST_TIMEOUT=1 cargo test -- --ignored timeout | ||
#[ignore] | ||
async fn timeout(_: TaskExecutor) { | ||
tokio::time::delay_for(std::time::Duration::from_secs( | ||
std::env::var("SUBSTRATE_TEST_TIMEOUT") | ||
.expect("env var SUBSTRATE_TEST_TIMEOUT has been provided by the user") | ||
.parse::<u64>() | ||
.unwrap() + 1, | ||
)) | ||
.await; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// This file is part of Substrate. | ||
|
||
// Copyright (C) 2020 Parity Technologies (UK) Ltd. | ||
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0 | ||
|
||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
|
||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
|
||
// You should have received a copy of the GNU General Public License | ||
// along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
|
||
#[test] | ||
fn substrate_test_utils_derive_trybuild() { | ||
let t = trybuild::TestCases::new(); | ||
t.compile_fail("tests/missing-func-parameter.rs"); | ||
t.compile_fail("tests/too-many-func-parameters.rs"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// This file is part of Substrate. | ||
|
||
// Copyright (C) 2020 Parity Technologies (UK) Ltd. | ||
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0 | ||
|
||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
|
||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
|
||
// You should have received a copy of the GNU General Public License | ||
// along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
|
||
#[substrate_test_utils::test] | ||
async fn missing_func_parameter() { | ||
assert!(true); | ||
} | ||
|
||
fn main() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
error: the test function accepts only one argument of type sc_service::TaskExecutor | ||
--> $DIR/missing-func-parameter.rs:20:1 | ||
| | ||
20 | async fn missing_func_parameter() { | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// This file is part of Substrate. | ||
|
||
// Copyright (C) 2020 Parity Technologies (UK) Ltd. | ||
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0 | ||
|
||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
|
||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
|
||
// You should have received a copy of the GNU General Public License | ||
// along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
|
||
#[allow(unused_imports)] | ||
use sc_service::TaskExecutor; | ||
|
||
#[substrate_test_utils::test] | ||
async fn too_many_func_parameters(task_executor_1: TaskExecutor, task_executor_2: TaskExecutor) { | ||
assert!(true); | ||
} | ||
|
||
fn main() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
error: the test function accepts only one argument of type sc_service::TaskExecutor | ||
--> $DIR/too-many-func-parameters.rs:23:1 | ||
| | ||
23 | async fn too_many_func_parameters(task_executor_1: TaskExecutor, task_executor_2: TaskExecutor) { | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because there are trybuild tests in the tests directory
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please just implement these tests as they are done everywhere else in the codebase.