blob: 065dccbe1d004c155577523413a87a5835cff6ba (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
#![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);
}
#[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");
}
|