Skip to content

Commit 60183a0

Browse files
author
Alecco
committed
repro for #1348 insert duplicate key
The cache shouldn't allow pages with same key (id, frame) in the system to be still in use somewhere else.
1 parent e07a6fc commit 60183a0

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

core/storage/page_cache.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ mod tests {
223223
use crate::{storage::page_cache::DumbLruPageCache, Page};
224224

225225
use super::PageCacheKey;
226+
use super::PageRef;
226227

227228
#[test]
228229
fn test_page_cache_evict() {
@@ -335,6 +336,40 @@ mod tests {
335336
key
336337
}
337338

339+
#[test]
340+
fn test_page_cache_insert_existing() {
341+
let mut cache = DumbLruPageCache::new(1);
342+
#[allow(clippy::arc_with_non_send_sync)]
343+
let page1 = Arc::new(Page::new(1));
344+
assert_eq!(Arc::strong_count(&page1), 1);
345+
let _ = insert_page_provided(&mut cache, 1, page1.clone());
346+
assert_eq!(Arc::strong_count(&page1), 2); // Should be another ref at page cache
347+
}
348+
349+
#[test]
350+
fn test_page_cache_insert_duplicate_key() {
351+
let mut cache = DumbLruPageCache::new(1);
352+
#[allow(clippy::arc_with_non_send_sync)]
353+
let page1 = Arc::new(Page::new(1));
354+
assert_eq!(Arc::strong_count(&page1), 1);
355+
let _ = insert_page_provided(&mut cache, 1, page1.clone());
356+
#[allow(clippy::arc_with_non_send_sync)]
357+
let page2 = Arc::new(Page::new(1));
358+
let _ = insert_page_provided(&mut cache, 1, page2.clone()); // This should fail
359+
assert_eq!(Arc::strong_count(&page1), 2); // first page should stay (referenced) in cache
360+
assert_eq!(Arc::strong_count(&page2), 1);
361+
}
362+
363+
fn insert_page_provided(
364+
cache: &mut DumbLruPageCache,
365+
id: usize,
366+
page: PageRef,
367+
) -> PageCacheKey {
368+
let key = PageCacheKey::new(id, None);
369+
cache.insert(key.clone(), page.clone());
370+
key
371+
}
372+
338373
#[test]
339374
fn test_page_cache_insert_sequential() {
340375
let mut cache = DumbLruPageCache::new(2);

0 commit comments

Comments
 (0)