1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-25 15:29:32 -05:00

fix(core): Wake up the runtime if there are ticks scheduled (#12933)

This commit is contained in:
Andreu Botella 2021-11-30 00:31:12 +01:00 committed by GitHub
parent 18a63dd977
commit 48c57001c8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -825,7 +825,10 @@ impl JsRuntime {
// TODO(andreubotella) The event loop will spin as long as there are pending
// background tasks. We should look into having V8 notify us when a
// background task is done.
if state.have_unpolled_ops || has_pending_background_tasks {
if state.have_unpolled_ops
|| has_pending_background_tasks
|| has_tick_scheduled
{
state.waker.wake();
}
@ -834,6 +837,7 @@ impl JsRuntime {
|| has_pending_dyn_imports
|| has_pending_dyn_module_evaluation
|| has_pending_background_tasks
|| has_tick_scheduled
{
// pass, will be polled again
} else {
@ -846,6 +850,7 @@ impl JsRuntime {
if has_pending_refed_ops
|| has_pending_dyn_imports
|| has_pending_background_tasks
|| has_tick_scheduled
{
// pass, will be polled again
} else if state.dyn_module_evaluate_idle_counter >= 1 {
@ -2527,9 +2532,10 @@ assertEquals(1, notify_return_value);
assert_eq!(state.js_nexttick_cbs.len(), 2);
}
#[tokio::test]
async fn test_has_tick_scheduled() {
run_in_task(|cx| {
#[test]
fn test_has_tick_scheduled() {
use futures::task::ArcWake;
let macrotask = Arc::new(AtomicUsize::default());
let macrotask_ = Arc::clone(&macrotask);
@ -2569,23 +2575,42 @@ assertEquals(1, notify_return_value);
"#,
)
.unwrap();
struct ArcWakeImpl(Arc<AtomicUsize>);
impl ArcWake for ArcWakeImpl {
fn wake_by_ref(arc_self: &Arc<Self>) {
arc_self.0.fetch_add(1, Ordering::Relaxed);
}
}
let awoken_times = Arc::new(AtomicUsize::new(0));
let waker =
futures::task::waker(Arc::new(ArcWakeImpl(awoken_times.clone())));
let cx = &mut Context::from_waker(&waker);
assert!(matches!(runtime.poll_event_loop(cx, false), Poll::Pending));
assert_eq!(1, macrotask.load(Ordering::Relaxed));
assert_eq!(1, next_tick.load(Ordering::Relaxed));
assert_eq!(awoken_times.swap(0, Ordering::Relaxed), 1);
assert!(matches!(runtime.poll_event_loop(cx, false), Poll::Pending));
assert_eq!(awoken_times.swap(0, Ordering::Relaxed), 1);
assert!(matches!(runtime.poll_event_loop(cx, false), Poll::Pending));
assert_eq!(awoken_times.swap(0, Ordering::Relaxed), 1);
assert!(matches!(runtime.poll_event_loop(cx, false), Poll::Pending));
assert_eq!(awoken_times.swap(0, Ordering::Relaxed), 1);
let state_rc = JsRuntime::state(runtime.v8_isolate());
state_rc.borrow_mut().has_tick_scheduled = false;
assert!(matches!(
runtime.poll_event_loop(cx, false),
Poll::Ready(Ok(()))
));
assert_eq!(awoken_times.load(Ordering::Relaxed), 0);
assert!(matches!(
runtime.poll_event_loop(cx, false),
Poll::Ready(Ok(()))
));
});
assert_eq!(awoken_times.load(Ordering::Relaxed), 0);
}
#[test]