1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-25 00:29:09 -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,22 +656,12 @@ 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()
@ -691,15 +681,21 @@ mod tests {
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());
assert!(result
.unwrap()
.code .code
.as_bytes() .as_bytes()
.starts_with("console.log(\"Hello World\");".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(
tokio_util::run(lazy(move || {
state
.ts_compiler
.bundle_async(
state.clone(), state.clone(),
module_name, module_name,
String::from("$deno$/bundle.js"), String::from("$deno$/bundle.js"),
); )
assert!(tokio_util::block_on(out).is_ok()); .then(|result| {
assert!(result.is_ok());
Ok(())
})
}))
} }
#[test] #[test]

View file

@ -612,38 +612,6 @@ mod tests {
use crate::fs as deno_fs; use crate::fs as deno_fs;
use tempfile::TempDir; use tempfile::TempDir;
impl SourceFileFetcher {
/// Fetch remote source code.
fn fetch_remote_source(
self: &Self,
module_url: &Url,
use_disk_cache: bool,
no_remote_fetch: bool,
redirect_limit: i64,
) -> Result<SourceFile, ErrBox> {
tokio_util::block_on(self.fetch_remote_source_async(
module_url,
use_disk_cache,
no_remote_fetch,
redirect_limit,
))
}
/// Synchronous version of get_source_file_async
fn get_source_file(
self: &Self,
module_url: &Url,
use_disk_cache: bool,
no_remote_fetch: bool,
) -> Result<SourceFile, ErrBox> {
tokio_util::block_on(self.get_source_file_async(
module_url,
use_disk_cache,
no_remote_fetch,
))
}
}
fn setup_file_fetcher(dir_path: &Path) -> SourceFileFetcher { fn setup_file_fetcher(dir_path: &Path) -> SourceFileFetcher {
SourceFileFetcher::new( SourceFileFetcher::new(
DiskCache::new(&dir_path.to_path_buf().join("deps")), DiskCache::new(&dir_path.to_path_buf().join("deps")),
@ -728,17 +696,24 @@ mod tests {
fn test_get_source_code_1() { fn test_get_source_code_1() {
let http_server_guard = crate::test_util::http_server(); let http_server_guard = crate::test_util::http_server();
let (temp_dir, fetcher) = test_setup(); let (temp_dir, fetcher) = test_setup();
// http_util::fetch_sync_string requires tokio let fetcher_1 = fetcher.clone();
tokio_util::init(|| { let fetcher_2 = fetcher.clone();
let module_url = let module_url =
Url::parse("http://localhost:4545/tests/subdir/mod2.ts").unwrap(); Url::parse("http://localhost:4545/tests/subdir/mod2.ts").unwrap();
let module_url_1 = module_url.clone();
let module_url_2 = module_url.clone();
let headers_file_name = fetcher.deps_cache.location.join( let headers_file_name = fetcher.deps_cache.location.join(
fetcher fetcher
.deps_cache .deps_cache
.get_cache_filename_with_extension(&module_url, "headers.json"), .get_cache_filename_with_extension(&module_url, "headers.json"),
); );
let headers_file_name_1 = headers_file_name.clone();
let headers_file_name_2 = headers_file_name.clone();
let headers_file_name_3 = headers_file_name.clone();
let result = fetcher.get_source_file(&module_url, true, false); let fut = fetcher
.get_source_file_async(&module_url, true, false)
.then(move |result| {
assert!(result.is_ok()); assert!(result.is_ok());
let r = result.unwrap(); let r = result.unwrap();
assert_eq!( assert_eq!(
@ -747,53 +722,61 @@ mod tests {
); );
assert_eq!(&(r.media_type), &msg::MediaType::TypeScript); assert_eq!(&(r.media_type), &msg::MediaType::TypeScript);
// Should not create .headers.json file due to matching ext // Should not create .headers.json file due to matching ext
assert!(fs::read_to_string(&headers_file_name).is_err()); assert!(fs::read_to_string(&headers_file_name_1).is_err());
// Modify .headers.json, write using fs write and read using save_source_code_headers // Modify .headers.json, write using fs write and read using save_source_code_headers
let _ = let _ = fs::write(
fs::write(&headers_file_name, "{ \"mime_type\": \"text/javascript\" }"); &headers_file_name_1,
let result2 = fetcher.get_source_file(&module_url, true, false); "{ \"mime_type\": \"text/javascript\" }",
);
fetcher_1.get_source_file_async(&module_url, true, false)
})
.then(move |result2| {
assert!(result2.is_ok()); assert!(result2.is_ok());
let r2 = result2.unwrap(); let r2 = result2.unwrap();
assert_eq!( assert_eq!(
r2.source_code, r2.source_code,
"export { printHello } from \"./print_hello.ts\";\n".as_bytes() "export { printHello } from \"./print_hello.ts\";\n".as_bytes()
); );
// If get_source_file does not call remote, this should be JavaScript // If get_source_file_async does not call remote, this should be JavaScript
// as we modified before! (we do not overwrite .headers.json due to no http fetch) // as we modified before! (we do not overwrite .headers.json due to no http fetch)
assert_eq!(&(r2.media_type), &msg::MediaType::JavaScript); assert_eq!(&(r2.media_type), &msg::MediaType::JavaScript);
assert_eq!( assert_eq!(
fetcher fetcher_2
.get_source_code_headers(&module_url) .get_source_code_headers(&module_url_1)
.mime_type .mime_type
.unwrap(), .unwrap(),
"text/javascript" "text/javascript"
); );
// Modify .headers.json again, but the other way around // Modify .headers.json again, but the other way around
let _ = fetcher.save_source_code_headers( let _ = fetcher_2.save_source_code_headers(
&module_url, &module_url_1,
Some("application/json".to_owned()), Some("application/json".to_owned()),
None, None,
); );
let result3 = fetcher.get_source_file(&module_url, true, false); fetcher_2.get_source_file_async(&module_url_1, true, false)
})
.then(move |result3| {
assert!(result3.is_ok()); assert!(result3.is_ok());
let r3 = result3.unwrap(); let r3 = result3.unwrap();
assert_eq!( assert_eq!(
r3.source_code, r3.source_code,
"export { printHello } from \"./print_hello.ts\";\n".as_bytes() "export { printHello } from \"./print_hello.ts\";\n".as_bytes()
); );
// If get_source_file does not call remote, this should be JavaScript // If get_source_file_async does not call remote, this should be JavaScript
// as we modified before! (we do not overwrite .headers.json due to no http fetch) // as we modified before! (we do not overwrite .headers.json due to no http fetch)
assert_eq!(&(r3.media_type), &msg::MediaType::Json); assert_eq!(&(r3.media_type), &msg::MediaType::Json);
assert!(fs::read_to_string(&headers_file_name) assert!(fs::read_to_string(&headers_file_name_2)
.unwrap() .unwrap()
.contains("application/json")); .contains("application/json"));
// let's create fresh instance of DenoDir (simulating another freshh Deno process) // let's create fresh instance of DenoDir (simulating another freshh Deno process)
// and don't use cache // and don't use cache
let fetcher = setup_file_fetcher(temp_dir.path()); let fetcher = setup_file_fetcher(temp_dir.path());
let result4 = fetcher.get_source_file(&module_url, false, false); fetcher.get_source_file_async(&module_url_2, false, false)
})
.then(move |result4| {
assert!(result4.is_ok()); assert!(result4.is_ok());
let r4 = result4.unwrap(); let r4 = result4.unwrap();
let expected4 = let expected4 =
@ -801,8 +784,12 @@ mod tests {
assert_eq!(r4.source_code, expected4); assert_eq!(r4.source_code, expected4);
// Now the old .headers.json file should have gone! Resolved back to TypeScript // Now the old .headers.json file should have gone! Resolved back to TypeScript
assert_eq!(&(r4.media_type), &msg::MediaType::TypeScript); assert_eq!(&(r4.media_type), &msg::MediaType::TypeScript);
assert!(fs::read_to_string(&headers_file_name).is_err()); assert!(fs::read_to_string(&headers_file_name_3).is_err());
Ok(())
}); });
// http_util::fetch_sync_string requires tokio
tokio_util::run(fut);
drop(http_server_guard); drop(http_server_guard);
} }
@ -810,18 +797,20 @@ mod tests {
fn test_get_source_code_2() { fn test_get_source_code_2() {
let http_server_guard = crate::test_util::http_server(); let http_server_guard = crate::test_util::http_server();
let (temp_dir, fetcher) = test_setup(); let (temp_dir, fetcher) = test_setup();
// http_util::fetch_sync_string requires tokio let fetcher_1 = fetcher.clone();
tokio_util::init(|| {
let module_url = let module_url =
Url::parse("http://localhost:4545/tests/subdir/mismatch_ext.ts") Url::parse("http://localhost:4545/tests/subdir/mismatch_ext.ts").unwrap();
.unwrap(); let module_url_1 = module_url.clone();
let module_url_2 = module_url.clone();
let headers_file_name = fetcher.deps_cache.location.join( let headers_file_name = fetcher.deps_cache.location.join(
fetcher fetcher
.deps_cache .deps_cache
.get_cache_filename_with_extension(&module_url, "headers.json"), .get_cache_filename_with_extension(&module_url, "headers.json"),
); );
let result = fetcher.get_source_file(&module_url, true, false); let fut = fetcher
.get_source_file_async(&module_url, true, false)
.then(move |result| {
assert!(result.is_ok()); assert!(result.is_ok());
let r = result.unwrap(); let r = result.unwrap();
let expected = "export const loaded = true;\n".as_bytes(); let expected = "export const loaded = true;\n".as_bytes();
@ -842,12 +831,14 @@ mod tests {
Some("text/typescript".to_owned()), Some("text/typescript".to_owned()),
None, None,
); );
let result2 = fetcher.get_source_file(&module_url, true, false); fetcher.get_source_file_async(&module_url, true, false)
})
.then(move |result2| {
assert!(result2.is_ok()); assert!(result2.is_ok());
let r2 = result2.unwrap(); let r2 = result2.unwrap();
let expected2 = "export const loaded = true;\n".as_bytes(); let expected2 = "export const loaded = true;\n".as_bytes();
assert_eq!(r2.source_code, expected2); assert_eq!(r2.source_code, expected2);
// If get_source_file does not call remote, this should be TypeScript // If get_source_file_async does not call remote, this should be TypeScript
// as we modified before! (we do not overwrite .headers.json due to no http fetch) // as we modified before! (we do not overwrite .headers.json due to no http fetch)
assert_eq!(&(r2.media_type), &msg::MediaType::TypeScript); assert_eq!(&(r2.media_type), &msg::MediaType::TypeScript);
assert!(fs::read_to_string(&headers_file_name).is_err()); assert!(fs::read_to_string(&headers_file_name).is_err());
@ -855,7 +846,9 @@ mod tests {
// let's create fresh instance of DenoDir (simulating another freshh Deno process) // let's create fresh instance of DenoDir (simulating another freshh Deno process)
// and don't use cache // and don't use cache
let fetcher = setup_file_fetcher(temp_dir.path()); let fetcher = setup_file_fetcher(temp_dir.path());
let result3 = fetcher.get_source_file(&module_url, false, false); fetcher.get_source_file_async(&module_url_1, false, false)
})
.then(move |result3| {
assert!(result3.is_ok()); assert!(result3.is_ok());
let r3 = result3.unwrap(); let r3 = result3.unwrap();
let expected3 = "export const loaded = true;\n".as_bytes(); let expected3 = "export const loaded = true;\n".as_bytes();
@ -864,13 +857,16 @@ mod tests {
// (due to http fetch) // (due to http fetch)
assert_eq!(&(r3.media_type), &msg::MediaType::JavaScript); assert_eq!(&(r3.media_type), &msg::MediaType::JavaScript);
assert_eq!( assert_eq!(
fetcher fetcher_1
.get_source_code_headers(&module_url) .get_source_code_headers(&module_url_2)
.mime_type .mime_type
.unwrap(), .unwrap(),
"text/javascript" "text/javascript"
); );
Ok(())
}); });
tokio_util::run(fut);
drop(http_server_guard); drop(http_server_guard);
} }
@ -924,8 +920,7 @@ mod tests {
fn test_get_source_code_3() { fn test_get_source_code_3() {
let http_server_guard = crate::test_util::http_server(); let http_server_guard = crate::test_util::http_server();
let (_temp_dir, fetcher) = test_setup(); let (_temp_dir, fetcher) = test_setup();
// Test basic follow and headers recording
tokio_util::init(|| {
let redirect_module_url = let redirect_module_url =
Url::parse("http://localhost:4546/tests/subdir/redirects/redirect1.js") Url::parse("http://localhost:4546/tests/subdir/redirects/redirect1.js")
.unwrap(); .unwrap();
@ -945,9 +940,12 @@ mod tests {
let redirect_target_filename = let redirect_target_filename =
redirect_target_filepath.to_str().unwrap().to_string(); redirect_target_filepath.to_str().unwrap().to_string();
let mod_meta = fetcher // Test basic follow and headers recording
.get_source_file(&redirect_module_url, true, false) let fut = fetcher
.unwrap(); .get_source_file_async(&redirect_module_url, true, false)
.then(move |result| {
assert!(result.is_ok());
let mod_meta = result.unwrap();
// File that requires redirection is not downloaded. // File that requires redirection is not downloaded.
assert!(fs::read_to_string(&redirect_source_filename).is_err()); assert!(fs::read_to_string(&redirect_source_filename).is_err());
// ... but its .headers.json is created. // ... but its .headers.json is created.
@ -968,7 +966,10 @@ mod tests {
// Examine the meta result. // Examine the meta result.
assert_eq!(mod_meta.url.clone(), target_module_url); assert_eq!(mod_meta.url.clone(), target_module_url);
Ok(())
}); });
tokio_util::run(fut);
drop(http_server_guard); drop(http_server_guard);
} }
@ -976,8 +977,6 @@ mod tests {
fn test_get_source_code_4() { fn test_get_source_code_4() {
let http_server_guard = crate::test_util::http_server(); let http_server_guard = crate::test_util::http_server();
let (_temp_dir, fetcher) = test_setup(); let (_temp_dir, fetcher) = test_setup();
// Test double redirects and headers recording
tokio_util::init(|| {
let double_redirect_url = let double_redirect_url =
Url::parse("http://localhost:4548/tests/subdir/redirects/redirect1.js") Url::parse("http://localhost:4548/tests/subdir/redirects/redirect1.js")
.unwrap(); .unwrap();
@ -1002,10 +1001,12 @@ mod tests {
.location .location
.join("http/localhost_PORT4545/tests/subdir/redirects/redirect1.js"); .join("http/localhost_PORT4545/tests/subdir/redirects/redirect1.js");
let mod_meta = fetcher // Test double redirects and headers recording
.get_source_file(&double_redirect_url, true, false) let fut = fetcher
.unwrap(); .get_source_file_async(&double_redirect_url, true, false)
.then(move |result| {
assert!(result.is_ok());
let mod_meta = result.unwrap();
assert!(fs::read_to_string(&double_redirect_path).is_err()); assert!(fs::read_to_string(&double_redirect_path).is_err());
assert!(fs::read_to_string(&redirect_path).is_err()); assert!(fs::read_to_string(&redirect_path).is_err());
@ -1032,7 +1033,10 @@ mod tests {
// Examine the meta result. // Examine the meta result.
assert_eq!(mod_meta.url.clone(), target_url); assert_eq!(mod_meta.url.clone(), target_url);
Ok(())
}); });
tokio_util::run(fut);
drop(http_server_guard); drop(http_server_guard);
} }
@ -1040,8 +1044,7 @@ mod tests {
fn test_get_source_code_5() { fn test_get_source_code_5() {
let http_server_guard = crate::test_util::http_server(); let http_server_guard = crate::test_util::http_server();
let (_temp_dir, fetcher) = test_setup(); let (_temp_dir, fetcher) = test_setup();
// Test that redirect target is not downloaded twice for different redirect source.
tokio_util::init(|| {
let double_redirect_url = let double_redirect_url =
Url::parse("http://localhost:4548/tests/subdir/redirects/redirect1.js") Url::parse("http://localhost:4548/tests/subdir/redirects/redirect1.js")
.unwrap(); .unwrap();
@ -1054,11 +1057,13 @@ mod tests {
.deps_cache .deps_cache
.location .location
.join("http/localhost_PORT4545/tests/subdir/redirects/redirect1.js"); .join("http/localhost_PORT4545/tests/subdir/redirects/redirect1.js");
let target_path_ = target_path.clone();
fetcher // Test that redirect target is not downloaded twice for different redirect source.
.get_source_file(&double_redirect_url, true, false) let fut = fetcher
.unwrap(); .get_source_file_async(&double_redirect_url, true, false)
.then(move |result| {
assert!(result.is_ok());
let result = fs::File::open(&target_path); let result = fs::File::open(&target_path);
assert!(result.is_ok()); assert!(result.is_ok());
let file = result.unwrap(); let file = result.unwrap();
@ -1069,9 +1074,14 @@ mod tests {
// When another file is fetched that also point to redirect target, then redirect target // When another file is fetched that also point to redirect target, then redirect target
// shouldn't be downloaded again. It can be verified using source header file creation // shouldn't be downloaded again. It can be verified using source header file creation
// timestamp (should be the same as after first `get_source_file`) // timestamp (should be the same as after first `get_source_file`)
fetcher.get_source_file(&redirect_url, true, false).unwrap(); fetcher
.get_source_file_async(&redirect_url, true, false)
let result = fs::File::open(&target_path); .map(move |r| (r, file_modified))
})
.then(move |result| {
assert!(result.is_ok());
let (_, file_modified) = result.unwrap();
let result = fs::File::open(&target_path_);
assert!(result.is_ok()); assert!(result.is_ok());
let file_2 = result.unwrap(); let file_2 = result.unwrap();
// save modified timestamp for headers file // save modified timestamp for headers file
@ -1079,7 +1089,10 @@ mod tests {
let file_modified_2 = file_metadata_2.modified().unwrap(); let file_modified_2 = file_metadata_2.modified().unwrap();
assert_eq!(file_modified, file_modified_2); assert_eq!(file_modified, file_modified_2);
Ok(())
}); });
tokio_util::run(fut);
drop(http_server_guard); drop(http_server_guard);
} }
@ -1087,21 +1100,25 @@ mod tests {
fn test_get_source_code_6() { fn test_get_source_code_6() {
let http_server_guard = crate::test_util::http_server(); let http_server_guard = crate::test_util::http_server();
let (_temp_dir, fetcher) = test_setup(); let (_temp_dir, fetcher) = test_setup();
// Test that redirections can be limited
tokio_util::init(|| {
let double_redirect_url = let double_redirect_url =
Url::parse("http://localhost:4548/tests/subdir/redirects/redirect1.js") Url::parse("http://localhost:4548/tests/subdir/redirects/redirect1.js")
.unwrap(); .unwrap();
let result = // Test that redirections can be limited
fetcher.fetch_remote_source(&double_redirect_url, false, false, 2); let fut = fetcher
.fetch_remote_source_async(&double_redirect_url, false, false, 2)
.then(move |result| {
assert!(result.is_ok()); assert!(result.is_ok());
let result = fetcher.fetch_remote_source_async(&double_redirect_url, false, false, 1)
fetcher.fetch_remote_source(&double_redirect_url, false, false, 1); })
.then(move |result| {
assert!(result.is_err()); assert!(result.is_err());
let err = result.err().unwrap(); let err = result.err().unwrap();
assert_eq!(err.kind(), ErrorKind::TooManyRedirects); assert_eq!(err.kind(), ErrorKind::TooManyRedirects);
Ok(())
}); });
tokio_util::run(fut);
drop(http_server_guard); drop(http_server_guard);
} }
@ -1109,31 +1126,40 @@ mod tests {
fn test_get_source_code_no_fetch() { fn test_get_source_code_no_fetch() {
let http_server_guard = crate::test_util::http_server(); let http_server_guard = crate::test_util::http_server();
let (_temp_dir, fetcher) = test_setup(); let (_temp_dir, fetcher) = test_setup();
tokio_util::init(|| { let fetcher_1 = fetcher.clone();
let fetcher_2 = fetcher.clone();
let module_url = let module_url =
Url::parse("http://localhost:4545/tests/002_hello.ts").unwrap(); Url::parse("http://localhost:4545/tests/002_hello.ts").unwrap();
let module_url_1 = module_url.clone();
let module_url_2 = module_url.clone();
// file hasn't been cached before and remote downloads are not allowed // file hasn't been cached before and remote downloads are not allowed
let result = fetcher.get_source_file(&module_url, true, true); let fut = fetcher
.get_source_file_async(&module_url, true, true)
.then(move |result| {
assert!(result.is_err()); assert!(result.is_err());
let err = result.err().unwrap(); let err = result.err().unwrap();
assert_eq!(err.kind(), ErrorKind::NotFound); assert_eq!(err.kind(), ErrorKind::NotFound);
// download and cache file // download and cache file
let result = fetcher.get_source_file(&module_url, true, false); fetcher_1.get_source_file_async(&module_url_1, true, false)
})
.then(move |result| {
assert!(result.is_ok()); assert!(result.is_ok());
// module is already cached, should be ok even with `no_remote_fetch` // module is already cached, should be ok even with `no_remote_fetch`
let result = fetcher.get_source_file(&module_url, true, true); fetcher_2.get_source_file_async(&module_url_2, true, true)
})
.then(move |result| {
assert!(result.is_ok()); assert!(result.is_ok());
Ok(())
}); });
tokio_util::run(fut);
drop(http_server_guard); drop(http_server_guard);
} }
#[test] #[test]
fn test_fetch_source_async_1() { fn test_fetch_source_async_1() {
let http_server_guard = crate::test_util::http_server(); let http_server_guard = crate::test_util::http_server();
tokio_util::init(|| {
let (_temp_dir, fetcher) = test_setup(); let (_temp_dir, fetcher) = test_setup();
let module_url = let module_url =
Url::parse("http://127.0.0.1:4545/tests/subdir/mt_video_mp2t.t3.ts") Url::parse("http://127.0.0.1:4545/tests/subdir/mt_video_mp2t.t3.ts")
@ -1144,19 +1170,15 @@ mod tests {
.get_cache_filename_with_extension(&module_url, "headers.json"), .get_cache_filename_with_extension(&module_url, "headers.json"),
); );
let result = tokio_util::block_on(fetcher.fetch_remote_source_async( let fut = fetcher
&module_url, .fetch_remote_source_async(&module_url, false, false, 10)
false, .then(move |result| {
false,
10,
));
assert!(result.is_ok()); assert!(result.is_ok());
let r = result.unwrap(); let r = result.unwrap();
assert_eq!(r.source_code, b"export const loaded = true;\n"); assert_eq!(r.source_code, b"export const loaded = true;\n");
assert_eq!(&(r.media_type), &msg::MediaType::TypeScript); assert_eq!(&(r.media_type), &msg::MediaType::TypeScript);
// matching ext, no .headers.json file created // matching ext, no .headers.json file created
assert!(fs::read_to_string(&headers_file_name).is_err()); assert!(fs::read_to_string(&headers_file_name).is_err());
// Modify .headers.json, make sure read from local // Modify .headers.json, make sure read from local
let _ = fetcher.save_source_code_headers( let _ = fetcher.save_source_code_headers(
&module_url, &module_url,
@ -1169,14 +1191,17 @@ mod tests {
assert_eq!(r2.source_code, b"export const loaded = true;\n"); assert_eq!(r2.source_code, b"export const loaded = true;\n");
// Not MediaType::TypeScript due to .headers.json modification // Not MediaType::TypeScript due to .headers.json modification
assert_eq!(&(r2.media_type), &msg::MediaType::JavaScript); assert_eq!(&(r2.media_type), &msg::MediaType::JavaScript);
Ok(())
}); });
tokio_util::run(fut);
drop(http_server_guard); drop(http_server_guard);
} }
#[test] #[test]
fn test_fetch_source_1() { fn test_fetch_source_1() {
let http_server_guard = crate::test_util::http_server(); let http_server_guard = crate::test_util::http_server();
tokio_util::init(|| {
let (_temp_dir, fetcher) = test_setup(); let (_temp_dir, fetcher) = test_setup();
let module_url = let module_url =
Url::parse("http://localhost:4545/tests/subdir/mt_video_mp2t.t3.ts") Url::parse("http://localhost:4545/tests/subdir/mt_video_mp2t.t3.ts")
@ -1187,7 +1212,9 @@ mod tests {
.get_cache_filename_with_extension(&module_url, "headers.json"), .get_cache_filename_with_extension(&module_url, "headers.json"),
); );
let result = fetcher.fetch_remote_source(&module_url, false, false, 10); let fut = fetcher
.fetch_remote_source_async(&module_url, false, false, 10)
.then(move |result| {
assert!(result.is_ok()); assert!(result.is_ok());
let r = result.unwrap(); let r = result.unwrap();
assert_eq!(r.source_code, "export const loaded = true;\n".as_bytes()); assert_eq!(r.source_code, "export const loaded = true;\n".as_bytes());
@ -1207,68 +1234,80 @@ mod tests {
assert_eq!(r2.source_code, "export const loaded = true;\n".as_bytes()); assert_eq!(r2.source_code, "export const loaded = true;\n".as_bytes());
// Not MediaType::TypeScript due to .headers.json modification // Not MediaType::TypeScript due to .headers.json modification
assert_eq!(&(r2.media_type), &msg::MediaType::JavaScript); assert_eq!(&(r2.media_type), &msg::MediaType::JavaScript);
Ok(())
}); });
tokio_util::run(fut);
drop(http_server_guard); drop(http_server_guard);
} }
#[test] #[test]
fn test_fetch_source_2() { fn test_fetch_source_2() {
let http_server_guard = crate::test_util::http_server(); let http_server_guard = crate::test_util::http_server();
tokio_util::init(|| {
let (_temp_dir, fetcher) = test_setup(); let (_temp_dir, fetcher) = test_setup();
let fetcher_1 = fetcher.clone();
let fetcher_2 = fetcher.clone();
let fetcher_3 = fetcher.clone();
let module_url = let module_url =
Url::parse("http://localhost:4545/tests/subdir/no_ext").unwrap(); Url::parse("http://localhost:4545/tests/subdir/no_ext").unwrap();
let result = fetcher.fetch_remote_source(&module_url, false, false, 10); let module_url_2 =
Url::parse("http://localhost:4545/tests/subdir/mismatch_ext.ts").unwrap();
let module_url_2_ = module_url_2.clone();
let module_url_3 =
Url::parse("http://localhost:4545/tests/subdir/unknown_ext.deno")
.unwrap();
let module_url_3_ = module_url_3.clone();
let fut = fetcher
.fetch_remote_source_async(&module_url, false, false, 10)
.then(move |result| {
assert!(result.is_ok()); assert!(result.is_ok());
let r = result.unwrap(); let r = result.unwrap();
assert_eq!(r.source_code, "export const loaded = true;\n".as_bytes()); assert_eq!(r.source_code, "export const loaded = true;\n".as_bytes());
assert_eq!(&(r.media_type), &msg::MediaType::TypeScript); assert_eq!(&(r.media_type), &msg::MediaType::TypeScript);
// no ext, should create .headers.json file // no ext, should create .headers.json file
assert_eq!( assert_eq!(
fetcher fetcher_1
.get_source_code_headers(&module_url) .get_source_code_headers(&module_url)
.mime_type .mime_type
.unwrap(), .unwrap(),
"text/typescript" "text/typescript"
); );
fetcher_1.fetch_remote_source_async(&module_url_2, false, false, 10)
let module_url_2 = })
Url::parse("http://localhost:4545/tests/subdir/mismatch_ext.ts") .then(move |result| {
.unwrap(); assert!(result.is_ok());
let result_2 = let r2 = result.unwrap();
fetcher.fetch_remote_source(&module_url_2, false, false, 10);
assert!(result_2.is_ok());
let r2 = result_2.unwrap();
assert_eq!(r2.source_code, "export const loaded = true;\n".as_bytes()); assert_eq!(r2.source_code, "export const loaded = true;\n".as_bytes());
assert_eq!(&(r2.media_type), &msg::MediaType::JavaScript); assert_eq!(&(r2.media_type), &msg::MediaType::JavaScript);
// mismatch ext, should create .headers.json file // mismatch ext, should create .headers.json file
assert_eq!( assert_eq!(
fetcher fetcher_2
.get_source_code_headers(&module_url_2) .get_source_code_headers(&module_url_2_)
.mime_type .mime_type
.unwrap(), .unwrap(),
"text/javascript" "text/javascript"
); );
// test unknown extension // test unknown extension
let module_url_3 = fetcher_2.fetch_remote_source_async(&module_url_3, false, false, 10)
Url::parse("http://localhost:4545/tests/subdir/unknown_ext.deno") })
.unwrap(); .then(move |result| {
let result_3 = assert!(result.is_ok());
fetcher.fetch_remote_source(&module_url_3, false, false, 10); let r3 = result.unwrap();
assert!(result_3.is_ok());
let r3 = result_3.unwrap();
assert_eq!(r3.source_code, "export const loaded = true;\n".as_bytes()); assert_eq!(r3.source_code, "export const loaded = true;\n".as_bytes());
assert_eq!(&(r3.media_type), &msg::MediaType::TypeScript); assert_eq!(&(r3.media_type), &msg::MediaType::TypeScript);
// unknown ext, should create .headers.json file // unknown ext, should create .headers.json file
assert_eq!( assert_eq!(
fetcher fetcher_3
.get_source_code_headers(&module_url_3) .get_source_code_headers(&module_url_3_)
.mime_type .mime_type
.unwrap(), .unwrap(),
"text/typescript" "text/typescript"
); );
futures::future::ok(())
}); });
tokio_util::run(fut);
drop(http_server_guard); drop(http_server_guard);
} }

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
.execute_mod_async(&module_specifier, false)
.then(|result| {
if let Err(err) = result { if let Err(err) = result {
eprintln!("execute_mod err {:?}", err); eprintln!("execute_mod err {:?}", err);
} }
tokio_util::panic_on_error(worker) 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
.execute_mod_async(&module_specifier, false)
.then(|result| {
if let Err(err) = result { if let Err(err) = result {
eprintln!("execute_mod err {:?}", err); eprintln!("execute_mod err {:?}", err);
} }
tokio_util::panic_on_error(worker) 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
.execute_mod_async(&module_specifier, false)
.then(|result| {
if let Err(err) = result { if let Err(err) = result {
eprintln!("execute_mod err {:?}", err); eprintln!("execute_mod err {:?}", err);
} }
tokio_util::panic_on_error(worker) tokio_util::panic_on_error(worker)
})
})); }));
let metrics = &state_.metrics; let metrics = &state_.metrics;