@@ -1269,6 +1269,9 @@ where
12691269 {
12701270 let hash = self . base . hash ( key) ;
12711271 if let Some ( kv) = self . base . remove_entry ( key, hash) {
1272+ if self . base . is_removal_notifier_enabled ( ) {
1273+ self . base . notify_invalidate ( & kv. key , & kv. entry )
1274+ }
12721275 let op = WriteOp :: Remove ( kv) ;
12731276 let now = self . base . current_time_from_expiration_clock ( ) ;
12741277 let hk = self . base . housekeeper . as_ref ( ) ;
@@ -1963,7 +1966,21 @@ mod tests {
19631966
19641967 #[ test]
19651968 fn basic_single_blocking_api ( ) {
1966- let mut cache = Cache :: new ( 3 ) ;
1969+ // The following `Vec`s will hold actual and expected notifications.
1970+ let actual = Arc :: new ( Mutex :: new ( Vec :: new ( ) ) ) ;
1971+ let mut expected = Vec :: new ( ) ;
1972+
1973+ // Create an eviction listener.
1974+ let a1 = Arc :: clone ( & actual) ;
1975+ // We use non-async mutex in the eviction listener (because the listener
1976+ // is a regular closure).
1977+ let listener = move |k, v, cause| a1. lock ( ) . push ( ( k, v, cause) ) ;
1978+
1979+ // Create a cache with the eviction listener.
1980+ let mut cache = Cache :: builder ( )
1981+ . max_capacity ( 3 )
1982+ . eviction_listener_with_queued_delivery_mode ( listener)
1983+ . build ( ) ;
19671984 cache. reconfigure_for_testing ( ) ;
19681985
19691986 // Make the cache exterior immutable.
@@ -1988,24 +2005,30 @@ mod tests {
19882005
19892006 // "d" should not be admitted because its frequency is too low.
19902007 cache. blocking ( ) . insert ( "d" , "david" ) ; // count: d -> 0
2008+ expected. push ( ( Arc :: new ( "d" ) , "david" , RemovalCause :: Size ) ) ;
19912009 cache. sync ( ) ;
19922010 assert_eq ! ( cache. get( & "d" ) , None ) ; // d -> 1
19932011
19942012 cache. blocking ( ) . insert ( "d" , "david" ) ;
2013+ expected. push ( ( Arc :: new ( "d" ) , "david" , RemovalCause :: Size ) ) ;
19952014 cache. sync ( ) ;
19962015 assert_eq ! ( cache. get( & "d" ) , None ) ; // d -> 2
19972016
19982017 // "d" should be admitted and "c" should be evicted
19992018 // because d's frequency is higher than c's.
20002019 cache. blocking ( ) . insert ( "d" , "dennis" ) ;
2020+ expected. push ( ( Arc :: new ( "c" ) , "cindy" , RemovalCause :: Size ) ) ;
20012021 cache. sync ( ) ;
20022022 assert_eq ! ( cache. get( & "a" ) , Some ( "alice" ) ) ;
20032023 assert_eq ! ( cache. get( & "b" ) , Some ( "bob" ) ) ;
20042024 assert_eq ! ( cache. get( & "c" ) , None ) ;
20052025 assert_eq ! ( cache. get( & "d" ) , Some ( "dennis" ) ) ;
20062026
20072027 cache. blocking ( ) . invalidate ( & "b" ) ;
2028+ expected. push ( ( Arc :: new ( "b" ) , "bob" , RemovalCause :: Explicit ) ) ;
20082029 assert_eq ! ( cache. get( & "b" ) , None ) ;
2030+
2031+ verify_notification_vec ( & cache, actual, & expected) ;
20092032 }
20102033
20112034 #[ tokio:: test]
0 commit comments