Skip to content

Commit

Permalink
Feat: Spring24 P4 update test (#711)
Browse files Browse the repository at this point in the history
* update test

Signed-off-by: AveryQi115 <[email protected]>

* single insert + fmt

Signed-off-by: AveryQi115 <[email protected]>

---------

Signed-off-by: AveryQi115 <[email protected]>
  • Loading branch information
AveryQi115 authored Apr 5, 2024
1 parent 248bbb0 commit 70bcc50
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions test/txn/txn_abort_serializable_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<BustubInstance>();
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<std::thread> 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<std::mutex> lck(result_mutex);
success_cnt += static_cast<int>(res);
}
});
commit_threads.emplace_back([txn3, &bustub, &result_mutex, &success_cnt]() {
auto res = bustub->txn_manager_->Commit(txn3);
{
std::lock_guard<std::mutex> lck(result_mutex);
success_cnt += static_cast<int>(res);
}
});

for (auto &&thread : commit_threads) {
thread.join();
}
EXPECT_EQ(success_cnt, 1);
}
}
}

TEST(TxnBonusTest, DISABLED_AbortTest) { // NOLINT
fmt::println(stderr, "--- AbortTest1: Simple Abort ---");
{
Expand Down

0 comments on commit 70bcc50

Please sign in to comment.