-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransaction_manager_tests.cpp
More file actions
197 lines (156 loc) · 6.35 KB
/
transaction_manager_tests.cpp
File metadata and controls
197 lines (156 loc) · 6.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/**
* @file transaction_manager_tests.cpp
* @brief Unit tests for Transaction Manager
*/
#include <gtest/gtest.h>
#include <string>
#include "catalog/catalog.hpp"
#include "common/config.hpp"
#include "storage/buffer_pool_manager.hpp"
#include "storage/storage_manager.hpp"
#include "transaction/lock_manager.hpp"
#include "transaction/transaction.hpp"
#include "transaction/transaction_manager.hpp"
using namespace cloudsql;
using namespace cloudsql::transaction;
namespace {
TEST(TransactionManagerTests, Basic) {
auto catalog = Catalog::create();
storage::StorageManager disk_manager("./test_data");
storage::BufferPoolManager bpm(cloudsql::config::Config::DEFAULT_BUFFER_POOL_SIZE,
disk_manager);
LockManager lm;
TransactionManager tm(lm, *catalog, bpm, bpm.get_log_manager());
Transaction* const txn1 = tm.begin();
ASSERT_NE(txn1, nullptr);
EXPECT_EQ(txn1->get_state(), TransactionState::RUNNING);
tm.commit(txn1);
EXPECT_EQ(txn1->get_state(), TransactionState::COMMITTED);
Transaction* const txn2 = tm.begin();
tm.abort(txn2);
EXPECT_EQ(txn2->get_state(), TransactionState::ABORTED);
}
TEST(TransactionManagerTests, Isolation) {
auto catalog = Catalog::create();
storage::StorageManager disk_manager("./test_data");
storage::BufferPoolManager bpm(cloudsql::config::Config::DEFAULT_BUFFER_POOL_SIZE,
disk_manager);
LockManager lm;
TransactionManager tm(lm, *catalog, bpm, bpm.get_log_manager());
Transaction* const txn1 = tm.begin();
Transaction* const txn2 = tm.begin();
EXPECT_GT(txn2->get_id(), txn1->get_id());
tm.commit(txn1);
tm.commit(txn2);
}
TEST(TransactionManagerTests, BeginWithIsolationLevels) {
auto catalog = Catalog::create();
storage::StorageManager disk_manager("./test_data");
storage::BufferPoolManager bpm(cloudsql::config::Config::DEFAULT_BUFFER_POOL_SIZE,
disk_manager);
LockManager lm;
TransactionManager tm(lm, *catalog, bpm, bpm.get_log_manager());
Transaction* const txn1 = tm.begin(IsolationLevel::READ_COMMITTED);
ASSERT_NE(txn1, nullptr);
EXPECT_EQ(txn1->get_state(), TransactionState::RUNNING);
Transaction* const txn2 = tm.begin(IsolationLevel::REPEATABLE_READ);
ASSERT_NE(txn2, nullptr);
EXPECT_EQ(txn2->get_state(), TransactionState::RUNNING);
Transaction* const txn3 = tm.begin(IsolationLevel::SERIALIZABLE);
ASSERT_NE(txn3, nullptr);
EXPECT_EQ(txn3->get_state(), TransactionState::RUNNING);
tm.commit(txn1);
tm.commit(txn2);
tm.commit(txn3);
}
TEST(TransactionManagerTests, PrepareTransaction) {
auto catalog = Catalog::create();
storage::StorageManager disk_manager("./test_data");
storage::BufferPoolManager bpm(cloudsql::config::Config::DEFAULT_BUFFER_POOL_SIZE,
disk_manager);
LockManager lm;
TransactionManager tm(lm, *catalog, bpm, bpm.get_log_manager());
Transaction* const txn = tm.begin();
ASSERT_NE(txn, nullptr);
EXPECT_EQ(txn->get_state(), TransactionState::RUNNING);
tm.prepare(txn);
EXPECT_EQ(txn->get_state(), TransactionState::PREPARED);
tm.commit(txn);
EXPECT_EQ(txn->get_state(), TransactionState::COMMITTED);
}
TEST(TransactionManagerTests, GetTransaction) {
auto catalog = Catalog::create();
storage::StorageManager disk_manager("./test_data");
storage::BufferPoolManager bpm(cloudsql::config::Config::DEFAULT_BUFFER_POOL_SIZE,
disk_manager);
LockManager lm;
TransactionManager tm(lm, *catalog, bpm, bpm.get_log_manager());
Transaction* const txn1 = tm.begin();
ASSERT_NE(txn1, nullptr);
// Get existing transaction
EXPECT_EQ(tm.get_transaction(txn1->get_id()), txn1);
// Get non-existent transaction
EXPECT_EQ(tm.get_transaction(9999), nullptr);
tm.commit(txn1);
// After commit, transaction is no longer active
EXPECT_EQ(tm.get_transaction(txn1->get_id()), nullptr);
}
TEST(TransactionManagerTests, CommitIdempotent) {
auto catalog = Catalog::create();
storage::StorageManager disk_manager("./test_data");
storage::BufferPoolManager bpm(cloudsql::config::Config::DEFAULT_BUFFER_POOL_SIZE,
disk_manager);
LockManager lm;
TransactionManager tm(lm, *catalog, bpm, bpm.get_log_manager());
Transaction* const txn = tm.begin();
ASSERT_NE(txn, nullptr);
tm.commit(txn);
EXPECT_EQ(txn->get_state(), TransactionState::COMMITTED);
// Commit again should be safe (idempotent)
tm.commit(txn);
EXPECT_EQ(txn->get_state(), TransactionState::COMMITTED);
}
TEST(TransactionManagerTests, AbortIdempotent) {
auto catalog = Catalog::create();
storage::StorageManager disk_manager("./test_data");
storage::BufferPoolManager bpm(cloudsql::config::Config::DEFAULT_BUFFER_POOL_SIZE,
disk_manager);
LockManager lm;
TransactionManager tm(lm, *catalog, bpm, bpm.get_log_manager());
Transaction* const txn = tm.begin();
ASSERT_NE(txn, nullptr);
tm.abort(txn);
EXPECT_EQ(txn->get_state(), TransactionState::ABORTED);
// Abort again should be safe (idempotent)
tm.abort(txn);
EXPECT_EQ(txn->get_state(), TransactionState::ABORTED);
}
TEST(TransactionManagerTests, MultipleTransactions) {
auto catalog = Catalog::create();
storage::StorageManager disk_manager("./test_data");
storage::BufferPoolManager bpm(cloudsql::config::Config::DEFAULT_BUFFER_POOL_SIZE,
disk_manager);
LockManager lm;
TransactionManager tm(lm, *catalog, bpm, bpm.get_log_manager());
Transaction* txns[5];
for (int i = 0; i < 5; ++i) {
txns[i] = tm.begin();
ASSERT_NE(txns[i], nullptr);
EXPECT_EQ(txns[i]->get_state(), TransactionState::RUNNING);
}
// Verify IDs are ordered
for (int i = 1; i < 5; ++i) {
EXPECT_GT(txns[i]->get_id(), txns[i - 1]->get_id());
}
// Commit even-indexed transactions, abort odd-indexed
for (int i = 0; i < 5; ++i) {
if (i % 2 == 0) {
tm.commit(txns[i]);
EXPECT_EQ(txns[i]->get_state(), TransactionState::COMMITTED);
} else {
tm.abort(txns[i]);
EXPECT_EQ(txns[i]->get_state(), TransactionState::ABORTED);
}
}
}
} // namespace