@@ -284,6 +284,38 @@ async fn test_remove() {
284
284
assert_eq ! ( cache. get( "test" . to_owned( ) ) . await . unwrap( ) , "test" . to_owned( ) ) ;
285
285
}
286
286
287
+ #[ tokio:: test]
288
+ async fn test_remove_if ( ) {
289
+ let ( cache, _) = LoadingCache :: new ( move |key : u64 | {
290
+ async move {
291
+ tokio:: time:: sleep ( Duration :: from_millis ( 500 ) ) . await ;
292
+ Ok ( key * 2 )
293
+ }
294
+ } ) ;
295
+ let cache: LoadingCache < u64 , u64 , u8 > = cache;
296
+
297
+ cache. set ( 1 , 2 ) . await . ok ( ) ;
298
+ cache. set ( 2 , 4 ) . await . ok ( ) ;
299
+ cache. set ( 3 , 6 ) . await . ok ( ) ;
300
+ cache. set ( 4 , 8 ) . await . ok ( ) ;
301
+ cache. set ( 5 , 10 ) . await . ok ( ) ;
302
+
303
+ assert ! ( cache. get_if_present( 1 ) . await . unwrap( ) . is_some( ) ) ;
304
+ assert ! ( cache. get_if_present( 2 ) . await . unwrap( ) . is_some( ) ) ;
305
+ assert ! ( cache. get_if_present( 3 ) . await . unwrap( ) . is_some( ) ) ;
306
+ assert ! ( cache. get_if_present( 4 ) . await . unwrap( ) . is_some( ) ) ;
307
+ assert ! ( cache. get_if_present( 5 ) . await . unwrap( ) . is_some( ) ) ;
308
+
309
+ cache. remove_if ( |( k, _) | k > & 3 ) . await . unwrap ( ) ;
310
+
311
+ assert ! ( cache. get_if_present( 1 ) . await . unwrap( ) . is_some( ) ) ;
312
+ assert ! ( cache. get_if_present( 2 ) . await . unwrap( ) . is_some( ) ) ;
313
+ assert ! ( cache. get_if_present( 3 ) . await . unwrap( ) . is_some( ) ) ;
314
+ // next two should be none after remove
315
+ assert ! ( cache. get_if_present( 4 ) . await . unwrap( ) . is_none( ) ) ;
316
+ assert ! ( cache. get_if_present( 5 ) . await . unwrap( ) . is_none( ) ) ;
317
+ }
318
+
287
319
#[ tokio:: test]
288
320
async fn test_load_error ( ) {
289
321
let ( cache, _) = LoadingCache :: new ( move |_key : String | {
0 commit comments