#![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() -> ! {
use tracing_subscriber::layer::SubscriberExt as _;
tracing::subscriber::set_global_default(
tracing_subscriber::registry().with(encore::telemetry::tracing::logs_layer()),
)
.unwrap();
tracing_log::LogTracer::init().unwrap();
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");
}
#[test]
fn standalone_layer_flushes_before_exit() {
standalone("standalone_layer_flushes_before_exit", "tracing");
}
#[test]
fn bridged_logs_flush_before_exit() {
standalone("bridged_logs_flush_before_exit", "bridge");
}
#[test]
fn direct_logs_flush_before_exit() {
standalone("direct_logs_flush_before_exit", "direct");
}
#[test]
fn direct_logger_flushes_before_exit() {
standalone("direct_logger_flushes_before_exit", "logger-flush");
}
fn standalone(test: &str, mode: &str) {
if std::env::var(APP_ENV).as_deref() == Ok("standalone") {
if mode == "direct" || mode == "logger-flush" {
encore::log::set_logger(encore::telemetry::log::logger()).unwrap();
encore::log::set_max_level(encore::log::LevelFilter::Info);
} else {
use tracing_subscriber::layer::SubscriberExt as _;
tracing::subscriber::set_global_default(
tracing_subscriber::registry().with(encore::telemetry::tracing::logs_layer()),
)
.unwrap();
if mode == "bridge" {
tracing_log::LogTracer::init().unwrap();
}
}
for i in 0..LINES {
if mode == "tracing" {
tracing::info!("standalone line {i}");
} else {
encore::log::info!("standalone line {i}");
}
}
if mode == "logger-flush" {
encore::telemetry::log::logger().flush();
} else {
encore::telemetry::log::flush();
}
std::process::exit(0);
}
let output = Command::new(std::env::current_exe().unwrap())
.args([test, "--exact", "--nocapture", "--test-threads=1"])
.env(APP_ENV, "standalone")
.env_remove("ENCORE_RUNTIME_CONFIG")
.env_remove("ENCORE_LOG_FORMAT")
.env_remove("ENCORE_NOLOG")
.env_remove("RUST_LOG")
.stdout(Stdio::null())
.output()
.unwrap();
assert!(output.status.success(), "{test}: {:?}", output.status);
let out = String::from_utf8(output.stderr).unwrap();
assert_eq!(out.lines().count(), LINES, "{test}: all lines flushed");
for (i, line) in out.lines().enumerate() {
let line: serde_json::Value = serde_json::from_str(line).unwrap();
assert_eq!(line["message"], format!("standalone line {i}"));
}
}