Debian Codegraph @

tests/by-util/test_sleep.rs

raw
1 // This file is part of the uutils coreutils package.
2 //
3 // For the full copyright and license information, please view the LICENSE
4 // file that was distributed with this source code.
5
6 use rstest::rstest;
7
8 use uucore::display::Quotable;
9 // spell-checker:ignore dont SIGBUS SIGSEGV sigsegv sigbus infd
10 use uutests::new_ucmd;
11
12 #[cfg(unix)]
13 use nix::sys::signal::Signal::{SIGBUS, SIGSEGV};
14 use std::io::ErrorKind;
15 use std::time::{Duration, Instant};
16
17 #[test]
18 fn test_invalid_time_interval() {
19 new_ucmd!()
20 .arg("xyz")
21 .fails()
22 .usage_error("invalid time interval 'xyz'");
23 new_ucmd!()
24 .args(&["--", "-1"])
25 .fails()
26 .usage_error("invalid time interval '-1'");
27 }
28
29 #[test]
30 fn test_sleep_no_suffix() {
31 let millis_100 = Duration::from_millis(100);
32 let before_test = Instant::now();
33
34 new_ucmd!().args(&["0.1"]).succeeds().stdout_only("");
35
36 let duration = before_test.elapsed();
37 assert!(duration >= millis_100);
38 }
39
40 #[test]
41 fn test_sleep_s_suffix() {
42 let millis_100 = Duration::from_millis(100);
43 let before_test = Instant::now();
44
45 new_ucmd!().args(&["0.1s"]).succeeds().stdout_only("");
46
47 let duration = before_test.elapsed();
48 assert!(duration >= millis_100);
49 }
50
51 #[test]
52 fn test_sleep_m_suffix() {
53 let millis_600 = Duration::from_millis(600);
54 let before_test = Instant::now();
55
56 new_ucmd!().args(&["0.01m"]).succeeds().stdout_only("");
57
58 let duration = before_test.elapsed();
59 assert!(duration >= millis_600);
60 }
61
62 #[test]
63 fn test_sleep_h_suffix() {
64 let millis_360 = Duration::from_millis(360);
65 let before_test = Instant::now();
66
67 new_ucmd!().args(&["0.0001h"]).succeeds().stdout_only("");
68
69 let duration = before_test.elapsed();
70 assert!(duration >= millis_360);
71 }
72
73 #[test]
74 fn test_sleep_negative_duration() {
75 new_ucmd!().args(&["-1"]).fails();
76 new_ucmd!().args(&["-1s"]).fails();
77 new_ucmd!().args(&["-1m"]).fails();
78 new_ucmd!().args(&["-1h"]).fails();
79 new_ucmd!().args(&["-1d"]).fails();
80 }
81
82 #[test]
83 fn test_sleep_zero_duration() {
84 new_ucmd!().args(&["0"]).succeeds().stdout_only("");
85 new_ucmd!().args(&["0s"]).succeeds().stdout_only("");
86 new_ucmd!().args(&["0m"]).succeeds().stdout_only("");
87 new_ucmd!().args(&["0h"]).succeeds().stdout_only("");
88 new_ucmd!().args(&["0d"]).succeeds().stdout_only("");
89 }
90
91 #[test]
92 fn test_sleep_no_argument() {
93 new_ucmd!()
94 .fails()
95 .stderr_contains("the following required arguments were not provided"); // clap provided message
96 }
97
98 #[test]
99 fn test_sleep_sum_duration_same_suffix() {
100 let millis_200 = Duration::from_millis(100 + 100);
101 let before_test = Instant::now();
102
103 new_ucmd!()
104 .args(&["0.1s", "0.1s"])
105 .succeeds()
106 .stdout_only("");
107
108 let duration = before_test.elapsed();
109 assert!(duration >= millis_200);
110 }
111
112 #[test]
113 fn test_sleep_sum_duration_different_suffix() {
114 let millis_700 = Duration::from_millis(100 + 600);
115 let before_test = Instant::now();
116
117 new_ucmd!()
118 .args(&["0.1s", "0.01m"])
119 .succeeds()
120 .stdout_only("");
121
122 let duration = before_test.elapsed();
123 assert!(duration >= millis_700);
124 }
125
126 #[test]
127 fn test_sleep_sum_duration_many() {
128 let millis_900 = Duration::from_millis(100 + 100 + 300 + 400);
129 let before_test = Instant::now();
130
131 new_ucmd!()
132 .args(&["0.1s", "0.1s", "0.3s", "0.4s"])
133 .succeeds()
134 .stdout_only("");
135
136 let duration = before_test.elapsed();
137 assert!(duration >= millis_900);
138 }
139
140 #[test]
141 fn test_sleep_wrong_time() {
142 new_ucmd!().args(&["0.1s", "abc"]).fails();
143 }
144
145 #[test]
146 #[cfg(unix)]
147 fn test_sleep_stops_after_sigsegv() {
148 let mut child = new_ucmd!()
149 .arg("100")
150 .timeout(Duration::from_secs(10))
151 .run_no_wait();
152
153 child
154 .delay(100)
155 .kill_with_custom_signal(SIGSEGV)
156 .make_assertion()
157 .with_current_output()
158 .signal_is(SIGSEGV as i32) // make sure it was us who terminated the process
159 .no_output();
160 }
161
162 #[test]
163 #[cfg(unix)]
164 fn test_sleep_stops_after_sigbus() {
165 let mut child = new_ucmd!()
166 .arg("100")
167 .timeout(Duration::from_secs(10))
168 .run_no_wait();
169
170 child
171 .delay(100)
172 .kill_with_custom_signal(SIGBUS)
173 .make_assertion()
174 .with_current_output()
175 .signal_is(SIGBUS as i32) // make sure it was us who terminated the process
176 .no_output();
177 }
178
179 #[test]
180 fn test_sleep_when_single_input_exceeds_max_duration_then_no_error() {
181 let mut child = new_ucmd!()
182 .arg(format!("{}", u128::from(u64::MAX) + 1))
183 .timeout(Duration::from_secs(10))
184 .run_no_wait();
185
186 #[cfg(unix)]
187 child
188 .delay(100)
189 .kill()
190 .make_assertion()
191 .with_current_output()
192 .signal_is(9) // make sure it was us who terminated the process
193 .no_output();
194 #[cfg(windows)]
195 child
196 .delay(100)
197 .kill()
198 .make_assertion()
199 .with_current_output()
200 .failure()
201 .no_output();
202 }
203
204 #[test]
205 fn test_sleep_when_multiple_inputs_exceed_max_duration_then_no_error() {
206 let mut child = new_ucmd!()
207 .arg(format!("{}", u64::MAX))
208 .arg("1")
209 .timeout(Duration::from_secs(10))
210 .run_no_wait();
211
212 #[cfg(unix)]
213 child
214 .delay(100)
215 .kill()
216 .make_assertion()
217 .with_current_output()
218 .signal_is(9) // make sure it was us who terminated the process
219 .no_output();
220 #[cfg(windows)]
221 child
222 .delay(100)
223 .kill()
224 .make_assertion()
225 .with_current_output()
226 .failure()
227 .no_output();
228 }
229
230 #[rstest]
231 #[case::whitespace_prefix(" 0.1s")]
232 #[case::multiple_whitespace_prefix(" 0.1s")]
233 fn test_sleep_when_input_has_leading_whitespace_then_no_error(#[case] input: &str) {
234 new_ucmd!()
235 .arg(input)
236 .timeout(Duration::from_secs(10))
237 .succeeds()
238 .no_output();
239 }
240
241 #[rstest]
242 #[case::whitespace_suffix("0.1s ")]
243 #[case::mixed_newlines_spaces_tabs("\n\t0.1s \n ")]
244 fn test_sleep_when_input_has_trailing_whitespace_then_error(#[case] input: &str) {
245 new_ucmd!()
246 .arg(input)
247 .timeout(Duration::from_secs(10))
248 .fails()
249 .usage_error(format!("invalid time interval {}", input.quote()));
250 }
251
252 #[rstest]
253 #[case::only_space(" ")]
254 #[case::only_tab("\t")]
255 #[case::only_newline("\n")]
256 fn test_sleep_when_input_has_only_whitespace_then_error(#[case] input: &str) {
257 new_ucmd!()
258 .arg(input)
259 .timeout(Duration::from_secs(10))
260 .fails()
261 .usage_error(format!("invalid time interval {}", input.quote()));
262 }
263
264 #[test]
265 fn test_sleep_when_multiple_input_some_with_error_then_shows_all_errors() {
266 let expected = "invalid time interval 'abc'\n\
267 sleep: invalid time interval '1years'\n\
268 sleep: invalid time interval ' '";
269
270 // Even if one of the arguments is valid, but the rest isn't, we should still fail and exit early.
271 // So, the timeout of 10 seconds ensures we haven't executed `thread::sleep` with the only valid
272 // interval of `100000.0` seconds.
273 new_ucmd!()
274 .args(&["abc", "100000.0", "1years", " "])
275 .timeout(Duration::from_secs(10))
276 .fails()
277 .usage_error(expected);
278 }
279
280 #[test]
281 fn test_negative_interval() {
282 new_ucmd!()
283 .args(&["--", "-1"])
284 .fails()
285 .usage_error("invalid time interval '-1'");
286 }
287
288 #[rstest]
289 #[case::int("0x0")]
290 #[case::negative_zero("-0x0")]
291 #[case::int_suffix("0x0s")]
292 #[case::int_suffix("0x0h")]
293 #[case::frac("0x0.1")]
294 #[case::frac_suffix("0x0.1s")]
295 #[case::frac_suffix("0x0.001h")]
296 #[case::scientific("0x1.0p-3")]
297 #[case::scientific_suffix("0x1.0p-4s")]
298 fn test_valid_hex_duration(#[case] input: &str) {
299 new_ucmd!().args(&["--", input]).succeeds().no_output();
300 }
301
302 #[rstest]
303 #[case::negative("-0x1")]
304 #[case::negative_suffix("-0x1s")]
305 #[case::negative_frac_suffix("-0x0.1s")]
306 #[case::wrong_capitalization("infD")]
307 #[case::wrong_capitalization("INFD")]
308 #[case::wrong_capitalization("iNfD")]
309 #[case::single_quote("'1")]
310 fn test_invalid_duration(#[case] input: &str) {
311 new_ucmd!()
312 .args(&["--", input])
313 .fails()
314 .usage_error(format!("invalid time interval {}", input.quote()));
315 }
316
317 #[cfg(unix)]
318 #[test]
319 #[should_panic = "Program must be run first or has not finished"]
320 fn test_cmd_result_signal_when_still_running_then_panic() {
321 let mut child = new_ucmd!().arg("60").run_no_wait();
322
323 child
324 .make_assertion()
325 .is_alive()
326 .with_current_output()
327 .signal();
328 }
329
330 #[cfg(unix)]
331 #[test]
332 fn test_cmd_result_signal_when_kill_then_signal() {
333 let mut child = new_ucmd!().arg("60").run_no_wait();
334
335 child.kill();
336 child
337 .make_assertion()
338 .is_not_alive()
339 .with_current_output()
340 .signal_is(9)
341 .signal_name_is("SIGKILL")
342 .signal_name_is("KILL")
343 .signal_name_is("9")
344 .signal()
345 .expect("Signal was none");
346
347 child
348 .wait()
349 .unwrap()
350 .signal_is(9)
351 .signal_name_is("SIGKILL")
352 .signal_name_is("KILL")
353 .signal_name_is("9")
354 .signal()
355 .expect("Signal was none");
356 }
357
358 #[cfg(unix)]
359 #[rstest]
360 #[case::signal_only_part_of_name("IGKILL")] // spell-checker: disable-line
361 #[case::signal_just_sig("SIG")]
362 #[case::signal_value_too_high("100")]
363 #[case::signal_value_negative("-1")]
364 #[should_panic = "Invalid signal name or value"]
365 fn test_cmd_result_signal_when_invalid_signal_name_then_panic(#[case] signal_name: &str) {
366 let mut child = new_ucmd!().arg("60").run_no_wait();
367
368 child.kill();
369 child.wait().unwrap().signal_name_is(signal_name);
370 }
371
372 #[test]
373 #[cfg(unix)]
374 fn test_cmd_result_signal_name_is_accepts_lowercase() {
375 let mut child = new_ucmd!().arg("60").run_no_wait();
376 child.kill();
377 let result = child.wait().unwrap();
378 result.signal_name_is("sigkill");
379 result.signal_name_is("kill");
380 }
381
382 #[test]
383 fn test_uchild_when_wait_and_timeout_is_reached_then_timeout_error() {
384 let child = new_ucmd!()
385 .timeout(Duration::from_secs(1))
386 .arg("10.0")
387 .run_no_wait();
388
389 match child.wait() {
390 Err(error) if error.kind() == ErrorKind::Other => {
391 std::assert_eq!(error.to_string(), "wait: Timeout of '1s' reached");
392 }
393 Err(error) => panic!("Assertion failed: Expected error with timeout but was: {error}"),
394 Ok(_) => panic!("Assertion failed: Expected timeout of `wait`."),
395 }
396 }
397
398 #[rstest]
399 #[timeout(Duration::from_secs(5))]
400 fn test_uchild_when_kill_and_timeout_higher_than_kill_time_then_no_panic() {
401 let mut child = new_ucmd!()
402 .timeout(Duration::from_secs(60))
403 .arg("20.0")
404 .run_no_wait();
405
406 child.kill().make_assertion().is_not_alive();
407 }
408
409 #[test]
410 fn test_uchild_when_try_kill_and_timeout_is_reached_then_error() {
411 let mut child = new_ucmd!()
412 .timeout(Duration::ZERO)
413 .arg("10.0")
414 .run_no_wait();
415
416 match child.try_kill() {
417 Err(error) if error.kind() == ErrorKind::Other => {
418 std::assert_eq!(error.to_string(), "kill: Timeout of '0s' reached");
419 }
420 Err(error) => panic!("Assertion failed: Expected error with timeout but was: {error}"),
421 Ok(()) => panic!("Assertion failed: Expected timeout of `try_kill`."),
422 }
423 }
424
425 #[test]
426 #[should_panic = "kill: Timeout of '0s' reached"]
427 fn test_uchild_when_kill_with_timeout_and_timeout_is_reached_then_panic() {
428 let mut child = new_ucmd!()
429 .timeout(Duration::ZERO)
430 .arg("10.0")
431 .run_no_wait();
432
433 child.kill();
434 panic!("Assertion failed: Expected timeout of `kill`.");
435 }
436
437 #[test]
438 #[should_panic(expected = "wait: Timeout of '1.1s' reached")]
439 fn test_ucommand_when_run_with_timeout_and_timeout_is_reached_then_panic() {
440 new_ucmd!()
441 .timeout(Duration::from_millis(1100))
442 .arg("10.0")
443 .run();
444
445 panic!("Assertion failed: Expected timeout of `run`.")
446 }
447
448 #[rstest]
449 #[timeout(Duration::from_secs(10))]
450 fn test_ucommand_when_run_with_timeout_higher_then_execution_time_then_no_panic() {
451 new_ucmd!()
452 .timeout(Duration::from_secs(60))
453 .arg("1.0")
454 .run();
455 }