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
This commit is contained in:
Bartek Iwańczuk 2019-11-22 18:46:57 +01:00 committed by Ry Dahl
parent 363b968bfc
commit c6bb3d5a10
5 changed files with 116 additions and 143 deletions

View file

@ -209,6 +209,26 @@ mod tests {
use futures::executor::block_on;
use std::sync::atomic::Ordering;
pub fn run_in_task<F>(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<I, E, F>(f: F) -> impl Future<Output = Result<I, ()>>
where
F: Future<Output = Result<I, E>>,
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();