diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 09:22:09 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 09:22:09 +0000 |
commit | 43a97878ce14b72f0981164f87f2e35e14151312 (patch) | |
tree | 620249daf56c0258faa40cbdcf9cfba06de2a846 /third_party/rust/tempfile/tests/tempfile.rs | |
parent | Initial commit. (diff) | |
download | firefox-43a97878ce14b72f0981164f87f2e35e14151312.tar.xz firefox-43a97878ce14b72f0981164f87f2e35e14151312.zip |
Adding upstream version 110.0.1.upstream/110.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/tempfile/tests/tempfile.rs')
-rw-r--r-- | third_party/rust/tempfile/tests/tempfile.rs | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/third_party/rust/tempfile/tests/tempfile.rs b/third_party/rust/tempfile/tests/tempfile.rs new file mode 100644 index 0000000000..f4dddb2906 --- /dev/null +++ b/third_party/rust/tempfile/tests/tempfile.rs @@ -0,0 +1,65 @@ +#![deny(rust_2018_idioms)] + +use std::fs; +use std::io::{Read, Seek, SeekFrom, Write}; +use std::sync::mpsc::{sync_channel, TryRecvError}; +use std::thread; + +#[test] +fn test_basic() { + let mut tmpfile = tempfile::tempfile().unwrap(); + write!(tmpfile, "abcde").unwrap(); + tmpfile.seek(SeekFrom::Start(0)).unwrap(); + let mut buf = String::new(); + tmpfile.read_to_string(&mut buf).unwrap(); + assert_eq!("abcde", buf); +} + +#[test] +fn test_cleanup() { + let tmpdir = tempfile::tempdir().unwrap(); + { + let mut tmpfile = tempfile::tempfile_in(&tmpdir).unwrap(); + write!(tmpfile, "abcde").unwrap(); + } + let num_files = fs::read_dir(&tmpdir).unwrap().count(); + assert!(num_files == 0); +} + +// Only run this test on Linux. MacOS doesn't like us creating so many files, apparently. +#[cfg(target_os = "linux")] +#[test] +fn test_pathological_cleaner() { + let tmpdir = tempfile::tempdir().unwrap(); + let (tx, rx) = sync_channel(0); + let cleaner_thread = thread::spawn(move || { + let tmp_path = rx.recv().unwrap(); + while rx.try_recv() == Err(TryRecvError::Empty) { + let files = fs::read_dir(&tmp_path).unwrap(); + for f in files { + // skip errors + if f.is_err() { + continue; + } + let f = f.unwrap(); + let _ = fs::remove_file(f.path()); + } + } + }); + + // block until cleaner_thread makes progress + tx.send(tmpdir.path().to_owned()).unwrap(); + // need 40-400 iterations to encounter race with cleaner on original system + for _ in 0..10000 { + let mut tmpfile = tempfile::tempfile_in(&tmpdir).unwrap(); + write!(tmpfile, "abcde").unwrap(); + tmpfile.seek(SeekFrom::Start(0)).unwrap(); + let mut buf = String::new(); + tmpfile.read_to_string(&mut buf).unwrap(); + assert_eq!("abcde", buf); + } + + // close the channel to make cleaner_thread exit + drop(tx); + cleaner_thread.join().expect("The cleaner thread failed"); +} |