forked from 1jehuang/jcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplatform_tests.rs
More file actions
103 lines (86 loc) · 3.05 KB
/
platform_tests.rs
File metadata and controls
103 lines (86 loc) · 3.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
use super::*;
#[test]
fn desired_nofile_soft_limit_only_raises_when_possible() {
assert_eq!(desired_nofile_soft_limit(1024, 524_288, 8192), Some(8192));
assert_eq!(desired_nofile_soft_limit(8192, 524_288, 8192), None);
assert_eq!(desired_nofile_soft_limit(1024, 4096, 8192), Some(4096));
}
#[cfg(unix)]
#[test]
fn spawn_detached_creates_new_session() {
use tempfile::NamedTempFile;
let output = NamedTempFile::new().expect("temp file");
let output_path = output.path().to_string_lossy().to_string();
let parent_sid = unsafe { libc::getsid(0) };
let mut cmd = std::process::Command::new("sh");
cmd.arg("-c")
.arg("ps -o sid= -p $$ > \"$JCODE_TEST_OUTPUT\"")
.env("JCODE_TEST_OUTPUT", &output_path)
.stdout(std::process::Stdio::null())
.stderr(std::process::Stdio::null());
let mut child = super::spawn_detached(&mut cmd).expect("spawn detached child");
let status = child.wait().expect("wait for child");
assert!(status.success(), "child should exit successfully");
let child_sid = std::fs::read_to_string(&output_path)
.expect("read child sid")
.trim()
.parse::<u32>()
.expect("parse child sid");
assert_eq!(
child_sid,
child.id(),
"detached child should lead its own session"
);
assert_ne!(
child_sid as i32, parent_sid,
"detached child should not share parent session"
);
}
#[cfg(windows)]
#[test]
fn is_process_running_reports_exited_children_as_stopped() {
use std::process::{Command, Stdio};
use std::time::Duration;
let mut cmd = Command::new("cmd.exe");
cmd.args(["/C", "ping -n 3 127.0.0.1 >NUL"])
.stdout(Stdio::null())
.stderr(Stdio::null());
let mut child = cmd.spawn().expect("spawn child");
let pid = child.id();
assert!(
super::is_process_running(pid),
"child should initially be running"
);
let status = child.wait().expect("wait for child");
assert!(status.success(), "child should exit successfully");
std::thread::sleep(Duration::from_millis(100));
assert!(
!super::is_process_running(pid),
"exited child should not be reported as running"
);
}
#[cfg(windows)]
#[test]
fn spawn_replacement_process_returns_without_waiting_for_child_exit() {
use std::process::{Command, Stdio};
use std::time::{Duration, Instant};
let mut cmd = Command::new("cmd.exe");
cmd.args(["/C", "ping -n 4 127.0.0.1 >NUL"])
.stdout(Stdio::null())
.stderr(Stdio::null());
let start = Instant::now();
let mut child = super::spawn_replacement_process(&mut cmd)
.expect("spawn replacement process should succeed");
let elapsed = start.elapsed();
assert!(
elapsed < Duration::from_secs(1),
"replacement spawn should not block, took {:?}",
elapsed
);
assert!(
child.try_wait().expect("poll child status").is_none(),
"replacement child should still be running immediately after spawn"
);
child.kill().ok();
let _ = child.wait();
}