Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
Use async test helper to simplify node testing (#1578)
Browse files Browse the repository at this point in the history
  • Loading branch information
cecton committed Aug 13, 2020
1 parent ae69d7a commit ae445ef
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 91 deletions.
21 changes: 21 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions node/test-service/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
55 changes: 15 additions & 40 deletions node/test-service/tests/build-blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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;
}
73 changes: 22 additions & 51 deletions node/test-service/tests/call-function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

0 comments on commit ae445ef

Please sign in to comment.