From 4a9f42950140b36b1916ab0e0320d721223b1095 Mon Sep 17 00:00:00 2001 From: Matt Mastracci Date: Tue, 5 Dec 2023 09:26:06 -0700 Subject: refactor(cli): refactor bench/test for future module changes (#21460) Extracting some refactorings for the module work that will land in https://github.com/denoland/deno_core/pull/359/ --- runtime/web_worker.rs | 2 -- runtime/worker.rs | 24 +++++++++++++++++++++++- 2 files changed, 23 insertions(+), 3 deletions(-) (limited to 'runtime') diff --git a/runtime/web_worker.rs b/runtime/web_worker.rs index e1d4651d6..e5adf4cc6 100644 --- a/runtime/web_worker.rs +++ b/runtime/web_worker.rs @@ -698,7 +698,6 @@ impl WebWorker { event_loop_result = self.js_runtime.run_event_loop(false) => { event_loop_result?; - receiver.await } } @@ -730,7 +729,6 @@ impl WebWorker { return Ok(()); } event_loop_result?; - receiver.await } } diff --git a/runtime/worker.rs b/runtime/worker.rs index e91c85969..e3e97de7e 100644 --- a/runtime/worker.rs +++ b/runtime/worker.rs @@ -6,6 +6,7 @@ use std::sync::atomic::Ordering::Relaxed; use std::sync::Arc; use std::task::Context; use std::task::Poll; +use std::time::Duration; use std::time::Instant; use deno_broadcast_channel::InMemoryBroadcastChannel; @@ -29,6 +30,7 @@ use deno_core::ModuleLoader; use deno_core::ModuleSpecifier; use deno_core::OpMetricsFactoryFn; use deno_core::OpMetricsSummaryTracker; +use deno_core::PollEventLoopOptions; use deno_core::RuntimeOptions; use deno_core::SharedArrayBufferStore; use deno_core::Snapshot; @@ -553,12 +555,32 @@ impl MainWorker { event_loop_result = self.run_event_loop(false) => { event_loop_result?; - receiver.await } } } + /// Run the event loop up to a given duration. If the runtime resolves early, returns + /// early. Will always poll the runtime at least once. + pub async fn run_up_to_duration( + &mut self, + duration: Duration, + ) -> Result<(), AnyError> { + match tokio::time::timeout( + duration, + self.js_runtime.run_event_loop2(PollEventLoopOptions { + wait_for_inspector: false, + pump_v8_message_loop: true, + }), + ) + .await + { + Ok(Ok(_)) => Ok(()), + Err(_) => Ok(()), + Ok(Err(e)) => Err(e), + } + } + /// Loads, instantiates and executes specified JavaScript module. pub async fn execute_side_module( &mut self, -- cgit v1.2.3