Upgrade tokio to 1.10 (#9575)

* Upgrade tokio to 1.10

* Fix test runner

* Try fix it

* Update Cargo.lock

* Review feedback

* ahhhh

* FML

* FMT

* Fix tests
This commit is contained in:
Bastian Köcher
2021-08-24 16:31:19 +02:00
committed by GitHub
parent d722c44248
commit f92e5c5e65
24 changed files with 143 additions and 378 deletions
+1 -1
View File
@@ -15,7 +15,7 @@ targets = ["x86_64-unknown-linux-gnu"]
[dependencies]
log = "0.4.11"
regex = "1.4.2"
tokio = { version = "0.2.21", features = [ "signal", "rt-core", "rt-threaded", "blocking" ] }
tokio = { version = "1.10", features = [ "signal", "rt-multi-thread" ] }
futures = "0.3.9"
fdlimit = "0.2.1"
libp2p = "0.39.1"
+3 -4
View File
@@ -73,8 +73,7 @@ where
/// Build a tokio runtime with all features
pub fn build_runtime() -> std::result::Result<tokio::runtime::Runtime, std::io::Error> {
tokio::runtime::Builder::new()
.threaded_scheduler()
tokio::runtime::Builder::new_multi_thread()
.on_thread_start(|| {
TOKIO_THREADS_ALIVE.inc();
TOKIO_THREADS_TOTAL.inc();
@@ -87,7 +86,7 @@ pub fn build_runtime() -> std::result::Result<tokio::runtime::Runtime, std::io::
}
fn run_until_exit<F, E>(
mut tokio_runtime: tokio::runtime::Runtime,
tokio_runtime: tokio::runtime::Runtime,
future: F,
task_manager: TaskManager,
) -> std::result::Result<(), E>
@@ -152,7 +151,7 @@ impl<C: SubstrateCli> Runner<C> {
/// A helper function that runs a node with tokio and stops if the process receives the signal
/// `SIGTERM` or `SIGINT`.
pub fn run_node_until_exit<F, E>(
mut self,
self,
initialize: impl FnOnce(Configuration) -> F,
) -> std::result::Result<(), E>
where
@@ -45,7 +45,7 @@ sp-timestamp = { path = "../../../primitives/timestamp", version = "4.0.0-dev" }
prometheus-endpoint = { package = "substrate-prometheus-endpoint", path = "../../../utils/prometheus", version = "0.9.0" }
[dev-dependencies]
tokio = { version = "0.2", features = ["rt-core", "macros"] }
tokio = { version = "1.10.0", features = ["rt-multi-thread", "macros"] }
sc-basic-authorship = { path = "../../basic-authorship", version = "0.10.0-dev" }
substrate-test-runtime-client = { path = "../../../test-utils/runtime/client", version = "2.0.0" }
substrate-test-runtime-transaction-pool = { path = "../../../test-utils/runtime/transaction-pool", version = "2.0.0" }
@@ -308,7 +308,7 @@ mod tests {
consensus_data_provider: None,
});
std::thread::spawn(|| {
let mut rt = tokio::runtime::Runtime::new().unwrap();
let rt = tokio::runtime::Runtime::new().unwrap();
// spawn the background authorship task
rt.block_on(future);
});
@@ -365,7 +365,7 @@ mod tests {
create_inherent_data_providers: |_, _| async { Ok(()) },
});
std::thread::spawn(|| {
let mut rt = tokio::runtime::Runtime::new().unwrap();
let rt = tokio::runtime::Runtime::new().unwrap();
// spawn the background authorship task
rt.block_on(future);
});
@@ -443,7 +443,7 @@ mod tests {
create_inherent_data_providers: |_, _| async { Ok(()) },
});
std::thread::spawn(|| {
let mut rt = tokio::runtime::Runtime::new().unwrap();
let rt = tokio::runtime::Runtime::new().unwrap();
// spawn the background authorship task
rt.block_on(future);
});
+1 -1
View File
@@ -57,5 +57,5 @@ sc-network-test = { version = "0.8.0", path = "../network/test" }
sp-keyring = { version = "4.0.0-dev", path = "../../primitives/keyring" }
substrate-test-runtime-client = { version = "2.0.0", path = "../../test-utils/runtime/client" }
sp-tracing = { version = "4.0.0-dev", path = "../../primitives/tracing" }
tokio = { version = "0.2", features = ["rt-core"] }
tokio = "1.10"
tempfile = "3.1.0"
@@ -1244,7 +1244,7 @@ fn finalize_3_voters_1_light_observer() {
#[test]
fn voter_catches_up_to_latest_round_when_behind() {
sp_tracing::try_init_simple();
let mut runtime = Runtime::new().unwrap();
let runtime = Runtime::new().unwrap();
let peers = &[Ed25519Keyring::Alice, Ed25519Keyring::Bob];
let voters = make_ids(peers);
+1 -1
View File
@@ -87,5 +87,5 @@ directories = "3.0.2"
[dev-dependencies]
substrate-test-runtime-client = { version = "2.0.0", path = "../../test-utils/runtime/client" }
substrate-test-runtime = { version = "2.0.0", path = "../../test-utils/runtime/" }
tokio = { version = "0.2.25", default-features = false }
tokio = { version = "1.10", features = ["time"] }
async-std = { version = "1.6.5", default-features = false }
+1 -1
View File
@@ -304,8 +304,8 @@ async fn build_network_future<
}
}
#[cfg(not(target_os = "unknown"))]
// Wrapper for HTTP and WS servers that makes sure they are properly shut down.
#[cfg(not(target_os = "unknown"))]
mod waiting {
pub struct HttpServer(pub Option<sc_rpc_server::HttpServer>);
impl Drop for HttpServer {
@@ -311,7 +311,13 @@ impl TaskManager {
Box::pin(async move {
join_all(children_shutdowns).await;
completion_future.await;
drop(keep_alive);
// The keep_alive stuff is holding references to some RPC handles etc. These
// RPC handles spawn their own tokio stuff and that doesn't like to be closed in an
// async context. So, we move the deletion to some other thread.
std::thread::spawn(move || {
let _ = keep_alive;
});
})
}
@@ -35,6 +35,12 @@ impl DropTester {
*self.0.lock() += 1;
DropTesterRef(self.clone())
}
fn wait_on_drop(&self) {
while *self != 0 {
std::thread::sleep(std::time::Duration::from_millis(10));
}
}
}
impl PartialEq<usize> for DropTester {
@@ -65,7 +71,7 @@ fn ensure_drop_tester_working() {
async fn run_background_task(_keep_alive: impl Any) {
loop {
tokio::time::delay_for(Duration::from_secs(1)).await;
tokio::time::sleep(Duration::from_secs(1)).await;
}
}
@@ -74,7 +80,7 @@ async fn run_background_task_blocking(duration: Duration, _keep_alive: impl Any)
// block for X sec (not interruptible)
std::thread::sleep(duration);
// await for 1 sec (interruptible)
tokio::time::delay_for(Duration::from_secs(1)).await;
tokio::time::sleep(Duration::from_secs(1)).await;
}
}
@@ -84,7 +90,7 @@ fn new_task_manager(task_executor: TaskExecutor) -> TaskManager {
#[test]
fn ensure_tasks_are_awaited_on_shutdown() {
let mut runtime = tokio::runtime::Runtime::new().unwrap();
let runtime = tokio::runtime::Runtime::new().unwrap();
let handle = runtime.handle().clone();
let task_executor: TaskExecutor = (move |future, _| handle.spawn(future).map(|_| ())).into();
@@ -95,15 +101,15 @@ fn ensure_tasks_are_awaited_on_shutdown() {
spawn_handle.spawn("task2", run_background_task(drop_tester.new_ref()));
assert_eq!(drop_tester, 2);
// allow the tasks to even start
runtime.block_on(async { tokio::time::delay_for(Duration::from_secs(1)).await });
runtime.block_on(async { tokio::time::sleep(Duration::from_secs(1)).await });
assert_eq!(drop_tester, 2);
runtime.block_on(task_manager.clean_shutdown());
assert_eq!(drop_tester, 0);
drop_tester.wait_on_drop();
}
#[test]
fn ensure_keep_alive_during_shutdown() {
let mut runtime = tokio::runtime::Runtime::new().unwrap();
let runtime = tokio::runtime::Runtime::new().unwrap();
let handle = runtime.handle().clone();
let task_executor: TaskExecutor = (move |future, _| handle.spawn(future).map(|_| ())).into();
@@ -114,15 +120,15 @@ fn ensure_keep_alive_during_shutdown() {
spawn_handle.spawn("task1", run_background_task(()));
assert_eq!(drop_tester, 1);
// allow the tasks to even start
runtime.block_on(async { tokio::time::delay_for(Duration::from_secs(1)).await });
runtime.block_on(async { tokio::time::sleep(Duration::from_secs(1)).await });
assert_eq!(drop_tester, 1);
runtime.block_on(task_manager.clean_shutdown());
assert_eq!(drop_tester, 0);
drop_tester.wait_on_drop();
}
#[test]
fn ensure_blocking_futures_are_awaited_on_shutdown() {
let mut runtime = tokio::runtime::Runtime::new().unwrap();
let runtime = tokio::runtime::Runtime::new().unwrap();
let handle = runtime.handle().clone();
let task_executor: TaskExecutor = (move |future, _| handle.spawn(future).map(|_| ())).into();
@@ -139,7 +145,7 @@ fn ensure_blocking_futures_are_awaited_on_shutdown() {
);
assert_eq!(drop_tester, 2);
// allow the tasks to even start
runtime.block_on(async { tokio::time::delay_for(Duration::from_secs(1)).await });
runtime.block_on(async { tokio::time::sleep(Duration::from_secs(1)).await });
assert_eq!(drop_tester, 2);
runtime.block_on(task_manager.clean_shutdown());
assert_eq!(drop_tester, 0);
@@ -147,7 +153,7 @@ fn ensure_blocking_futures_are_awaited_on_shutdown() {
#[test]
fn ensure_no_task_can_be_spawn_after_terminate() {
let mut runtime = tokio::runtime::Runtime::new().unwrap();
let runtime = tokio::runtime::Runtime::new().unwrap();
let handle = runtime.handle().clone();
let task_executor: TaskExecutor = (move |future, _| handle.spawn(future).map(|_| ())).into();
@@ -158,17 +164,17 @@ fn ensure_no_task_can_be_spawn_after_terminate() {
spawn_handle.spawn("task2", run_background_task(drop_tester.new_ref()));
assert_eq!(drop_tester, 2);
// allow the tasks to even start
runtime.block_on(async { tokio::time::delay_for(Duration::from_secs(1)).await });
runtime.block_on(async { tokio::time::sleep(Duration::from_secs(1)).await });
assert_eq!(drop_tester, 2);
task_manager.terminate();
spawn_handle.spawn("task3", run_background_task(drop_tester.new_ref()));
runtime.block_on(task_manager.clean_shutdown());
assert_eq!(drop_tester, 0);
drop_tester.wait_on_drop();
}
#[test]
fn ensure_task_manager_future_ends_when_task_manager_terminated() {
let mut runtime = tokio::runtime::Runtime::new().unwrap();
let runtime = tokio::runtime::Runtime::new().unwrap();
let handle = runtime.handle().clone();
let task_executor: TaskExecutor = (move |future, _| handle.spawn(future).map(|_| ())).into();
@@ -179,7 +185,7 @@ fn ensure_task_manager_future_ends_when_task_manager_terminated() {
spawn_handle.spawn("task2", run_background_task(drop_tester.new_ref()));
assert_eq!(drop_tester, 2);
// allow the tasks to even start
runtime.block_on(async { tokio::time::delay_for(Duration::from_secs(1)).await });
runtime.block_on(async { tokio::time::sleep(Duration::from_secs(1)).await });
assert_eq!(drop_tester, 2);
task_manager.terminate();
runtime.block_on(task_manager.future()).expect("future has ended without error");
@@ -189,7 +195,7 @@ fn ensure_task_manager_future_ends_when_task_manager_terminated() {
#[test]
fn ensure_task_manager_future_ends_with_error_when_essential_task_fails() {
let mut runtime = tokio::runtime::Runtime::new().unwrap();
let runtime = tokio::runtime::Runtime::new().unwrap();
let handle = runtime.handle().clone();
let task_executor: TaskExecutor = (move |future, _| handle.spawn(future).map(|_| ())).into();
@@ -201,7 +207,7 @@ fn ensure_task_manager_future_ends_with_error_when_essential_task_fails() {
spawn_handle.spawn("task2", run_background_task(drop_tester.new_ref()));
assert_eq!(drop_tester, 2);
// allow the tasks to even start
runtime.block_on(async { tokio::time::delay_for(Duration::from_secs(1)).await });
runtime.block_on(async { tokio::time::sleep(Duration::from_secs(1)).await });
assert_eq!(drop_tester, 2);
spawn_essential_handle.spawn("task3", async { panic!("task failed") });
runtime
@@ -209,12 +215,12 @@ fn ensure_task_manager_future_ends_with_error_when_essential_task_fails() {
.expect_err("future()'s Result must be Err");
assert_eq!(drop_tester, 2);
runtime.block_on(task_manager.clean_shutdown());
assert_eq!(drop_tester, 0);
drop_tester.wait_on_drop();
}
#[test]
fn ensure_children_tasks_ends_when_task_manager_terminated() {
let mut runtime = tokio::runtime::Runtime::new().unwrap();
let runtime = tokio::runtime::Runtime::new().unwrap();
let handle = runtime.handle().clone();
let task_executor: TaskExecutor = (move |future, _| handle.spawn(future).map(|_| ())).into();
@@ -233,17 +239,17 @@ fn ensure_children_tasks_ends_when_task_manager_terminated() {
spawn_handle_child_2.spawn("task4", run_background_task(drop_tester.new_ref()));
assert_eq!(drop_tester, 4);
// allow the tasks to even start
runtime.block_on(async { tokio::time::delay_for(Duration::from_secs(1)).await });
runtime.block_on(async { tokio::time::sleep(Duration::from_secs(1)).await });
assert_eq!(drop_tester, 4);
task_manager.terminate();
runtime.block_on(task_manager.future()).expect("future has ended without error");
runtime.block_on(task_manager.clean_shutdown());
assert_eq!(drop_tester, 0);
drop_tester.wait_on_drop();
}
#[test]
fn ensure_task_manager_future_ends_with_error_when_childs_essential_task_fails() {
let mut runtime = tokio::runtime::Runtime::new().unwrap();
let runtime = tokio::runtime::Runtime::new().unwrap();
let handle = runtime.handle().clone();
let task_executor: TaskExecutor = (move |future, _| handle.spawn(future).map(|_| ())).into();
@@ -263,7 +269,7 @@ fn ensure_task_manager_future_ends_with_error_when_childs_essential_task_fails()
spawn_handle_child_2.spawn("task4", run_background_task(drop_tester.new_ref()));
assert_eq!(drop_tester, 4);
// allow the tasks to even start
runtime.block_on(async { tokio::time::delay_for(Duration::from_secs(1)).await });
runtime.block_on(async { tokio::time::sleep(Duration::from_secs(1)).await });
assert_eq!(drop_tester, 4);
spawn_essential_handle_child_1.spawn("task5", async { panic!("task failed") });
runtime
@@ -271,12 +277,12 @@ fn ensure_task_manager_future_ends_with_error_when_childs_essential_task_fails()
.expect_err("future()'s Result must be Err");
assert_eq!(drop_tester, 4);
runtime.block_on(task_manager.clean_shutdown());
assert_eq!(drop_tester, 0);
drop_tester.wait_on_drop();
}
#[test]
fn ensure_task_manager_future_continues_when_childs_not_essential_task_fails() {
let mut runtime = tokio::runtime::Runtime::new().unwrap();
let runtime = tokio::runtime::Runtime::new().unwrap();
let handle = runtime.handle().clone();
let task_executor: TaskExecutor = (move |future, _| handle.spawn(future).map(|_| ())).into();
@@ -295,12 +301,12 @@ fn ensure_task_manager_future_continues_when_childs_not_essential_task_fails() {
spawn_handle_child_2.spawn("task4", run_background_task(drop_tester.new_ref()));
assert_eq!(drop_tester, 4);
// allow the tasks to even start
runtime.block_on(async { tokio::time::delay_for(Duration::from_secs(1)).await });
runtime.block_on(async { tokio::time::sleep(Duration::from_secs(1)).await });
assert_eq!(drop_tester, 4);
spawn_handle_child_1.spawn("task5", async { panic!("task failed") });
runtime.block_on(async {
let t1 = task_manager.future().fuse();
let t2 = tokio::time::delay_for(Duration::from_secs(3)).fuse();
let t2 = tokio::time::sleep(Duration::from_secs(3)).fuse();
pin_mut!(t1, t2);
@@ -311,5 +317,5 @@ fn ensure_task_manager_future_continues_when_childs_not_essential_task_fails() {
});
assert_eq!(drop_tester, 4);
runtime.block_on(task_manager.clean_shutdown());
assert_eq!(drop_tester, 0);
drop_tester.wait_on_drop();
}
+1 -2
View File
@@ -14,8 +14,7 @@ targets = ["x86_64-unknown-linux-gnu"]
[dependencies]
hex-literal = "0.3.1"
tempfile = "3.1.0"
tokio = "0.1.22"
futures01 = { package = "futures", version = "0.1.29" }
tokio = { version = "1.10.0", features = ["time"] }
log = "0.4.8"
fdlimit = "0.2.1"
parking_lot = "0.11.1"
+31 -34
View File
@@ -18,8 +18,7 @@
//! Service integration test utils.
use futures::{FutureExt as _, TryFutureExt as _};
use futures01::{Future, Poll, Stream};
use futures::{task::Poll, Future, FutureExt, TryFutureExt as _};
use log::{debug, info};
use parking_lot::Mutex;
use sc_client_api::{Backend, CallExecutor};
@@ -36,9 +35,9 @@ use sc_service::{
use sc_transaction_pool_api::TransactionPool;
use sp_blockchain::HeaderBackend;
use sp_runtime::{generic::BlockId, traits::Block as BlockT};
use std::{iter, net::Ipv4Addr, pin::Pin, sync::Arc, time::Duration};
use std::{iter, net::Ipv4Addr, pin::Pin, sync::Arc, task::Context, time::Duration};
use tempfile::TempDir;
use tokio::{prelude::FutureExt, runtime::Runtime, timer::Interval};
use tokio::{runtime::Runtime, time};
#[cfg(test)]
mod client;
@@ -57,7 +56,7 @@ struct TestNet<G, E, F, L, U> {
}
pub trait TestNetNode:
Clone + Future<Item = (), Error = sc_service::Error> + Send + 'static
Clone + Future<Output = Result<(), sc_service::Error>> + Send + 'static
{
type Block: BlockT;
type Backend: Backend<Self::Block>;
@@ -109,11 +108,10 @@ impl<TBl: BlockT, TBackend, TExec, TRtApi, TExPool> Clone
impl<TBl: BlockT, TBackend, TExec, TRtApi, TExPool> Future
for TestNetComponents<TBl, TBackend, TExec, TRtApi, TExPool>
{
type Item = ();
type Error = sc_service::Error;
type Output = Result<(), sc_service::Error>;
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
futures::compat::Compat::new(&mut self.task_manager.lock().future()).poll()
fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
Pin::new(&mut self.task_manager.lock().future()).poll(cx)
}
}
@@ -161,33 +159,36 @@ where
{
let full_nodes = self.full_nodes.clone();
let light_nodes = self.light_nodes.clone();
let interval = Interval::new_interval(Duration::from_millis(100))
.map_err(|_| ())
.for_each(move |_| {
let future = async move {
let mut interval = time::interval(Duration::from_millis(100));
loop {
interval.tick().await;
let full_ready = full_nodes
.iter()
.all(|&(ref id, ref service, _, _)| full_predicate(*id, service));
if !full_ready {
return Ok(())
continue
}
let light_ready = light_nodes
.iter()
.all(|&(ref id, ref service, _)| light_predicate(*id, service));
if !light_ready {
Ok(())
} else {
Err(())
if light_ready {
return
}
})
.timeout(MAX_WAIT_TIME);
}
};
match self.runtime.block_on(interval) {
Ok(()) => unreachable!("interval always fails; qed"),
Err(ref err) if err.is_inner() => (),
Err(_) => panic!("Waited for too long"),
if self
.runtime
.block_on(async move { time::timeout(MAX_WAIT_TIME, future).await })
.is_err()
{
panic!("Waited for too long");
}
}
}
@@ -306,11 +307,11 @@ where
light: impl Iterator<Item = impl FnOnce(Configuration) -> Result<L, Error>>,
authorities: impl Iterator<Item = (String, impl FnOnce(Configuration) -> Result<(F, U), Error>)>,
) {
let executor = self.runtime.executor();
let handle = self.runtime.handle().clone();
let task_executor: TaskExecutor = {
let executor = executor.clone();
let executor = handle.clone();
(move |fut: Pin<Box<dyn futures::Future<Output = ()> + Send>>, _| {
executor.spawn(fut.unit_error().compat());
executor.spawn(fut.unit_error());
async {}
})
.into()
@@ -330,7 +331,7 @@ where
let (service, user_data) =
authority(node_config).expect("Error creating test node service");
executor.spawn(service.clone().map_err(|_| ()));
handle.spawn(service.clone().map_err(|_| ()));
let addr = addr
.with(multiaddr::Protocol::P2p(service.network().local_peer_id().clone().into()));
self.authority_nodes.push((self.nodes, service, user_data, addr));
@@ -350,7 +351,7 @@ where
let addr = node_config.network.listen_addresses.iter().next().unwrap().clone();
let (service, user_data) = full(node_config).expect("Error creating test node service");
executor.spawn(service.clone().map_err(|_| ()));
handle.spawn(service.clone().map_err(|_| ()));
let addr = addr
.with(multiaddr::Protocol::P2p(service.network().local_peer_id().clone().into()));
self.full_nodes.push((self.nodes, service, user_data, addr));
@@ -370,7 +371,7 @@ where
let addr = node_config.network.listen_addresses.iter().next().unwrap().clone();
let service = light(node_config).expect("Error creating test node service");
executor.spawn(service.clone().map_err(|_| ()));
handle.spawn(service.clone().map_err(|_| ()));
let addr = addr
.with(multiaddr::Protocol::P2p(service.network().local_peer_id().clone().into()));
self.light_nodes.push((self.nodes, service, addr));
@@ -406,7 +407,7 @@ pub fn connectivity<G, E, Fb, F, Lb, L>(
{
let temp = tempdir_with_prefix("substrate-connectivity-test");
let runtime = {
{
let mut network = TestNet::new(
&temp,
spec.clone(),
@@ -444,12 +445,8 @@ pub fn connectivity<G, E, Fb, F, Lb, L>(
connected == expected_light_connections
},
);
network.runtime
};
runtime.shutdown_now().wait().expect("Error shutting down runtime");
temp.close().expect("Error removing temp dir");
}
{