-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlock_manager_tests.cpp
More file actions
298 lines (234 loc) · 7.68 KB
/
lock_manager_tests.cpp
File metadata and controls
298 lines (234 loc) · 7.68 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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/**
* @file lock_manager_tests.cpp
* @brief Unit tests for Lock Manager
*/
#include <gtest/gtest.h>
#include <atomic>
#include <chrono>
#include <thread>
#include "storage/heap_table.hpp"
#include "transaction/lock_manager.hpp"
#include "transaction/transaction.hpp"
using namespace cloudsql::transaction;
using namespace cloudsql::storage;
namespace {
constexpr auto TEST_SLEEP_MS = std::chrono::milliseconds(100);
TEST(LockManagerTests, Shared) {
LockManager lm;
Transaction txn1(1);
Transaction txn2(2);
HeapTable::TupleId rid1(1, 1);
EXPECT_TRUE(lm.acquire_shared(&txn1, rid1));
EXPECT_TRUE(lm.acquire_shared(&txn2, rid1));
static_cast<void>(lm.unlock(&txn1, rid1));
static_cast<void>(lm.unlock(&txn2, rid1));
}
TEST(LockManagerTests, Exclusive) {
LockManager lm;
Transaction txn1(1);
Transaction txn2(2);
HeapTable::TupleId rid1(1, 1);
EXPECT_TRUE(lm.acquire_exclusive(&txn1, rid1));
EXPECT_FALSE(lm.acquire_shared(&txn2, rid1));
static_cast<void>(lm.unlock(&txn1, rid1));
EXPECT_TRUE(lm.acquire_shared(&txn2, rid1));
static_cast<void>(lm.unlock(&txn2, rid1));
}
TEST(LockManagerTests, Upgrade) {
LockManager lm;
Transaction txn1(1);
HeapTable::TupleId rid1(1, 1);
EXPECT_TRUE(lm.acquire_shared(&txn1, rid1));
EXPECT_TRUE(lm.acquire_exclusive(&txn1, rid1));
static_cast<void>(lm.unlock(&txn1, rid1));
}
TEST(LockManagerTests, Wait) {
LockManager lm;
Transaction txn1(1);
Transaction txn2(2);
Transaction txn3(3);
HeapTable::TupleId rid1(1, 1);
std::atomic<int> shared_granted{0};
// 1. Get Exclusive
EXPECT_TRUE(lm.acquire_exclusive(&txn1, rid1));
// 2. Try to get Shared from two other txns (should block)
std::thread t2([&]() {
if (lm.acquire_shared(&txn2, rid1)) {
shared_granted++;
}
});
std::thread t3([&]() {
if (lm.acquire_shared(&txn3, rid1)) {
shared_granted++;
}
});
// Small sleep to ensure threads are waiting
std::this_thread::sleep_for(TEST_SLEEP_MS);
EXPECT_EQ(shared_granted.load(), 0);
// 3. Release Exclusive (should grant both shared)
static_cast<void>(lm.unlock(&txn1, rid1));
t2.join();
t3.join();
EXPECT_EQ(shared_granted.load(), 2);
static_cast<void>(lm.unlock(&txn2, rid1));
static_cast<void>(lm.unlock(&txn3, rid1));
}
TEST(LockManagerTests, Deadlock) {
LockManager lm;
Transaction txn1(1);
Transaction txn2(2);
HeapTable::TupleId ridA(1, 1);
HeapTable::TupleId ridB(1, 2);
// txn1 holds A, txn2 holds B
EXPECT_TRUE(lm.acquire_exclusive(&txn1, ridA));
EXPECT_TRUE(lm.acquire_exclusive(&txn2, ridB));
// txn1 waits for B
std::thread t1([&]() { static_cast<void>(lm.acquire_exclusive(&txn1, ridB)); });
// Small sleep to ensure t1 is waiting
std::this_thread::sleep_for(TEST_SLEEP_MS);
// txn2 waits for A -> Deadlock!
static_cast<void>(lm.unlock(&txn1, ridA));
static_cast<void>(lm.acquire_exclusive(&txn2, ridA));
static_cast<void>(lm.unlock(&txn2, ridB));
t1.join();
static_cast<void>(lm.unlock(&txn1, ridB));
static_cast<void>(lm.unlock(&txn2, ridA));
}
// ============= New Tests =============
/**
* @brief Verifies unlocking a non-existent lock returns false
*/
TEST(LockManagerTests, UnlockNonExistent) {
LockManager lm;
Transaction txn(1);
HeapTable::TupleId rid(1, 1);
// Unlock on non-existent lock should return false
EXPECT_FALSE(lm.unlock(&txn, rid));
}
/**
* @brief Verifies exclusive lock blocks shared lock acquisition
*/
TEST(LockManagerTests, ExclusiveBlocksShared) {
LockManager lm;
Transaction txn1(1);
Transaction txn2(2);
HeapTable::TupleId rid(1, 1);
// Acquire exclusive
EXPECT_TRUE(lm.acquire_exclusive(&txn1, rid));
// Shared should block (using try with immediate timeout behavior)
std::atomic<bool> shared_acquired{false};
std::thread t([&]() {
if (lm.acquire_shared(&txn2, rid)) {
shared_acquired = true;
}
});
// Give thread time to try acquiring
std::this_thread::sleep_for(TEST_SLEEP_MS);
EXPECT_FALSE(shared_acquired.load());
// Release exclusive
static_cast<void>(lm.unlock(&txn1, rid));
t.join();
EXPECT_TRUE(shared_acquired.load());
static_cast<void>(lm.unlock(&txn2, rid));
}
/**
* @brief Verifies shared lock blocks exclusive lock acquisition
*/
TEST(LockManagerTests, SharedBlocksExclusive) {
LockManager lm;
Transaction txn1(1);
Transaction txn2(2);
HeapTable::TupleId rid(1, 1);
// Acquire shared
EXPECT_TRUE(lm.acquire_shared(&txn1, rid));
// Exclusive should block
std::atomic<bool> exclusive_acquired{false};
std::thread t([&]() {
if (lm.acquire_exclusive(&txn2, rid)) {
exclusive_acquired = true;
}
});
// Give thread time to try acquiring
std::this_thread::sleep_for(TEST_SLEEP_MS);
EXPECT_FALSE(exclusive_acquired.load());
// Release shared
static_cast<void>(lm.unlock(&txn1, rid));
t.join();
EXPECT_TRUE(exclusive_acquired.load());
static_cast<void>(lm.unlock(&txn2, rid));
}
/**
* @brief Verifies unlock only releases the specific transaction's lock
*/
TEST(LockManagerTests, UnlockReleasesOnlyTxn) {
LockManager lm;
Transaction txn1(1);
Transaction txn2(2);
HeapTable::TupleId rid(1, 1);
// Two transactions acquire shared
EXPECT_TRUE(lm.acquire_shared(&txn1, rid));
EXPECT_TRUE(lm.acquire_shared(&txn2, rid));
// Unlock txn1 - should not affect txn2
static_cast<void>(lm.unlock(&txn1, rid));
// txn2 should still hold the lock - can still read
EXPECT_TRUE(lm.acquire_shared(&txn2, rid));
static_cast<void>(lm.unlock(&txn2, rid));
}
/**
* @brief Verifies lock re-acquisition by same transaction is idempotent
*/
TEST(LockManagerTests, LockSameRIDTwice) {
LockManager lm;
Transaction txn(1);
HeapTable::TupleId rid(1, 1);
// Lock manager allows re-acquisition by same transaction
EXPECT_TRUE(lm.acquire_shared(&txn, rid));
EXPECT_TRUE(lm.acquire_shared(&txn, rid));
// Should only need one unlock
static_cast<void>(lm.unlock(&txn, rid));
}
/**
* @brief Verifies three transactions can hold shared locks simultaneously
*/
TEST(LockManagerTests, MultipleSharedLocks) {
LockManager lm;
Transaction txn1(1);
Transaction txn2(2);
Transaction txn3(3);
HeapTable::TupleId rid(1, 1);
EXPECT_TRUE(lm.acquire_shared(&txn1, rid));
EXPECT_TRUE(lm.acquire_shared(&txn2, rid));
EXPECT_TRUE(lm.acquire_shared(&txn3, rid));
static_cast<void>(lm.unlock(&txn1, rid));
static_cast<void>(lm.unlock(&txn2, rid));
static_cast<void>(lm.unlock(&txn3, rid));
}
/**
* @brief Verifies transactions can hold locks on different RIDs independently
*/
TEST(LockManagerTests, LockDifferentRIDs) {
LockManager lm;
Transaction txn1(1);
Transaction txn2(2);
HeapTable::TupleId ridA(1, 1);
HeapTable::TupleId ridB(1, 2);
// txn1 gets exclusive on ridA, txn2 gets exclusive on ridB - both should succeed
EXPECT_TRUE(lm.acquire_exclusive(&txn1, ridA));
EXPECT_TRUE(lm.acquire_exclusive(&txn2, ridB));
static_cast<void>(lm.unlock(&txn1, ridA));
static_cast<void>(lm.unlock(&txn2, ridB));
}
/**
* @brief Verifies exclusive lock followed by shared on different RID succeeds
*/
TEST(LockManagerTests, ExclusiveThenShared) {
LockManager lm;
Transaction txn(1);
HeapTable::TupleId ridA(1, 1);
HeapTable::TupleId ridB(1, 2);
EXPECT_TRUE(lm.acquire_exclusive(&txn, ridA));
EXPECT_TRUE(lm.acquire_shared(&txn, ridB));
static_cast<void>(lm.unlock(&txn, ridA));
static_cast<void>(lm.unlock(&txn, ridB));
}
} // namespace