Unverified Commit ae445efc authored by Cecile Tonglet's avatar Cecile Tonglet Committed by GitHub
Browse files

Use async test helper to simplify node testing (#1578)

parent ae69d7a0
Pipeline #103844 passed with stages
in 17 minutes and 8 seconds
......@@ -5438,6 +5438,7 @@ dependencies = [
"sp-runtime",
"sp-state-machine",
"substrate-test-client",
"substrate-test-utils",
"tempfile",
"tokio 0.2.21",
]
......@@ -8511,6 +8512,26 @@ dependencies = [
"substrate-test-runtime",
]
[[package]]
name = "substrate-test-utils"
version = "2.0.0-rc5"
source = "git+https://github.com/paritytech/substrate#473a23f462c60fb5a063ea8dff9261e27ac0ea48"
dependencies = [
"futures 0.3.5",
"substrate-test-utils-derive",
"tokio 0.2.21",
]
[[package]]
name = "substrate-test-utils-derive"
version = "0.8.0-rc5"
source = "git+https://github.com/paritytech/substrate#473a23f462c60fb5a063ea8dff9261e27ac0ea48"
dependencies = [
"proc-macro-crate",
"quote 1.0.7",
"syn 1.0.33",
]
[[package]]
name = "substrate-wasm-builder-runner"
version = "1.0.6"
......
......@@ -53,4 +53,5 @@ substrate-test-client = { git = "https://github.com/paritytech/substrate", branc
[dev-dependencies]
pallet-balances = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
serde_json = "1.0"
substrate-test-utils = { git = "https://github.com/paritytech/substrate", branch = "master" }
tokio = { version = "0.2", features = ["macros"] }
......@@ -14,21 +14,13 @@
// You should have received a copy of the GNU General Public License
// along with Polkadot. If not, see <http://www.gnu.org/licenses/>.
use tokio::{time::delay_for as sleep, task::spawn};
use futures::{future, pin_mut, select, FutureExt as _};
use futures::{future, pin_mut, select};
use polkadot_test_service::*;
use service::TaskExecutor;
use sp_keyring::Sr25519Keyring;
use std::time::Duration;
static INTEGRATION_TEST_ALLOWED_TIME: Option<&str> = option_env!("INTEGRATION_TEST_ALLOWED_TIME");
#[tokio::test]
async fn ensure_test_service_build_blocks() {
let task_executor: TaskExecutor = (move |fut, _| {
spawn(fut).map(|_| ())
})
.into();
#[substrate_test_utils::test]
async fn ensure_test_service_build_blocks(task_executor: TaskExecutor) {
let mut alice = run_test_node(
task_executor.clone(),
Sr25519Keyring::Alice,
......@@ -41,38 +33,21 @@ async fn ensure_test_service_build_blocks() {
|| {},
vec![alice.addr.clone()],
);
let t1 = sleep(Duration::from_secs(
INTEGRATION_TEST_ALLOWED_TIME
.and_then(|x| x.parse().ok())
.unwrap_or(600),
))
.fuse();
let t2 = async {
{
let t1 = future::join(alice.wait_for_blocks(3), bob.wait_for_blocks(3)).fuse();
let t2 = alice.task_manager.future().fuse();
let t3 = bob.task_manager.future().fuse();
pin_mut!(t1, t2, t3);
{
let t1 = future::join(alice.wait_for_blocks(3), bob.wait_for_blocks(3)).fuse();
let t2 = alice.task_manager.future().fuse();
let t3 = bob.task_manager.future().fuse();
select! {
_ = t1 => {},
_ = t2 => panic!("service Alice failed"),
_ = t3 => panic!("service Bob failed"),
}
}
pin_mut!(t1, t2, t3);
alice.task_manager.terminate();
bob.task_manager.terminate();
select! {
_ = t1 => {},
_ = t2 => panic!("service Alice failed"),
_ = t3 => panic!("service Bob failed"),
}
}
.fuse();
pin_mut!(t1, t2);
select! {
_ = t1 => {
panic!("the test took too long, maybe no blocks have been produced");
},
_ = t2 => {},
}
alice.task_manager.clean_shutdown().await;
bob.task_manager.clean_shutdown().await;
}
......@@ -14,60 +14,31 @@
// You should have received a copy of the GNU General Public License
// along with Polkadot. If not, see <http://www.gnu.org/licenses/>.
use tokio::{time::delay_for as sleep, task::spawn};
use futures::{pin_mut, select, FutureExt as _};
use polkadot_test_service::*;
use service::TaskExecutor;
use sp_keyring::Sr25519Keyring::{Alice, Bob};
use std::time::Duration;
static INTEGRATION_TEST_ALLOWED_TIME: Option<&str> = option_env!("INTEGRATION_TEST_ALLOWED_TIME");
#[tokio::test]
async fn call_function_actually_work() {
let mut alice = run_test_node(
(move |fut, _| {
spawn(fut).map(|_| ())
})
.into(),
Alice,
|| {},
Vec::new(),
#[substrate_test_utils::test]
async fn call_function_actually_work(task_executor: TaskExecutor) {
let alice = run_test_node(task_executor, Alice, || {}, Vec::new());
let function = polkadot_test_runtime::Call::Balances(pallet_balances::Call::transfer(
Default::default(),
1,
));
let output = alice.call_function(function, Bob).await.unwrap();
let res = output.result.expect("return value expected");
let json = serde_json::from_str::<serde_json::Value>(res.as_str()).expect("valid JSON");
let object = json.as_object().expect("JSON is an object");
assert!(object.contains_key("jsonrpc"), "key jsonrpc exists");
let result = object.get("result");
let result = result.expect("key result exists");
assert_eq!(
result.as_str().map(|x| x.starts_with("0x")),
Some(true),
"result starts with 0x"
);
let t1 = sleep(Duration::from_secs(
INTEGRATION_TEST_ALLOWED_TIME
.and_then(|x| x.parse().ok())
.unwrap_or(600),
))
.fuse();
let t2 = async {
let function = polkadot_test_runtime::Call::Balances(pallet_balances::Call::transfer(
Default::default(),
1,
));
let output = alice.call_function(function, Bob).await.unwrap();
let res = output.result.expect("return value expected");
let json = serde_json::from_str::<serde_json::Value>(res.as_str()).expect("valid JSON");
let object = json.as_object().expect("JSON is an object");
assert!(object.contains_key("jsonrpc"), "key jsonrpc exists");
let result = object.get("result");
let result = result.expect("key result exists");
assert_eq!(
result.as_str().map(|x| x.starts_with("0x")),
Some(true),
"result starts with 0x"
);
alice.task_manager.terminate();
}
.fuse();
pin_mut!(t1, t2);
select! {
_ = t1 => {
panic!("the test took too long, maybe no blocks have been produced");
},
_ = t2 => {},
}
alice.task_manager.clean_shutdown().await;
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment