]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- |
2 | // vim: ts=8 sw=2 smarttab | |
3 | ||
4 | #include "test/librbd/test_fixture.h" | |
5 | #include "test/librbd/test_support.h" | |
6 | #include "librbd/BlockGuard.h" | |
7 | ||
8 | namespace librbd { | |
9 | ||
10 | class TestIOBlockGuard : public TestFixture { | |
11 | public: | |
12 | static uint32_t s_index; | |
13 | ||
14 | struct Operation { | |
15 | uint32_t index; | |
16 | Operation() : index(++s_index) { | |
17 | } | |
18 | Operation(Operation &&rhs) : index(rhs.index) { | |
19 | } | |
20 | Operation(const Operation &) = delete; | |
21 | ||
22 | Operation& operator=(Operation &&rhs) { | |
23 | index = rhs.index; | |
24 | return *this; | |
25 | } | |
26 | ||
27 | bool operator==(const Operation &rhs) const { | |
28 | return index == rhs.index; | |
29 | } | |
30 | }; | |
31 | ||
32 | typedef std::list<Operation> Operations; | |
33 | ||
34 | typedef BlockGuard<Operation> OpBlockGuard; | |
35 | ||
36 | void SetUp() override { | |
37 | TestFixture::SetUp(); | |
38 | m_cct = reinterpret_cast<CephContext*>(m_ioctx.cct()); | |
39 | } | |
40 | ||
41 | CephContext *m_cct; | |
42 | }; | |
43 | ||
44 | TEST_F(TestIOBlockGuard, NonDetainedOps) { | |
45 | OpBlockGuard op_block_guard(m_cct); | |
46 | ||
47 | Operation op1; | |
48 | BlockGuardCell *cell1; | |
49 | ASSERT_EQ(0, op_block_guard.detain({1, 3}, &op1, &cell1)); | |
50 | ||
51 | Operation op2; | |
52 | BlockGuardCell *cell2; | |
53 | ASSERT_EQ(0, op_block_guard.detain({0, 1}, &op2, &cell2)); | |
54 | ||
55 | Operation op3; | |
56 | BlockGuardCell *cell3; | |
57 | ASSERT_EQ(0, op_block_guard.detain({3, 6}, &op3, &cell3)); | |
58 | ||
59 | Operations released_ops; | |
60 | op_block_guard.release(cell1, &released_ops); | |
61 | ASSERT_TRUE(released_ops.empty()); | |
62 | ||
63 | op_block_guard.release(cell2, &released_ops); | |
64 | ASSERT_TRUE(released_ops.empty()); | |
65 | ||
66 | op_block_guard.release(cell3, &released_ops); | |
67 | ASSERT_TRUE(released_ops.empty()); | |
68 | } | |
69 | ||
70 | TEST_F(TestIOBlockGuard, DetainedOps) { | |
71 | OpBlockGuard op_block_guard(m_cct); | |
72 | ||
73 | Operation op1; | |
74 | BlockGuardCell *cell1; | |
75 | ASSERT_EQ(0, op_block_guard.detain({1, 3}, &op1, &cell1)); | |
76 | ||
77 | Operation op2; | |
78 | BlockGuardCell *cell2; | |
79 | ASSERT_EQ(1, op_block_guard.detain({2, 6}, &op2, &cell2)); | |
80 | ASSERT_EQ(nullptr, cell2); | |
81 | ||
82 | Operation op3; | |
83 | BlockGuardCell *cell3; | |
84 | ASSERT_EQ(2, op_block_guard.detain({0, 2}, &op3, &cell3)); | |
85 | ASSERT_EQ(nullptr, cell3); | |
86 | ||
87 | Operations expected_ops; | |
88 | expected_ops.push_back(std::move(op2)); | |
89 | expected_ops.push_back(std::move(op3)); | |
90 | Operations released_ops; | |
91 | op_block_guard.release(cell1, &released_ops); | |
92 | ASSERT_EQ(expected_ops, released_ops); | |
93 | } | |
94 | ||
95 | uint32_t TestIOBlockGuard::s_index = 0; | |
96 | ||
97 | } // namespace librbd | |
98 |