From 70bcc505ff681922c547051058c064cba1d46c22 Mon Sep 17 00:00:00 2001 From: Avery Date: Fri, 5 Apr 2024 11:21:02 -0400 Subject: [PATCH] Feat: Spring24 P4 update test (#711) * update test Signed-off-by: AveryQi115 * single insert + fmt Signed-off-by: AveryQi115 --------- Signed-off-by: AveryQi115 --- test/txn/txn_abort_serializable_test.cpp | 54 ++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/test/txn/txn_abort_serializable_test.cpp b/test/txn/txn_abort_serializable_test.cpp index 9f5c35c5d..9e02da22f 100644 --- a/test/txn/txn_abort_serializable_test.cpp +++ b/test/txn/txn_abort_serializable_test.cpp @@ -33,6 +33,60 @@ TEST(TxnBonusTest, DISABLED_SerializableTest) { // NOLINT } } +TEST(TxnBonusTest, DISABLE_ConcurrentSerializableTest) { // NOLINT + fmt::println(stderr, "--- SerializableTest2: Concurrent Serializable ---"); + { + for (int i = 0; i < 10; i++) { + auto bustub = std::make_unique(); + EnsureIndexScan(*bustub); + Execute(*bustub, "CREATE TABLE maintable(a int, b int primary key)"); + auto table_info = bustub->catalog_->GetTable("maintable"); + auto txn1 = BeginTxnSerializable(*bustub, "txn1"); + std::string query = "INSERT INTO maintable VALUES "; + for (int i = 0; i < 1000; i++) { + auto str_value = std::to_string(i + 1000); + query += i == 0 ? "(1," + str_value + ")" : ", (1," + str_value + ")"; + auto str_value2 = std::to_string(i + 2000); + query += ", (0, " + str_value2 + ")"; + } + WithTxn(txn1, ExecuteTxn(*bustub, _var, _txn, query)); + WithTxn(txn1, CommitTxn(*bustub, _var, _txn)); + + auto txn2 = BeginTxnSerializable(*bustub, "txn2"); + auto txn3 = BeginTxnSerializable(*bustub, "txn3"); + WithTxn(txn3, ExecuteTxn(*bustub, _var, _txn, "UPDATE maintable SET a = 1 WHERE a = 0")); + WithTxn(txn2, ExecuteTxn(*bustub, _var, _txn, "UPDATE maintable SET a = 0 WHERE a = 1")); + TxnMgrDbg("after two updates", bustub->txn_manager_.get(), table_info, table_info->table_.get()); + + std::vector commit_threads; + const int thread_cnt = 2; + commit_threads.reserve(thread_cnt); + int success_cnt = 0; + std::mutex result_mutex; + + commit_threads.emplace_back([txn2, &bustub, &result_mutex, &success_cnt]() { + auto res = bustub->txn_manager_->Commit(txn2); + { + std::lock_guard lck(result_mutex); + success_cnt += static_cast(res); + } + }); + commit_threads.emplace_back([txn3, &bustub, &result_mutex, &success_cnt]() { + auto res = bustub->txn_manager_->Commit(txn3); + { + std::lock_guard lck(result_mutex); + success_cnt += static_cast(res); + } + }); + + for (auto &&thread : commit_threads) { + thread.join(); + } + EXPECT_EQ(success_cnt, 1); + } + } +} + TEST(TxnBonusTest, DISABLED_AbortTest) { // NOLINT fmt::println(stderr, "--- AbortTest1: Simple Abort ---"); {