for _ in0..TASKS {
runtime.spawn(std::future::pending::<()>());
}
let count_realized = count.load(Ordering::SeqCst);
assert_eq!(
TASKS, count_realized, "Total number of spawned task hook invocations was incorrect, expected {TASKS}, got {count_realized}"
);
let count_ids_realized = ids.lock().unwrap().len();
assert_eq!(
TASKS, count_ids_realized, "Total number of spawned task hook invocations was incorrect, expected {TASKS}, got {count_realized}"
);
}
/// Assert that the terminate task hook always fires when set. #[test] fn terminate_task_hook_fires() { let count = Arc::new(AtomicUsize::new(0)); let count2 = Arc::clone(&count);
/// Test that the correct spawn location is provided to the task hooks on a /// current thread runtime. #[test] fn task_hook_spawn_location_current_thread() { let spawns = Arc::new(AtomicUsize::new(0)); let poll_starts = Arc::new(AtomicUsize::new(0)); let poll_ends = Arc::new(AtomicUsize::new(0));
let task = runtime.spawn(asyncmove { tokio::task::yield_now().await });
runtime.block_on(asyncmove { // Spawn tasks using both `runtime.spawn(...)` and `tokio::spawn(...)` // to ensure the correct location is captured in both code paths.
task.await.unwrap();
tokio::spawn(asyncmove {}).await.unwrap();
// tick the runtime a bunch to close out tasks for _ in0..ITERATIONS {
tokio::task::yield_now().await;
}
});
/// Test that the correct spawn location is provided to the task hooks on a /// multi-thread runtime. /// /// Testing this separately is necessary as the spawn code paths are different /// and we should ensure that `#[track_caller]` is passed through correctly /// for both runtimes. #[cfg_attr(
target_os = "wasi",
ignore = "WASI does not support multi-threaded runtime"
)] #[test] fn task_hook_spawn_location_multi_thread() { let spawns = Arc::new(AtomicUsize::new(0)); let poll_starts = Arc::new(AtomicUsize::new(0)); let poll_ends = Arc::new(AtomicUsize::new(0));
let task = runtime.spawn(asyncmove { tokio::task::yield_now().await });
runtime.block_on(asyncmove { // Spawn tasks using both `runtime.spawn(...)` and `tokio::spawn(...)` // to ensure the correct location is captured in both code paths.
task.await.unwrap();
tokio::spawn(asyncmove {}).await.unwrap();
// tick the runtime a bunch to close out tasks for _ in0..ITERATIONS {
tokio::task::yield_now().await;
}
});
// Give the runtime to shut down so that we see all the expected calls to // the task hooks.
runtime.shutdown_timeout(std::time::Duration::from_secs(60));
// Note: we "read" the counters using `fetch_add(0, SeqCst)` rather than // `load(SeqCst)` because read-write-modify operations are guaranteed to // observe the latest value, while the load is not. // This avoids a race that may cause test flakiness.
assert_eq!(spawns.fetch_add(0, Ordering::SeqCst), 2); let poll_starts = poll_starts.fetch_add(0, Ordering::SeqCst);
assert!(poll_starts > 2);
assert_eq!(poll_starts, poll_ends.fetch_add(0, Ordering::SeqCst));
}
fn mk_spawn_location_hook(
event: &'static str,
count: &Arc<AtomicUsize>,
) -> implFn(&tokio::runtime::TaskMeta<'_>) { let count = Arc::clone(count); move |data| {
eprintln!("{event} ({:?}): {:?}", data.id(), data.spawned_at()); // Assert that the spawn location is in this file. // Don't make assertions about line number/column here, as these // may change as new code is added to the test file...
assert_eq!(
data.spawned_at().file(),
file!(), "incorrect spawn location in {event} hook",
);
count.fetch_add(1, Ordering::SeqCst);
}
}
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.15 Sekunden
(vorverarbeitet am 2026-08-26)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.