mirror of
https://github.com/lexmount/moli.git
synced 2026-11-21 00:03:15 +00:00
feat(fetch): add event-loop idle wait mode
Wait for user timers, native async operations, dedicated workers, WebSockets and receiving MessagePorts using their existing resource owners and Page task sources. Finish a microtask checkpoint before observing exhaustion, drain terminal callbacks, and follow replacement navigations through the shared idle wait driver. Apply the existing absolute timeout to idle readiness and recheck after automation scripts. Keep internal AbortSignal timeouts and transferable-stream plumbing from independently retaining the loop. Add CLI and native observer regressions.
This commit is contained in:
@@ -194,4 +194,23 @@ impl Page {
|
||||
RendererPageReply::Unit => Ok(()),
|
||||
)
|
||||
}
|
||||
|
||||
pub(crate) async fn wait_for_idle(
|
||||
&mut self,
|
||||
loader: &ResourceRequestClient,
|
||||
timeout: Duration,
|
||||
) -> Result<()> {
|
||||
let timeout_ms = timeout.as_millis().min(u128::from(u64::MAX)) as u64;
|
||||
let (reply, page_state) = self
|
||||
.handle
|
||||
.wait_for_idle(timeout_ms, loader.clone())
|
||||
.await?;
|
||||
self.replace_page_state(page_state);
|
||||
expect_page_reply!(
|
||||
reply,
|
||||
"wait for event loop idle page command",
|
||||
"a unit reply",
|
||||
RendererPageReply::Unit => Ok(()),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,6 +16,7 @@ pub enum FetchTimeoutPhase {
|
||||
WaitingForSubresourceResponse,
|
||||
WaitingForSelector,
|
||||
WaitingForScript,
|
||||
WaitingForIdle,
|
||||
}
|
||||
|
||||
impl FetchTimeoutPhase {
|
||||
@@ -54,6 +55,7 @@ impl fmt::Display for FetchTimeoutPhase {
|
||||
Self::WaitingForSubresourceResponse => "waiting for a subresource response",
|
||||
Self::WaitingForSelector => "waiting for a selector",
|
||||
Self::WaitingForScript => "waiting for a script to become truthy",
|
||||
Self::WaitingForIdle => "waiting for idle",
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -100,6 +100,7 @@ fn is_fetch_readiness_timeout(error: &anyhow::Error, wait_until: RenderedDomWait
|
||||
RenderedDomWaitUntil::DomStable => "timed out waiting for domstable",
|
||||
RenderedDomWaitUntil::DomContentLoaded
|
||||
| RenderedDomWaitUntil::Load
|
||||
| RenderedDomWaitUntil::Idle
|
||||
| RenderedDomWaitUntil::Done => return false,
|
||||
};
|
||||
|
||||
@@ -179,6 +180,10 @@ pub enum RenderedDomWaitUntil {
|
||||
Load,
|
||||
NetworkIdle,
|
||||
DomStable,
|
||||
/// Drain user-owned asynchronous work and its callbacks after Load.
|
||||
/// Future timers and live resources keep this wait pending; an unbacked
|
||||
/// Promise does not. Unlike NetworkIdle, timeout is an error.
|
||||
Idle,
|
||||
Done,
|
||||
}
|
||||
|
||||
@@ -189,7 +194,7 @@ impl RenderedDomWaitUntil {
|
||||
fn base_stage(self) -> PageVmInitStage {
|
||||
match self {
|
||||
Self::DomContentLoaded | Self::DomStable => PageVmInitStage::DomContentLoaded,
|
||||
Self::Load | Self::NetworkIdle | Self::Done => PageVmInitStage::Load,
|
||||
Self::Load | Self::NetworkIdle | Self::Idle | Self::Done => PageVmInitStage::Load,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -510,8 +515,8 @@ impl Browser {
|
||||
}
|
||||
|
||||
/// Reaches the concrete lifecycle boundary that makes a live Page
|
||||
/// available. For NetworkIdle and DomStable this is only the Load or DCL
|
||||
/// base stage; their best-effort observation runs outside materialization.
|
||||
/// available. For Idle, NetworkIdle and DomStable this is only the Load or
|
||||
/// DCL base stage; their observations run outside materialization.
|
||||
async fn fetch_document_to_base_stage(
|
||||
&self,
|
||||
request: Request,
|
||||
@@ -729,6 +734,14 @@ impl Browser {
|
||||
.await
|
||||
}
|
||||
|
||||
/// Waits for user timers, network operations and live Worker, WebSocket and
|
||||
/// receiving MessagePort resources to finish, including their callbacks.
|
||||
pub async fn wait_for_page_idle(&self, page: &mut Page, timeout: Duration) -> Result<()> {
|
||||
let deadline = FetchDeadline::new(timeout)?;
|
||||
self.wait_for_page_readiness_with_deadline(page, RenderedDomWaitUntil::Idle, deadline)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn wait_for_selector(
|
||||
&self,
|
||||
page: &mut Page,
|
||||
@@ -819,18 +832,28 @@ impl Browser {
|
||||
.await
|
||||
}
|
||||
|
||||
/// Observes NetworkIdle or DomStable with the unspent part of `deadline`.
|
||||
/// Observes Idle, NetworkIdle or DomStable with the unspent deadline.
|
||||
///
|
||||
/// A timeout in this observation is deliberately best-effort: the Page
|
||||
/// has already reached its required Load/DCL base stage, so the current
|
||||
/// snapshot is returned with a warning. Errors unrelated to timeout still
|
||||
/// fail the fetch. Concrete lifecycle modes are no-ops here.
|
||||
/// fail the fetch. Idle requires resource exhaustion and propagates timeout
|
||||
/// as an error. Concrete lifecycle modes are no-ops here.
|
||||
pub async fn wait_for_page_readiness_with_deadline(
|
||||
&self,
|
||||
page: &mut Page,
|
||||
wait_until: RenderedDomWaitUntil,
|
||||
deadline: FetchDeadline,
|
||||
) -> Result<()> {
|
||||
if wait_until == RenderedDomWaitUntil::Idle {
|
||||
let loader = self.resource_request_client();
|
||||
return deadline
|
||||
.wait(
|
||||
FetchTimeoutPhase::WaitingForIdle,
|
||||
page.wait_for_idle(&loader, deadline.timeout()),
|
||||
)
|
||||
.await;
|
||||
}
|
||||
if !wait_until.has_best_effort_page_wait() {
|
||||
return Ok(());
|
||||
}
|
||||
@@ -851,6 +874,7 @@ impl Browser {
|
||||
}
|
||||
RenderedDomWaitUntil::DomContentLoaded
|
||||
| RenderedDomWaitUntil::Load
|
||||
| RenderedDomWaitUntil::Idle
|
||||
| RenderedDomWaitUntil::Done => unreachable!(
|
||||
"concrete lifecycle modes returned before starting a best-effort Page wait"
|
||||
),
|
||||
|
||||
@@ -419,7 +419,8 @@ pub(crate) use self::message_ports::{
|
||||
discard_message_port_channel, dispatch_message_port_events_for_port_collecting_errors,
|
||||
dispatch_one_authorized_message_port_event, ensure_message_port_wrapper_for_id,
|
||||
ensure_message_port_wrapper_for_id_in_realm, message_port_id_from_object,
|
||||
message_port_transfer_from_object, new_disconnected_message_port_object,
|
||||
message_port_keeps_event_loop_alive, message_port_transfer_from_object,
|
||||
new_disconnected_message_port_object,
|
||||
};
|
||||
pub(crate) use self::microtask_checkpoint::{
|
||||
install_agent_microtask_checkpoint_tasks, run_end_of_microtask_checkpoint_tasks,
|
||||
|
||||
@@ -24,8 +24,8 @@ pub(crate) use state::{
|
||||
MessagePortRealmBinding, TransferredMessagePort, detach_message_port_owner_for_transfer,
|
||||
detach_transferred_message_port, discard_message_port_channel,
|
||||
ensure_message_port_wrapper_for_id, ensure_message_port_wrapper_for_id_in_realm,
|
||||
message_port_id_from_object, message_port_transfer_from_object,
|
||||
new_disconnected_message_port_object,
|
||||
message_port_id_from_object, message_port_keeps_event_loop_alive,
|
||||
message_port_transfer_from_object, new_disconnected_message_port_object,
|
||||
};
|
||||
pub(in crate::context_bootstrap) use state::{
|
||||
close_message_port_object, current_message_port_registry, set_internal_message_port_handlers,
|
||||
|
||||
@@ -12,6 +12,7 @@ use crate::{
|
||||
use moli_webapi_declare::{WebApiFunctionTemplate, WebApiObject};
|
||||
|
||||
const MESSAGE_PORT_ID_SLOT: &str = "__lmMessagePortId";
|
||||
const MESSAGE_PORT_INTERNAL_SLOT: &str = "__lmMessagePortInternal";
|
||||
|
||||
/// A closed connection remains transferable; a neutered wrapper does not.
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
@@ -447,6 +448,19 @@ pub(in crate::context_bootstrap::message_ports) fn message_port_is_closed<'s>(
|
||||
message_port_bool_slot(scope, port, MESSAGE_PORT_CLOSED_SLOT).unwrap_or(false)
|
||||
}
|
||||
|
||||
/// Inspect native slots and listener storage, without invoking author getters.
|
||||
pub(crate) fn message_port_keeps_event_loop_alive<'s>(
|
||||
scope: &mut v8::PinScope<'s, '_>,
|
||||
port: v8::Local<'s, v8::Object>,
|
||||
) -> bool {
|
||||
!message_port_is_closed(scope, port)
|
||||
&& message_port_is_started(scope, port)
|
||||
&& !message_port_bool_slot(scope, port, MESSAGE_PORT_INTERNAL_SLOT).unwrap_or(false)
|
||||
&& ["message", "messageerror"].iter().any(|event_type| {
|
||||
!simple_object_event_listeners_snapshot(scope, port, event_type).is_empty()
|
||||
})
|
||||
}
|
||||
|
||||
fn message_port_onmessage_getter_callback<'s>(
|
||||
scope: &mut v8::PinScope<'s, '_>,
|
||||
args: v8::FunctionCallbackArguments<'s>,
|
||||
@@ -563,6 +577,9 @@ pub(in crate::context_bootstrap) fn set_internal_message_port_handlers<'s>(
|
||||
onmessage: v8::Local<'s, v8::Function>,
|
||||
onmessageerror: v8::Local<'s, v8::Function>,
|
||||
) {
|
||||
// Transferable streams use private ports for bookkeeping. A pending read
|
||||
// without an active producer must not keep the host event loop alive.
|
||||
set_message_port_bool_slot(scope, port, MESSAGE_PORT_INTERNAL_SLOT, true);
|
||||
set_message_port_event_handler(
|
||||
scope,
|
||||
port,
|
||||
|
||||
@@ -92,7 +92,17 @@ impl DocumentRuntime {
|
||||
scope: &mut v8::PinScope<'s, '_>,
|
||||
callback: v8::Local<'s, v8::Function>,
|
||||
) -> bool {
|
||||
self.timeouts.queue_internal_task(scope, callback)
|
||||
self.queue_internal_task_after(scope, callback, 0)
|
||||
}
|
||||
|
||||
pub(crate) fn queue_internal_task_after<'s>(
|
||||
&mut self,
|
||||
scope: &mut v8::PinScope<'s, '_>,
|
||||
callback: v8::Local<'s, v8::Function>,
|
||||
delay_ms: u64,
|
||||
) -> bool {
|
||||
self.timeouts
|
||||
.queue_internal_task_after(scope, callback, delay_ms)
|
||||
}
|
||||
|
||||
pub(crate) fn queue_timeout_with_receiver<'s>(
|
||||
@@ -322,6 +332,10 @@ impl DocumentRuntime {
|
||||
self.timeouts.ms_to_next()
|
||||
}
|
||||
|
||||
pub(crate) fn has_keep_alive_timers(&self) -> bool {
|
||||
self.timeouts.has_keep_alive_timers()
|
||||
}
|
||||
|
||||
pub(crate) fn next_timeout_deadline(&self) -> Option<std::time::Instant> {
|
||||
self.timeouts.next_deadline()
|
||||
}
|
||||
|
||||
@@ -278,10 +278,11 @@ impl HostTimeoutScheduler {
|
||||
/// Browser tasks share the Window task scheduler and its retirement checks,
|
||||
/// but are not JS timer handles that clearTimeout can cancel.
|
||||
/// Returns whether the task was admitted by the owner and Realm checks.
|
||||
pub(crate) fn queue_internal_task<'s>(
|
||||
pub(crate) fn queue_internal_task_after<'s>(
|
||||
&mut self,
|
||||
scope: &mut v8::PinScope<'s, '_>,
|
||||
callback: v8::Local<'s, v8::Function>,
|
||||
delay_ms: u64,
|
||||
) -> bool {
|
||||
let receiver = scope.get_current_context().global(scope);
|
||||
let Some(callback) = scheduled_timer_function(scope, callback, receiver) else {
|
||||
@@ -302,7 +303,7 @@ impl HostTimeoutScheduler {
|
||||
is_interval: false,
|
||||
extra_args: Vec::new(),
|
||||
},
|
||||
0,
|
||||
delay_ms,
|
||||
Instant::now(),
|
||||
);
|
||||
true
|
||||
@@ -968,6 +969,12 @@ impl HostTimeoutScheduler {
|
||||
self.scheduler.ms_to_next(Instant::now())
|
||||
}
|
||||
|
||||
pub(crate) fn has_keep_alive_timers(&self) -> bool {
|
||||
self.scheduler.has_pending_matching(|task| {
|
||||
!matches!(task.callback, ScheduledTimerCallback::InternalFunction(_))
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) fn next_deadline(&self) -> Option<Instant> {
|
||||
self.scheduler.next_deadline()
|
||||
}
|
||||
|
||||
@@ -365,6 +365,13 @@ pub enum PageVmInitStage {
|
||||
Load,
|
||||
}
|
||||
|
||||
/// Host observations of a live Page, independent of its browser lifecycle.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum RendererPageIdleWaitMode {
|
||||
Network,
|
||||
EventLoop,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum RendererReplyBoundary {
|
||||
/// Resolve page creation only after the requested lifecycle milestone.
|
||||
|
||||
@@ -54,15 +54,9 @@ pub(crate) fn abort_signal_timeout_callback<'s>(
|
||||
rv.set(signal.into());
|
||||
return;
|
||||
};
|
||||
let timeout_id = host.queue_timeout(
|
||||
scope,
|
||||
callback,
|
||||
parsed.milliseconds,
|
||||
crate::host::HostTimerOwner::Window,
|
||||
Vec::new(),
|
||||
);
|
||||
// AbortSignal.timeout() intentionally exposes no cancel handle.
|
||||
let _ = timeout_id;
|
||||
// This is browser work, not a user timer: it has no clearTimeout handle
|
||||
// and does not independently keep an explicit event-loop wait alive.
|
||||
host.queue_internal_task_after(scope, callback, parsed.milliseconds);
|
||||
rv.set(signal.into());
|
||||
}
|
||||
|
||||
|
||||
@@ -3,6 +3,36 @@ use super::*;
|
||||
use crate::dom::native::Node;
|
||||
|
||||
impl PageScriptHost {
|
||||
/// User-owned resources that can still deliver Window callbacks. Runtime
|
||||
/// service lanes, inspector queues and storage maintenance are excluded.
|
||||
pub(crate) fn has_event_loop_keep_alive_resources(
|
||||
&self,
|
||||
scope: &mut v8::PinScope<'_, '_>,
|
||||
) -> bool {
|
||||
!self.websockets.is_empty()
|
||||
|| self.workers.values().any(|worker| match &worker.execution {
|
||||
WorkerExecutionState::Loading { terminated, .. } => !terminated,
|
||||
// The canonical connection stays registered until both relay
|
||||
// queues have delivered their terminal markers. A closed
|
||||
// worker inbox alone could overtake its final postMessage.
|
||||
WorkerExecutionState::Running { .. } => true,
|
||||
})
|
||||
|| self.message_port_wrappers.iter().any(|(port_id, entry)| {
|
||||
if !self.window_execution_context_identity_is_current(entry.identity)
|
||||
|| self
|
||||
.message_port_registry
|
||||
.message_port_peer_id(*port_id)
|
||||
.is_none()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
let context = v8::Local::new(scope, &entry.context);
|
||||
let scope = &mut v8::ContextScope::new(scope, context);
|
||||
let port = v8::Local::new(scope, &entry.wrapper);
|
||||
crate::context_bootstrap::message_port_keeps_event_loop_alive(scope, port)
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) fn document_has_browsing_context(&self, document: DomHandle) -> bool {
|
||||
// A detached iframe compatibility window can be a document's defaultView
|
||||
// without registering a browsing context. Check the native association.
|
||||
|
||||
@@ -777,15 +777,15 @@ impl PageVmFollowedNavigationMetadata {
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) enum PageVmNetworkIdleWaitAdvance {
|
||||
pub(super) enum PageVmIdleWaitAdvance {
|
||||
Completed,
|
||||
TriggeredNavigation,
|
||||
Progressed {
|
||||
state: PageVmNetworkIdleWaitState,
|
||||
state: PageVmIdleWaitState,
|
||||
},
|
||||
Waiting {
|
||||
sleep_for: std::time::Duration,
|
||||
state: PageVmNetworkIdleWaitState,
|
||||
state: PageVmIdleWaitState,
|
||||
},
|
||||
}
|
||||
|
||||
@@ -796,10 +796,45 @@ pub(super) enum PageVmSubresourceResponseWaitAdvance {
|
||||
Waiting { sleep_for: std::time::Duration },
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
pub(super) struct PageVmNetworkIdleWaitState {
|
||||
quiet_since: Option<Instant>,
|
||||
observed_activity_epoch: Option<u64>,
|
||||
#[derive(Clone, Copy)]
|
||||
pub(super) enum PageVmIdleWaitState {
|
||||
Network {
|
||||
quiet_since: Option<Instant>,
|
||||
observed_activity_epoch: Option<u64>,
|
||||
},
|
||||
EventLoop,
|
||||
}
|
||||
|
||||
impl Default for PageVmIdleWaitState {
|
||||
fn default() -> Self {
|
||||
Self::Network {
|
||||
quiet_since: None,
|
||||
observed_activity_epoch: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl PageVmIdleWaitState {
|
||||
fn new(mode: crate::RendererPageIdleWaitMode) -> Self {
|
||||
match mode {
|
||||
crate::RendererPageIdleWaitMode::Network => Self::default(),
|
||||
crate::RendererPageIdleWaitMode::EventLoop => Self::EventLoop,
|
||||
}
|
||||
}
|
||||
|
||||
fn label(self) -> &'static str {
|
||||
match self {
|
||||
Self::Network { .. } => "networkidle",
|
||||
Self::EventLoop => "idle",
|
||||
}
|
||||
}
|
||||
|
||||
fn reset(self) -> Self {
|
||||
match self {
|
||||
Self::Network { .. } => Self::default(),
|
||||
Self::EventLoop => Self::EventLoop,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
|
||||
@@ -17,8 +17,7 @@ use super::owner_local_store::{
|
||||
RendererPreparedDocumentResidence, RetiringPageEntry,
|
||||
advance_document_lifecycle_one_page_turn_via_local_task,
|
||||
advance_dom_stable_wait_turn_on_entry_via_local_task,
|
||||
advance_network_idle_wait_turn_on_entry_via_local_task,
|
||||
advance_page_owner_one_turn_via_local_task,
|
||||
advance_idle_wait_turn_on_entry_via_local_task, advance_page_owner_one_turn_via_local_task,
|
||||
advance_pending_phase_one_navigation_on_entry_via_local_task,
|
||||
advance_runtime_command_lifecycle_on_entry_via_local_task,
|
||||
advance_runtime_expression_await_turn_on_entry_via_local_task,
|
||||
@@ -34,7 +33,8 @@ use super::owner_local_store::{
|
||||
dispatch_async_command_on_entry_via_local_task,
|
||||
finalize_pending_page_creation_on_bound_owner_local_store,
|
||||
follow_pending_location_navigation_one_turn_on_entry_via_local_task,
|
||||
has_pending_document_lifecycle_turn_on_entry, install_page_vm_on_bound_owner_local_store,
|
||||
has_pending_document_lifecycle_turn_on_entry, has_ready_page_tasks_on_bound_owner_local_store,
|
||||
install_page_vm_on_bound_owner_local_store,
|
||||
install_phase_one_blocked_page_on_bound_owner_local_store,
|
||||
next_owner_maintenance_deadline_on_bound_owner_local_store,
|
||||
next_page_task_deadline_on_bound_owner_local_store, observe_document_lifecycle_on_entry,
|
||||
@@ -265,8 +265,9 @@ pub enum RendererOwnerCommand {
|
||||
inspector_session_id: Option<String>,
|
||||
pause_guard: RendererRuntimeInspectorSessionDetachGuard,
|
||||
},
|
||||
WaitForNetworkIdle {
|
||||
WaitForIdle {
|
||||
token: RendererPageToken,
|
||||
mode: crate::RendererPageIdleWaitMode,
|
||||
timeout_ms: u64,
|
||||
loader: ResourceRequestClient,
|
||||
},
|
||||
@@ -736,9 +737,9 @@ enum RenderRuntimeTurn {
|
||||
token: RendererPageToken,
|
||||
document: RendererDocumentLifecycleIdentity,
|
||||
},
|
||||
WaitLivePageNetworkIdle {
|
||||
WaitLivePageIdle {
|
||||
token: RendererPageToken,
|
||||
state: PageVmNetworkIdleWaitState,
|
||||
state: PageVmIdleWaitState,
|
||||
deadline: Instant,
|
||||
loader: ResourceRequestClient,
|
||||
},
|
||||
@@ -830,7 +831,7 @@ impl RenderRuntimeTurn {
|
||||
Self::RunDevToolsMainCommand { token, .. }
|
||||
| Self::RunLivePageCommand { token, .. }
|
||||
| Self::ContinueLivePageRuntimeCommandLifecycle { token, .. }
|
||||
| Self::WaitLivePageNetworkIdle { token, .. }
|
||||
| Self::WaitLivePageIdle { token, .. }
|
||||
| Self::WaitLivePageDomStable { token, .. }
|
||||
| Self::WaitLivePageSelector { token, .. }
|
||||
| Self::WaitLivePageScriptTruthy { token, .. }
|
||||
@@ -843,7 +844,7 @@ impl RenderRuntimeTurn {
|
||||
|
||||
fn committed_page_view_deadline(&self) -> Option<Instant> {
|
||||
match self {
|
||||
Self::WaitLivePageNetworkIdle { deadline, .. }
|
||||
Self::WaitLivePageIdle { deadline, .. }
|
||||
| Self::WaitLivePageDomStable { deadline, .. }
|
||||
| Self::WaitLivePageSelector { deadline, .. }
|
||||
| Self::WaitLivePageScriptTruthy { deadline, .. }
|
||||
@@ -1019,7 +1020,7 @@ fn renderer_command_admission_page_token(
|
||||
match command {
|
||||
RendererOwnerCommand::RunAsyncPageCommand { token, .. }
|
||||
| RendererOwnerCommand::RunProtocolPageCommand { token, .. }
|
||||
| RendererOwnerCommand::WaitForNetworkIdle { token, .. }
|
||||
| RendererOwnerCommand::WaitForIdle { token, .. }
|
||||
| RendererOwnerCommand::WaitForDomStable { token, .. } => Some(*token),
|
||||
_ => None,
|
||||
}
|
||||
|
||||
@@ -786,23 +786,25 @@ impl RendererOwnerHandle {
|
||||
))
|
||||
.into()
|
||||
}
|
||||
RendererOwnerCommand::WaitForNetworkIdle {
|
||||
RendererOwnerCommand::WaitForIdle {
|
||||
token,
|
||||
mode,
|
||||
timeout_ms,
|
||||
loader,
|
||||
} => {
|
||||
let state = PageVmIdleWaitState::new(mode);
|
||||
let deadline = match checked_live_page_wait_deadline(
|
||||
Instant::now(),
|
||||
timeout_ms,
|
||||
"networkidle",
|
||||
state.label(),
|
||||
) {
|
||||
Ok(deadline) => deadline,
|
||||
Err(error) => return Err(error).into(),
|
||||
};
|
||||
RenderRuntimeDispatchOutcome::ContinueNextTurn(Box::new(
|
||||
RenderRuntimeTurn::WaitLivePageNetworkIdle {
|
||||
RenderRuntimeTurn::WaitLivePageIdle {
|
||||
token,
|
||||
state: PageVmNetworkIdleWaitState::default(),
|
||||
state,
|
||||
deadline,
|
||||
loader,
|
||||
},
|
||||
|
||||
@@ -1136,13 +1136,13 @@ impl RendererOwnerHandle {
|
||||
self.signal_internal_document_lifecycle_turn_if_resident(token, document);
|
||||
RenderRuntimeDispatchOutcome::BackgroundComplete(Ok(()))
|
||||
}
|
||||
RenderRuntimeTurn::WaitLivePageNetworkIdle {
|
||||
RenderRuntimeTurn::WaitLivePageIdle {
|
||||
token,
|
||||
state,
|
||||
deadline,
|
||||
loader,
|
||||
} => {
|
||||
self.wait_live_page_network_idle_turn(token, state, deadline, loader)
|
||||
self.wait_live_page_idle_turn(token, state, deadline, loader)
|
||||
.await
|
||||
}
|
||||
RenderRuntimeTurn::WaitLivePageDomStable {
|
||||
|
||||
@@ -426,10 +426,10 @@ impl RendererOwnerHandle {
|
||||
.await
|
||||
}
|
||||
|
||||
pub(super) async fn wait_live_page_network_idle_turn(
|
||||
pub(super) async fn wait_live_page_idle_turn(
|
||||
&self,
|
||||
token: RendererPageToken,
|
||||
state: PageVmNetworkIdleWaitState,
|
||||
state: PageVmIdleWaitState,
|
||||
deadline: Instant,
|
||||
loader: ResourceRequestClient,
|
||||
) -> RenderRuntimeDispatchOutcome {
|
||||
@@ -440,9 +440,9 @@ impl RendererOwnerHandle {
|
||||
let remaining = deadline.saturating_duration_since(Instant::now());
|
||||
if remaining.is_zero() {
|
||||
self.restore_live_page_entry(token, entry);
|
||||
return Err(anyhow!("timed out waiting for networkidle")).into();
|
||||
return Err(anyhow!("timed out waiting for {}", state.label())).into();
|
||||
}
|
||||
let (entry, wait_result) = advance_network_idle_wait_turn_on_entry_via_local_task(
|
||||
let (mut entry, wait_result) = advance_idle_wait_turn_on_entry_via_local_task(
|
||||
self.state.local_executor.clone(),
|
||||
entry,
|
||||
state,
|
||||
@@ -450,7 +450,21 @@ impl RendererOwnerHandle {
|
||||
)
|
||||
.await;
|
||||
match wait_result {
|
||||
Ok(PageVmNetworkIdleWaitAdvance::Completed) => {
|
||||
Ok(PageVmIdleWaitAdvance::Completed)
|
||||
if matches!(state, PageVmIdleWaitState::EventLoop)
|
||||
&& has_ready_page_tasks_on_bound_owner_local_store(token, &mut entry) =>
|
||||
{
|
||||
self.restore_live_page_entry(token, entry);
|
||||
RenderRuntimeDispatchOutcome::ContinueNextTurn(Box::new(
|
||||
RenderRuntimeTurn::WaitLivePageIdle {
|
||||
token,
|
||||
state,
|
||||
deadline,
|
||||
loader,
|
||||
},
|
||||
))
|
||||
}
|
||||
Ok(PageVmIdleWaitAdvance::Completed) => {
|
||||
self.finish_live_page_entry_with_page_state(
|
||||
token,
|
||||
entry,
|
||||
@@ -459,18 +473,22 @@ impl RendererOwnerHandle {
|
||||
)
|
||||
.await
|
||||
}
|
||||
Ok(PageVmNetworkIdleWaitAdvance::TriggeredNavigation) => self
|
||||
Ok(PageVmIdleWaitAdvance::TriggeredNavigation) => self
|
||||
.continue_live_page_pending_navigation(
|
||||
token,
|
||||
entry,
|
||||
PageVmInitStage::Load,
|
||||
0,
|
||||
LivePagePendingNavigationCompletion::ContinueNetworkIdle { deadline, loader },
|
||||
LivePagePendingNavigationCompletion::ContinueIdle {
|
||||
state,
|
||||
deadline,
|
||||
loader,
|
||||
},
|
||||
),
|
||||
Ok(PageVmNetworkIdleWaitAdvance::Progressed { state }) => {
|
||||
Ok(PageVmIdleWaitAdvance::Progressed { state }) => {
|
||||
self.restore_live_page_entry(token, entry);
|
||||
RenderRuntimeDispatchOutcome::ContinueNextTurn(Box::new(
|
||||
RenderRuntimeTurn::WaitLivePageNetworkIdle {
|
||||
RenderRuntimeTurn::WaitLivePageIdle {
|
||||
token,
|
||||
state,
|
||||
deadline,
|
||||
@@ -478,10 +496,10 @@ impl RendererOwnerHandle {
|
||||
},
|
||||
))
|
||||
}
|
||||
Ok(PageVmNetworkIdleWaitAdvance::Waiting { sleep_for, state }) => {
|
||||
Ok(PageVmIdleWaitAdvance::Waiting { sleep_for, state }) => {
|
||||
if Instant::now() >= deadline {
|
||||
self.restore_live_page_entry(token, entry);
|
||||
Err(anyhow!("timed out waiting for networkidle")).into()
|
||||
Err(anyhow!("timed out waiting for {}", state.label())).into()
|
||||
} else {
|
||||
self.restore_live_page_entry(token, entry);
|
||||
let ready_at = Instant::now()
|
||||
@@ -489,7 +507,7 @@ impl RendererOwnerHandle {
|
||||
.unwrap_or(deadline)
|
||||
.min(deadline);
|
||||
RenderRuntimeDispatchOutcome::ContinueAfterPageWakeOrDeadline {
|
||||
turn: Box::new(RenderRuntimeTurn::WaitLivePageNetworkIdle {
|
||||
turn: Box::new(RenderRuntimeTurn::WaitLivePageIdle {
|
||||
token,
|
||||
state,
|
||||
deadline,
|
||||
|
||||
@@ -123,7 +123,7 @@ impl RendererOwnerHandle {
|
||||
| RenderRuntimeTurn::RunDevToolsMainCommand { .. }
|
||||
| RenderRuntimeTurn::RunLivePageCommand { .. }
|
||||
| RenderRuntimeTurn::ResumeLivePageDocumentLifecycleAfterReply { .. }
|
||||
| RenderRuntimeTurn::WaitLivePageNetworkIdle { .. }
|
||||
| RenderRuntimeTurn::WaitLivePageIdle { .. }
|
||||
| RenderRuntimeTurn::WaitLivePageDomStable { .. }
|
||||
| RenderRuntimeTurn::WaitLivePageSelector { .. }
|
||||
| RenderRuntimeTurn::WaitLivePageScriptTruthy {
|
||||
@@ -388,17 +388,16 @@ impl RendererOwnerHandle {
|
||||
)
|
||||
.await
|
||||
}
|
||||
LivePagePendingNavigationCompletion::ContinueNetworkIdle { deadline, loader } => {
|
||||
LivePagePendingNavigationCompletion::ContinueIdle {
|
||||
state,
|
||||
deadline,
|
||||
loader,
|
||||
} => {
|
||||
if let Err(error) = self.refresh_live_page_before_wait(token, entry).await {
|
||||
return Err(error).into();
|
||||
}
|
||||
self.wait_live_page_network_idle_turn(
|
||||
token,
|
||||
PageVmNetworkIdleWaitState::default(),
|
||||
deadline,
|
||||
loader,
|
||||
)
|
||||
.await
|
||||
self.wait_live_page_idle_turn(token, state.reset(), deadline, loader)
|
||||
.await
|
||||
}
|
||||
LivePagePendingNavigationCompletion::ContinueDomStable { deadline, loader } => {
|
||||
if let Err(error) = self.refresh_live_page_before_wait(token, entry).await {
|
||||
@@ -495,7 +494,7 @@ impl RendererOwnerHandle {
|
||||
)
|
||||
.await
|
||||
}
|
||||
LivePagePendingNavigationCompletion::ContinueNetworkIdle { .. }
|
||||
LivePagePendingNavigationCompletion::ContinueIdle { .. }
|
||||
| LivePagePendingNavigationCompletion::ContinueDomStable { .. }
|
||||
| LivePagePendingNavigationCompletion::ContinueSubresourceResponse { .. } => {
|
||||
self.restore_live_page_entry(token, entry);
|
||||
|
||||
@@ -685,11 +685,35 @@ impl RendererPageHandle {
|
||||
&self,
|
||||
timeout_ms: u64,
|
||||
loader: ResourceRequestClient,
|
||||
) -> Result<(RendererPageReply, Arc<RendererPageState>)> {
|
||||
self.wait_for_idle_with_mode(crate::RendererPageIdleWaitMode::Network, timeout_ms, loader)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn wait_for_idle(
|
||||
&self,
|
||||
timeout_ms: u64,
|
||||
loader: ResourceRequestClient,
|
||||
) -> Result<(RendererPageReply, Arc<RendererPageState>)> {
|
||||
self.wait_for_idle_with_mode(
|
||||
crate::RendererPageIdleWaitMode::EventLoop,
|
||||
timeout_ms,
|
||||
loader,
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
async fn wait_for_idle_with_mode(
|
||||
&self,
|
||||
mode: crate::RendererPageIdleWaitMode,
|
||||
timeout_ms: u64,
|
||||
loader: ResourceRequestClient,
|
||||
) -> Result<(RendererPageReply, Arc<RendererPageState>)> {
|
||||
match self
|
||||
.render_runtime
|
||||
.dispatch(RendererOwnerCommand::WaitForNetworkIdle {
|
||||
.dispatch(RendererOwnerCommand::WaitForIdle {
|
||||
token: self.token(),
|
||||
mode,
|
||||
timeout_ms,
|
||||
loader,
|
||||
})
|
||||
@@ -697,7 +721,8 @@ impl RendererPageHandle {
|
||||
{
|
||||
RendererOwnerReply::AsyncPageCommandRan(result) => Ok(result.into_reply_and_state()),
|
||||
_ => Err(anyhow!(
|
||||
"renderer owner returned non-async page-command reply for wait-for-network-idle"
|
||||
"renderer owner returned non-async page-command reply for wait-for-{}",
|
||||
PageVmIdleWaitState::new(mode).label()
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -541,17 +541,17 @@ pub(in crate::runtime) async fn commit_page_state_on_entry_via_local_task_with_p
|
||||
.await
|
||||
}
|
||||
|
||||
pub(in crate::runtime) async fn advance_network_idle_wait_turn_on_entry_via_local_task(
|
||||
pub(in crate::runtime) async fn advance_idle_wait_turn_on_entry_via_local_task(
|
||||
local_executor: JsLocalExecutor,
|
||||
entry: LivePageEntry,
|
||||
state: PageVmNetworkIdleWaitState,
|
||||
state: PageVmIdleWaitState,
|
||||
remaining: std::time::Duration,
|
||||
) -> (LivePageEntry, Result<PageVmNetworkIdleWaitAdvance>) {
|
||||
) -> (LivePageEntry, Result<PageVmIdleWaitAdvance>) {
|
||||
run_entry_on_bound_owner_local_store_local_task(local_executor, entry, move |entry| {
|
||||
Box::pin(async move {
|
||||
entry
|
||||
.page_vm_mut()
|
||||
.advance_network_idle_wait_turn(state, remaining)
|
||||
.advance_idle_wait_turn(state, remaining)
|
||||
.await
|
||||
})
|
||||
})
|
||||
@@ -743,6 +743,28 @@ impl PageReadyDescriptorSnapshot {
|
||||
}
|
||||
}
|
||||
|
||||
/// Observe the same runnable sources used by the Page arbiter while a command
|
||||
/// has its VM checked out. An exhausted resource set must still drain queued
|
||||
/// completion callbacks before an event-loop idle wait may return.
|
||||
pub(in crate::runtime) fn has_ready_page_tasks_on_bound_owner_local_store(
|
||||
token: RendererPageToken,
|
||||
entry: &mut LivePageEntry,
|
||||
) -> bool {
|
||||
with_bound_render_runtime_owner_local_store_session(|session| {
|
||||
let Some(slot) = session
|
||||
.store
|
||||
.page_hosts
|
||||
.get_mut(&token.local_host_id)
|
||||
.and_then(|host| host.pages.get_mut(&token.page_id))
|
||||
else {
|
||||
return false;
|
||||
};
|
||||
!page_ready_descriptor_snapshot(entry, &mut slot.task_sources)
|
||||
.eligible
|
||||
.is_empty()
|
||||
})
|
||||
}
|
||||
|
||||
pub(super) fn page_ready_descriptor_snapshot(
|
||||
entry: &mut LivePageEntry,
|
||||
task_sources: &mut RendererPageOwnedTaskSources,
|
||||
|
||||
@@ -336,7 +336,8 @@ pub(super) enum LivePagePendingNavigationCompletion {
|
||||
reply: Box<RendererPageReply>,
|
||||
capture_policy: super::RendererPageStateCapturePolicy,
|
||||
},
|
||||
ContinueNetworkIdle {
|
||||
ContinueIdle {
|
||||
state: PageVmIdleWaitState,
|
||||
deadline: std::time::Instant,
|
||||
loader: ResourceRequestClient,
|
||||
},
|
||||
@@ -372,7 +373,10 @@ impl LivePagePendingNavigationCompletion {
|
||||
Self::CompletePageCreation { .. } => "creating page",
|
||||
Self::CompletePageReplacement { .. } => "replacing page Document",
|
||||
Self::ReplyWithSnapshot { .. } => "refreshing page",
|
||||
Self::ContinueNetworkIdle { .. } => "waiting for networkidle",
|
||||
Self::ContinueIdle { state, .. } => match state {
|
||||
PageVmIdleWaitState::Network { .. } => "waiting for networkidle",
|
||||
PageVmIdleWaitState::EventLoop => "waiting for idle",
|
||||
},
|
||||
Self::ContinueDomStable { .. } => "waiting for domstable",
|
||||
Self::ContinueSubresourceResponse { .. } => "waiting for subresource response",
|
||||
}
|
||||
@@ -392,7 +396,7 @@ impl LivePagePendingNavigationCompletion {
|
||||
Self::CompletePageCreation { .. }
|
||||
| Self::CompletePageReplacement { .. }
|
||||
| Self::ReplyWithSnapshot { .. }
|
||||
| Self::ContinueNetworkIdle { .. }
|
||||
| Self::ContinueIdle { .. }
|
||||
| Self::ContinueDomStable { .. }
|
||||
| Self::ContinueSubresourceResponse { .. } => {
|
||||
LivePageNavigationFailureRecipient::Initiator
|
||||
@@ -414,7 +418,7 @@ impl LivePagePendingNavigationCompletion {
|
||||
} => navigation_reply_policy.returns_with_pending_navigation(),
|
||||
Self::Background
|
||||
| Self::ReplyWithSnapshot { .. }
|
||||
| Self::ContinueNetworkIdle { .. }
|
||||
| Self::ContinueIdle { .. }
|
||||
| Self::ContinueDomStable { .. }
|
||||
| Self::ContinueSubresourceResponse { .. } => false,
|
||||
}
|
||||
@@ -437,7 +441,7 @@ impl LivePagePendingNavigationCompletion {
|
||||
Self::Background
|
||||
| Self::PublishedPageCreation { .. }
|
||||
| Self::ReplyWithSnapshot { .. }
|
||||
| Self::ContinueNetworkIdle { .. }
|
||||
| Self::ContinueIdle { .. }
|
||||
| Self::ContinueDomStable { .. }
|
||||
| Self::ContinueSubresourceResponse { .. } => (Self::Background, true),
|
||||
}
|
||||
|
||||
@@ -22,15 +22,45 @@ fn network_idle_sleep_for(
|
||||
}
|
||||
|
||||
impl PageVm {
|
||||
pub(in crate::runtime) async fn advance_network_idle_wait_turn(
|
||||
pub(in crate::runtime) async fn advance_idle_wait_turn(
|
||||
&mut self,
|
||||
mut state: PageVmNetworkIdleWaitState,
|
||||
mut state: PageVmIdleWaitState,
|
||||
remaining: std::time::Duration,
|
||||
) -> Result<PageVmNetworkIdleWaitAdvance> {
|
||||
) -> Result<PageVmIdleWaitAdvance> {
|
||||
if self.vm().has_pending_location_navigation() {
|
||||
return Ok(PageVmNetworkIdleWaitAdvance::TriggeredNavigation);
|
||||
return Ok(PageVmIdleWaitAdvance::TriggeredNavigation);
|
||||
}
|
||||
|
||||
let PageVmIdleWaitState::Network {
|
||||
quiet_since,
|
||||
observed_activity_epoch,
|
||||
} = &mut state
|
||||
else {
|
||||
// Resource exhaustion is observed after a real checkpoint, never
|
||||
// inferred from an empty JavaScript stack or a quiet network.
|
||||
self.vm_mut().finish_event_loop_idle_checkpoint()?;
|
||||
if self.vm().has_pending_location_navigation() {
|
||||
return Ok(PageVmIdleWaitAdvance::TriggeredNavigation);
|
||||
}
|
||||
if !self.vm_mut().has_event_loop_keep_alive_work()?
|
||||
&& !self.vm().has_pending_child_document_lifecycle()
|
||||
&& self.page_task_queue.is_empty()
|
||||
{
|
||||
return Ok(PageVmIdleWaitAdvance::Completed);
|
||||
}
|
||||
let sleep_for = self
|
||||
.vm()
|
||||
.ms_to_next_timeout()
|
||||
.map(std::time::Duration::from_millis)
|
||||
.unwrap_or(remaining)
|
||||
.min(remaining);
|
||||
return if sleep_for.is_zero() {
|
||||
Ok(PageVmIdleWaitAdvance::Progressed { state })
|
||||
} else {
|
||||
Ok(PageVmIdleWaitAdvance::Waiting { sleep_for, state })
|
||||
};
|
||||
};
|
||||
|
||||
let pending_requests = self.pending_subresource_request_count();
|
||||
let pending_child_frame_lifecycle = self.vm().has_pending_child_document_lifecycle();
|
||||
let activity_epoch = self.vm().subresource_activity_epoch();
|
||||
@@ -44,27 +74,27 @@ impl PageVm {
|
||||
let pending_idle_work = pending_requests + usize::from(pending_child_frame_lifecycle);
|
||||
let now = std::time::Instant::now();
|
||||
let quiet_elapsed = if pending_idle_work == 0 {
|
||||
if state.observed_activity_epoch != Some(activity_epoch) {
|
||||
state.observed_activity_epoch = Some(activity_epoch);
|
||||
state.quiet_since = Some(now);
|
||||
if *observed_activity_epoch != Some(activity_epoch) {
|
||||
*observed_activity_epoch = Some(activity_epoch);
|
||||
*quiet_since = Some(now);
|
||||
}
|
||||
let quiet_since = state.quiet_since.get_or_insert(now);
|
||||
let quiet_since = quiet_since.get_or_insert(now);
|
||||
let quiet_elapsed = now.saturating_duration_since(*quiet_since);
|
||||
if quiet_elapsed >= NETWORK_IDLE_QUIET_WINDOW {
|
||||
return Ok(PageVmNetworkIdleWaitAdvance::Completed);
|
||||
return Ok(PageVmIdleWaitAdvance::Completed);
|
||||
}
|
||||
quiet_elapsed
|
||||
} else {
|
||||
state.observed_activity_epoch = Some(activity_epoch);
|
||||
state.quiet_since = None;
|
||||
*observed_activity_epoch = Some(activity_epoch);
|
||||
*quiet_since = None;
|
||||
std::time::Duration::ZERO
|
||||
};
|
||||
let sleep_for =
|
||||
network_idle_sleep_for(ms_to_next, pending_idle_work, quiet_elapsed, remaining);
|
||||
if sleep_for.is_zero() {
|
||||
Ok(PageVmNetworkIdleWaitAdvance::Progressed { state })
|
||||
Ok(PageVmIdleWaitAdvance::Progressed { state })
|
||||
} else {
|
||||
Ok(PageVmNetworkIdleWaitAdvance::Waiting { sleep_for, state })
|
||||
Ok(PageVmIdleWaitAdvance::Waiting { sleep_for, state })
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use super::*;
|
||||
|
||||
use crate::page_task_queue::{RendererPageReadyDescriptor, RendererPageSchedulerTask};
|
||||
use crate::runtime::{PageVmNetworkIdleWaitAdvance, PageVmNetworkIdleWaitState};
|
||||
use crate::runtime::{PageVmIdleWaitAdvance, PageVmIdleWaitState};
|
||||
|
||||
#[tokio::test(flavor = "current_thread")]
|
||||
async fn network_idle_wait_observes_deadline_without_checkpointing_the_agent() {
|
||||
@@ -21,15 +21,11 @@ Promise.resolve().then(() => __networkIdleObserverCheckpoint += 1);
|
||||
)?;
|
||||
|
||||
let advance = page_vm
|
||||
.advance_network_idle_wait_turn(
|
||||
PageVmNetworkIdleWaitState::default(),
|
||||
Duration::from_secs(1),
|
||||
)
|
||||
.advance_idle_wait_turn(PageVmIdleWaitState::default(), Duration::from_secs(1))
|
||||
.await?;
|
||||
assert!(matches!(
|
||||
advance,
|
||||
PageVmNetworkIdleWaitAdvance::Waiting { .. }
|
||||
| PageVmNetworkIdleWaitAdvance::Progressed { .. }
|
||||
PageVmIdleWaitAdvance::Waiting { .. } | PageVmIdleWaitAdvance::Progressed { .. }
|
||||
));
|
||||
assert_eq!(
|
||||
page_vm
|
||||
@@ -46,6 +42,56 @@ Promise.resolve().then(() => __networkIdleObserverCheckpoint += 1);
|
||||
.expect("NetworkIdle observer authority test should run");
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "current_thread")]
|
||||
async fn event_loop_idle_checkpoints_before_observing_future_work() {
|
||||
run_page_vm_async_test(async move {
|
||||
let loader_owner =
|
||||
crate::network::ResourceRequestClient::new(&FetchConfig::default()).expect("loader");
|
||||
let loader = loader_owner.handle();
|
||||
let mut page_vm = test_page_vm_with_loader(&loader, Vec::new());
|
||||
page_vm
|
||||
.vm_mut()
|
||||
.eval_without_microtask_checkpoint_for_test(
|
||||
r#"
|
||||
globalThis.__idleCheckpoint = 0;
|
||||
globalThis.__unbackedPromise = new Promise(() => {});
|
||||
Promise.resolve().then(() => {
|
||||
__idleCheckpoint += 1;
|
||||
globalThis.__idleTimer = setTimeout(() => {}, 60000);
|
||||
});
|
||||
"queued"
|
||||
"#,
|
||||
)?;
|
||||
|
||||
let advance = page_vm
|
||||
.advance_idle_wait_turn(PageVmIdleWaitState::EventLoop, Duration::from_secs(1))
|
||||
.await?;
|
||||
assert!(matches!(
|
||||
advance,
|
||||
PageVmIdleWaitAdvance::Waiting { .. } | PageVmIdleWaitAdvance::Progressed { .. }
|
||||
));
|
||||
assert_eq!(
|
||||
page_vm
|
||||
.vm_mut()
|
||||
.eval_without_microtask_checkpoint_for_test("String(__idleCheckpoint)")?,
|
||||
"1",
|
||||
);
|
||||
|
||||
page_vm
|
||||
.vm_mut()
|
||||
.eval_without_microtask_checkpoint_for_test("clearTimeout(__idleTimer); 'cleared'")?;
|
||||
assert!(matches!(
|
||||
page_vm
|
||||
.advance_idle_wait_turn(PageVmIdleWaitState::EventLoop, Duration::from_secs(1))
|
||||
.await?,
|
||||
PageVmIdleWaitAdvance::Completed,
|
||||
));
|
||||
Ok::<_, anyhow::Error>(())
|
||||
})
|
||||
.await
|
||||
.expect("event-loop idle checkpoint test should run");
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "current_thread")]
|
||||
async fn selected_timer_task_checkpoints_without_a_wait_observer() {
|
||||
run_page_vm_async_test(async move {
|
||||
|
||||
@@ -799,6 +799,7 @@ mod browsing_contexts;
|
||||
mod child_frame_runtime;
|
||||
mod document_environment;
|
||||
mod document_settings;
|
||||
mod event_loop;
|
||||
pub(crate) use document_settings::{
|
||||
DocumentSetting, DocumentSettingEffect, DocumentSettingsApplication,
|
||||
};
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
//! Keep-alive observations for the explicit host event-loop exhaustion wait.
|
||||
//!
|
||||
//! These are queries of canonical resource owners, not a second set of
|
||||
//! counters. Pending Promises without backing work and renderer maintenance
|
||||
//! do not keep the page alive. Queued task callbacks are checked separately by
|
||||
//! the Page arbiter before a wait is allowed to finish.
|
||||
|
||||
use super::ScriptVm;
|
||||
use anyhow::Result;
|
||||
|
||||
impl ScriptVm {
|
||||
/// The idle observer owns its final checkpoint, separately from selected
|
||||
/// Page-task completion. Promise reactions may create new keep-alive work.
|
||||
pub(crate) fn finish_event_loop_idle_checkpoint(&mut self) -> Result<()> {
|
||||
let result = self.perform_owner_lane_task_microtask_checkpoints();
|
||||
self.sync_child_browsing_context_records();
|
||||
self.finish_runtime_turn_with_style_drain(
|
||||
crate::style_engine::StyleInvalidationTurnExitBoundary::RuntimePendingWorkFlush,
|
||||
result,
|
||||
)
|
||||
}
|
||||
|
||||
pub(crate) fn has_event_loop_keep_alive_work(&mut self) -> Result<bool> {
|
||||
if self.document_runtime.has_keep_alive_timers()
|
||||
|| self.pending_subresource_request_count() > 0
|
||||
|| self.has_pending_native_module_job()
|
||||
|| self.has_pending_runtime_script_work()
|
||||
|| self.has_pending_webcrypto_tasks()
|
||||
|| self.has_pending_opfs_tasks()
|
||||
{
|
||||
return Ok(true);
|
||||
}
|
||||
let host = self._context_host.clone();
|
||||
self.with_current_main_world_scope(|scope, _| {
|
||||
Ok(host.borrow().has_event_loop_keep_alive_resources(scope))
|
||||
})
|
||||
.map(|pending| pending.unwrap_or(false))
|
||||
}
|
||||
}
|
||||
@@ -201,6 +201,14 @@ impl<T> TimerScheduler<T> {
|
||||
})
|
||||
}
|
||||
|
||||
/// Whether a live timer matches, including timers whose deadlines are in
|
||||
/// the future. Canceled heap entries do not keep their runtime alive.
|
||||
pub fn has_pending_matching(&self, mut predicate: impl FnMut(&T) -> bool) -> bool {
|
||||
self.pending
|
||||
.iter()
|
||||
.any(|timer| self.active.contains(&timer.id) && predicate(&timer.payload))
|
||||
}
|
||||
|
||||
pub fn finish_running(&mut self, id: TimerId) {
|
||||
self.running.remove(&id);
|
||||
self.cancelled_running.remove(&id);
|
||||
@@ -321,6 +329,19 @@ fn timer_precedes<T>(left: &ScheduledTimer<T>, right: &ScheduledTimer<T>) -> boo
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn keep_alive_observes_future_timers_but_not_canceled_or_other_payloads() {
|
||||
let now = Instant::now();
|
||||
let mut scheduler = TimerScheduler::default();
|
||||
let id = scheduler.schedule_after("user", 60000, now);
|
||||
scheduler.schedule_after("internal", 60000, now);
|
||||
assert!(!scheduler.has_ready_timer(now));
|
||||
assert!(scheduler.has_pending_matching(|kind| *kind == "user"));
|
||||
assert!(scheduler.cancel(id));
|
||||
assert!(!scheduler.has_pending_matching(|kind| *kind == "user"));
|
||||
assert!(scheduler.has_pending_matching(|kind| *kind == "internal"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn timers_wait_until_their_deadline_in_every_ready_query() {
|
||||
let now = Instant::now();
|
||||
|
||||
+11
-1
@@ -154,6 +154,7 @@ pub async fn run_cli_with_config<W: Write>(
|
||||
.await
|
||||
.context("failed while waiting for page delay")?;
|
||||
}
|
||||
readiness.wait_for_idle(&browser, &mut page).await?;
|
||||
Ok::<(), anyhow::Error>(())
|
||||
}
|
||||
.await;
|
||||
@@ -213,9 +214,18 @@ pub async fn run_cli_with_config<W: Write>(
|
||||
eval_output::execute(&mut page, expression)
|
||||
.await
|
||||
.map_err(with_output_context)?;
|
||||
readiness
|
||||
.wait_for_idle(&browser, &mut page)
|
||||
.await
|
||||
.map_err(with_output_context)?;
|
||||
fetch_dump::render_page_output_async(&mut page, &config.fetch).await
|
||||
} else {
|
||||
eval_output::evaluate(&mut page, expression).await
|
||||
async {
|
||||
let result = eval_output::evaluate(&mut page, expression).await?;
|
||||
readiness.wait_for_idle(&browser, &mut page).await?;
|
||||
Ok(result)
|
||||
}
|
||||
.await
|
||||
}
|
||||
} else {
|
||||
fetch_dump::render_page_output_async(&mut page, &config.fetch).await
|
||||
|
||||
@@ -70,7 +70,9 @@ impl ReadinessPlan {
|
||||
)
|
||||
.await
|
||||
}
|
||||
RenderedDomWaitUntil::NetworkIdle | RenderedDomWaitUntil::DomStable => {
|
||||
RenderedDomWaitUntil::NetworkIdle
|
||||
| RenderedDomWaitUntil::DomStable
|
||||
| RenderedDomWaitUntil::Idle => {
|
||||
browser
|
||||
.fetch_request_document_allow_http_error_with_wait_until_deadline(
|
||||
request,
|
||||
@@ -107,6 +109,17 @@ impl ReadinessPlan {
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Include work scheduled by automation predicates, delays and --eval in
|
||||
/// the explicit idle lifecycle, sharing the original readiness deadline.
|
||||
pub(super) async fn wait_for_idle(&self, browser: &Browser, page: &mut Page) -> Result<()> {
|
||||
if self.wait_until == RenderedDomWaitUntil::Idle {
|
||||
browser
|
||||
.wait_for_page_readiness_with_deadline(page, self.wait_until, self.deadline)
|
||||
.await?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
fn rendered_wait_until(wait_until: FetchWaitUntil) -> RenderedDomWaitUntil {
|
||||
@@ -115,6 +128,7 @@ fn rendered_wait_until(wait_until: FetchWaitUntil) -> RenderedDomWaitUntil {
|
||||
FetchWaitUntil::Load => RenderedDomWaitUntil::Load,
|
||||
FetchWaitUntil::NetworkIdle => RenderedDomWaitUntil::NetworkIdle,
|
||||
FetchWaitUntil::DomStable => RenderedDomWaitUntil::DomStable,
|
||||
FetchWaitUntil::Idle => RenderedDomWaitUntil::Idle,
|
||||
FetchWaitUntil::Done => RenderedDomWaitUntil::Done,
|
||||
}
|
||||
}
|
||||
|
||||
+5
-2
@@ -275,8 +275,8 @@ pub struct FetchArgs {
|
||||
/// Maximum total readiness time in milliseconds. Initial and HTTP-error
|
||||
/// replacement navigations, the selected lifecycle stage, response match,
|
||||
/// selector, and script waits share one absolute deadline. Network-idle and
|
||||
/// DOM-stable return the current page when it expires. Pass `--log-level warn`
|
||||
/// to see timeout warnings.
|
||||
/// DOM-stable return the current page when it expires; idle fails with a
|
||||
/// timeout error. Pass `--log-level warn` to see timeout warnings.
|
||||
#[arg(
|
||||
short,
|
||||
long = "timeout-ms",
|
||||
@@ -765,6 +765,9 @@ pub enum FetchWaitUntil {
|
||||
Load,
|
||||
NetworkIdle,
|
||||
DomStable,
|
||||
/// Wait for user async work and callbacks to finish. Active intervals,
|
||||
/// Workers, WebSockets and receiving MessagePorts keep the wait pending.
|
||||
Idle,
|
||||
Done,
|
||||
}
|
||||
|
||||
|
||||
@@ -51,6 +51,8 @@ mod disable_js;
|
||||
mod eval;
|
||||
#[path = "fetch_cli/http_fail.rs"]
|
||||
mod http_fail;
|
||||
#[path = "fetch_cli/idle.rs"]
|
||||
mod idle;
|
||||
#[path = "fetch_cli/layout_regressions.rs"]
|
||||
mod layout_regressions;
|
||||
#[path = "fetch_cli/markdown.rs"]
|
||||
|
||||
@@ -0,0 +1,487 @@
|
||||
use super::{Output, clean_output, fixture_runtime, run_moli, unique_temp_file_path};
|
||||
use anyhow::Result;
|
||||
use axum::{
|
||||
Router,
|
||||
extract::ws::{Message, WebSocketUpgrade},
|
||||
response::{Html, IntoResponse},
|
||||
routing::get,
|
||||
};
|
||||
use base64::{Engine as _, engine::general_purpose::STANDARD};
|
||||
use std::time::Duration;
|
||||
|
||||
fn document(script: &str) -> String {
|
||||
format!(
|
||||
"data:text/html;base64,{}",
|
||||
STANDARD.encode(format!("<main>pending</main><script>{script}</script>"))
|
||||
)
|
||||
}
|
||||
|
||||
fn run(url: &str, timeout_ms: &str, extra: &[&str]) -> Result<Output> {
|
||||
let mut args = vec![
|
||||
"moli",
|
||||
"fetch",
|
||||
"--http-no-proxy",
|
||||
"*",
|
||||
"--log-level",
|
||||
"error",
|
||||
"--wait",
|
||||
"idle",
|
||||
"--timeout-ms",
|
||||
timeout_ms,
|
||||
];
|
||||
args.extend_from_slice(extra);
|
||||
args.push(url);
|
||||
run_moli(args)
|
||||
}
|
||||
|
||||
fn assert_done(output: Output) {
|
||||
let stderr = clean_output(&output.stderr);
|
||||
let stdout = clean_output(&output.stdout);
|
||||
assert!(output.status.success(), "stderr={stderr}");
|
||||
assert!(stdout.contains("<main>done</main>"), "stdout={stdout}");
|
||||
}
|
||||
|
||||
struct Server {
|
||||
_runtime: tokio::runtime::Runtime,
|
||||
task: tokio::task::JoinHandle<()>,
|
||||
url: String,
|
||||
}
|
||||
|
||||
impl Server {
|
||||
fn new(script: &str) -> Result<Self> {
|
||||
let runtime = fixture_runtime()?;
|
||||
let listener = runtime.block_on(tokio::net::TcpListener::bind("127.0.0.1:0"))?;
|
||||
let url = format!("http://{}/", listener.local_addr()?);
|
||||
let html = format!("<main>pending</main><script>{script}</script>");
|
||||
let app = Router::new()
|
||||
.route("/", get(move || { let html = html.clone(); async move { Html(html) } }))
|
||||
.route("/data", get(|| async {
|
||||
tokio::time::sleep(Duration::from_millis(100)).await;
|
||||
"payload"
|
||||
}))
|
||||
.route("/worker.js", get(|| async {
|
||||
([("content-type", "text/javascript")],
|
||||
"setTimeout(() => { postMessage('payload'); close(); }, 100)")
|
||||
}))
|
||||
.route("/waiting-worker.js", get(|| async {
|
||||
([("content-type", "text/javascript")],
|
||||
"postMessage('ready'); onmessage = () => {}")
|
||||
}))
|
||||
.route("/burst-worker.js", get(|| async {
|
||||
([("content-type", "text/javascript")],
|
||||
"for (let i = 0; i < 16; ++i) postMessage(i); close()")
|
||||
}))
|
||||
.route("/module.js", get(|| async {
|
||||
tokio::time::sleep(Duration::from_millis(100)).await;
|
||||
([("content-type", "text/javascript")], "export default 'payload'")
|
||||
}))
|
||||
.route("/next", get(|| async { Html(
|
||||
"<main>pending</main><script>setTimeout(() => document.querySelector('main').textContent='done',100)</script>"
|
||||
) }))
|
||||
.route("/socket", get(|upgrade: WebSocketUpgrade| async {
|
||||
upgrade.on_upgrade(|mut socket| async move {
|
||||
tokio::time::sleep(Duration::from_millis(100)).await;
|
||||
if socket.send(Message::Text("payload".into())).await.is_ok() {
|
||||
// Stay open until the client closes, so the test also
|
||||
// exercises the connection's active lifetime.
|
||||
while socket.recv().await.is_some() {}
|
||||
}
|
||||
}).into_response()
|
||||
}));
|
||||
let task = runtime.spawn(async move { axum::serve(listener, app).await.unwrap() });
|
||||
Ok(Self {
|
||||
_runtime: runtime,
|
||||
task,
|
||||
url,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for Server {
|
||||
fn drop(&mut self) {
|
||||
self.task.abort();
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn idle_accepts_documents_without_a_javascript_realm() -> Result<()> {
|
||||
for extra in [
|
||||
vec!["--dump", "html"],
|
||||
vec!["--dump", "html", "--disable-js"],
|
||||
] {
|
||||
assert_done(run("data:text/html,<main>done</main>", "3000", &extra)?);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn idle_waits_for_future_timers_and_their_microtask_followups() -> Result<()> {
|
||||
let url = document(
|
||||
r#"
|
||||
Promise.resolve().then(() => queueMicrotask(() => {
|
||||
setTimeout(() => {
|
||||
document.querySelector('main').textContent = 'first';
|
||||
Promise.resolve().then(() => setTimeout(() => {
|
||||
document.querySelector('main').textContent = 'done';
|
||||
}, 30));
|
||||
}, 650);
|
||||
}));
|
||||
"#,
|
||||
);
|
||||
assert_done(run(&url, "3000", &["--dump", "html"])?);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn idle_waits_until_an_interval_is_cleared() -> Result<()> {
|
||||
let url = document(
|
||||
r#"
|
||||
let count = 0;
|
||||
const id = setInterval(() => {
|
||||
if (++count === 3) {
|
||||
clearInterval(id);
|
||||
queueMicrotask(() => document.querySelector('main').textContent = 'done');
|
||||
}
|
||||
}, 40);
|
||||
"#,
|
||||
);
|
||||
assert_done(run(&url, "3000", &["--dump", "html"])?);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn idle_ignores_canceled_timers_internal_timeouts_and_unbacked_promises() -> Result<()> {
|
||||
let url = document(
|
||||
r#"
|
||||
clearTimeout(setTimeout(() => {}, 60000));
|
||||
clearInterval(setInterval(() => {}, 60000));
|
||||
globalThis.pending = new Promise(() => {});
|
||||
globalThis.signal = AbortSignal.timeout(60000);
|
||||
queueMicrotask(() => document.querySelector('main').textContent = 'done');
|
||||
"#,
|
||||
);
|
||||
assert_done(run(&url, "2000", &["--dump", "html"])?);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn idle_waits_for_fetch_and_xhr_callbacks_and_their_timers() -> Result<()> {
|
||||
let server = Server::new(
|
||||
r#"
|
||||
const done = [];
|
||||
function finish(value) {
|
||||
done.push(value);
|
||||
if (done.length === 2) setTimeout(() => {
|
||||
document.querySelector('main').textContent =
|
||||
done.every(x => x === 'payload') ? 'done' : 'wrong payload';
|
||||
}, 30);
|
||||
}
|
||||
fetch('/data').then(r => r.text()).then(finish);
|
||||
const xhr = new XMLHttpRequest();
|
||||
xhr.open('GET', '/data');
|
||||
xhr.onloadend = () => Promise.resolve().then(() => finish(xhr.responseText));
|
||||
xhr.send();
|
||||
"#,
|
||||
)?;
|
||||
assert_done(run(&server.url, "5000", &["--dump", "html"])?);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn idle_keeps_fetch_alive_until_an_internal_abort_timeout_fires() -> Result<()> {
|
||||
let server = Server::new(
|
||||
r#"
|
||||
fetch('/data', {signal: AbortSignal.timeout(20)}).then(
|
||||
() => document.querySelector('main').textContent = 'not aborted',
|
||||
error => setTimeout(() => {
|
||||
document.querySelector('main').textContent =
|
||||
error.name === 'TimeoutError' ? 'done' : error.name;
|
||||
}, 30)
|
||||
);
|
||||
"#,
|
||||
)?;
|
||||
assert_done(run(&server.url, "5000", &["--dump", "html"])?);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn idle_waits_for_dynamic_module_evaluation_and_its_callbacks() -> Result<()> {
|
||||
let server = Server::new(
|
||||
r#"
|
||||
import('/module.js').then(module => setTimeout(() => {
|
||||
document.querySelector('main').textContent = module.default === 'payload' ? 'done' : 'wrong payload';
|
||||
}, 30));
|
||||
"#,
|
||||
)?;
|
||||
assert_done(run(&server.url, "5000", &["--dump", "html"])?);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn idle_drains_worker_messages_before_the_worker_closes() -> Result<()> {
|
||||
let server = Server::new(
|
||||
r#"
|
||||
const worker = new Worker('/worker.js');
|
||||
worker.onmessage = event => queueMicrotask(() => {
|
||||
document.querySelector('main').textContent = event.data === 'payload' ? 'done' : 'wrong payload';
|
||||
});
|
||||
"#,
|
||||
)?;
|
||||
assert_done(run(&server.url, "5000", &["--dump", "html"])?);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn idle_stops_waiting_after_a_worker_is_terminated() -> Result<()> {
|
||||
let server = Server::new(
|
||||
r#"
|
||||
const worker = new Worker('/waiting-worker.js');
|
||||
worker.onmessage = event => {
|
||||
worker.terminate();
|
||||
queueMicrotask(() => {
|
||||
document.querySelector('main').textContent = event.data === 'ready' ? 'done' : 'wrong payload';
|
||||
});
|
||||
};
|
||||
"#,
|
||||
)?;
|
||||
assert_done(run(&server.url, "5000", &["--dump", "html"])?);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn idle_drains_all_worker_messages_after_immediate_close() -> Result<()> {
|
||||
let server = Server::new(
|
||||
r#"
|
||||
const worker = new Worker('/burst-worker.js');
|
||||
const messages = [];
|
||||
worker.onmessage = event => {
|
||||
messages.push(event.data);
|
||||
if (messages.length === 16) queueMicrotask(() => {
|
||||
document.querySelector('main').textContent =
|
||||
messages.every((value, index) => value === index) ? 'done' : 'out of order';
|
||||
});
|
||||
};
|
||||
"#,
|
||||
)?;
|
||||
assert_done(run(&server.url, "5000", &["--dump", "html"])?);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn idle_ignores_a_worker_terminated_while_its_script_is_loading() -> Result<()> {
|
||||
let server = Server::new(
|
||||
"const worker = new Worker('/waiting-worker.js'); worker.terminate(); document.querySelector('main').textContent = 'done'",
|
||||
)?;
|
||||
assert_done(run(&server.url, "3000", &["--dump", "html"])?);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn idle_waits_for_websocket_close_and_its_callback() -> Result<()> {
|
||||
let server = Server::new(
|
||||
r#"
|
||||
const socket = new WebSocket(location.origin.replace('http:', 'ws:') + '/socket');
|
||||
let received = false;
|
||||
socket.onmessage = event => { received = event.data === 'payload'; socket.close(); };
|
||||
socket.onclose = () => setTimeout(() => {
|
||||
document.querySelector('main').textContent = received ? 'done' : 'no message';
|
||||
}, 30);
|
||||
"#,
|
||||
)?;
|
||||
assert_done(run(&server.url, "5000", &["--dump", "html"])?);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn idle_drains_started_message_ports_and_observes_close() -> Result<()> {
|
||||
let url = document(
|
||||
r#"
|
||||
const channel = new MessageChannel();
|
||||
channel.port1.onmessage = event => {
|
||||
channel.port1.close();
|
||||
channel.port2.close();
|
||||
queueMicrotask(() => document.querySelector('main').textContent = event.data);
|
||||
};
|
||||
channel.port2.postMessage('done');
|
||||
"#,
|
||||
);
|
||||
assert_done(run(&url, "3000", &["--dump", "html"])?);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn idle_observes_native_message_port_listeners_without_calling_author_getters() -> Result<()> {
|
||||
let url = document(
|
||||
r#"
|
||||
const c = new MessageChannel();
|
||||
c.port1.addEventListener('message', e => {
|
||||
c.port1.close(); c.port2.close();
|
||||
document.querySelector('main').textContent = e.data;
|
||||
});
|
||||
c.port1.start();
|
||||
Object.defineProperty(c.port1, 'onmessage', {
|
||||
get() { throw new Error('must not inspect public properties'); }
|
||||
});
|
||||
c.port2.postMessage('done');
|
||||
"#,
|
||||
);
|
||||
assert_done(run(&url, "3000", &["--dump", "html"])?);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn idle_ignores_internal_stream_transfer_ports_without_backing_work() -> Result<()> {
|
||||
let url = document(
|
||||
r#"
|
||||
const stream = new ReadableStream();
|
||||
globalThis.copy = structuredClone(stream, {transfer: [stream]});
|
||||
globalThis.pending = copy.getReader().read();
|
||||
queueMicrotask(() => document.querySelector('main').textContent = 'done');
|
||||
"#,
|
||||
);
|
||||
assert_done(run(&url, "2000", &["--dump", "html"])?);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn idle_ignores_unstarted_ports_and_ports_without_receivers() -> Result<()> {
|
||||
let url = document(
|
||||
r#"
|
||||
const a = new MessageChannel();
|
||||
a.port1.addEventListener('message', () => { throw Error('not started'); });
|
||||
a.port2.postMessage('queued');
|
||||
const b = new MessageChannel();
|
||||
b.port1.start();
|
||||
b.port2.postMessage('no receiver');
|
||||
queueMicrotask(() => document.querySelector('main').textContent = 'done');
|
||||
"#,
|
||||
);
|
||||
assert_done(run(&url, "2000", &["--dump", "html"])?);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn idle_has_a_strict_timeout_for_resources_that_stay_alive() -> Result<()> {
|
||||
for script in [
|
||||
"setInterval(() => {}, 30)",
|
||||
"const c = new MessageChannel(); c.port1.onmessage = () => {}",
|
||||
] {
|
||||
let output = run(&document(script), "800", &["--dump", "html"])?;
|
||||
assert!(!output.status.success());
|
||||
assert!(output.stdout.is_empty());
|
||||
assert!(
|
||||
clean_output(&output.stderr).contains("waiting for idle"),
|
||||
"{}",
|
||||
clean_output(&output.stderr)
|
||||
);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn idle_has_a_strict_timeout_for_live_workers_and_websockets() -> Result<()> {
|
||||
for script in [
|
||||
"globalThis.worker = new Worker('/waiting-worker.js')",
|
||||
"globalThis.socket = new WebSocket(location.origin.replace('http:', 'ws:') + '/socket')",
|
||||
] {
|
||||
let server = Server::new(script)?;
|
||||
let output = run(&server.url, "1000", &["--dump", "html"])?;
|
||||
assert!(!output.status.success());
|
||||
assert!(output.stdout.is_empty());
|
||||
assert!(
|
||||
clean_output(&output.stderr).contains("waiting for idle"),
|
||||
"{}",
|
||||
clean_output(&output.stderr)
|
||||
);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn idle_waits_for_work_started_by_eval_before_exporting() -> Result<()> {
|
||||
let script =
|
||||
"setTimeout(() => document.querySelector('main').textContent = 'done', 100); 'value'";
|
||||
assert_done(run(
|
||||
&document(""),
|
||||
"3000",
|
||||
&["--dump", "html", "--eval", script],
|
||||
)?);
|
||||
let output = run(&document(""), "3000", &["--eval", script])?;
|
||||
assert!(output.status.success(), "{}", clean_output(&output.stderr));
|
||||
assert_eq!(clean_output(&output.stdout), "value\n");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn idle_waits_for_work_started_by_eval_file() -> Result<()> {
|
||||
let path = unique_temp_file_path("wait-idle", "eval.js")?;
|
||||
std::fs::write(
|
||||
&path,
|
||||
"setTimeout(() => document.querySelector('main').textContent = 'done', 100)",
|
||||
)?;
|
||||
let output = run(
|
||||
&document(""),
|
||||
"3000",
|
||||
&["--dump", "html", "--eval-file", path.to_str().unwrap()],
|
||||
);
|
||||
std::fs::remove_file(path)?;
|
||||
assert_done(output?);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn idle_also_drains_work_created_by_the_readiness_script() -> Result<()> {
|
||||
assert_done(run(
|
||||
&document(""),
|
||||
"3000",
|
||||
&[
|
||||
"--dump",
|
||||
"html",
|
||||
"--wait-script",
|
||||
"(setTimeout(() => document.querySelector('main').textContent='done', 100), true)",
|
||||
],
|
||||
)?);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn idle_waits_for_child_window_timers_and_releases_removed_windows() -> Result<()> {
|
||||
let url = document(
|
||||
r#"
|
||||
const frame = document.createElement('iframe');
|
||||
document.body.append(frame);
|
||||
const child = frame.contentWindow;
|
||||
child.setInterval(() => {}, 60000);
|
||||
child.setTimeout(() => {
|
||||
frame.remove();
|
||||
queueMicrotask(() => document.querySelector('main').textContent = 'done');
|
||||
}, 100);
|
||||
"#,
|
||||
);
|
||||
assert_done(run(&url, "3000", &["--dump", "html"])?);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn idle_keeps_one_deadline_for_work_started_after_initial_readiness() -> Result<()> {
|
||||
let output = run(
|
||||
&document("setTimeout(() => {}, 300)"),
|
||||
"1000",
|
||||
&["--dump", "html", "--eval", "setTimeout(() => {}, 800)"],
|
||||
)?;
|
||||
assert!(!output.status.success());
|
||||
assert!(output.stdout.is_empty());
|
||||
assert!(
|
||||
clean_output(&output.stderr).contains("timed out after 1000 ms while waiting for idle"),
|
||||
"{}",
|
||||
clean_output(&output.stderr)
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn idle_follows_navigation_and_keeps_waiting_on_the_replacement_document() -> Result<()> {
|
||||
let server = Server::new("setTimeout(() => location.href = '/next', 40)")?;
|
||||
assert_done(run(&server.url, "5000", &["--dump", "html"])?);
|
||||
Ok(())
|
||||
}
|
||||
Reference in New Issue
Block a user