@@ -235,6 +235,44 @@ TEST(BufferPoolManagerTest, DISABLED_PagePinMediumTest) {
235
235
remove (db_fname);
236
236
}
237
237
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
+
238
276
TEST (BufferPoolManagerTest, DISABLED_ContentionTest) {
239
277
auto disk_manager = std::make_shared<DiskManager>(db_fname);
240
278
auto bpm = std::make_shared<BufferPoolManager>(FRAMES, disk_manager.get (), K_DIST);
0 commit comments