summaryrefslogtreecommitdiff
path: root/runtime/worker.rs
diff options
context:
space:
mode:
authorMatt Mastracci <matthew@mastracci.com>2023-12-05 09:26:06 -0700
committerGitHub <noreply@github.com>2023-12-05 09:26:06 -0700
commit4a9f42950140b36b1916ab0e0320d721223b1095 (patch)
tree772f89bb86f9c9120700de6ae6859b0d9a704da6 /runtime/worker.rs
parentce83a849a4612a170b08b4cd2a8e78c6456cdcf4 (diff)
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/
Diffstat (limited to 'runtime/worker.rs')
-rw-r--r--runtime/worker.rs24
1 files changed, 23 insertions, 1 deletions
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,