use super::*;
use crate::test_metrics::{registry, value};
use tempfile::tempdir;
#[tokio::test]
async fn fallthrough_callbacks_are_not_called_without_disk_pressure() {
for disk_capacity in [0, 4 << 20] {
let directory = tempdir().unwrap();
let (registry, backend) = registry();
let cache = Cache::builder()
.directory(directory.path())
.disk_capacity(disk_capacity)
.memory_capacity(1 << 20)
.with_metrics(&backend)
.open()
.unwrap();
let key = "download";
let hash = cache.hash_key(key);
let source = Bytes::from_static(b"abcd");
let fetch_cache = cache.clone();
let fetch_source = source.clone();
let bytes = cache
.get_or_fetch_with_disk_fallthrough(
key,
|| panic!("disk miss"),
move || async move {
let mut buffer = fetch_cache.allocate_buffer(fetch_source.len()).unwrap();
buffer.as_mut().copy_from_slice(&fetch_source);
Ok::<_, &str>(buffer)
},
)
.await
.unwrap();
assert_eq!(bytes, "abcd");
assert_eq!(
cache
.get_or_fetch_with_disk_fallthrough(
key,
|| panic!("memory hit"),
|| async { Err::<AlignedBuffer, _>("must not fetch") },
)
.await
.unwrap(),
bytes
);
assert!(cache.inner.memory.remove(&hash));
if let Some(disk) = &cache.inner.disk {
disk.insert_batch(vec![(hash, source.clone())]).await.unwrap();
assert_eq!(
cache
.get_or_fetch_with_disk_fallthrough(
key,
|| panic!("queue has room"),
|| async { Err::<AlignedBuffer, _>("must not fetch") },
)
.await
.unwrap(),
bytes
);
assert_eq!(value(®istry, "feuer_lookup_total", &[("outcome", "disk_hit")]), 1.0);
}
assert_eq!(value(®istry, "feuer_lookup_total", &[("outcome", "callback")]), 1.0);
assert_eq!(
value(®istry, "feuer_lookup_total", &[("outcome", "memory_hit")]),
1.0
);
let object = "whole object";
let bytes = Bytes::from_static(b"object");
let fetch_bytes = bytes.clone();
assert_eq!(
cache
.get_or_fetch_with_disk_fallthrough(
object,
|| panic!("disk miss"),
move || async move { Ok::<_, &str>(fetch_bytes) },
)
.await
.unwrap(),
bytes
);
assert_eq!(
cache
.get_or_fetch_with_disk_fallthrough(
object,
|| panic!("memory hit"),
|| async { Err::<Bytes, _>("must not fetch") },
)
.await
.unwrap(),
bytes
);
}
}