use std::{collections::HashMap, sync::Arc};
use anyhow::{anyhow, bail};
#[cfg(target_family = "unix")]
use anyhow::{Context, Result};
use serial_test::serial;
use tokio::{process::Command, sync::RwLock, time::Duration};
mod common;
use crate::common::{init, start_nats, test_dir_with_subfolder, wait_for_no_hosts};
#[tokio::test]
#[serial]
#[cfg(target_family = "unix")]
async fn integration_dev_hello_actor_serial() -> Result<()> {
let test_setup = init(
"hello", "hello",
)
.await?;
let project_dir = test_setup.project_dir;
let dir = test_dir_with_subfolder("dev_hello_actor");
wait_for_no_hosts()
.await
.context("one or more unexpected wasmcloud instances running")?;
let mut nats = start_nats(5895, &dir).await?;
let dev_cmd = Arc::new(RwLock::new(
Command::new(env!("CARGO_BIN_EXE_wash"))
.args([
"dev",
"--nats-port",
"5895",
"--nats-connect-only",
"--ctl-port",
"5895",
"--use-host-subprocess",
])
.kill_on_drop(true)
.envs(HashMap::from([("WASH_EXPERIMENTAL", "true")]))
.spawn()
.context("failed running cargo dev")?,
));
let watch_dev_cmd = dev_cmd.clone();
let signed_file_path = Arc::new(project_dir.join("build/hello_s.wasm"));
let expected_path = signed_file_path.clone();
let _ = tokio::time::timeout(
Duration::from_secs(1200),
tokio::spawn(async move {
loop {
if let Ok(Some(exit_status)) = watch_dev_cmd.write().await.try_wait() {
if !exit_status.success() {
bail!("dev command failed");
}
}
if expected_path.exists() {
break Ok(());
}
tokio::time::sleep(Duration::from_secs(5)).await;
}
}),
)
.await
.context("timed out while waiting for file path to get created")?;
assert!(signed_file_path.exists(), "signed actor file was built",);
let process_pid = dev_cmd
.write()
.await
.id()
.context("failed to get child process pid")?;
nix::sys::signal::kill(
nix::unistd::Pid::from_raw(process_pid as i32),
nix::sys::signal::Signal::SIGINT,
)
.expect("cannot send ctrl-c");
let _ = tokio::time::timeout(Duration::from_secs(15), dev_cmd.write().await.wait())
.await
.context("dev command did not exit")?;
wait_for_no_hosts()
.await
.context("wasmcloud instance failed to exit cleanly (processes still left over)")?;
nats.kill().await.map_err(|e| anyhow!(e))?;
Ok(())
}