From c6bb3d5a10ba8acceadcaa66050abcaefb7bc0bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Fri, 22 Nov 2019 18:46:57 +0100 Subject: remove tokio_util::block_on (#3388) This PR removes tokio_util::block_on - refactored compiler and file fetcher slightly so that we can safely block there - that's because only blocking path consist of only synchronous operations. Additionally I removed excessive use of tokio_util::panic_on_error and tokio_util::run_in_task and moved both functions to cli/worker.rs, to tests module. Closes #2960 --- cli/worker.rs | 34 +++++++++++++++++++++++++++------- 1 file changed, 27 insertions(+), 7 deletions(-) (limited to 'cli/worker.rs') diff --git a/cli/worker.rs b/cli/worker.rs index 08ac43659..b9802d581 100644 --- a/cli/worker.rs +++ b/cli/worker.rs @@ -209,6 +209,26 @@ mod tests { use futures::executor::block_on; use std::sync::atomic::Ordering; + pub fn run_in_task(f: F) + where + F: FnOnce() + Send + 'static, + { + let fut = futures::future::lazy(move |_cx| { + f(); + Ok(()) + }); + + tokio_util::run(fut) + } + + pub fn panic_on_error(f: F) -> impl Future> + where + F: Future>, + E: std::fmt::Debug, + { + f.map_err(|err| panic!("Future got unexpected error: {:?}", err)) + } + #[test] fn execute_mod_esm_imports_a() { let p = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")) @@ -243,7 +263,7 @@ mod tests { if let Err(err) = result { eprintln!("execute_mod err {:?}", err); } - tokio_util::panic_on_error(worker).await + panic_on_error(worker).await }); let metrics = &state_.metrics; @@ -283,7 +303,7 @@ mod tests { if let Err(err) = result { eprintln!("execute_mod err {:?}", err); } - tokio_util::panic_on_error(worker).await + panic_on_error(worker).await }); let metrics = &state_.metrics; @@ -333,7 +353,7 @@ mod tests { if let Err(err) = result { eprintln!("execute_mod err {:?}", err); } - tokio_util::panic_on_error(worker).await + panic_on_error(worker).await }); assert_eq!(state_.metrics.resolve_count.load(Ordering::SeqCst), 3); @@ -364,7 +384,7 @@ mod tests { #[test] fn test_worker_messages() { - tokio_util::run_in_task(|| { + run_in_task(|| { let mut worker = create_test_worker(); let source = r#" onmessage = function(e) { @@ -412,7 +432,7 @@ mod tests { #[test] fn removed_from_resource_table_on_close() { - tokio_util::run_in_task(|| { + run_in_task(|| { let mut worker = create_test_worker(); worker .execute("onmessage = () => { delete window.onmessage; }") @@ -444,7 +464,7 @@ mod tests { #[test] fn execute_mod_resolve_error() { - tokio_util::run_in_task(|| { + run_in_task(|| { // "foo" is not a valid module specifier so this should return an error. let mut worker = create_test_worker(); let module_specifier = @@ -457,7 +477,7 @@ mod tests { #[test] fn execute_mod_002_hello() { - tokio_util::run_in_task(|| { + run_in_task(|| { // This assumes cwd is project root (an assumption made throughout the // tests). let mut worker = create_test_worker(); -- cgit v1.2.3