remove more calls to futures::executor::block_on (#4775)

This commit is contained in:
Bartek Iwańczuk 2020-04-16 16:29:28 +02:00 committed by GitHub
parent 5f250bb148
commit 0c48470b35
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 58 additions and 64 deletions

View File

@ -1,6 +1,6 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
use super::dispatch_json::{Deserialize, JsonOp, Value}; use super::dispatch_json::{Deserialize, JsonOp, Value};
use super::io::{StreamResource, StreamResourceHolder}; use super::io::{std_file_resource, StreamResource, StreamResourceHolder};
use crate::op_error::OpError; use crate::op_error::OpError;
use crate::signal::kill; use crate::signal::kill;
use crate::state::State; use crate::state::State;
@ -22,19 +22,11 @@ pub fn init(i: &mut Isolate, s: &State) {
fn clone_file(rid: u32, state: &State) -> Result<std::fs::File, OpError> { fn clone_file(rid: u32, state: &State) -> Result<std::fs::File, OpError> {
let mut state = state.borrow_mut(); let mut state = state.borrow_mut();
let repr_holder = state
.resource_table std_file_resource(&mut state.resource_table, rid, move |r| match r {
.get_mut::<StreamResourceHolder>(rid) Ok(std_file) => std_file.try_clone().map_err(OpError::from),
.ok_or_else(OpError::bad_resource_id)?; Err(_) => Err(OpError::bad_resource_id()),
match repr_holder.resource { })
StreamResource::FsFile(Some((ref mut file, _))) => {
let tokio_file = futures::executor::block_on(file.try_clone())?;
let std_file = futures::executor::block_on(tokio_file.into_std());
Ok(std_file)
}
StreamResource::FsFile(None) => Err(OpError::resource_unavailable()),
_ => Err(OpError::bad_resource_id()),
}
} }
fn subprocess_stdio_map(s: &str) -> std::process::Stdio { fn subprocess_stdio_map(s: &str) -> std::process::Stdio {

View File

@ -65,22 +65,41 @@ pub fn op_set_raw(
use winapi::shared::minwindef::FALSE; use winapi::shared::minwindef::FALSE;
use winapi::um::{consoleapi, handleapi}; use winapi::um::{consoleapi, handleapi};
let state = state_.borrow_mut(); let mut state = state_.borrow_mut();
let resource_holder = state.resource_table.get::<StreamResourceHolder>(rid); let resource_holder =
state.resource_table.get_mut::<StreamResourceHolder>(rid);
if resource_holder.is_none() { if resource_holder.is_none() {
return Err(OpError::bad_resource_id()); return Err(OpError::bad_resource_id());
} }
let resource_holder = resource_holder.unwrap();
// For now, only stdin. // For now, only stdin.
let handle = match &resource_holder.unwrap().resource { let handle = match &mut resource_holder.resource {
StreamResource::Stdin(_, _) => std::io::stdin().as_raw_handle(), StreamResource::Stdin(_, _) => std::io::stdin().as_raw_handle(),
StreamResource::FsFile(None) => { StreamResource::FsFile(ref mut option_file_metadata) => {
return Err(OpError::resource_unavailable()) if let Some((tokio_file, metadata)) = option_file_metadata.take() {
} match tokio_file.try_into_std() {
StreamResource::FsFile(Some((f, _))) => { Ok(std_file) => {
let tokio_file = futures::executor::block_on(f.try_clone())?; let raw_handle = std_file.as_raw_handle();
let std_file = futures::executor::block_on(tokio_file.into_std()); // Turn the std_file handle back into a tokio file, put it back
std_file.as_raw_handle() // in the resource table.
let tokio_file = tokio::fs::File::from_std(std_file);
resource_holder.resource =
StreamResource::FsFile(Some((tokio_file, metadata)));
// return the result.
raw_handle
}
Err(tokio_file) => {
// This function will return an error containing the file if
// some operation is in-flight.
resource_holder.resource =
StreamResource::FsFile(Some((tokio_file, metadata)));
return Err(OpError::resource_unavailable());
}
}
} else {
return Err(OpError::resource_unavailable());
}
} }
_ => { _ => {
return Err(OpError::bad_resource_id()); return Err(OpError::bad_resource_id());
@ -127,9 +146,7 @@ pub fn op_set_raw(
(std::io::stdin().as_raw_fd(), &mut metadata.mode) (std::io::stdin().as_raw_fd(), &mut metadata.mode)
} }
StreamResource::FsFile(Some((f, ref mut metadata))) => { StreamResource::FsFile(Some((f, ref mut metadata))) => {
let tokio_file = futures::executor::block_on(f.try_clone())?; (f.as_raw_fd(), &mut metadata.tty.mode)
let std_file = futures::executor::block_on(tokio_file.into_std());
(std_file.as_raw_fd(), &mut metadata.tty.mode)
} }
StreamResource::FsFile(None) => { StreamResource::FsFile(None) => {
return Err(OpError::resource_unavailable()) return Err(OpError::resource_unavailable())
@ -173,9 +190,7 @@ pub fn op_set_raw(
(std::io::stdin().as_raw_fd(), &mut metadata.mode) (std::io::stdin().as_raw_fd(), &mut metadata.mode)
} }
StreamResource::FsFile(Some((f, ref mut metadata))) => { StreamResource::FsFile(Some((f, ref mut metadata))) => {
let tokio_file = futures::executor::block_on(f.try_clone())?; (f.as_raw_fd(), &mut metadata.tty.mode)
let std_file = futures::executor::block_on(tokio_file.into_std());
(std_file.as_raw_fd(), &mut metadata.tty.mode)
} }
StreamResource::FsFile(None) => { StreamResource::FsFile(None) => {
return Err(OpError::resource_unavailable()); return Err(OpError::resource_unavailable());

View File

@ -270,17 +270,8 @@ mod tests {
use crate::startup_data; use crate::startup_data;
use crate::state::State; use crate::state::State;
use crate::tokio_util; use crate::tokio_util;
use futures::executor::block_on;
use std::sync::atomic::Ordering; 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());
tokio_util::run_basic(fut)
}
#[test] #[test]
fn execute_mod_esm_imports_a() { fn execute_mod_esm_imports_a() {
let p = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")) let p = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"))
@ -405,32 +396,28 @@ mod tests {
worker worker
} }
#[test] #[tokio::test]
fn execute_mod_resolve_error() { async fn execute_mod_resolve_error() {
run_in_task(|| { // "foo" is not a valid module specifier so this should return an error.
// "foo" is not a valid module specifier so this should return an error. let mut worker = create_test_worker();
let mut worker = create_test_worker(); let module_specifier =
let module_specifier = ModuleSpecifier::resolve_url_or_path("does-not-exist").unwrap();
ModuleSpecifier::resolve_url_or_path("does-not-exist").unwrap(); let result = worker.execute_module(&module_specifier).await;
let result = block_on(worker.execute_module(&module_specifier)); assert!(result.is_err());
assert!(result.is_err());
})
} }
#[test] #[tokio::test]
fn execute_mod_002_hello() { async fn execute_mod_002_hello() {
run_in_task(|| { // This assumes cwd is project root (an assumption made throughout the
// This assumes cwd is project root (an assumption made throughout the // tests).
// tests). let mut worker = create_test_worker();
let mut worker = create_test_worker(); 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("cli/tests/002_hello.ts");
.join("cli/tests/002_hello.ts"); let module_specifier =
let module_specifier = ModuleSpecifier::resolve_url_or_path(&p.to_string_lossy()).unwrap();
ModuleSpecifier::resolve_url_or_path(&p.to_string_lossy()).unwrap(); let result = worker.execute_module(&module_specifier).await;
let result = block_on(worker.execute_module(&module_specifier)); assert!(result.is_ok());
assert!(result.is_ok());
})
} }
} }