1 #![deny(rust_2018_idioms)]
4 use std
::io
::{Read, Seek, SeekFrom, Write}
;
5 use std
::sync
::mpsc
::{sync_channel, TryRecvError}
;
10 let mut tmpfile
= tempfile
::tempfile().unwrap();
11 write
!(tmpfile
, "abcde").unwrap();
12 tmpfile
.seek(SeekFrom
::Start(0)).unwrap();
13 let mut buf
= String
::new();
14 tmpfile
.read_to_string(&mut buf
).unwrap();
15 assert_eq
!("abcde", buf
);
20 let tmpdir
= tempfile
::tempdir().unwrap();
22 let mut tmpfile
= tempfile
::tempfile_in(&tmpdir
).unwrap();
23 write
!(tmpfile
, "abcde").unwrap();
25 let num_files
= fs
::read_dir(&tmpdir
).unwrap().count();
26 assert
!(num_files
== 0);
29 // Only run this test on Linux. MacOS doesn't like us creating so many files, apparently.
30 #[cfg(target_os = "linux")]
32 fn test_pathological_cleaner() {
33 let tmpdir
= tempfile
::tempdir().unwrap();
34 let (tx
, rx
) = sync_channel(0);
35 let cleaner_thread
= thread
::spawn(move || {
36 let tmp_path
= rx
.recv().unwrap();
37 while rx
.try_recv() == Err(TryRecvError
::Empty
) {
38 let files
= fs
::read_dir(&tmp_path
).unwrap();
45 let _
= fs
::remove_file(f
.path());
50 // block until cleaner_thread makes progress
51 tx
.send(tmpdir
.path().to_owned()).unwrap();
52 // need 40-400 iterations to encounter race with cleaner on original system
54 let mut tmpfile
= tempfile
::tempfile_in(&tmpdir
).unwrap();
55 write
!(tmpfile
, "abcde").unwrap();
56 tmpfile
.seek(SeekFrom
::Start(0)).unwrap();
57 let mut buf
= String
::new();
58 tmpfile
.read_to_string(&mut buf
).unwrap();
59 assert_eq
!("abcde", buf
);
62 // close the channel to make cleaner_thread exit
64 cleaner_thread
.join().expect("The cleaner thread failed");