Skip to content

Commit 70bcc50

Browse files
authored
Feat: Spring24 P4 update test (#711)
* update test Signed-off-by: AveryQi115 <[email protected]> * single insert + fmt Signed-off-by: AveryQi115 <[email protected]> --------- Signed-off-by: AveryQi115 <[email protected]>
1 parent 248bbb0 commit 70bcc50

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

test/txn/txn_abort_serializable_test.cpp

+54
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,60 @@ TEST(TxnBonusTest, DISABLED_SerializableTest) { // NOLINT
3333
}
3434
}
3535

36+
TEST(TxnBonusTest, DISABLE_ConcurrentSerializableTest) { // NOLINT
37+
fmt::println(stderr, "--- SerializableTest2: Concurrent Serializable ---");
38+
{
39+
for (int i = 0; i < 10; i++) {
40+
auto bustub = std::make_unique<BustubInstance>();
41+
EnsureIndexScan(*bustub);
42+
Execute(*bustub, "CREATE TABLE maintable(a int, b int primary key)");
43+
auto table_info = bustub->catalog_->GetTable("maintable");
44+
auto txn1 = BeginTxnSerializable(*bustub, "txn1");
45+
std::string query = "INSERT INTO maintable VALUES ";
46+
for (int i = 0; i < 1000; i++) {
47+
auto str_value = std::to_string(i + 1000);
48+
query += i == 0 ? "(1," + str_value + ")" : ", (1," + str_value + ")";
49+
auto str_value2 = std::to_string(i + 2000);
50+
query += ", (0, " + str_value2 + ")";
51+
}
52+
WithTxn(txn1, ExecuteTxn(*bustub, _var, _txn, query));
53+
WithTxn(txn1, CommitTxn(*bustub, _var, _txn));
54+
55+
auto txn2 = BeginTxnSerializable(*bustub, "txn2");
56+
auto txn3 = BeginTxnSerializable(*bustub, "txn3");
57+
WithTxn(txn3, ExecuteTxn(*bustub, _var, _txn, "UPDATE maintable SET a = 1 WHERE a = 0"));
58+
WithTxn(txn2, ExecuteTxn(*bustub, _var, _txn, "UPDATE maintable SET a = 0 WHERE a = 1"));
59+
TxnMgrDbg("after two updates", bustub->txn_manager_.get(), table_info, table_info->table_.get());
60+
61+
std::vector<std::thread> commit_threads;
62+
const int thread_cnt = 2;
63+
commit_threads.reserve(thread_cnt);
64+
int success_cnt = 0;
65+
std::mutex result_mutex;
66+
67+
commit_threads.emplace_back([txn2, &bustub, &result_mutex, &success_cnt]() {
68+
auto res = bustub->txn_manager_->Commit(txn2);
69+
{
70+
std::lock_guard<std::mutex> lck(result_mutex);
71+
success_cnt += static_cast<int>(res);
72+
}
73+
});
74+
commit_threads.emplace_back([txn3, &bustub, &result_mutex, &success_cnt]() {
75+
auto res = bustub->txn_manager_->Commit(txn3);
76+
{
77+
std::lock_guard<std::mutex> lck(result_mutex);
78+
success_cnt += static_cast<int>(res);
79+
}
80+
});
81+
82+
for (auto &&thread : commit_threads) {
83+
thread.join();
84+
}
85+
EXPECT_EQ(success_cnt, 1);
86+
}
87+
}
88+
}
89+
3690
TEST(TxnBonusTest, DISABLED_AbortTest) { // NOLINT
3791
fmt::println(stderr, "--- AbortTest1: Simple Abort ---");
3892
{

0 commit comments

Comments
 (0)