Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion src/unsync/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -951,7 +951,7 @@ where
self.deques.unlink_ao(&mut entry);
Deques::unlink_wo(&mut self.deques.write_order, &mut entry);
evicted_entry_count += 1;
evicted_policy_weight = evicted_policy_weight.saturating_sub(weight as u64);
evicted_policy_weight = evicted_policy_weight.saturating_add(weight as u64);
} else {
self.deques.write_order.pop_front();
}
Expand Down Expand Up @@ -1441,6 +1441,25 @@ mod tests {
};
}

#[test]
fn test_ttl_weight_leak() {
let mut cache = Cache::builder()
.max_capacity(100)
.time_to_live(Duration::from_secs(10))
.build();

let (clock, mock) = Clock::mock();
cache.set_expiration_clock(Some(clock));

cache.insert("a", "alice");
assert_eq!(cache.weighted_size(), 1);

mock.increment(Duration::from_secs(10)); // 10 secs (expired).

assert_eq!(cache.get(&"a"), None);
assert_eq!(cache.weighted_size(), 0);
}

#[test]
fn test_debug_format() {
let mut cache = Cache::new(10);
Expand Down