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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#include "test/librbd/test_mock_fixture.h"
#include "test/librbd/test_support.h"
#include "librbd/ManagedLock.h"
#include "librbd/managed_lock/AcquireRequest.h"
#include "librbd/managed_lock/BreakRequest.h"
#include "librbd/managed_lock/GetLockerRequest.h"
#include "librbd/managed_lock/ReacquireRequest.h"
#include "librbd/managed_lock/ReleaseRequest.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include <list>
namespace librbd {
struct MockManagedLockImageCtx : public MockImageCtx {
explicit MockManagedLockImageCtx(ImageCtx &image_ctx) : MockImageCtx(image_ctx) {}
};
namespace watcher {
template <>
struct Traits<MockManagedLockImageCtx> {
typedef librbd::MockImageWatcher Watcher;
};
}
struct MockMockManagedLock : public ManagedLock<MockManagedLockImageCtx> {
MockMockManagedLock(librados::IoCtx& ioctx, ContextWQ *work_queue,
const std::string& oid, librbd::MockImageWatcher *watcher,
managed_lock::Mode mode, bool blacklist_on_break_lock,
uint32_t blacklist_expire_seconds)
: ManagedLock<MockManagedLockImageCtx>(ioctx, work_queue, oid, watcher,
librbd::managed_lock::EXCLUSIVE, true, 0) {
};
virtual ~MockMockManagedLock() = default;
MOCK_METHOD2(post_reacquire_lock_handler, void(int, Context*));
MOCK_METHOD2(pre_release_lock_handler, void(bool, Context*));
MOCK_METHOD3(post_release_lock_handler, void(bool, int, Context*));
};
namespace managed_lock {
template<typename T>
struct BaseRequest {
static std::list<T *> s_requests;
Context *on_finish = nullptr;
static T* create(librados::IoCtx& ioctx, MockImageWatcher *watcher,
ContextWQ *work_queue, const std::string& oid,
const std::string& cookie, Context *on_finish) {
ceph_assert(!s_requests.empty());
T* req = s_requests.front();
req->on_finish = on_finish;
s_requests.pop_front();
return req;
}
BaseRequest() {
s_requests.push_back(reinterpret_cast<T*>(this));
}
};
template<typename T>
std::list<T *> BaseRequest<T>::s_requests;
template <>
struct AcquireRequest<MockManagedLockImageCtx> : public BaseRequest<AcquireRequest<MockManagedLockImageCtx> > {
static AcquireRequest* create(librados::IoCtx& ioctx,
MockImageWatcher *watcher,
ContextWQ *work_queue, const std::string& oid,
const std::string& cookie,
bool exclusive, bool blacklist_on_break_lock,
uint32_t blacklist_expire_seconds,
Context *on_finish) {
return BaseRequest::create(ioctx, watcher, work_queue, oid, cookie, on_finish);
}
MOCK_METHOD0(send, void());
};
template <>
struct ReacquireRequest<MockManagedLockImageCtx> : public BaseRequest<ReacquireRequest<MockManagedLockImageCtx> > {
static ReacquireRequest* create(librados::IoCtx &ioctx, const std::string& oid,
const string& old_cookie, const std::string& new_cookie,
bool exclusive, Context *on_finish) {
return BaseRequest::create(ioctx, nullptr, nullptr, oid, new_cookie,
on_finish);
}
MOCK_METHOD0(send, void());
};
template <>
struct ReleaseRequest<MockManagedLockImageCtx> : public BaseRequest<ReleaseRequest<MockManagedLockImageCtx> > {
static ReleaseRequest* create(librados::IoCtx& ioctx, MockImageWatcher *watcher,
ContextWQ *work_queue, const std::string& oid,
const std::string& cookie, Context *on_finish) {
return BaseRequest::create(ioctx, watcher, work_queue, oid, cookie,
on_finish);
}
MOCK_METHOD0(send, void());
};
template <>
struct GetLockerRequest<MockManagedLockImageCtx> {
static GetLockerRequest* create(librados::IoCtx& ioctx,
const std::string& oid, bool exclusive,
Locker *locker, Context *on_finish) {
ceph_abort_msg("unexpected call");
}
void send() {
ceph_abort_msg("unexpected call");
}
};
template <>
struct BreakRequest<MockManagedLockImageCtx> {
static BreakRequest* create(librados::IoCtx& ioctx, ContextWQ *work_queue,
const std::string& oid, const Locker &locker,
bool exclusive, bool blacklist_locker,
uint32_t blacklist_expire_seconds,
bool force_break_lock, Context *on_finish) {
ceph_abort_msg("unexpected call");
}
void send() {
ceph_abort_msg("unexpected call");
}
};
} // namespace managed_lock
} // namespace librbd
// template definitions
#include "librbd/ManagedLock.cc"
template class librbd::ManagedLock<librbd::MockManagedLockImageCtx>;
ACTION_P3(QueueRequest, request, r, wq) {
if (request->on_finish != nullptr) {
if (wq != nullptr) {
wq->queue(request->on_finish, r);
} else {
request->on_finish->complete(r);
}
}
}
ACTION_P2(QueueContext, r, wq) {
wq->queue(arg0, r);
}
ACTION_P(Notify, ctx) {
ctx->complete(0);
}
namespace librbd {
using ::testing::_;
using ::testing::DoAll;
using ::testing::Invoke;
using ::testing::InSequence;
using ::testing::Return;
using ::testing::WithArg;
class TestMockManagedLock : public TestMockFixture {
public:
typedef ManagedLock<MockManagedLockImageCtx> MockManagedLock;
typedef managed_lock::AcquireRequest<MockManagedLockImageCtx> MockAcquireRequest;
typedef managed_lock::ReacquireRequest<MockManagedLockImageCtx> MockReacquireRequest;
typedef managed_lock::ReleaseRequest<MockManagedLockImageCtx> MockReleaseRequest;
void expect_get_watch_handle(MockImageWatcher &mock_watcher,
uint64_t watch_handle = 1234567890) {
EXPECT_CALL(mock_watcher, get_watch_handle())
.WillOnce(Return(watch_handle));
}
void expect_acquire_lock(MockImageWatcher &watcher,
ContextWQ *work_queue,
MockAcquireRequest &acquire_request, int r) {
expect_get_watch_handle(watcher);
EXPECT_CALL(acquire_request, send())
.WillOnce(QueueRequest(&acquire_request, r, work_queue));
}
void expect_release_lock(ContextWQ *work_queue,
MockReleaseRequest &release_request, int r) {
EXPECT_CALL(release_request, send())
.WillOnce(QueueRequest(&release_request, r, work_queue));
}
void expect_reacquire_lock(MockImageWatcher& watcher,
ContextWQ *work_queue,
MockReacquireRequest &mock_reacquire_request,
int r) {
EXPECT_CALL(mock_reacquire_request, send())
.WillOnce(QueueRequest(&mock_reacquire_request, r, work_queue));
}
void expect_flush_notifies(MockImageWatcher *mock_watcher) {
EXPECT_CALL(*mock_watcher, flush(_))
.WillOnce(CompleteContext(0, (ContextWQ *)nullptr));
}
void expect_post_reacquired_lock_handler(MockImageWatcher& watcher,
MockMockManagedLock &managed_lock, uint64_t &client_id) {
expect_get_watch_handle(watcher);
EXPECT_CALL(managed_lock, post_reacquire_lock_handler(_, _))
.WillOnce(Invoke([&client_id](int r, Context *on_finish){
if (r >= 0) {
client_id = 98765;
}
on_finish->complete(r);}));
}
void expect_pre_release_lock_handler(MockMockManagedLock &managed_lock,
bool shutting_down, int r) {
EXPECT_CALL(managed_lock, pre_release_lock_handler(shutting_down, _))
.WillOnce(WithArg<1>(Invoke([r](Context *on_finish){
on_finish->complete(r);
})));
}
void expect_post_release_lock_handler(MockMockManagedLock &managed_lock,
bool shutting_down, int expect_r,
int r) {
EXPECT_CALL(managed_lock, post_release_lock_handler(shutting_down, expect_r, _))
.WillOnce(WithArg<2>(Invoke([r](Context *on_finish){
on_finish->complete(r);
})));
}
int when_acquire_lock(MockManagedLock &managed_lock) {
C_SaferCond ctx;
{
managed_lock.acquire_lock(&ctx);
}
return ctx.wait();
}
int when_release_lock(MockManagedLock &managed_lock) {
C_SaferCond ctx;
{
managed_lock.release_lock(&ctx);
}
return ctx.wait();
}
int when_shut_down(MockManagedLock &managed_lock) {
C_SaferCond ctx;
{
managed_lock.shut_down(&ctx);
}
return ctx.wait();
}
bool is_lock_owner(MockManagedLock &managed_lock) {
return managed_lock.is_lock_owner();
}
};
TEST_F(TestMockManagedLock, StateTransitions) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockManagedLockImageCtx mock_image_ctx(*ictx);
MockManagedLock managed_lock(ictx->md_ctx, ictx->op_work_queue,
ictx->header_oid, mock_image_ctx.image_watcher,
librbd::managed_lock::EXCLUSIVE, true, 0);
InSequence seq;
MockAcquireRequest request_lock_acquire1;
expect_acquire_lock(*mock_image_ctx.image_watcher, ictx->op_work_queue, request_lock_acquire1, 0);
ASSERT_EQ(0, when_acquire_lock(managed_lock));
ASSERT_TRUE(is_lock_owner(managed_lock));
MockReleaseRequest request_release;
expect_release_lock(ictx->op_work_queue, request_release, 0);
ASSERT_EQ(0, when_release_lock(managed_lock));
ASSERT_FALSE(is_lock_owner(managed_lock));
MockAcquireRequest request_lock_acquire2;
expect_acquire_lock(*mock_image_ctx.image_watcher, ictx->op_work_queue, request_lock_acquire2, 0);
ASSERT_EQ(0, when_acquire_lock(managed_lock));
ASSERT_TRUE(is_lock_owner(managed_lock));
MockReleaseRequest shutdown_release;
expect_release_lock(ictx->op_work_queue, shutdown_release, 0);
ASSERT_EQ(0, when_shut_down(managed_lock));
ASSERT_FALSE(is_lock_owner(managed_lock));
}
TEST_F(TestMockManagedLock, AcquireLockLockedState) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockManagedLockImageCtx mock_image_ctx(*ictx);
MockManagedLock managed_lock(ictx->md_ctx, ictx->op_work_queue,
ictx->header_oid, mock_image_ctx.image_watcher,
librbd::managed_lock::EXCLUSIVE, true, 0);
InSequence seq;
MockAcquireRequest try_lock_acquire;
expect_acquire_lock(*mock_image_ctx.image_watcher, ictx->op_work_queue, try_lock_acquire, 0);
ASSERT_EQ(0, when_acquire_lock(managed_lock));
ASSERT_EQ(0, when_acquire_lock(managed_lock));
MockReleaseRequest shutdown_release;
expect_release_lock(ictx->op_work_queue, shutdown_release, 0);
ASSERT_EQ(0, when_shut_down(managed_lock));
}
TEST_F(TestMockManagedLock, AcquireLockAlreadyLocked) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockManagedLockImageCtx mock_image_ctx(*ictx);
MockManagedLock managed_lock(ictx->md_ctx, ictx->op_work_queue,
ictx->header_oid, mock_image_ctx.image_watcher,
librbd::managed_lock::EXCLUSIVE, true, 0);
InSequence seq;
MockAcquireRequest try_lock_acquire;
expect_acquire_lock(*mock_image_ctx.image_watcher, ictx->op_work_queue, try_lock_acquire, -EAGAIN);
ASSERT_EQ(-EAGAIN, when_acquire_lock(managed_lock));
ASSERT_FALSE(is_lock_owner(managed_lock));
ASSERT_EQ(0, when_shut_down(managed_lock));
}
TEST_F(TestMockManagedLock, AcquireLockBusy) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockManagedLockImageCtx mock_image_ctx(*ictx);
MockManagedLock managed_lock(ictx->md_ctx, ictx->op_work_queue,
ictx->header_oid, mock_image_ctx.image_watcher,
librbd::managed_lock::EXCLUSIVE, true, 0);
InSequence seq;
MockAcquireRequest try_lock_acquire;
expect_acquire_lock(*mock_image_ctx.image_watcher, ictx->op_work_queue, try_lock_acquire, -EBUSY);
ASSERT_EQ(-EBUSY, when_acquire_lock(managed_lock));
ASSERT_FALSE(is_lock_owner(managed_lock));
ASSERT_EQ(0, when_shut_down(managed_lock));
}
TEST_F(TestMockManagedLock, AcquireLockError) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockManagedLockImageCtx mock_image_ctx(*ictx);
MockManagedLock managed_lock(ictx->md_ctx, ictx->op_work_queue,
ictx->header_oid, mock_image_ctx.image_watcher,
librbd::managed_lock::EXCLUSIVE, true, 0);
InSequence seq;
MockAcquireRequest try_lock_acquire;
expect_acquire_lock(*mock_image_ctx.image_watcher, ictx->op_work_queue, try_lock_acquire, -EINVAL);
ASSERT_EQ(-EINVAL, when_acquire_lock(managed_lock));
ASSERT_FALSE(is_lock_owner(managed_lock));
ASSERT_EQ(0, when_shut_down(managed_lock));
}
TEST_F(TestMockManagedLock, AcquireLockBlacklist) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockManagedLockImageCtx mock_image_ctx(*ictx);
MockManagedLock managed_lock(ictx->md_ctx, ictx->op_work_queue,
ictx->header_oid, mock_image_ctx.image_watcher,
librbd::managed_lock::EXCLUSIVE, true, 0);
InSequence seq;
// will abort after seeing blacklist error (avoid infinite request loop)
MockAcquireRequest request_lock_acquire;
expect_acquire_lock(*mock_image_ctx.image_watcher, ictx->op_work_queue, request_lock_acquire, -EBLACKLISTED);
ASSERT_EQ(-EBLACKLISTED, when_acquire_lock(managed_lock));
ASSERT_FALSE(is_lock_owner(managed_lock));
ASSERT_EQ(0, when_shut_down(managed_lock));
}
TEST_F(TestMockManagedLock, ReleaseLockUnlockedState) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockManagedLockImageCtx mock_image_ctx(*ictx);
MockManagedLock managed_lock(ictx->md_ctx, ictx->op_work_queue,
ictx->header_oid, mock_image_ctx.image_watcher,
librbd::managed_lock::EXCLUSIVE, true, 0);
InSequence seq;
ASSERT_EQ(0, when_release_lock(managed_lock));
ASSERT_EQ(0, when_shut_down(managed_lock));
}
TEST_F(TestMockManagedLock, ReleaseLockBlacklist) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockManagedLockImageCtx mock_image_ctx(*ictx);
MockMockManagedLock managed_lock(ictx->md_ctx, ictx->op_work_queue,
ictx->header_oid, mock_image_ctx.image_watcher,
librbd::managed_lock::EXCLUSIVE, true, 0);
InSequence seq;
MockAcquireRequest try_lock_acquire;
expect_acquire_lock(*mock_image_ctx.image_watcher, ictx->op_work_queue, try_lock_acquire, 0);
ASSERT_EQ(0, when_acquire_lock(managed_lock));
expect_pre_release_lock_handler(managed_lock, false, -EBLACKLISTED);
expect_post_release_lock_handler(managed_lock, false, -EBLACKLISTED, -EBLACKLISTED);
ASSERT_EQ(-EBLACKLISTED, when_release_lock(managed_lock));
ASSERT_FALSE(is_lock_owner(managed_lock));
ASSERT_EQ(0, when_shut_down(managed_lock));
}
TEST_F(TestMockManagedLock, ReleaseLockError) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockManagedLockImageCtx mock_image_ctx(*ictx);
MockManagedLock managed_lock(ictx->md_ctx, ictx->op_work_queue,
ictx->header_oid, mock_image_ctx.image_watcher,
librbd::managed_lock::EXCLUSIVE, true, 0);
InSequence seq;
MockAcquireRequest try_lock_acquire;
expect_acquire_lock(*mock_image_ctx.image_watcher, ictx->op_work_queue, try_lock_acquire, 0);
ASSERT_EQ(0, when_acquire_lock(managed_lock));
MockReleaseRequest release;
expect_release_lock(ictx->op_work_queue, release, -EINVAL);
ASSERT_EQ(-EINVAL, when_release_lock(managed_lock));
ASSERT_TRUE(is_lock_owner(managed_lock));
MockReleaseRequest shutdown_release;
expect_release_lock(ictx->op_work_queue, shutdown_release, 0);
ASSERT_EQ(0, when_shut_down(managed_lock));
ASSERT_FALSE(is_lock_owner(managed_lock));
}
TEST_F(TestMockManagedLock, ConcurrentRequests) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockManagedLockImageCtx mock_image_ctx(*ictx);
MockManagedLock managed_lock(ictx->md_ctx, ictx->op_work_queue,
ictx->header_oid, mock_image_ctx.image_watcher,
librbd::managed_lock::EXCLUSIVE, true, 0);
InSequence seq;
expect_get_watch_handle(*mock_image_ctx.image_watcher);
C_SaferCond wait_for_send_ctx1;
MockAcquireRequest acquire_error;
EXPECT_CALL(acquire_error, send())
.WillOnce(Notify(&wait_for_send_ctx1));
MockAcquireRequest request_acquire;
expect_acquire_lock(*mock_image_ctx.image_watcher, ictx->op_work_queue, request_acquire, 0);
MockReleaseRequest release;
C_SaferCond wait_for_send_ctx2;
EXPECT_CALL(release, send())
.WillOnce(Notify(&wait_for_send_ctx2));
C_SaferCond acquire_request_ctx1;
managed_lock.acquire_lock(&acquire_request_ctx1);
C_SaferCond acquire_lock_ctx1;
C_SaferCond acquire_lock_ctx2;
managed_lock.acquire_lock(&acquire_lock_ctx1);
managed_lock.acquire_lock(&acquire_lock_ctx2);
// fail the try_lock
ASSERT_EQ(0, wait_for_send_ctx1.wait());
acquire_error.on_finish->complete(-EINVAL);
ASSERT_EQ(-EINVAL, acquire_request_ctx1.wait());
C_SaferCond acquire_lock_ctx3;
managed_lock.acquire_lock(&acquire_lock_ctx3);
C_SaferCond release_lock_ctx1;
managed_lock.release_lock(&release_lock_ctx1);
// all three pending request locks should complete
ASSERT_EQ(-EINVAL, acquire_lock_ctx1.wait());
ASSERT_EQ(-EINVAL, acquire_lock_ctx2.wait());
ASSERT_EQ(0, acquire_lock_ctx3.wait());
// proceed with the release
ASSERT_EQ(0, wait_for_send_ctx2.wait());
release.on_finish->complete(0);
ASSERT_EQ(0, release_lock_ctx1.wait());
ASSERT_EQ(0, when_shut_down(managed_lock));
}
TEST_F(TestMockManagedLock, ReacquireLock) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockManagedLockImageCtx mock_image_ctx(*ictx);
MockManagedLock managed_lock(ictx->md_ctx, ictx->op_work_queue,
ictx->header_oid, mock_image_ctx.image_watcher,
librbd::managed_lock::EXCLUSIVE, true, 0);
InSequence seq;
MockAcquireRequest request_lock_acquire;
expect_acquire_lock(*mock_image_ctx.image_watcher, ictx->op_work_queue, request_lock_acquire, 0);
ASSERT_EQ(0, when_acquire_lock(managed_lock));
ASSERT_TRUE(is_lock_owner(managed_lock));
MockReacquireRequest mock_reacquire_request;
C_SaferCond reacquire_ctx;
expect_get_watch_handle(*mock_image_ctx.image_watcher, 98765);
expect_reacquire_lock(*mock_image_ctx.image_watcher, ictx->op_work_queue, mock_reacquire_request, 0);
managed_lock.reacquire_lock(&reacquire_ctx);
ASSERT_EQ(0, reacquire_ctx.wait());
MockReleaseRequest shutdown_release;
expect_release_lock(ictx->op_work_queue, shutdown_release, 0);
ASSERT_EQ(0, when_shut_down(managed_lock));
ASSERT_FALSE(is_lock_owner(managed_lock));
}
TEST_F(TestMockManagedLock, AttemptReacquireBlacklistedLock) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockManagedLockImageCtx mock_image_ctx(*ictx);
MockManagedLock managed_lock(ictx->md_ctx, ictx->op_work_queue,
ictx->header_oid, mock_image_ctx.image_watcher,
librbd::managed_lock::EXCLUSIVE, true, 0);
InSequence seq;
MockAcquireRequest request_lock_acquire;
expect_acquire_lock(*mock_image_ctx.image_watcher, ictx->op_work_queue,
request_lock_acquire, 0);
ASSERT_EQ(0, when_acquire_lock(managed_lock));
ASSERT_TRUE(is_lock_owner(managed_lock));
expect_get_watch_handle(*mock_image_ctx.image_watcher, 0);
MockReleaseRequest request_release;
expect_release_lock(ictx->op_work_queue, request_release, 0);
expect_get_watch_handle(*mock_image_ctx.image_watcher, 0);
managed_lock.reacquire_lock(nullptr);
ASSERT_EQ(0, when_shut_down(managed_lock));
ASSERT_FALSE(is_lock_owner(managed_lock));
}
TEST_F(TestMockManagedLock, ReacquireBlacklistedLock) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockManagedLockImageCtx mock_image_ctx(*ictx);
MockManagedLock managed_lock(ictx->md_ctx, ictx->op_work_queue,
ictx->header_oid, mock_image_ctx.image_watcher,
librbd::managed_lock::EXCLUSIVE, true, 0);
InSequence seq;
MockAcquireRequest request_lock_acquire;
expect_acquire_lock(*mock_image_ctx.image_watcher, ictx->op_work_queue,
request_lock_acquire, 0);
ASSERT_EQ(0, when_acquire_lock(managed_lock));
ASSERT_TRUE(is_lock_owner(managed_lock));
expect_get_watch_handle(*mock_image_ctx.image_watcher, 0);
MockReleaseRequest request_release;
expect_release_lock(ictx->op_work_queue, request_release, 0);
MockAcquireRequest request_lock_reacquire;
expect_acquire_lock(*mock_image_ctx.image_watcher, ictx->op_work_queue,
request_lock_reacquire, 0);
ASSERT_EQ(0, when_acquire_lock(managed_lock));
ASSERT_TRUE(is_lock_owner(managed_lock));
C_SaferCond reacquire_ctx;
managed_lock.reacquire_lock(&reacquire_ctx);
ASSERT_EQ(0, reacquire_ctx.wait());
MockReleaseRequest shutdown_release;
expect_release_lock(ictx->op_work_queue, shutdown_release, 0);
ASSERT_EQ(0, when_shut_down(managed_lock));
ASSERT_FALSE(is_lock_owner(managed_lock));
}
TEST_F(TestMockManagedLock, ReacquireLockError) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockManagedLockImageCtx mock_image_ctx(*ictx);
MockManagedLock managed_lock(ictx->md_ctx, ictx->op_work_queue,
ictx->header_oid, mock_image_ctx.image_watcher,
librbd::managed_lock::EXCLUSIVE, true, 0);
InSequence seq;
MockAcquireRequest request_lock_acquire;
expect_acquire_lock(*mock_image_ctx.image_watcher, ictx->op_work_queue, request_lock_acquire, 0);
ASSERT_EQ(0, when_acquire_lock(managed_lock));
ASSERT_TRUE(is_lock_owner(managed_lock));
MockReacquireRequest mock_reacquire_request;
C_SaferCond reacquire_ctx;
expect_get_watch_handle(*mock_image_ctx.image_watcher, 98765);
expect_reacquire_lock(*mock_image_ctx.image_watcher, ictx->op_work_queue, mock_reacquire_request, -EOPNOTSUPP);
MockReleaseRequest reacquire_lock_release;
expect_release_lock(ictx->op_work_queue, reacquire_lock_release, 0);
MockAcquireRequest reacquire_lock_acquire;
expect_acquire_lock(*mock_image_ctx.image_watcher, ictx->op_work_queue, reacquire_lock_acquire, 0);
managed_lock.reacquire_lock(&reacquire_ctx);
ASSERT_EQ(0, reacquire_ctx.wait());
MockReleaseRequest shutdown_release;
expect_release_lock(ictx->op_work_queue, shutdown_release, 0);
ASSERT_EQ(0, when_shut_down(managed_lock));
ASSERT_FALSE(is_lock_owner(managed_lock));
}
TEST_F(TestMockManagedLock, ReacquireWithSameCookie) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockManagedLockImageCtx mock_image_ctx(*ictx);
MockMockManagedLock managed_lock(ictx->md_ctx, ictx->op_work_queue,
ictx->header_oid, mock_image_ctx.image_watcher,
librbd::managed_lock::EXCLUSIVE, true, 0);
InSequence seq;
MockAcquireRequest request_lock_acquire;
expect_acquire_lock(*mock_image_ctx.image_watcher, ictx->op_work_queue, request_lock_acquire, 0);
ASSERT_EQ(0, when_acquire_lock(managed_lock));
ASSERT_TRUE(is_lock_owner(managed_lock));
// watcher with same cookie after rewatch
uint64_t client_id = 0;
C_SaferCond reacquire_ctx;
expect_post_reacquired_lock_handler(*mock_image_ctx.image_watcher, managed_lock, client_id);
managed_lock.reacquire_lock(&reacquire_ctx);
ASSERT_LT(0U, client_id);
ASSERT_TRUE(is_lock_owner(managed_lock));
MockReleaseRequest shutdown_release;
expect_pre_release_lock_handler(managed_lock, true, 0);
expect_release_lock(ictx->op_work_queue, shutdown_release, 0);
expect_post_release_lock_handler(managed_lock, true, 0, 0);
ASSERT_EQ(0, when_shut_down(managed_lock));
}
TEST_F(TestMockManagedLock, ShutDownWhileWaiting) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockManagedLockImageCtx mock_image_ctx(*ictx);
MockMockManagedLock managed_lock(ictx->md_ctx, ictx->op_work_queue,
ictx->header_oid, mock_image_ctx.image_watcher,
librbd::managed_lock::EXCLUSIVE, true, 0);
InSequence seq;
expect_get_watch_handle(*mock_image_ctx.image_watcher, 0);
C_SaferCond acquire_ctx;
managed_lock.acquire_lock(&acquire_ctx);
ASSERT_EQ(0, when_shut_down(managed_lock));
ASSERT_EQ(-ESHUTDOWN, acquire_ctx.wait());
ASSERT_FALSE(is_lock_owner(managed_lock));
}
} // namespace librbd
|