diff --git a/crates/repo/src/repo.rs b/crates/repo/src/repo.rs index 666b9419..23e08cc0 100644 --- a/crates/repo/src/repo.rs +++ b/crates/repo/src/repo.rs @@ -2516,10 +2516,21 @@ impl Repo { } } +#[cfg(test)] +thread_local! { + /// Per-thread clock override for tests that need two commits to share a + /// timestamp without racing a wall-clock second boundary. + pub(crate) static TEST_NOW: std::cell::Cell> = const { std::cell::Cell::new(None) }; +} + /// Current unix time in seconds, for snapshot timestamps. Snapshot ids /// legitimately depend on commit time (as in Git); nothing in the system /// requires two separate commits of identical content to share an id. pub(crate) fn unix_now() -> i64 { + #[cfg(test)] + if let Some(t) = TEST_NOW.with(std::cell::Cell::get) { + return t; + } std::time::SystemTime::now() .duration_since(std::time::UNIX_EPOCH) .map(|d| d.as_secs() as i64) diff --git a/crates/repo/src/ws.rs b/crates/repo/src/ws.rs index f01c1b58..523827b6 100644 --- a/crates/repo/src/ws.rs +++ b/crates/repo/src/ws.rs @@ -2008,16 +2008,10 @@ mod tests { let dir = session.workspaces[0].dir.clone(); std::fs::write(dir.join("a.txt"), "edited-a\n").unwrap(); - // Reproduction requires the two `harvest_workspace` calls below to - // fall in the same `unix_now()` second (both mint a commit from the - // same parent/tree/author/message; only the timestamp can differ). - // Align to a fresh second boundary first so the whole sequence below - // — which takes low tens of milliseconds — has close to a full - // second of headroom, instead of racing an arbitrary boundary. - let start_second = crate::repo::unix_now(); - while crate::repo::unix_now() == start_second { - std::thread::sleep(std::time::Duration::from_millis(5)); - } + // Both harvests mint a commit from the same parent/tree/author/message; + // only the timestamp can differ. Pin the clock (this thread only) so + // the re-minted candidate id matches regardless of load. + crate::repo::TEST_NOW.with(|c| c.set(Some(1_700_000_000))); let outcomes = repo.ws_harvest(None, "t", None).unwrap(); let landed_tip = match &outcomes[0] { @@ -2036,8 +2030,7 @@ mod tests { // manifest rewrite that would have resolved the entry: re-mark ws-1 // live and restore its checkout dir with identical content, so a // re-harvest re-mints an identical candidate id (same parent, tree, - // author, and message; timestamp matches too as long as this test - // doesn't straddle a wall-clock second boundary). + // author, message, and the pinned timestamp). std::fs::create_dir_all(&dir).unwrap(); std::fs::write(dir.join("a.txt"), "edited-a\n").unwrap(); let crashed = WsSession { @@ -2078,6 +2071,7 @@ mod tests { ); assert!(repo.ws_session().unwrap().is_none()); + crate::repo::TEST_NOW.with(|c| c.set(None)); drop(repo); std::fs::remove_dir_all(&root).unwrap(); }