1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-24 08:09:08 -05:00

remove more calls to tokio_util::block_on (#3059)

towards #2960
This commit is contained in:
Bartek Iwańczuk 2019-10-06 21:03:30 +02:00 committed by Ryan Dahl
parent 3e02d7ddbc
commit e1d49fe0fe
4 changed files with 566 additions and 518 deletions

View file

@ -656,50 +656,46 @@ mod tests {
use crate::fs as deno_fs; use crate::fs as deno_fs;
use crate::tokio_util; use crate::tokio_util;
use deno::ModuleSpecifier; use deno::ModuleSpecifier;
use futures::future::lazy;
use std::path::PathBuf; use std::path::PathBuf;
use tempfile::TempDir; use tempfile::TempDir;
impl TsCompiler {
fn compile_sync(
self: &Self,
state: ThreadSafeState,
source_file: &SourceFile,
) -> Result<CompiledModule, ErrBox> {
tokio_util::block_on(self.compile_async(state, source_file))
}
}
#[test] #[test]
fn test_compile_sync() { fn test_compile_async() {
tokio_util::init(|| { let p = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"))
let p = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")) .parent()
.parent() .unwrap()
.unwrap() .join("tests/002_hello.ts")
.join("tests/002_hello.ts") .to_owned();
.to_owned(); let specifier =
let specifier = ModuleSpecifier::resolve_url_or_path(p.to_str().unwrap()).unwrap();
ModuleSpecifier::resolve_url_or_path(p.to_str().unwrap()).unwrap();
let out = SourceFile { let out = SourceFile {
url: specifier.as_url().clone(), url: specifier.as_url().clone(),
filename: PathBuf::from(p.to_str().unwrap().to_string()), filename: PathBuf::from(p.to_str().unwrap().to_string()),
media_type: msg::MediaType::TypeScript, media_type: msg::MediaType::TypeScript,
source_code: include_bytes!("../tests/002_hello.ts").to_vec(), source_code: include_bytes!("../tests/002_hello.ts").to_vec(),
}; };
let mock_state = ThreadSafeState::mock(vec![ let mock_state = ThreadSafeState::mock(vec![
String::from("deno"), String::from("deno"),
String::from("hello.js"), String::from("hello.js"),
]); ]);
let compiled = mock_state
tokio_util::run(lazy(move || {
mock_state
.ts_compiler .ts_compiler
.compile_sync(mock_state.clone(), &out) .compile_async(mock_state.clone(), &out)
.unwrap(); .then(|result| {
assert!(compiled assert!(result.is_ok());
.code assert!(result
.as_bytes() .unwrap()
.starts_with("console.log(\"Hello World\");".as_bytes())); .code
}) .as_bytes()
.starts_with("console.log(\"Hello World\");".as_bytes()));
Ok(())
})
}))
} }
#[test] #[test]
@ -719,12 +715,20 @@ mod tests {
p.to_string_lossy().into(), p.to_string_lossy().into(),
String::from("$deno$/bundle.js"), String::from("$deno$/bundle.js"),
]); ]);
let out = state.ts_compiler.bundle_async(
state.clone(), tokio_util::run(lazy(move || {
module_name, state
String::from("$deno$/bundle.js"), .ts_compiler
); .bundle_async(
assert!(tokio_util::block_on(out).is_ok()); state.clone(),
module_name,
String::from("$deno$/bundle.js"),
)
.then(|result| {
assert!(result.is_ok());
Ok(())
})
}))
} }
#[test] #[test]

File diff suppressed because it is too large Load diff

View file

@ -139,22 +139,22 @@ mod tests {
use super::*; use super::*;
use crate::tokio_util; use crate::tokio_util;
pub fn fetch_string_once_sync(url: &Url) -> Result<FetchOnceResult, ErrBox> {
tokio_util::block_on(fetch_string_once(url))
}
#[test] #[test]
fn test_fetch_sync_string() { fn test_fetch_sync_string() {
let http_server_guard = crate::test_util::http_server(); let http_server_guard = crate::test_util::http_server();
// Relies on external http server. See tools/http_server.py // Relies on external http server. See tools/http_server.py
let url = Url::parse("http://127.0.0.1:4545/package.json").unwrap(); let url = Url::parse("http://127.0.0.1:4545/package.json").unwrap();
tokio_util::init(|| match fetch_string_once_sync(&url).unwrap() {
FetchOnceResult::Code(code, maybe_content_type) => { let fut = fetch_string_once(&url).then(|result| match result {
Ok(FetchOnceResult::Code(code, maybe_content_type)) => {
assert!(!code.is_empty()); assert!(!code.is_empty());
assert_eq!(maybe_content_type, Some("application/json".to_string())); assert_eq!(maybe_content_type, Some("application/json".to_string()));
Ok(())
} }
_ => unreachable!(), _ => panic!(),
}); });
tokio_util::run(fut);
drop(http_server_guard); drop(http_server_guard);
} }
@ -165,10 +165,15 @@ mod tests {
let url = Url::parse("http://127.0.0.1:4546/package.json").unwrap(); let url = Url::parse("http://127.0.0.1:4546/package.json").unwrap();
// Dns resolver substitutes `127.0.0.1` with `localhost` // Dns resolver substitutes `127.0.0.1` with `localhost`
let target_url = Url::parse("http://localhost:4545/package.json").unwrap(); let target_url = Url::parse("http://localhost:4545/package.json").unwrap();
tokio_util::init(|| { let fut = fetch_string_once(&url).then(move |result| match result {
let result = fetch_string_once_sync(&url).unwrap(); Ok(FetchOnceResult::Redirect(url)) => {
assert_eq!(result, FetchOnceResult::Redirect(target_url)); assert_eq!(url, target_url);
Ok(())
}
_ => panic!(),
}); });
tokio_util::run(fut);
drop(http_server_guard); drop(http_server_guard);
} }

View file

@ -4,7 +4,6 @@ use crate::ops::json_op;
use crate::ops::minimal_op; use crate::ops::minimal_op;
use crate::ops::*; use crate::ops::*;
use crate::state::ThreadSafeState; use crate::state::ThreadSafeState;
use crate::tokio_util;
use deno; use deno;
use deno::ErrBox; use deno::ErrBox;
use deno::ModuleSpecifier; use deno::ModuleSpecifier;
@ -345,15 +344,6 @@ impl Worker {
} }
}) })
} }
/// Executes the provided JavaScript module.
pub fn execute_mod(
&mut self,
module_specifier: &ModuleSpecifier,
is_prefetch: bool,
) -> Result<(), ErrBox> {
tokio_util::block_on(self.execute_mod_async(module_specifier, is_prefetch))
}
} }
impl Future for Worker { impl Future for Worker {
@ -399,11 +389,14 @@ mod tests {
tokio_util::run(lazy(move || { tokio_util::run(lazy(move || {
let mut worker = let mut worker =
Worker::new("TEST".to_string(), StartupData::None, state); Worker::new("TEST".to_string(), StartupData::None, state);
let result = worker.execute_mod(&module_specifier, false); worker
if let Err(err) = result { .execute_mod_async(&module_specifier, false)
eprintln!("execute_mod err {:?}", err); .then(|result| {
} if let Err(err) = result {
tokio_util::panic_on_error(worker) eprintln!("execute_mod err {:?}", err);
}
tokio_util::panic_on_error(worker)
})
})); }));
let metrics = &state_.metrics; let metrics = &state_.metrics;
@ -433,11 +426,14 @@ mod tests {
tokio_util::run(lazy(move || { tokio_util::run(lazy(move || {
let mut worker = let mut worker =
Worker::new("TEST".to_string(), StartupData::None, state); Worker::new("TEST".to_string(), StartupData::None, state);
let result = worker.execute_mod(&module_specifier, false); worker
if let Err(err) = result { .execute_mod_async(&module_specifier, false)
eprintln!("execute_mod err {:?}", err); .then(|result| {
} if let Err(err) = result {
tokio_util::panic_on_error(worker) eprintln!("execute_mod err {:?}", err);
}
tokio_util::panic_on_error(worker)
})
})); }));
let metrics = &state_.metrics; let metrics = &state_.metrics;
@ -449,6 +445,7 @@ mod tests {
#[test] #[test]
fn execute_006_url_imports() { fn execute_006_url_imports() {
let http_server_guard = crate::test_util::http_server(); let http_server_guard = crate::test_util::http_server();
let p = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")) let p = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.parent() .parent()
.unwrap() .unwrap()
@ -469,11 +466,14 @@ mod tests {
state, state,
); );
worker.execute("denoMain()").unwrap(); worker.execute("denoMain()").unwrap();
let result = worker.execute_mod(&module_specifier, false); worker
if let Err(err) = result { .execute_mod_async(&module_specifier, false)
eprintln!("execute_mod err {:?}", err); .then(|result| {
} if let Err(err) = result {
tokio_util::panic_on_error(worker) eprintln!("execute_mod err {:?}", err);
}
tokio_util::panic_on_error(worker)
})
})); }));
let metrics = &state_.metrics; let metrics = &state_.metrics;