Skip to content

Commit

Permalink
cache: disable caching for tests (#4943)
Browse files Browse the repository at this point in the history
As `#[sqlx::test]` spawn a database per test, the cache
could yield unexpected results
  • Loading branch information
uael authored Dec 18, 2024
1 parent fe4334d commit a040513
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions backend/windmill-common/src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ impl<Key: Eq + Hash + fs::Item, Val: Clone> FsBackedCache<Key, Val> {
Key: Clone,
F: Future<Output = error::Result<T>>,
{
if cfg!(test) {
// Disable caching in tests: since `#[sqlx::test]` spawn a database per test, the cache
// could yield unexpected results.
return with.await.map(map);
}
self.cache
.get_or_insert_async(&key, async {
fs::import_or_insert_with(self.path(&key), with)
Expand Down Expand Up @@ -584,7 +589,7 @@ pub mod job {
raw_flow: Option<Json<Box<RawValue>>>,
) -> impl Future<Output = error::Result<RawData>> + 'a {
let loc = Location::caller();
PREVIEWS.get_or_insert_async(job, async move {
let fetch = async move {
match (raw_lock, raw_code, raw_flow) {
(None, None, None) => sqlx::query!(
"SELECT raw_code, raw_lock, raw_flow AS \"raw_flow: Json<Box<RawValue>>\" \
Expand All @@ -602,7 +607,13 @@ pub mod job {
Some(Json(flow)) => RawData::Flow(Arc::new(FlowData::from_raw(flow))),
_ => RawData::Script(Arc::new(ScriptData::from_raw(lock, code))),
})
})
};
// Disable caching in tests: as `#[sqlx::test]` spawn a database per test, the cache
// could yield unexpected results.
#[cfg(test)]
return fetch;
#[cfg(not(test))]
PREVIEWS.get_or_insert_async(job, fetch)
}

#[track_caller]
Expand Down

0 comments on commit a040513

Please sign in to comment.