From 565a79ddc55fdb875a4d44a74bcfb85faa6266df Mon Sep 17 00:00:00 2001 From: Yuya Nishihara Date: Fri, 19 Apr 2024 12:51:06 +0900 Subject: [PATCH] tests: fix potential mtime flakiness in git gc tests Apparently, these gc() invocations rely on that the previous "git gc" packed all refs so there are no loose refs to compare mtimes. If there were new (or remaining) loose refs, mtime comparison could fail. I also added +1sec to effectively turn off the keep_newer option, which isn't important in these tests. --- lib/tests/test_git_backend.rs | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/lib/tests/test_git_backend.rs b/lib/tests/test_git_backend.rs index 1d072d8d4da..de2f8f7a738 100644 --- a/lib/tests/test_git_backend.rs +++ b/lib/tests/test_git_backend.rs @@ -15,7 +15,7 @@ use std::collections::HashSet; use std::process::Command; use std::sync::Arc; -use std::time::SystemTime; +use std::time::{Duration, SystemTime}; use jj_lib::backend::CommitId; use jj_lib::git_backend::GitBackend; @@ -126,9 +126,12 @@ fn test_gc() { }, ); + // Don't rely on the exact system time because file modification time might + // have lower precision for example. + let now = || SystemTime::now() + Duration::from_secs(1); + // All reachable: redundant no-gc refs will be removed - let now = SystemTime::now(); - repo.store().gc(repo.index(), now).unwrap(); + repo.store().gc(repo.index(), now()).unwrap(); assert_eq!( collect_no_gc_refs(&git_repo), hashset! { @@ -147,7 +150,7 @@ fn test_gc() { mut_index.add_commit(&commit_e); mut_index.add_commit(&commit_f); mut_index.add_commit(&commit_h); - repo.store().gc(mut_index.as_index(), now).unwrap(); + repo.store().gc(mut_index.as_index(), now()).unwrap(); assert_eq!( collect_no_gc_refs(&git_repo), hashset! { @@ -163,7 +166,7 @@ fn test_gc() { mut_index.add_commit(&commit_b); mut_index.add_commit(&commit_c); mut_index.add_commit(&commit_f); - repo.store().gc(mut_index.as_index(), now).unwrap(); + repo.store().gc(mut_index.as_index(), now()).unwrap(); assert_eq!( collect_no_gc_refs(&git_repo), hashset! { @@ -175,7 +178,7 @@ fn test_gc() { // B|C|F are no longer reachable let mut mut_index = base_index.start_modification(); mut_index.add_commit(&commit_a); - repo.store().gc(mut_index.as_index(), now).unwrap(); + repo.store().gc(mut_index.as_index(), now()).unwrap(); assert_eq!( collect_no_gc_refs(&git_repo), hashset! { @@ -184,6 +187,6 @@ fn test_gc() { ); // All unreachable - repo.store().gc(base_index.as_index(), now).unwrap(); + repo.store().gc(base_index.as_index(), now()).unwrap(); assert_eq!(collect_no_gc_refs(&git_repo), hashset! {}); }