//! 真实子进程覆盖管道背压、退出、输出预算与取消;不使用替身进程。 use dever_runtime::{ Bytes, process::{self, Limits}, task, }; use std::{ io, path::Path, time::{Duration, Instant}, }; fn limits() -> Limits { Limits { timeout_ms: 3000, output_limit: 1_048_576, } } #[test] fn command_drains_stdin_and_both_outputs_and_preserves_nonzero_exit() { task::run(true, 8, async |scope| { let input = Bytes::from(vec![0x85; 512 * 1024]); let output = process::run( scope, &Path::new("/bin/cat").into(), &[], Some(&input), limits(), ) .await?; assert_eq!(output.code, 0); assert_eq!(output.stdout, input); assert!(output.stderr.is_empty()); let output = process::run( scope, &Path::new("/bin/sh").into(), &["-c".into(), "printf out; printf err >&2; exit 9".into()], None, limits(), ) .await?; assert_eq!(output.code, 9); assert_eq!(output.stdout.as_ref(), b"out"); assert_eq!(output.stderr.as_ref(), b"err"); Ok::<_, io::Error>(()) }) .unwrap() .unwrap(); } #[test] fn command_limits_and_spawn_errors_retain_real_failures() { task::run(true, 8, async |scope| { let result = process::run( scope, &Path::new("/nonexistent-dever-command").into(), &[], None, limits(), ) .await; assert_eq!(result.unwrap_err().kind(), io::ErrorKind::NotFound); let result = process::run( scope, &Path::new("/bin/echo").into(), &["bad\0arg".into()], None, limits(), ) .await; assert_eq!(result.unwrap_err().kind(), io::ErrorKind::InvalidInput); let result = process::run( scope, &Path::new("/bin/sh").into(), &["-c".into(), "printf 12345; printf 67890 >&2".into()], None, Limits { output_limit: 8, ..limits() }, ) .await; assert_eq!(result.unwrap_err().kind(), io::ErrorKind::FileTooLarge); let start = Instant::now(); let result = process::run( scope, &Path::new("/bin/sleep").into(), &["30".into()], None, Limits { timeout_ms: 20, ..limits() }, ) .await; assert_eq!(result.unwrap_err().kind(), io::ErrorKind::TimedOut); assert!(start.elapsed() < Duration::from_secs(2)); Ok::<_, io::Error>(()) }) .unwrap() .unwrap(); } #[test] fn cancelled_command_is_reaped_before_scope_returns() { let start = Instant::now(); task::run(true, 8, async |scope| { let arguments = ["30".into()]; let executable = Path::new("/bin/sleep").into(); let call = process::run(scope, &executable, &arguments, None, limits()); assert!( tokio::time::timeout(Duration::from_millis(20), call) .await .is_err() ); Ok::<_, io::Error>(()) }) .unwrap() .unwrap(); assert!(start.elapsed() < Duration::from_secs(2)); } #[test] fn command_exit_and_cancellation_stop_descendants_holding_the_pipes() { for wait in [false, true] { let file = std::env::temp_dir().join(format!("dever-process-{}-{wait}.pid", std::process::id())); let script = format!( "sleep 30 & echo $! >\"$1\"; {}", if wait { "wait" } else { "exit 4" } ); let start = Instant::now(); task::run(true, 8, async |scope| { let args = [ "-c".into(), script, "test".into(), file.to_str().unwrap().into(), ]; let executable = Path::new("/bin/sh").into(); let call = process::run(scope, &executable, &args, None, limits()); if wait { assert!( tokio::time::timeout(Duration::from_millis(100), call) .await .is_err() ); } else { assert_eq!(call.await?.code, 4); } Ok::<_, io::Error>(()) }) .unwrap() .unwrap(); assert!(start.elapsed() < Duration::from_secs(2)); let pid = std::fs::read_to_string(&file).unwrap(); // 后代由系统的 reaper 回收。KILL 只保证信号已送达,进程可能仍在退出路径上, // 所以在期限内等待它变成僵尸或消失;超时仍存活才算清理失败。 let deadline = Instant::now() + Duration::from_secs(1); loop { match std::fs::read_to_string(format!("/proc/{}/stat", pid.trim())) { Ok(stat) if stat.rsplit_once(") ").unwrap().1.starts_with('Z') => break, Ok(stat) => { assert!(Instant::now() < deadline, "descendant still alive: {stat}"); std::thread::sleep(Duration::from_millis(5)); } Err(error) => { assert_eq!(error.kind(), io::ErrorKind::NotFound); break; } } } std::fs::remove_file(file).unwrap(); } }