summaryrefslogtreecommitdiff
path: root/cli/worker.rs
diff options
context:
space:
mode:
authorBartek IwaƄczuk <biwanczuk@gmail.com>2019-11-22 18:46:57 +0100
committerRy Dahl <ry@tinyclouds.org>2019-11-22 12:46:57 -0500
commitc6bb3d5a10ba8acceadcaa66050abcaefb7bc0bb (patch)
treee60f007215fc0f146db4457db85eda8f4716d314 /cli/worker.rs
parent363b968bfcef26c30f84e485beec6194e5b1dd98 (diff)
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
Diffstat (limited to 'cli/worker.rs')
-rw-r--r--cli/worker.rs34
1 files changed, 27 insertions, 7 deletions
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: 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();