Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions crates/repo/src/repo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Option<i64>> = 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)
Expand Down
18 changes: 6 additions & 12 deletions crates/repo/src/ws.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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] {
Expand All @@ -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 {
Expand Down Expand Up @@ -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();
}
Expand Down
Loading