#![cfg(unix)]
use std::io::Read;
use std::process::{Command, Stdio};
use encore_runtimecfg::pb;
const APP_ENV: &str = "ENCORE_LOG_EXIT_TEST_APP";
const LINES: usize = 9000;
fn runtime_config() -> String {
let cfg = pb::RuntimeConfig {
environment: Some(pb::Environment {
app_id: "app".into(),
env_name: "test".into(),
..Default::default()
}),
deployment: Some(pb::Deployment {
hosted_services: vec![pb::HostedService {
name: "svc".into(),
..Default::default()
}],
..Default::default()
}),
..Default::default()
};
encore_runtimecfg::encoding::encode_env_value(&cfg)
}
#[test]
fn lines_queued_at_exit_are_written() {
if std::env::var_os(APP_ENV).is_some() {
app();
}
let mut child = Command::new(std::env::current_exe().unwrap())
.args([
"lines_queued_at_exit_are_written",
"--exact",
"--nocapture",
"--test-threads=1",
])
.env(APP_ENV, "1")
.env("ENCORE_RUNTIME_CONFIG", runtime_config())
.env_remove("ENCORE_LOG_FORMAT")
.stdout(Stdio::null())
.stderr(Stdio::piped())
.spawn()
.unwrap();
let mut stderr = child.stderr.take().unwrap();
let mut out = Vec::new();
let mut buf = [0; 4096];
loop {
match stderr.read(&mut buf).unwrap() {
0 => break,
n => out.extend_from_slice(&buf[..n]),
}
std::thread::sleep(std::time::Duration::from_millis(1));
}
let status = child.wait().unwrap();
let out = String::from_utf8_lossy(&out);
assert!(status.success(), "app status {status}");
let written = out.lines().filter(|l| l.contains("burst line")).count();
assert_eq!(written, LINES, "lines written before exit");
}
fn app() -> ! {
encore::log::init();
encore::initialize();
std::thread::spawn(|| {
std::thread::sleep(std::time::Duration::from_millis(500));
for i in 0..LINES {
log::info!("burst line {i}");
}
unsafe { libc::kill(libc::getpid(), libc::SIGTERM) };
});
encore::run_blocking();
unreachable!("run_blocking exits");
}