@@ -33,6 +33,60 @@ TEST(TxnBonusTest, DISABLED_SerializableTest) { // NOLINT
33
33
}
34
34
}
35
35
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
+
36
90
TEST (TxnBonusTest, DISABLED_AbortTest) { // NOLINT
37
91
fmt::println (stderr, " --- AbortTest1: Simple Abort ---" );
38
92
{
0 commit comments