Skip to content

Commit

Permalink
Add EvictableTest (#745)
Browse files Browse the repository at this point in the history
add evictable test
  • Loading branch information
connortsui20 committed Sep 18, 2024
1 parent b95f3f2 commit c745b11
Showing 1 changed file with 71 additions and 0 deletions.
71 changes: 71 additions & 0 deletions test/buffer/buffer_pool_manager_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -316,4 +316,75 @@ TEST(BufferPoolManagerTest, DISABLED_DeadlockTest) {
child.join();
}

TEST(BufferPoolManagerTest, DISABLED_EvictableTest) {
// Test if the evictable status of a frame is always correct.
size_t rounds = 1000;
size_t num_readers = 8;

auto disk_manager = std::make_shared<DiskManager>(db_fname);
// Only allocate 1 frame of memory to the buffer pool manager.
auto bpm = std::make_shared<BufferPoolManager>(1, disk_manager.get(), K_DIST);

for (size_t i = 0; i < rounds; i++) {
std::mutex mutex;
std::condition_variable cv;

// This signal tells the readers that they can start reading after the main thread has already taken the read latch.
bool signal = false;

// This page will be loaded into the only available frame.
page_id_t winner_pid = bpm->NewPage();
// We will attempt to load this page into the occupied frame, and it should fail every time.
page_id_t loser_pid = bpm->NewPage();

std::vector<std::thread> readers;
for (size_t j = 0; j < num_readers; j++) {
readers.emplace_back([&]() {
std::unique_lock<std::mutex> lock(mutex);

// Wait until the main thread has taken a read latch on the page.
while (!signal) {
cv.wait(lock);
}

// Read the page in shared mode.
auto read_guard = bpm->ReadPage(winner_pid);

// Since the only frame is pinned, no thread should be able to bring in a new page.
ASSERT_FALSE(bpm->CheckedReadPage(loser_pid).has_value());
});
}

std::unique_lock<std::mutex> lock(mutex);

if (i % 2 == 0) {
// Take the read latch on the page and pin it.
auto read_guard = bpm->ReadPage(winner_pid);

// Wake up all of the readers.
signal = true;
cv.notify_all();
lock.unlock();

// Allow other threads to read.
read_guard.Drop();
} else {
// Take the read latch on the page and pin it.
auto write_guard = bpm->WritePage(winner_pid);

// Wake up all of the readers.
signal = true;
cv.notify_all();
lock.unlock();

// Allow other threads to read.
write_guard.Drop();
}

for (size_t i = 0; i < num_readers; i++) {
readers[i].join();
}
}
}

} // namespace bustub

0 comments on commit c745b11

Please sign in to comment.