#![cfg_attr(feature = "nightly", feature(impl_trait_in_assoc_type))]
use std::process::Command;
use hazard3_executor::Spawner;
use hazard3_executor::raw::Executor;
const CHILD_ENV: &str = "EMBASSY_EXECUTOR_MAIN_UNWIND_CHILD";
const DROP_MARKER: &str = "main_unwind: panic payload dropped";
const ABORT_MESSAGE: &str = "hazard3_executor::main must not unwind";
struct NoopPender;
impl hazard3_executor::pender::Pender for NoopPender {
fn pend(_context: *mut ()) {}
}
hazard3_executor::pender_impl!(NoopPender);
struct TestExecutor {
inner: Executor,
}
impl TestExecutor {
fn new() -> Self {
Self {
inner: Executor::new(core::ptr::null_mut()),
}
}
fn run(&'static mut self, init: impl FnOnce(Spawner)) -> ! {
init(self.inner.spawner());
loop {
unsafe { self.inner.poll() };
}
}
}
struct Payload;
impl Drop for Payload {
fn drop(&mut self) {
eprintln!("{DROP_MARKER}");
}
}
#[hazard3_executor::main(executor = "TestExecutor")]
async fn main(_spawner: Spawner) {
if std::env::var_os(CHILD_ENV).is_some() {
std::panic::panic_any(Payload);
}
if cfg!(miri) {
std::process::exit(0);
}
let output = Command::new(std::env::current_exe().unwrap())
.env(CHILD_ENV, "1")
.output()
.unwrap();
let stderr = String::from_utf8_lossy(&output.stderr);
if stderr.contains(DROP_MARKER) || output.status.success() || !stderr.contains(ABORT_MESSAGE) {
eprintln!("child exited with {} and stderr:\n{stderr}", output.status);
eprintln!(
"test main_unwind ... FAILED: the child did not abort with `{ABORT_MESSAGE}` before dropping its payload"
);
std::process::exit(1);
}
println!("test main_unwind ... ok");
std::process::exit(0);
}