mirror of
https://github.com/storytold/effectcraft.git
synced 2026-10-30 04:57:54 -06:00
Disk cache: a failed free-space reading clears the last one (#2551)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
52dda004a5
commit
74ea0be88f
@@ -56,9 +56,8 @@ impl<T: Copy + Send + 'static> Watch<T> {
|
||||
}
|
||||
let (latest, busy) = (self.latest.clone(), self.busy.clone());
|
||||
let read = move || {
|
||||
if let Some(m) = read() {
|
||||
*latest.lock().unwrap_or_else(PoisonError::into_inner) = Some(m);
|
||||
}
|
||||
// A failed reading clears the last one: a stale value is worse than none.
|
||||
*latest.lock().unwrap_or_else(PoisonError::into_inner) = read();
|
||||
busy.store(false, Ordering::Release);
|
||||
};
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
@@ -72,7 +71,7 @@ impl<T: Copy + Send + 'static> Watch<T> {
|
||||
}
|
||||
}
|
||||
|
||||
/// The last reading (`None` until one finished).
|
||||
/// The last reading (`None` until one finished or when the last one failed).
|
||||
pub fn latest(&self) -> Option<T> {
|
||||
*self.latest.lock().unwrap_or_else(PoisonError::into_inner)
|
||||
}
|
||||
@@ -288,6 +287,26 @@ mod tests {
|
||||
assert_eq!(m.available, 1000);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_failed_reading_clears_the_last_one() {
|
||||
let w = Watch::<u64>::default();
|
||||
let wait = || {
|
||||
for _ in 0..500 {
|
||||
if !w.busy.load(Ordering::Acquire) {
|
||||
return;
|
||||
}
|
||||
std::thread::sleep(std::time::Duration::from_millis(2));
|
||||
}
|
||||
panic!("the reading never finished");
|
||||
};
|
||||
w.poll_with("ec-test", || Some(7));
|
||||
wait();
|
||||
assert_eq!(w.latest(), Some(7));
|
||||
w.poll_with("ec-test", || None);
|
||||
wait();
|
||||
assert_eq!(w.latest(), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parses_df() {
|
||||
let linux = "Filesystem 1024-blocks Used Available Capacity Mounted on\n/dev/nvme0n1p2 488281250 123456789 364824461 26% /\n";
|
||||
|
||||
Reference in New Issue
Block a user