Skip to content

Commit 3306d20

Browse files
committed
Fuzzy up touch file test in case sleep() is not totally precise.
1 parent 138256f commit 3306d20

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

src/utils/extern_args.rs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ mod test {
200200
fn verify_touch() -> Result<()> {
201201
const FILE_CONTENT: &[u8] =
202202
b"I am some random text with crlfs \r\n but also nls \n and terminated with a nl \n";
203-
const DELAY: Duration = Duration::from_millis(10); // don't hang up tests for too long.
203+
const DELAY: Duration = Duration::from_millis(20); // don't hang up tests for too long, but maybe 10ms is too short?
204204

205205
let temp_dir = tempfile::TempDir::new()?;
206206
let mut victim_path = temp_dir.path().to_owned();
@@ -215,15 +215,20 @@ mod test {
215215
let act_content = fs::read(&victim_path)?;
216216

217217
assert_eq!(FILE_CONTENT, act_content);
218+
let tdif = new_md
219+
.modified()
220+
.expect("getting modified time new")
221+
.duration_since(old_md.modified().expect("getting modified time old"))
222+
.expect("system time botch");
223+
// can't expect sleep 20ms to actually delay exactly that --
224+
// but the test is to verify that `touch` made the file look any newer.
225+
// Give ourselves 50% slop under what we were aiming for and call it good enough.
218226
assert!(
219-
new_md
220-
.modified()
221-
.expect("getting modified time")
222-
.duration_since(old_md.modified().expect("getting modified time old"))
223-
.expect("system botch")
224-
>= DELAY
227+
tdif >= (DELAY / 2),
228+
"verify_touch: expected {:?}, actual {:?}",
229+
DELAY,
230+
tdif
225231
);
226-
227232
Ok(())
228233
}
229234
}

0 commit comments

Comments
 (0)