cosca 0.10.0

Unified cross-platform subprocess management: spawning, stdio, process trees, stable identity, and elevation.
Documentation
//! The graceful ops on a nested member, whose caller is itself contained. The checks run in a
//! re-exec of this binary spawned contained, so that its own crate spawn is nested and its thread
//! holds the record of the break it sends (see [`super`]); the driver in
//! `child::ctrl_break_tests` reads only whether the checks finished.

use super::{
    assert_acked, assert_break_sent_to, assert_in_our_console, assert_refused_by_the_direct_send_for_no_group,
    connect_reporter, kill_release_reap, report_done, spawn_member, Handler,
};
use crate::stop::proxy::protocol::ProxyAnswer;
use crate::stop::proxy::proxy_tests::Fixture;
use crate::{Containment, StopTarget};

/// The fully-qualified libtest path of [`fixture_nested_terminate_reporter`].
pub(crate) const REPORTER_TEST: &str = "test_child::ctrl_break::nested::fixture_nested_terminate_reporter";

/// The environment variable carrying the address the reporter reports to.
pub(crate) const ADDR_ENV: &str = "COSCA_FIXTURE_NESTED_TERMINATE_ADDR";

/// Spawns a nested member that acks `CTRL_BREAK`: [`Containment::Delegated`], so it owns no tree
/// teardown, yet it leads a console process group of its own. A `terminate` must reach that group
/// although no tree operation on any handle could, and the nested member's own handler must
/// acknowledge it.
#[skuld::test]
fn fixture_nested_terminate_reporter() {
    let Some(mut report) = connect_reporter(ADDR_ENV) else {
        return;
    };
    let _warns = crate::log_capture::warn_watch();

    let (child, mut sock, _) = spawn_member(Handler::Ack, None, crate::ContainMode::Strongest, |_| {});
    let pid = child.pid();
    assert_eq!(
        child.containment(),
        Containment::Delegated,
        "this process is contained, so its own spawn is nested"
    );
    assert!(StopTarget::from(&child).leads_group_on_our_console());
    assert_in_our_console(pid, "the nested member");

    // A nested member owns no tree teardown. The refusal comes before any signal, so the one break
    // asserted below is `terminate`'s.
    assert!(
        matches!(child.terminate_tree(), Err(crate::error::Error::Unsupported { .. })),
        "a nested member still owns no tree teardown"
    );
    child.terminate().expect("terminate a nested console-group child");
    assert_break_sent_to(pid);
    assert_acked(&mut sock, "the nested member");

    // Its handler returned "handled", so it still runs; only the kill ends it.
    kill_release_reap(&child, sock, false);

    report_done(&mut report);
}

/// The fully-qualified libtest path of [`fixture_nested_without_group_reporter`].
pub(crate) const NO_GROUP_REPORTER_TEST: &str = "test_child::ctrl_break::nested::fixture_nested_without_group_reporter";

/// The environment variable carrying the address that reporter reports to.
pub(crate) const NO_GROUP_ADDR_ENV: &str = "COSCA_FIXTURE_NESTED_NO_GROUP_ADDR";

/// Spawns a nested member given `new_process_group(false)`: [`Containment::Delegated`] still, but it
/// leads no group, so the default stop is the direct send's refusal naming the setter. No proxy is started
/// and no event is sent. The member stays in its spawner's group, which is this reporter's.
#[skuld::test]
fn fixture_nested_without_group_reporter() {
    let Some(mut report) = connect_reporter(NO_GROUP_ADDR_ENV) else {
        return;
    };
    let _warns = crate::log_capture::warn_watch();

    let fixture = Fixture::answering(ProxyAnswer::Sent.code());
    let (child, sock, _) = spawn_member(Handler::Ack, None, crate::ContainMode::Strongest, |cmd| {
        _ = cmd.new_process_group(false);
        cmd.ctrl_proxy(fixture.setting());
    });
    assert_eq!(
        child.containment(),
        Containment::Delegated,
        "this process is contained, so its own spawn is nested"
    );
    assert!(!StopTarget::from(&child).leads_group_on_our_console());
    let err = child
        .terminate()
        .expect_err("a nested member that leads no group is refused by the direct send");
    assert_refused_by_the_direct_send_for_no_group(&err);
    assert!(fixture.ran().is_none(), "the proxy was started");

    kill_release_reap(&child, sock, false);

    report_done(&mut report);
}