From 1bead6960c96d099b309706d6c35954c2d219188 Mon Sep 17 00:00:00 2001 From: Drew Gallardo Date: Wed, 17 Jun 2026 11:56:40 -0700 Subject: [PATCH] fix: pin mock clock in eventual consistency test (#3547) This PR fixes a flaky test I hit on Windows test in #3528. Looks like `test_eventual_consistency_background_refresh` was failing with `v_cached` expected 1, got 2. There was a pr which swapped `tokio::time::sleep(300ms)` for `clock::advance_by(300ms)`, which is pretty much fine but the test necer pinned the clock so the first `get()` locks the `cached_at` on wall time. Therefore, if our CI is taking long enough the ttl expires before the value assertion in the test. So now we can add a `pin()` and call it first `get()`. After that we can advance the clock manually with no problems. Also, it's worth noting that I tried pinning in `BackgroundCache::new()` first. That broke another test `test_reload_resets_consistency_timer`, which uses real `tokio::time::sleep` and needs wall clock after `clear_mock()`. So the pin stays in this test only. And this should unblock us. Failing instances: - https://github.com/lancedb/lancedb/actions/runs/27567527236/job/81495265474?pr=3528 - https://github.com/lancedb/lancedb/actions/runs/27560366489/job/81470414928 --- rust/lancedb/src/table/dataset.rs | 4 ++++ rust/lancedb/src/utils/background_cache.rs | 9 +++++++++ 2 files changed, 13 insertions(+) diff --git a/rust/lancedb/src/table/dataset.rs b/rust/lancedb/src/table/dataset.rs index 2dd3b7189..e37c3fc9f 100644 --- a/rust/lancedb/src/table/dataset.rs +++ b/rust/lancedb/src/table/dataset.rs @@ -518,6 +518,10 @@ mod tests { let wrapper = DatasetConsistencyWrapper::new_latest(ds, Some(Duration::from_millis(200))); + // Freeze `cached_at` on the mock clock so a slow external write below can't + // expire the TTL before the explicit advance_by() does (flake on loaded CI). + clock::pin(); + // Populate the cache let v1 = wrapper.get().await.unwrap().version().version; assert_eq!(v1, 1); diff --git a/rust/lancedb/src/utils/background_cache.rs b/rust/lancedb/src/utils/background_cache.rs index bdf033fec..e493fd4c6 100644 --- a/rust/lancedb/src/utils/background_cache.rs +++ b/rust/lancedb/src/utils/background_cache.rs @@ -329,6 +329,15 @@ pub mod clock { }); } + /// Start mock time at the current instant if not already pinned. + pub fn pin() { + MOCK_NOW.with(|mock| { + if mock.get().is_none() { + mock.set(Some(Instant::now())); + } + }); + } + #[allow(dead_code)] pub fn clear_mock() { MOCK_NOW.with(|mock| mock.set(None));