]>
git.proxmox.com Git - ceph.git/blob - ceph/src/test/journal/test_Journaler.cc
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 #include "include/stringify.h"
6 #include "journal/Journaler.h"
7 #include "journal/Settings.h"
9 #include "test/librados/test.h"
10 #include "test/journal/RadosTestFixture.h"
12 #include "gtest/gtest.h"
14 // reinclude our assert to clobber the system one
15 #include "include/assert.h"
17 class TestJournaler
: public RadosTestFixture
{
20 static const std::string CLIENT_ID
;
22 static std::string
get_temp_journal_id() {
23 return stringify(++_journal_id
);
26 void SetUp() override
{
27 RadosTestFixture::SetUp();
28 m_journal_id
= get_temp_journal_id();
29 m_journaler
= new journal::Journaler(m_work_queue
, m_timer
, &m_timer_lock
,
30 m_ioctx
, m_journal_id
, CLIENT_ID
, {});
33 void TearDown() override
{
35 RadosTestFixture::TearDown();
38 int create_journal(uint8_t order
, uint8_t splay_width
) {
40 m_journaler
->create(order
, splay_width
, -1, &cond
);
44 int init_journaler() {
46 m_journaler
->init(&cond
);
50 int shut_down_journaler() {
52 m_journaler
->shut_down(&ctx
);
56 int register_client(const std::string
&client_id
, const std::string
&desc
) {
57 journal::Journaler
journaler(m_work_queue
, m_timer
, &m_timer_lock
,
58 m_ioctx
, m_journal_id
, client_id
, {});
62 journaler
.register_client(data
, &cond
);
66 int update_client(const std::string
&client_id
, const std::string
&desc
) {
67 journal::Journaler
journaler(m_work_queue
, m_timer
, &m_timer_lock
,
68 m_ioctx
, m_journal_id
, client_id
, {});
72 journaler
.update_client(data
, &cond
);
76 int unregister_client(const std::string
&client_id
) {
77 journal::Journaler
journaler(m_work_queue
, m_timer
, &m_timer_lock
,
78 m_ioctx
, m_journal_id
, client_id
, {});
80 journaler
.unregister_client(&cond
);
84 static uint64_t _journal_id
;
86 std::string m_journal_id
;
87 journal::Journaler
*m_journaler
;
90 const std::string
TestJournaler::CLIENT_ID
= "client1";
91 uint64_t TestJournaler::_journal_id
= 0;
93 TEST_F(TestJournaler
, Create
) {
94 ASSERT_EQ(0, create_journal(12, 8));
97 TEST_F(TestJournaler
, CreateDuplicate
) {
98 ASSERT_EQ(0, create_journal(12, 8));
99 ASSERT_EQ(-EEXIST
, create_journal(12, 8));
102 TEST_F(TestJournaler
, CreateInvalidParams
) {
103 ASSERT_EQ(-EDOM
, create_journal(1, 8));
104 ASSERT_EQ(-EDOM
, create_journal(123, 8));
105 ASSERT_EQ(-EINVAL
, create_journal(12, 0));
108 TEST_F(TestJournaler
, Init
) {
109 ASSERT_EQ(0, create_journal(12, 8));
110 ASSERT_EQ(0, register_client(CLIENT_ID
, "foo"));
111 ASSERT_EQ(0, init_journaler());
112 ASSERT_EQ(0, shut_down_journaler());
115 TEST_F(TestJournaler
, InitDNE
) {
116 ASSERT_EQ(-ENOENT
, init_journaler());
117 ASSERT_EQ(0, shut_down_journaler());
120 TEST_F(TestJournaler
, RegisterClientDuplicate
) {
121 ASSERT_EQ(0, create_journal(12, 8));
122 ASSERT_EQ(0, register_client(CLIENT_ID
, "foo"));
123 ASSERT_EQ(-EEXIST
, register_client(CLIENT_ID
, "foo2"));
126 TEST_F(TestJournaler
, UpdateClient
) {
127 ASSERT_EQ(0, create_journal(12, 8));
128 ASSERT_EQ(0, register_client(CLIENT_ID
, "foo"));
129 ASSERT_EQ(0, update_client(CLIENT_ID
, "foo2"));
132 TEST_F(TestJournaler
, UpdateClientDNE
) {
133 ASSERT_EQ(0, create_journal(12, 8));
134 ASSERT_EQ(-ENOENT
, update_client(CLIENT_ID
, "foo"));
137 TEST_F(TestJournaler
, UnregisterClient
) {
138 ASSERT_EQ(0, create_journal(12, 8));
139 ASSERT_EQ(0, register_client(CLIENT_ID
, "foo"));
140 ASSERT_EQ(0, unregister_client(CLIENT_ID
));
141 // Test it does not exist and can be registered again
142 ASSERT_EQ(-ENOENT
, update_client(CLIENT_ID
, "foo"));
143 ASSERT_EQ(0, register_client(CLIENT_ID
, "foo"));
146 TEST_F(TestJournaler
, UnregisterClientDNE
) {
147 ASSERT_EQ(0, create_journal(12, 8));
148 ASSERT_EQ(-ENOENT
, unregister_client(CLIENT_ID
));
151 TEST_F(TestJournaler
, AllocateTag
) {
152 ASSERT_EQ(0, create_journal(12, 8));
154 cls::journal::Tag tag
;
157 data
.append(std::string(128, '1'));
159 // allocate a new tag class
161 m_journaler
->allocate_tag(data
, &tag
, &ctx1
);
162 ASSERT_EQ(0, ctx1
.wait());
163 ASSERT_EQ(cls::journal::Tag(0, 0, data
), tag
);
165 // re-use an existing tag class
167 m_journaler
->allocate_tag(tag
.tag_class
, bufferlist(), &tag
, &ctx2
);
168 ASSERT_EQ(0, ctx2
.wait());
169 ASSERT_EQ(cls::journal::Tag(1, 0, bufferlist()), tag
);
172 TEST_F(TestJournaler
, GetTags
) {
173 ASSERT_EQ(0, create_journal(12, 8));
174 ASSERT_EQ(0, register_client(CLIENT_ID
, "foo"));
176 std::list
<cls::journal::Tag
> expected_tags
;
177 for (size_t i
= 0; i
< 256; ++i
) {
179 cls::journal::Tag tag
;
181 m_journaler
->allocate_tag(bufferlist(), &tag
, &ctx
);
183 m_journaler
->allocate_tag(i
% 2, bufferlist(), &tag
, &ctx
);
185 ASSERT_EQ(0, ctx
.wait());
188 expected_tags
.push_back(tag
);
192 std::list
<cls::journal::Tag
> tags
;
194 m_journaler
->get_tags(0, &tags
, &ctx
);
195 ASSERT_EQ(0, ctx
.wait());
196 ASSERT_EQ(expected_tags
, tags
);