Skip to content

Commit 3e7af9a

Browse files
committed
tests: fix race condition in timestamp checking
Linux kernel caches a "coarse" time that is only updated by timer ticks in a periodic way (see CONFIG_HZ). This introduces an artifical delay to ensure the coarse cached time has been updated between timestamp reads. Sleeping for 100ms should be enough to ensure several timer interrrupts elapsed, for common kernel configurations.
1 parent 08b9353 commit 3e7af9a

File tree

1 file changed

+6
-11
lines changed

1 file changed

+6
-11
lines changed

src/lib.rs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -908,21 +908,16 @@ mod tests {
908908
let td = tempfile::tempdir().unwrap();
909909
let d = openat::Dir::open(td.path()).unwrap();
910910
d.ensure_dir("foo", 0o755).unwrap();
911-
d.syncfs().unwrap();
912911
let before = d.metadata("foo").unwrap();
912+
// File timestamps can not be updated faster than kernel ticking granularity,
913+
// so this artificially sleeps through several timer interrupts.
914+
std::thread::sleep(std::time::Duration::from_millis(100));
913915

914916
d.update_timestamps("foo").unwrap();
915-
d.syncfs().unwrap();
916917
let after = d.metadata("foo").unwrap();
917-
918-
assert!(
919-
before.stat().st_atime != after.stat().st_atime
920-
|| before.stat().st_atime_nsec != after.stat().st_atime_nsec
921-
);
922-
assert!(
923-
before.stat().st_mtime != after.stat().st_mtime
924-
|| before.stat().st_mtime_nsec != after.stat().st_mtime_nsec
925-
);
918+
if before.stat().st_mtime == after.stat().st_mtime {
919+
assert_ne!(before.stat().st_mtime_nsec, after.stat().st_mtime_nsec);
920+
}
926921
}
927922

928923
#[test]

0 commit comments

Comments
 (0)