From eaf0ae2bd2f18afe703b3e803a9a82d40e4566ed Mon Sep 17 00:00:00 2001 From: Tuomas Tynkkynen Date: Sat, 18 Jan 2025 20:59:05 +0200 Subject: [PATCH] test_pgrep: Try to fix flaky tests (#309) test_delimiter and test_delimiter_last_wins seem to rely on the system running the test having 2 or more 'sh' processes running, but that doesn't seem to be reliably the case in GitHub pipelines. Explicitly spawn two sleep processes instead. --- tests/by-util/test_pgrep.rs | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/tests/by-util/test_pgrep.rs b/tests/by-util/test_pgrep.rs index 6917d46..9c21dd5 100644 --- a/tests/by-util/test_pgrep.rs +++ b/tests/by-util/test_pgrep.rs @@ -3,6 +3,12 @@ // For the full copyright and license information, please view the LICENSE // file that was distributed with this source code. +#[cfg(target_os = "linux")] +use std::{ + array, + process::{Child, Command}, +}; + use crate::common::util::TestScenario; #[cfg(target_os = "linux")] use regex::Regex; @@ -151,24 +157,32 @@ fn test_valid_regex() { new_ucmd!().arg("a*").succeeds(); } +#[cfg(target_os = "linux")] +fn spawn_2_dummy_sleep_processes() -> [Child; 2] { + array::from_fn(|_| Command::new("sleep").arg("2").spawn().unwrap()) +} + #[cfg(target_os = "linux")] #[test] fn test_delimiter() { + let mut sleep_processes = spawn_2_dummy_sleep_processes(); for arg in ["-d", "--delimiter"] { new_ucmd!() - .arg("sh") + .arg("sleep") .arg(arg) .arg("|") .succeeds() .stdout_contains("|"); } + sleep_processes.iter_mut().for_each(|p| drop(p.kill())); } #[cfg(target_os = "linux")] #[test] fn test_delimiter_last_wins() { + let mut sleep_processes = spawn_2_dummy_sleep_processes(); new_ucmd!() - .arg("sh") + .arg("sleep") .arg("-d_") .arg("-d:") .succeeds() @@ -176,12 +190,13 @@ fn test_delimiter_last_wins() { .stdout_contains(":"); new_ucmd!() - .arg("sh") + .arg("sleep") .arg("-d:") .arg("-d_") .succeeds() .stdout_does_not_contain(":") .stdout_contains("_"); + sleep_processes.iter_mut().for_each(|p| drop(p.kill())); } #[test]