From ff7d9fe9618c26b270d498e3d04c1a34b3bb0271 Mon Sep 17 00:00:00 2001 From: PThorpe92 Date: Tue, 5 Aug 2025 12:51:17 -0400 Subject: [PATCH] Add basic test for buffer pool initialization and basic use --- core/storage/buffer_pool.rs | 83 +++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) diff --git a/core/storage/buffer_pool.rs b/core/storage/buffer_pool.rs index f2802febe..b08074d19 100644 --- a/core/storage/buffer_pool.rs +++ b/core/storage/buffer_pool.rs @@ -513,3 +513,86 @@ mod arena { unsafe { std::alloc::dealloc(ptr, layout) }; } } + +#[cfg(test)] +pub mod tests { + use super::*; + const TEST_SIZE: usize = 4096 * 50; + + #[test] + fn test_init_pool() { + let io: Arc = Arc::new(crate::MemoryIO::new()); + let pool = BufferPool::begin_init(&io, TEST_SIZE); + pool.finalize_with_page_size(BufferPool::DEFAULT_PAGE_SIZE) + .expect("pool to initialize"); + assert_eq!(pool.get_page().len(), BufferPool::DEFAULT_PAGE_SIZE); + assert_eq!( + pool.get_wal_frame().len(), + BufferPool::DEFAULT_ARENA_SIZE + WAL_FRAME_HEADER_SIZE, + "get_wal_frame should return page_size + WAL_FRAME_HEADER_SIZE" + ); + assert!( + pool.inner_mut().page_arena.is_some(), + "page arena should be initialized" + ); + assert!( + pool.inner_mut().wal_frame_arena.is_some(), + "wal frame arena should be initialized" + ); + let inner = pool.inner_mut(); + assert!( + inner + .page_arena + .as_ref() + .is_some_and(|a| a.id >= UNREGISTERED_START), + "page arena should not have ID in registered range with MemoryIO" + ); + assert!( + inner + .wal_frame_arena + .as_ref() + .is_some_and(|a| a.id >= UNREGISTERED_START), + "wal frame arena should not have ID in registered range with MemoryIO" + ); + let page = inner.get_page(); + assert_eq!( + inner + .page_arena + .as_ref() + .unwrap() + .allocated_pages + .load(Ordering::Relaxed), + 1 + ); + drop(page); + let frame = inner.get_frame(); + assert_eq!( + inner + .wal_frame_arena + .as_ref() + .unwrap() + .allocated_pages + .load(Ordering::Relaxed), + 1 + ); + drop(frame); + assert_eq!( + inner + .wal_frame_arena + .as_ref() + .unwrap() + .allocated_pages + .load(Ordering::Relaxed), + 0 + ); + assert_eq!( + inner + .page_arena + .as_ref() + .unwrap() + .allocated_pages + .load(Ordering::Relaxed), + 0 + ); + } +}