Merge pull request #4229 from dotCooCoo/fix/accounts-lock-mtime-test

suite-account: the lock test checks the real modification-time change of a full hold cycle
This commit is contained in:
BFlat authored and GitHub committed 2026-11-29 13:01:37 +07:00
commit 8e7977a7de
1 file changed
+29 -11
+29 -11
View File
@@ -63,6 +63,14 @@ fn hold_mark(lock: &File) -> Option<HoldMark> {
lock.metadata().ok().map(|m| (m.len(), m.modified().ok()))
}
/// Moves the held lock file's length to the next value in `1..=HOLDS`.
fn mark_hold(lock: &File) {
// A failure here only costs waiters the restart of their timeout for this hold.
if let Some((n, _)) = hold_mark(lock) {
let _ = lock.set_len(n % HOLDS + 1);
}
}
fn lock_path(path: &Path) -> PathBuf {
let name = path.file_name().and_then(|n| n.to_str()).unwrap_or("accounts.json");
path.with_file_name(format!("{name}.lock"))
@@ -102,10 +110,7 @@ pub(crate) fn with_lock<T>(path: &Path, f: impl FnOnce() -> Result<T, Error>) ->
}
}
}
// A failure here only costs waiters the restart of their timeout for this hold.
if let Some((n, _)) = hold_mark(&lock) {
let _ = lock.set_len(n % HOLDS + 1);
}
mark_hold(&lock);
let result = f();
let _ = lock.unlock();
result
@@ -156,13 +161,26 @@ mod tests {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("accounts.json");
with_lock(&path, || Ok(())).unwrap();
let lock = open_lock_file(&path).unwrap();
let before = hold_mark(&lock).unwrap();
let Some(modified) = before.1 else { return };
lock.set_modified(modified + Duration::from_secs(1)).unwrap();
let after = hold_mark(&lock).unwrap();
assert_eq!(before.0, after.0);
assert_ne!(before, after, "a new hold that leaves the same length must still change the mark");
let before = {
// On Windows, a handle that set the time explicitly keeps it, so this one is closed first.
let lock = open_lock_file(&path).unwrap();
hold_mark(&lock).and_then(|m| m.1).expect("the lock file must report a modification time on every platform CI runs");
// 2000-01-01T00:00:00Z, a time FAT volumes can store.
lock.set_modified(SystemTime::UNIX_EPOCH + Duration::new(946_684_800, 0)).unwrap();
hold_mark(&lock).unwrap()
};
// Each hold opens and locks its own handle, as `with_lock` does, without its account-file preparation.
for _ in 0..HOLDS {
let lock = open_lock_file(&path).unwrap();
lock.lock().unwrap();
mark_hold(&lock);
}
let after = hold_mark(&open_lock_file(&path).unwrap()).unwrap();
assert_eq!(before.0, after.0, "a full cycle of holds returns the lock file to the same length");
let (Some(before_time), Some(after_time)) = (before.1, after.1) else {
panic!("the lock file must report a modification time before and after the holds");
};
assert_ne!(before_time, after_time, "a full cycle of holds must still change the modification time");
}
#[test]