Claude runtime review
This commit is contained in:
@@ -160,6 +160,8 @@ impl std::error::Error for Elapsed {}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::cell::RefCell;
|
||||
use std::rc::Rc;
|
||||
use std::sync::{Arc, Mutex};
|
||||
use std::time::Duration;
|
||||
|
||||
@@ -199,4 +201,39 @@ mod tests {
|
||||
|
||||
assert_eq!(log.as_slice(), ["started", "slept", "timed out"]);
|
||||
}
|
||||
|
||||
/// Verify that `sleep(Duration::ZERO).await` yields to the macrotask queue
|
||||
/// before the future continues. A macrotask queued before the sleep must
|
||||
/// run before the future's continuation.
|
||||
#[test]
|
||||
fn sleep_zero_yields_to_macrotask_queue() {
|
||||
let order = std::thread::spawn(|| {
|
||||
let order = Rc::new(RefCell::new(Vec::<&'static str>::new()));
|
||||
|
||||
// Macrotask queued before the sleep.
|
||||
{
|
||||
let order = Rc::clone(&order);
|
||||
queue_task(move || order.borrow_mut().push("macrotask"));
|
||||
}
|
||||
|
||||
// Future that awaits sleep(ZERO) and then records its continuation.
|
||||
{
|
||||
let order = Rc::clone(&order);
|
||||
queue_future(async move {
|
||||
sleep(Duration::ZERO).await;
|
||||
order.borrow_mut().push("after_sleep");
|
||||
});
|
||||
}
|
||||
|
||||
run();
|
||||
Rc::try_unwrap(order).unwrap().into_inner()
|
||||
})
|
||||
.join()
|
||||
.expect("test thread should join");
|
||||
|
||||
// The macrotask must run before the sleep future continues, because
|
||||
// sleep(ZERO) resolves via a timer event (macrotask), so the queued
|
||||
// macrotask runs first.
|
||||
assert_eq!(order.as_slice(), ["macrotask", "after_sleep"]);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user