From 95e7b53402cc5d189a5336903a59b0fde807e590 Mon Sep 17 00:00:00 2001 From: Niyaz Nigmatullin Date: Mon, 12 Sep 2022 17:40:59 +0300 Subject: [PATCH 1/4] test_sort: make timeout smarter, wait if failed to create dir Before the change it slept for 0.1 seconds and right after that asserted if `sort` has created the directory. Sometimes `sort` didn't manage to create the directory in 0.1 seconds. So the change is it tries to wait for `timeout` starting with 0.1 seconds, and if directory was not found, it tries 4 more times, each time increasing timeout twice. Once the directory is found it breaks. --- tests/by-util/test_sort.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/tests/by-util/test_sort.rs b/tests/by-util/test_sort.rs index ff4c79427..e0bfb6025 100644 --- a/tests/by-util/test_sort.rs +++ b/tests/by-util/test_sort.rs @@ -1124,7 +1124,14 @@ fn test_tmp_files_deleted_on_sigint() { ]); 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 From ec8e610e48e156a9d42de97629c8dffc375223c0 Mon Sep 17 00:00:00 2001 From: Niyaz Nigmatullin Date: Mon, 12 Sep 2022 18:32:57 +0300 Subject: [PATCH 2/4] test_sort: created a new big file for sort Before, the sort could work faster and we could be late with the signal. Now we create a new big file, `sort` can't process it in a minute, so we can safely wait for the temporary directory to be created and send a signal afterwards --- tests/by-util/test_sort.rs | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/tests/by-util/test_sort.rs b/tests/by-util/test_sort.rs index e0bfb6025..cb0aa2683 100644 --- a/tests/by-util/test_sort.rs +++ b/tests/by-util/test_sort.rs @@ -1117,8 +1117,23 @@ 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; + use std::io::Write; + let mut file = at.make_file(file_name); + // approximately 20 MB + for _ in 0..40 { + let lines = rand::thread_rng() + .sample_iter(rand::distributions::uniform::Uniform::new(0, 10007)) + .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", ]); From 729d97e9939ad5143260bcb79fbb1c02e5315142 Mon Sep 17 00:00:00 2001 From: Niyaz Nigmatullin Date: Mon, 12 Sep 2022 19:36:51 +0300 Subject: [PATCH 3/4] test_sort: use Pcg32 random number generator --- Cargo.lock | 10 ++++++++++ Cargo.toml | 1 + tests/by-util/test_sort.rs | 6 +++--- 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 1fefbb83c..e90824cb3 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..2830ac531 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -391,6 +391,7 @@ glob = "0.3.0" libc = "0.2" pretty_assertions = "1" rand = "0.8" +rand_pcg = "0.3" regex = "1.6" sha1 = { version="0.10", features=["std"] } tempfile = "3" diff --git a/tests/by-util/test_sort.rs b/tests/by-util/test_sort.rs index cb0aa2683..5c753470a 100644 --- a/tests/by-util/test_sort.rs +++ b/tests/by-util/test_sort.rs @@ -1119,13 +1119,13 @@ fn test_tmp_files_deleted_on_sigint() { at.mkdir("tmp_dir"); let file_name = "big_file_to_sort.txt"; { - use rand::Rng; + 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::thread_rng() - .sample_iter(rand::distributions::uniform::Uniform::new(0, 10007)) + 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::(); From 43d85c648975a6e57e73923879d4a159e02d1ae4 Mon Sep 17 00:00:00 2001 From: Niyaz Nigmatullin Date: Mon, 12 Sep 2022 19:54:49 +0300 Subject: [PATCH 4/4] rand_pcg is used only in unix tests, moved to unix dev-dependencies --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 2830ac531..ce528c41e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -391,7 +391,6 @@ glob = "0.3.0" libc = "0.2" pretty_assertions = "1" rand = "0.8" -rand_pcg = "0.3" regex = "1.6" sha1 = { version="0.10", features=["std"] } tempfile = "3" @@ -409,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"