mod common;
use std::sync::atomic::{AtomicUsize, Ordering};
use common::*;
#[test]
fn test_file_auto_refresh() -> anyhow::Result<()> {
let i: AtomicUsize = AtomicUsize::new(0);
let cache = fcache::new()?.with_refresh_interval(Duration::ZERO);
let cache_file = cache.get_lazy("file.txt", move |mut file| {
file.write_fmt(format_args!("{}", i.load(Ordering::SeqCst)))?;
i.fetch_add(1, Ordering::SeqCst);
Ok(())
})?;
{
let mut content = String::new();
cache_file.open()?.read_to_string(&mut content)?;
assert_eq!(content, "0");
}
{
let mut content = String::new();
cache_file.open()?.read_to_string(&mut content)?;
assert_eq!(content, "1");
}
Ok(())
}
#[test]
fn test_file_manual_refresh() -> anyhow::Result<()> {
let i: AtomicUsize = AtomicUsize::new(0);
let cache = fcache::new()?.with_refresh_interval(Duration::MAX);
let cache_file = cache.get("file.txt", move |mut file| {
file.write_fmt(format_args!("{}", i.load(Ordering::SeqCst)))?;
i.fetch_add(1, Ordering::SeqCst);
Ok(())
})?;
{
let mut content = String::new();
cache_file.open()?.read_to_string(&mut content)?;
assert_eq!(content, "0");
}
let cache_file = cache_file.with_refresh_interval(Duration::ZERO);
cache_file.refresh()?;
let cache_file = cache_file.with_refresh_interval(Duration::MAX);
{
let mut content = String::new();
cache_file.open()?.read_to_string(&mut content)?;
assert_eq!(content, "1");
}
Ok(())
}
#[test]
fn test_file_force_refresh() -> anyhow::Result<()> {
let i: AtomicUsize = AtomicUsize::new(0);
let cache = fcache::new()?.with_refresh_interval(Duration::MAX);
let cache_file = cache.get("file.txt", move |mut file| {
file.write_fmt(format_args!("{}", i.load(Ordering::SeqCst)))?;
i.fetch_add(1, Ordering::SeqCst);
Ok(())
})?;
{
let mut content = String::new();
cache_file.open()?.read_to_string(&mut content)?;
assert_eq!(content, "0");
}
cache_file.force_refresh()?;
{
let mut content = String::new();
cache_file.open()?.read_to_string(&mut content)?;
assert_eq!(content, "1");
}
Ok(())
}