diff --git a/core/modules.rs b/core/modules.rs index bc795de5cf..9352301ba8 100644 --- a/core/modules.rs +++ b/core/modules.rs @@ -1740,6 +1740,7 @@ mod tests { use crate::RuntimeOptions; use crate::Snapshot; use deno_ops::op; + use futures::future::poll_fn; use futures::future::FutureExt; use parking_lot::Mutex; use std::fmt; @@ -1754,12 +1755,6 @@ mod tests { pub use crate::*; } - // TODO(ry) Sadly FuturesUnordered requires the current task to be set. So - // even though we are only using poll() in these tests and not Tokio, we must - // nevertheless run it in the tokio executor. Ideally run_in_task can be - // removed in the future. - use crate::runtime::tests::run_in_task; - #[derive(Default)] struct MockLoader { pub loads: Arc>>, @@ -1907,7 +1902,7 @@ import "/a.js"; } if inner.url == "file:///slow.js" && inner.counter < 2 { // TODO(ry) Hopefully in the future we can remove current task - // notification. See comment above run_in_task. + // notification. cx.waker().wake_by_ref(); return Poll::Pending; } @@ -2263,8 +2258,8 @@ import "/a.js"; futures::executor::block_on(receiver).unwrap().unwrap(); } - #[test] - fn dyn_import_err() { + #[tokio::test] + async fn dyn_import_err() { #[derive(Clone, Default)] struct DynImportErrLoader { pub count: Arc, @@ -2302,7 +2297,7 @@ import "/a.js"; }); // Test an erroneous dynamic import where the specified module isn't found. - run_in_task(move |cx| { + poll_fn(move |cx| { runtime .execute_script_static( "file:///dyn_import2.js", @@ -2320,7 +2315,9 @@ import "/a.js"; unreachable!(); } assert_eq!(count.load(Ordering::Relaxed), 4); + Poll::Ready(()) }) + .await; } #[derive(Clone, Default)] @@ -2369,8 +2366,8 @@ import "/a.js"; } } - #[test] - fn dyn_import_ok() { + #[tokio::test] + async fn dyn_import_ok() { let loader = Rc::new(DynImportOkLoader::default()); let prepare_load_count = loader.prepare_load_count.clone(); let resolve_count = loader.resolve_count.clone(); @@ -2379,7 +2376,7 @@ import "/a.js"; module_loader: Some(loader), ..Default::default() }); - run_in_task(move |cx| { + poll_fn(move |cx| { // Dynamically import mod_b runtime .execute_script_static( @@ -2413,11 +2410,13 @@ import "/a.js"; )); assert_eq!(resolve_count.load(Ordering::Relaxed), 7); assert_eq!(load_count.load(Ordering::Relaxed), 1); + Poll::Ready(()) }) + .await; } - #[test] - fn dyn_import_borrow_mut_error() { + #[tokio::test] + async fn dyn_import_borrow_mut_error() { // https://github.com/denoland/deno/issues/6054 let loader = Rc::new(DynImportOkLoader::default()); let prepare_load_count = loader.prepare_load_count.clone(); @@ -2426,7 +2425,7 @@ import "/a.js"; ..Default::default() }); - run_in_task(move |cx| { + poll_fn(move |cx| { runtime .execute_script_static( "file:///dyn_import3.js", @@ -2445,7 +2444,9 @@ import "/a.js"; assert_eq!(prepare_load_count.load(Ordering::Relaxed), 1); // Second poll triggers error let _ = runtime.poll_event_loop(cx, false); + Poll::Ready(()) }) + .await; } // Regression test for https://github.com/denoland/deno/issues/3736. @@ -2671,8 +2672,8 @@ import "/a.js"; futures::executor::block_on(fut); } - #[test] - fn slow_never_ready_modules() { + #[tokio::test] + async fn slow_never_ready_modules() { let loader = MockLoader::new(); let loads = loader.loads.clone(); let mut runtime = JsRuntime::new(RuntimeOptions { @@ -2680,7 +2681,7 @@ import "/a.js"; ..Default::default() }); - run_in_task(move |cx| { + poll_fn(move |cx| { let spec = resolve_url("file:///main.js").unwrap(); let mut recursive_load = runtime.load_main_module(&spec, None).boxed_local(); @@ -2694,8 +2695,7 @@ import "/a.js"; // "file:///never_ready.js", // "file:///slow.js" // But due to current task notification in DelayedSourceCodeFuture they - // all get loaded in a single poll. Also see the comment above - // run_in_task. + // all get loaded in a single poll. for _ in 0..10 { let result = recursive_load.poll_unpin(cx); @@ -2714,30 +2714,26 @@ import "/a.js"; ] ); } + Poll::Ready(()) }) + .await; } - #[test] - fn loader_disappears_after_error() { + #[tokio::test] + async fn loader_disappears_after_error() { let loader = MockLoader::new(); let mut runtime = JsRuntime::new(RuntimeOptions { module_loader: Some(loader), ..Default::default() }); - run_in_task(move |cx| { - let spec = resolve_url("file:///bad_import.js").unwrap(); - let mut load_fut = runtime.load_main_module(&spec, None).boxed_local(); - let result = load_fut.poll_unpin(cx); - if let Poll::Ready(Err(err)) = result { - assert_eq!( - err.downcast_ref::().unwrap(), - &MockError::ResolveErr - ); - } else { - unreachable!(); - } - }) + let spec = resolve_url("file:///bad_import.js").unwrap(); + let result = runtime.load_main_module(&spec, None).await; + let err = result.unwrap_err(); + assert_eq!( + err.downcast_ref::().unwrap(), + &MockError::ResolveErr + ); } #[test] diff --git a/core/runtime.rs b/core/runtime.rs index 1cbefb6fe9..8c78be55b5 100644 --- a/core/runtime.rs +++ b/core/runtime.rs @@ -2610,8 +2610,6 @@ pub mod tests { use crate::modules::SymbolicModule; use crate::ZeroCopyBuf; use deno_ops::op; - use futures::future::lazy; - use std::ops::FnOnce; use std::pin::Pin; use std::rc::Rc; use std::sync::atomic::AtomicUsize; @@ -2623,13 +2621,6 @@ pub mod tests { pub use crate::*; } - pub fn run_in_task(f: F) - where - F: FnOnce(&mut Context) + 'static, - { - futures::executor::block_on(lazy(move |cx| f(cx))); - } - #[derive(Copy, Clone)] pub enum Mode { Async, @@ -2864,7 +2855,7 @@ pub mod tests { #[tokio::test] async fn test_poll_value() { let mut runtime = JsRuntime::new(Default::default()); - run_in_task(move |cx| { + poll_fn(move |cx| { let value_global = runtime .execute_script_static("a.js", "Promise.resolve(1 + 2)") .unwrap(); @@ -2903,7 +2894,8 @@ pub mod tests { .unwrap(); let v = runtime.poll_value(&value_global, cx); matches!(v, Poll::Ready(Err(e)) if e.to_string() == "Promise resolution is still pending but the event loop has already resolved."); - }); + Poll::Ready(()) + }).await; } #[tokio::test] @@ -3061,10 +3053,10 @@ pub mod tests { assert_eq!(frame.column_number, Some(12)); } - #[test] - fn test_encode_decode() { + #[tokio::test] + async fn test_encode_decode() { let (mut runtime, _dispatch_count) = setup(Mode::Async); - run_in_task(move |cx| { + poll_fn(move |cx| { runtime .execute_script( "encode_decode_test.js", @@ -3075,13 +3067,15 @@ pub mod tests { if let Poll::Ready(Err(_)) = runtime.poll_event_loop(cx, false) { unreachable!(); } - }); + Poll::Ready(()) + }) + .await; } - #[test] - fn test_serialize_deserialize() { + #[tokio::test] + async fn test_serialize_deserialize() { let (mut runtime, _dispatch_count) = setup(Mode::Async); - run_in_task(move |cx| { + poll_fn(move |cx| { runtime .execute_script( "serialize_deserialize_test.js", @@ -3091,11 +3085,13 @@ pub mod tests { if let Poll::Ready(Err(_)) = runtime.poll_event_loop(cx, false) { unreachable!(); } - }); + Poll::Ready(()) + }) + .await; } - #[test] - fn test_error_builder() { + #[tokio::test] + async fn test_error_builder() { #[op] fn op_err() -> Result<(), Error> { Err(custom_error("DOMExceptionOperationError", "abc")) @@ -3111,7 +3107,7 @@ pub mod tests { get_error_class_fn: Some(&get_error_class_name), ..Default::default() }); - run_in_task(move |cx| { + poll_fn(move |cx| { runtime .execute_script_static( "error_builder_test.js", @@ -3121,7 +3117,9 @@ pub mod tests { if let Poll::Ready(Err(_)) = runtime.poll_event_loop(cx, false) { unreachable!(); } - }); + Poll::Ready(()) + }) + .await; } #[test] @@ -3646,10 +3644,10 @@ main(); assert_eq!(result.unwrap_err().to_string(), expected_error); } - #[test] - fn test_error_async_stack() { + #[tokio::test] + async fn test_error_async_stack() { let mut runtime = JsRuntime::new(RuntimeOptions::default()); - run_in_task(move |cx| { + poll_fn(move |cx| { runtime .execute_script_static( "error_async_stack.js", @@ -3680,11 +3678,13 @@ main(); } _ => panic!(), }; + Poll::Ready(()) }) + .await; } - #[test] - fn test_error_context() { + #[tokio::test] + async fn test_error_context() { use anyhow::anyhow; #[op] @@ -3703,7 +3703,7 @@ main(); ..Default::default() }); - run_in_task(move |cx| { + poll_fn(move |cx| { runtime .execute_script_static( "test_error_context_sync.js", @@ -3746,13 +3746,14 @@ if (errMessage !== "higher-level sync error: original sync error") { Poll::Ready(Err(err)) => panic!("{err:?}"), _ => panic!(), } - }) + Poll::Ready(()) + }).await; } - #[test] - fn test_pump_message_loop() { + #[tokio::test] + async fn test_pump_message_loop() { let mut runtime = JsRuntime::new(RuntimeOptions::default()); - run_in_task(move |cx| { + poll_fn(move |cx| { runtime .execute_script_static( "pump_message_loop.js", @@ -3797,7 +3798,9 @@ assertEquals(1, notify_return_value); r#"assertEquals(globalThis.resolved, true);"#, ) .unwrap(); + Poll::Ready(()) }) + .await; } #[test] @@ -4695,7 +4698,7 @@ Deno.core.opAsync("op_async_serialize_object_with_numbers_as_keys", { ..Default::default() }); - run_in_task(move |cx| { + poll_fn(move |cx| { let main_realm = runtime.global_realm(); let other_realm = runtime.create_realm().unwrap(); @@ -4747,7 +4750,9 @@ Deno.core.opAsync("op_async_serialize_object_with_numbers_as_keys", { runtime.poll_event_loop(cx, false), Poll::Ready(Ok(())) )); - }); + Poll::Ready(()) + }) + .await; } #[test]