Skip to content

Commit 7346353

Browse files
committed
test: add blocking, tokio
1 parent f6bf5bc commit 7346353

File tree

2 files changed

+114
-0
lines changed

2 files changed

+114
-0
lines changed

crates/libs/kill_tree/src/blocking.rs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,3 +124,58 @@ pub fn kill_tree_with_config(process_id: ProcessId, config: &Config) -> Result<O
124124
let process_infos = process_infos_provider.get_process_infos()?;
125125
crate::common::kill_tree_internal(process_id, config, process_infos)
126126
}
127+
128+
#[cfg(test)]
129+
mod tests {
130+
use super::*;
131+
use crate::get_available_max_process_id;
132+
133+
#[test]
134+
fn kill_tree_available_max_process_id() {
135+
let target_process_id = get_available_max_process_id();
136+
let result = kill_tree(target_process_id).expect("Failed to kill");
137+
assert_eq!(result.len(), 1);
138+
let output = &result[0];
139+
match output {
140+
crate::Output::Killed { .. } => {
141+
panic!("This should not happen");
142+
}
143+
crate::Output::MaybeAlreadyTerminated { process_id, source } => {
144+
assert_eq!(*process_id, target_process_id);
145+
assert_eq!(source.to_string(), "Unix error: ESRCH: No such process");
146+
}
147+
}
148+
}
149+
150+
#[test]
151+
fn kill_tree_with_config_sigkill_available_max_process_id() {
152+
let target_process_id = get_available_max_process_id();
153+
let config = Config {
154+
signal: String::from("SIGKILL"),
155+
..Default::default()
156+
};
157+
let result = kill_tree_with_config(target_process_id, &config).expect("Failed to kill");
158+
assert_eq!(result.len(), 1);
159+
let output = &result[0];
160+
match output {
161+
crate::Output::Killed { .. } => {
162+
panic!("This should not happen");
163+
}
164+
crate::Output::MaybeAlreadyTerminated { process_id, source } => {
165+
assert_eq!(*process_id, target_process_id);
166+
assert_eq!(source.to_string(), "Unix error: ESRCH: No such process");
167+
}
168+
}
169+
}
170+
171+
#[test]
172+
fn kill_tree_with_config_include_target_false_available_max_process_id() {
173+
let target_process_id = get_available_max_process_id();
174+
let config = Config {
175+
include_target: false,
176+
..Default::default()
177+
};
178+
let result = kill_tree_with_config(target_process_id, &config).expect("Failed to kill");
179+
assert_eq!(result.len(), 0);
180+
}
181+
}

crates/libs/kill_tree/src/tokio.rs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,3 +123,62 @@ pub async fn kill_tree_with_config(process_id: ProcessId, config: &Config) -> Re
123123
let process_infos = process_infos_provider.get_process_infos().await?;
124124
crate::common::kill_tree_internal(process_id, config, process_infos)
125125
}
126+
127+
#[cfg(test)]
128+
mod tests {
129+
use super::*;
130+
use crate::get_available_max_process_id;
131+
132+
#[::tokio::test]
133+
async fn kill_tree_available_max_process_id() {
134+
let target_process_id = get_available_max_process_id();
135+
let outputs = kill_tree(target_process_id).await.expect("Failed to kill");
136+
assert!(outputs.len() > 0);
137+
let output = &outputs[0];
138+
match output {
139+
crate::Output::Killed { .. } => {
140+
panic!("This should not happen");
141+
}
142+
crate::Output::MaybeAlreadyTerminated { process_id, source } => {
143+
assert_eq!(*process_id, target_process_id);
144+
assert_eq!(source.to_string(), "Unix error: ESRCH: No such process");
145+
}
146+
}
147+
}
148+
149+
#[::tokio::test]
150+
async fn kill_tree_with_config_sigkill_available_max_process_id() {
151+
let target_process_id = get_available_max_process_id();
152+
let config = Config {
153+
signal: String::from("SIGKILL"),
154+
..Default::default()
155+
};
156+
let outputs = kill_tree_with_config(target_process_id, &config)
157+
.await
158+
.expect("Failed to kill");
159+
assert!(outputs.len() > 0);
160+
let output = &outputs[0];
161+
match output {
162+
crate::Output::Killed { .. } => {
163+
panic!("This should not happen");
164+
}
165+
crate::Output::MaybeAlreadyTerminated { process_id, source } => {
166+
assert_eq!(*process_id, target_process_id);
167+
assert_eq!(source.to_string(), "Unix error: ESRCH: No such process");
168+
}
169+
}
170+
}
171+
172+
#[::tokio::test]
173+
async fn kill_tree_with_config_include_target_false_available_max_process_id() {
174+
let target_process_id = get_available_max_process_id();
175+
let config = Config {
176+
include_target: false,
177+
..Default::default()
178+
};
179+
let outputs = kill_tree_with_config(target_process_id, &config)
180+
.await
181+
.expect("Failed to kill");
182+
assert!(outputs.len() == 0);
183+
}
184+
}

0 commit comments

Comments
 (0)