From 9f30d146ab9871c1682f80c3ac365ebce2e66cd3 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Sat, 28 Nov 2026 20:13:24 -0800 Subject: [PATCH] suite-account: test a full cycle of real lock holds for the changed mark The test that tells apart two holds leaving the same length now backdates the lock file, runs HOLDS real holds, each on its own locked handle, and checks that the length is back where it started while the modification time has moved. Before, the test set the time itself, so it passed whatever the hold did to the file. The length update moves into mark_hold, which with_lock and the test share. --- crates/suite-account/src/lock.rs | 40 +++++++++++++++++++++++--------- 1 file changed, 29 insertions(+), 11 deletions(-) diff --git a/crates/suite-account/src/lock.rs b/crates/suite-account/src/lock.rs index 74906014b6..7ea21ce89a 100644 --- a/crates/suite-account/src/lock.rs +++ b/crates/suite-account/src/lock.rs @@ -63,6 +63,14 @@ fn hold_mark(lock: &File) -> Option { 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(path: &Path, f: impl FnOnce() -> Result) -> } } } - // 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]