Skip to content

Commit c04a18f

Browse files
Alonely0naoNao89
andcommitted
chore(timeout): Add and reinforce tests
Now tests time out, so we can track regressions in timeout. Also added one to track nested timeouts. Co-authored-by: naoNao89 <[email protected]>
1 parent 6958c54 commit c04a18f

File tree

2 files changed

+57
-6
lines changed

2 files changed

+57
-6
lines changed

.vscode/cspell.dictionaries/jargon.wordlist.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
AFAICT
2+
alrm
3+
sigalrm
4+
SIGALRM
25
asimd
36
ASIMD
47
alloc

tests/by-util/test_timeout.rs

Lines changed: 54 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
use std::time::Duration;
1+
use std::time::{Duration, Instant};
2+
use uutests::util::TestScenario;
23

34
// This file is part of the uutils coreutils package.
45
//
@@ -52,11 +53,13 @@ fn test_command_with_args() {
5253
fn test_verbose() {
5354
for verbose_flag in ["-v", "--verbose"] {
5455
new_ucmd!()
55-
.args(&[verbose_flag, ".1", "sleep", "1"])
56+
.args(&[verbose_flag, ".1", "sleep", "10"])
57+
.timeout(Duration::from_secs(1))
5658
.fails()
5759
.stderr_only("timeout: sending signal TERM to command 'sleep'\n");
5860
new_ucmd!()
59-
.args(&[verbose_flag, "-s0", "-k.1", ".1", "sleep", "1"])
61+
.args(&[verbose_flag, "-s0", "-k.1", ".1", "sleep", "10"])
62+
.timeout(Duration::from_secs(1))
6063
.fails()
6164
.stderr_only("timeout: sending signal EXIT to command 'sleep'\ntimeout: sending signal KILL to command 'sleep'\n");
6265
}
@@ -107,22 +110,26 @@ fn test_preserve_status() {
107110
fn test_preserve_status_even_when_send_signal() {
108111
// When sending CONT signal, process doesn't get killed or stopped.
109112
// So, expected result is success and code 0.
113+
let time = Instant::now();
110114
for cont_spelling in ["CONT", "cOnT", "SIGcont"] {
111115
new_ucmd!()
112-
.args(&["-s", cont_spelling, "--preserve-status", ".1", "sleep", "1"])
116+
.args(&["-s", cont_spelling, "--preserve-status", ".1", "sleep", "2"])
113117
.succeeds()
114118
.no_output();
115119
}
120+
assert!(time.elapsed().as_secs() >= 6); // Assert they run for one second each.
116121
}
117122

118123
#[test]
119124
fn test_dont_overflow() {
120125
new_ucmd!()
121126
.args(&["9223372036854775808d", "sleep", "0"])
127+
.timeout(Duration::from_secs(2))
122128
.succeeds()
123129
.no_output();
124130
new_ucmd!()
125131
.args(&["-k", "9223372036854775808d", "10", "sleep", "0"])
132+
.timeout(Duration::from_secs(2))
126133
.succeeds()
127134
.no_output();
128135
}
@@ -183,11 +190,12 @@ fn test_kill_subprocess() {
183190
new_ucmd!()
184191
.args(&[
185192
// Make sure the CI can spawn the subprocess.
186-
"1",
193+
"5",
187194
"sh",
188195
"-c",
189-
"trap 'echo inside_trap' TERM; sleep 5",
196+
"trap 'echo inside_trap' TERM; sleep 30",
190197
])
198+
.timeout(Duration::from_secs(6)) // assert it exits when it times out.
191199
.fails_with_code(124)
192200
.stdout_contains("inside_trap");
193201
}
@@ -243,3 +251,43 @@ fn test_command_cannot_invoke() {
243251
// Try to execute a directory (should give permission denied or similar)
244252
new_ucmd!().args(&["1", "/"]).fails_with_code(126);
245253
}
254+
255+
#[test]
256+
fn test_cascaded_timeout_with_bash_trap() {
257+
// Use bash if available, otherwise skip
258+
if std::process::Command::new("bash")
259+
.arg("--version")
260+
.output()
261+
.is_err()
262+
{
263+
// Skip test if bash is not available
264+
return;
265+
}
266+
267+
// Test with bash explicitly to ensure SIGINT handlers work
268+
let script = r"
269+
trap 'echo bash_trap_fired; exit 0' INT
270+
sleep 10
271+
";
272+
273+
let ts = TestScenario::new("timeout");
274+
let timeout_bin = ts.bin_path.to_str().unwrap();
275+
276+
ts.ucmd()
277+
.args(&[
278+
"-s",
279+
"ALRM",
280+
"0.3",
281+
timeout_bin,
282+
"timeout",
283+
"-s",
284+
"INT",
285+
"5",
286+
"bash",
287+
"-c",
288+
script,
289+
])
290+
.timeout(Duration::from_secs(6))
291+
.fails_with_code(124)
292+
.stdout_contains("bash_trap_fired");
293+
}

0 commit comments

Comments
 (0)