Skip to content

Commit 0055df8

Browse files
committed
Add missing test for remove_if
1 parent 4073a9b commit 0055df8

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

src/test.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,38 @@ async fn test_remove() {
284284
assert_eq!(cache.get("test".to_owned()).await.unwrap(), "test".to_owned());
285285
}
286286

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+
287319
#[tokio::test]
288320
async fn test_load_error() {
289321
let (cache, _) = LoadingCache::new(move |_key: String| {

0 commit comments

Comments
 (0)