diff --git a/Cargo.lock b/Cargo.lock index cc8cf8424..a28ed3e07 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -367,6 +367,7 @@ dependencies = [ "pretty_assertions", "procfs", "rand", + "rand_pcg", "regex", "rlimit", "selinux", @@ -1664,6 +1665,15 @@ dependencies = [ "getrandom", ] +[[package]] +name = "rand_pcg" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "59cad018caf63deb318e5a4586d99a24424a364f40f1e5778c29aca23f4fc73e" +dependencies = [ + "rand_core", +] + [[package]] name = "rayon" version = "1.5.3" diff --git a/Cargo.toml b/Cargo.toml index 6086b968e..ce528c41e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -408,6 +408,7 @@ rlimit = "0.8.3" [target.'cfg(unix)'.dev-dependencies] nix = { version = "0.25", default-features = false, features = ["process", "signal", "user"] } rust-users = { version="0.11", package="users" } +rand_pcg = "0.3" [build-dependencies] phf_codegen = "0.11.1" diff --git a/tests/by-util/test_sort.rs b/tests/by-util/test_sort.rs index ff4c79427..5c753470a 100644 --- a/tests/by-util/test_sort.rs +++ b/tests/by-util/test_sort.rs @@ -1117,14 +1117,36 @@ fn test_tmp_files_deleted_on_sigint() { let (at, mut ucmd) = at_and_ucmd!(); at.mkdir("tmp_dir"); + let file_name = "big_file_to_sort.txt"; + { + use rand::{Rng, SeedableRng}; + use std::io::Write; + let mut file = at.make_file(file_name); + // approximately 20 MB + for _ in 0..40 { + let lines = rand_pcg::Pcg32::seed_from_u64(123) + .sample_iter(rand::distributions::uniform::Uniform::new(0, 10000)) + .take(100000) + .map(|x| x.to_string() + "\n") + .collect::(); + file.write_all(lines.as_bytes()).unwrap(); + } + } ucmd.args(&[ - "ext_sort.txt", + file_name, "--buffer-size=1", // with a small buffer size `sort` will be forced to create a temporary directory very soon. "--temporary-directory=tmp_dir", ]); let mut child = ucmd.run_no_wait(); // wait a short amount of time so that `sort` can create a temporary directory. - std::thread::sleep(Duration::from_millis(100)); + let mut timeout = Duration::from_millis(100); + for _ in 0..5 { + std::thread::sleep(timeout); + if read_dir(at.plus("tmp_dir")).unwrap().next().is_some() { + break; + } + timeout *= 2; + } // `sort` should have created a temporary directory. assert!(read_dir(at.plus("tmp_dir")).unwrap().next().is_some()); // kill sort with SIGINT