Skip to content

Commit

Permalink
Support notification on eviction
Browse files Browse the repository at this point in the history
Tweak an unit test.
  • Loading branch information
tatsuya6502 committed May 31, 2022
1 parent b4e0171 commit 1727b78
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/notification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::sync::Arc;
// TODO: Perhaps `Arc<dyn Fn(...)>` is enough for the most use cases because
// Sync would require captured values to be interior mutable?
// pub(crate) type EvictionListener<K, V> =
// Arc<Mutex<dyn Fn(Arc<K>, V, RemovalCause) + Send + Sync + 'static>>;
// Arc<Mutex<dyn FnMut(Arc<K>, V, RemovalCause) + Send + Sync + 'static>>;
pub(crate) type EvictionListener<K, V> =
Arc<dyn Fn(Arc<K>, V, RemovalCause) + Send + Sync + 'static>;

Expand Down
11 changes: 6 additions & 5 deletions src/sync/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1943,7 +1943,7 @@ mod tests {

#[test]
fn test_removal_notifications() {
// These `Vec`s will store actual and expected notifications.
// These `Vec`s will hold actual and expected notifications.
let actual = Arc::new(Mutex::new(Vec::new()));
let mut expected = Vec::new();

Expand Down Expand Up @@ -1986,6 +1986,7 @@ mod tests {

// Retry.
cache.insert('e', "eliza");
// and the LRU entry will be evicted.
expected.push((Arc::new('b'), "bob", RemovalCause::Size));
cache.sync();
assert_eq!(cache.entry_count(), 3);
Expand All @@ -1999,10 +2000,10 @@ mod tests {
// Ensure all scheduled notifications have been processed.
std::thread::sleep(Duration::from_secs(1));

// Verify the events.
let actual_events = &*actual.lock();
assert_eq!(actual_events.len(), expected.len());
for (i, (actual, expected)) in actual_events.iter().zip(expected).enumerate() {
// Verify the notifications.
let actual = &*actual.lock();
assert_eq!(actual.len(), expected.len());
for (i, (actual, expected)) in actual.iter().zip(expected).enumerate() {
assert_eq!(actual, &expected, "expected[{}]", i);
}
}
Expand Down

0 comments on commit 1727b78

Please sign in to comment.