vthread 0.1.0

Bounded carrier-affine virtual threads for Rust
Documentation
use crate::{Error, error::ScopeRunError};

#[test]
fn local_domain_error_preserves_borrow_and_reclaims_children() {
    crate::run(|scope| {
        scope
            .spawn("local domain", || {
                let value = String::from("domain error");
                let mut reclaimed = false;
                struct Mark<'a>(&'a mut bool);
                impl Drop for Mark<'_> {
                    fn drop(&mut self) {
                        *self.0 = true;
                    }
                }
                let result = crate::try_local_scope(|local| {
                    let mark = Mark(&mut reclaimed);
                    let _child = local
                        .spawn("borrowed", move || {
                            let _mark = mark;
                            crate::sleep(std::time::Duration::from_secs(60))
                        })
                        .unwrap();
                    Err::<(), _>(&value)
                });
                assert!(matches!(result, Err(ScopeRunError::Body(error)) if error == &value));
                assert!(reclaimed);
            })?
            .join()
    })
    .unwrap();
}

#[test]
fn local_generic_entry_outside_a_task_reports_runtime_failure() {
    assert!(matches!(
        crate::try_local_scope(|_| Ok::<_, &str>(())),
        Err(ScopeRunError::Runtime(Error::OutsideVThread))
    ));
}

#[test]
fn local_domain_and_child_failures_survive_without_cancelling_the_parent() {
    crate::run(|scope| {
        scope
            .spawn("local failures", || {
                let result = crate::try_local_scope(|local| {
                    let child = local
                        .spawn("unobserved", || panic!("child failure"))
                        .unwrap();
                    while !child.is_finished() {
                        crate::yield_now().unwrap();
                    }
                    Err::<(), _>("domain failure")
                });
                let Err(ScopeRunError::BodyAndRuntime { body, runtime }) = result else {
                    panic!("local body and child failures must both survive");
                };
                assert_eq!(body, "domain failure");
                assert!(matches!(runtime, Error::TaskPanicked { .. }));
                crate::checkpoint().unwrap();
            })?
            .join()
    })
    .unwrap();
}

#[test]
fn local_generic_body_failure_preserves_a_local_deadline() {
    use std::time::{Duration, Instant};
    crate::run(|scope| {
        scope
            .spawn("local deadline", || {
                let result = crate::try_local_scope_with_deadline(
                    Instant::now() + Duration::from_millis(10),
                    |_| {
                        crate::sleep(Duration::from_millis(30)).unwrap();
                        Err::<(), _>("domain failure")
                    },
                );
                assert!(matches!(
                    result,
                    Err(ScopeRunError::BodyAndRuntime {
                        body: "domain failure",
                        runtime: Error::DeadlineExceeded,
                    })
                ));
                crate::checkpoint().unwrap();
            })?
            .join()
    })
    .unwrap();
}

#[test]
fn injected_construction_unwind_releases_admission_and_drains() {
    use std::panic::{AssertUnwindSafe, catch_unwind};
    let runtime = crate::Runtime::builder()
        .carriers(1)
        .max_vthreads(2)
        .carrier_queue_capacity(2)
        .stack_cache_capacity(0)
        .stall_policy(crate::StallPolicy::AbortAfter(
            std::time::Duration::from_millis(20),
        ))
        .build()
        .unwrap();
    runtime
        .run_scope(|root| {
            root.spawn("parent", || {
                crate::local_scope(|local| {
                    super::super::inject_construction_panic();
                    assert!(
                        catch_unwind(AssertUnwindSafe(|| {
                            local.spawn("injected-construction-panic", || ())
                        }))
                        .is_err()
                    );
                    assert_eq!(local.spawn("after-injected-panic", || 52)?.join()?, 52);
                    Ok(())
                })
                .unwrap();
            })?
            .join()
        })
        .unwrap();
    let snapshot = runtime.snapshot();
    assert_eq!((snapshot.active(), snapshot.stats().admitted()), (0, 2));
    assert_eq!(snapshot.stats().rejected(), 1);
    runtime.shutdown().unwrap();
}