Skip to content

Commit bd6c48e

Browse files
authored
Add new frame latching test (#753)
add new rwlatch test
1 parent 9009a5c commit bd6c48e

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

test/buffer/buffer_pool_manager_test.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,44 @@ TEST(BufferPoolManagerTest, DISABLED_PagePinMediumTest) {
235235
remove(db_fname);
236236
}
237237

238+
TEST(BufferPoolManagerTest, DISABLED_PageAccessTest) {
239+
const size_t rounds = 50;
240+
241+
auto disk_manager = std::make_shared<DiskManager>(db_fname);
242+
auto bpm = std::make_shared<BufferPoolManager>(1, disk_manager.get(), K_DIST);
243+
244+
auto pid = bpm->NewPage();
245+
char buf[BUSTUB_PAGE_SIZE];
246+
247+
auto thread = std::thread([&]() {
248+
// The writer can keep writing to the same page.
249+
for (size_t i = 0; i < rounds; i++) {
250+
std::this_thread::sleep_for(std::chrono::milliseconds(5));
251+
auto guard = bpm->WritePage(pid);
252+
strcpy(guard.GetDataMut(), std::to_string(i).c_str()); // NOLINT
253+
}
254+
});
255+
256+
for (size_t i = 0; i < rounds; i++) {
257+
// Wait for a bit before taking the latch, allowing the writer to write some stuff.
258+
std::this_thread::sleep_for(std::chrono::milliseconds(10));
259+
260+
// While we are reading, nobody should be able to modify the data.
261+
auto guard = bpm->ReadPage(pid);
262+
263+
// Save the data we observe.
264+
memcpy(buf, guard.GetData(), BUSTUB_PAGE_SIZE);
265+
266+
// Sleep for a bit. If latching is working properly, nothing should be writing to the page.
267+
std::this_thread::sleep_for(std::chrono::milliseconds(10));
268+
269+
// Check that the data is unmodified.
270+
EXPECT_EQ(0, strcmp(guard.GetData(), buf));
271+
}
272+
273+
thread.join();
274+
}
275+
238276
TEST(BufferPoolManagerTest, DISABLED_ContentionTest) {
239277
auto disk_manager = std::make_shared<DiskManager>(db_fname);
240278
auto bpm = std::make_shared<BufferPoolManager>(FRAMES, disk_manager.get(), K_DIST);

0 commit comments

Comments
 (0)