test(xhr): cover expired upload timers without narrow network races

Add a controlled native regression for upload microtasks cancelling or extending an already-ready timeout. Keep the Window and worker HTTP matrix with scheduling margins suitable for parallel execution.

Validation: cargo fmt --all; cargo clippy --workspace --all-targets --all-features -- -D warnings; cargo nextest run --no-fail-fast. Checks cover the integrated workspace with 32-way concurrency and no automatic retries.
This commit is contained in:
ldm0
2026-11-21 20:03:39 +08:00
parent 9d20a41c00
commit 71f8611a88
2 changed files with 113 additions and 3 deletions
@@ -5359,3 +5359,110 @@ xhr.send('payload');
"loadstart:1|abort-ready:4|reopened:1|loadstart:1|abort:1|after-abort:1|load:4:replacement"
);
}
#[test]
fn xhr_upload_microtask_can_replace_an_already_ready_timeout() {
for replacement in [0, 10_000] {
let mut vm = new_storage_test_vm("https://xhr-upload-ready-timeout.test/");
vm.set_fetch_subresource_interception(
crate::types::SubresourceInterceptionClasses::for_resource_type(
crate::types::SubresourceResourceType::Xhr,
),
);
vm.eval(&format!(
r#"
globalThis.xhr = new XMLHttpRequest();
globalThis.events = [];
for (const type of ['progress', 'load', 'loadend']) {{
xhr.upload.addEventListener(type, () => events.push(type));
}}
xhr.upload.addEventListener('loadend', () => Promise.resolve().then(() => {{
xhr.timeout = {replacement};
events.push('microtask');
}}));
xhr.ontimeout = () => events.push('timeout');
xhr.open('POST', '/held');
xhr.send('Test Message');
"#
))
.expect("intercept upload without depending on network scheduling");
let pending = vm.take_pending_subresource_fetch_infos();
assert_eq!(pending.len(), 1);
let internal_id = pending[0].internal_id;
let context = vm.current_main_world().unwrap().context.clone();
vm.renderer_document_isolate
.with_entered_renderer_document_isolate(|isolate| {
let scope = std::pin::pin!(v8::HandleScope::new(isolate));
let scope = &mut scope.init();
let context = v8::Local::new(scope, &context);
let scope = &mut v8::ContextScope::new(scope, context);
let xhr = context
.global(scope)
.get(scope, v8str(scope, "xhr").into())
.unwrap();
let xhr = v8::Local::<v8::Object>::try_from(xhr).unwrap();
// Backdate only the request's native start timestamp. The public
// setter must schedule a genuinely ready timer, without sleeping.
let now = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_millis() as f64;
crate::network_host::set_xhr_state_number(
scope,
xhr,
crate::network_host::XHR_TIMEOUT_START_MS_SLOT,
now - 100.0,
);
let timeout = v8::Number::new(scope, 30.0);
assert_eq!(
xhr.set(scope, v8str(scope, "timeout").into(), timeout.into()),
Some(true)
);
Ok(())
})
.expect("schedule expired timeout through the public setter");
assert!(
vm.has_ready_timeout(),
"the old timeout must already be due"
);
let context = vm.current_main_world().unwrap().context.clone();
vm.renderer_document_isolate
.with_entered_renderer_document_isolate(|isolate| {
let scope = std::pin::pin!(v8::HandleScope::new(isolate));
let scope = &mut scope.init();
let context = v8::Local::new(scope, &context);
let scope = &mut v8::ContextScope::new(scope, context);
let xhr = context
.global(scope)
.get(scope, v8str(scope, "xhr").into())
.unwrap();
let xhr = v8::Local::<v8::Object>::try_from(xhr).unwrap();
assert!(crate::network_host::apply_xhr_upload_event(
scope,
xhr,
internal_id,
moli_fetch::UploadEvent::Complete {
loaded: 12,
total: 12
},
));
scope.perform_microtask_checkpoint();
Ok(())
})
.expect("apply an admitted upload task and its microtask checkpoint");
assert!(
!vm.has_ready_timeout(),
"the reaction must retire the ready timer"
);
assert_eq!(vm.ms_to_next_timeout().is_some(), replacement != 0);
assert_eq!(
vm.eval("JSON.stringify([xhr.timeout, xhr.readyState, events])")
.unwrap(),
format!(r#"[{replacement},1,["progress","load","loadend","microtask"]]"#),
);
vm.eval("xhr.abort()")
.expect("retire the intercepted request");
}
}
+6 -3
View File
@@ -16,9 +16,12 @@ async function probeUploads(scope, base, pendingOrigin) {
{name: 'absent body success', bytes: null, timeout: 0, head: 150, expected: 'load'},
{name: 'disable elapsed timeout', timeout: 30, head: 300, busy: 150, after: 0, complete: true, expected: 'load'},
{name: 'author abort discards queued completion', timeout: 0, head: 500, busy: 150, abort: true, expected: 'abort'},
{name: 'upload microtask disables timeout after headers', timeout: 30, head: 20, body: 400, busy: 150, afterUpload: 0, complete: true, expected: 'load'},
{name: 'upload microtask disables timeout after periodic report', timeout: 30, head: 500, busy: 150, afterUpload: 0, complete: true, expected: 'load'},
{name: 'upload microtask extends timeout', timeout: 30, head: 20, body: 400, busy: 150, afterUpload: 1000, complete: true, expected: 'load'}
// Do not require a real HTTP response to win a 10 ms scheduling window.
// The native upload/timeout regression covers an already-expired timer;
// transport tests separately gate response-start and periodic reports.
{name: 'upload microtask disables timeout with early headers', timeout: 500, head: 20, body: 1500, busy: 800, afterUpload: 0, complete: true, expected: 'load'},
{name: 'upload microtask disables timeout while response is held', timeout: 500, head: 1500, busy: 800, afterUpload: 0, complete: true, expected: 'load'},
{name: 'upload microtask extends timeout', timeout: 500, head: 20, body: 1500, busy: 800, afterUpload: 5000, complete: true, expected: 'load'}
];
const facts = [];
for (const config of cases) {