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
|
// SPDX-License-Identifier: BSD-3-Clause
/* Copyright 2016-2020, Intel Corporation */
/*
* obj_cpp_make_persistent.cpp -- cpp make_persistent test for objects
*/
#include "unittest.hpp"
#include <libpmemobj++/make_persistent.hpp>
#include <libpmemobj++/p.hpp>
#include <libpmemobj++/persistent_ptr.hpp>
#include <libpmemobj++/pool.hpp>
#include <libpmemobj++/transaction.hpp>
#include <libpmemobj/ctl.h>
#define LAYOUT "cpp"
namespace nvobj = pmem::obj;
namespace
{
const int TEST_ARR_SIZE = 10;
class foo {
public:
foo() : bar(1)
{
for (int i = 0; i < TEST_ARR_SIZE; ++i)
this->arr[i] = 1;
}
foo(int val) : bar(val)
{
for (int i = 0; i < TEST_ARR_SIZE; ++i)
this->arr[i] = static_cast<char>(val);
}
foo(int val, char arr_val) : bar(val)
{
for (int i = 0; i < TEST_ARR_SIZE; ++i)
this->arr[i] = arr_val;
}
/*
* Assert values of foo.
*/
void
check_foo(int val, char arr_val)
{
UT_ASSERTeq(val, this->bar);
for (int i = 0; i < TEST_ARR_SIZE; ++i)
UT_ASSERTeq(arr_val, this->arr[i]);
}
nvobj::p<int> bar;
nvobj::p<char> arr[TEST_ARR_SIZE];
};
struct big_struct {
char data[PMEMOBJ_MIN_POOL];
};
struct struct_throwing {
struct_throwing()
{
throw magic_number;
}
char data[8];
static constexpr int magic_number = 42;
};
struct root {
nvobj::persistent_ptr<foo> pfoo;
nvobj::persistent_ptr<big_struct> bstruct;
nvobj::persistent_ptr<struct_throwing> throwing;
};
/*
* test_make_no_args -- (internal) test make_persistent without arguments
*/
void
test_make_no_args(nvobj::pool<struct root> &pop)
{
nvobj::persistent_ptr<root> r = pop.root();
try {
nvobj::transaction::run(pop, [&] {
UT_ASSERT(r->pfoo == nullptr);
r->pfoo = nvobj::make_persistent<foo>();
r->pfoo->check_foo(1, 1);
nvobj::delete_persistent<foo>(r->pfoo);
r->pfoo = nullptr;
});
} catch (...) {
UT_ASSERT(0);
}
UT_ASSERT(r->pfoo == nullptr);
}
/*
* test_make_args -- (internal) test make_persistent with arguments
*/
void
test_make_args(nvobj::pool<struct root> &pop)
{
nvobj::persistent_ptr<root> r = pop.root();
try {
nvobj::transaction::run(pop, [&] {
UT_ASSERT(r->pfoo == nullptr);
r->pfoo = nvobj::make_persistent<foo>(2);
r->pfoo->check_foo(2, 2);
nvobj::delete_persistent<foo>(r->pfoo);
r->pfoo = nvobj::make_persistent<foo>(3, 4);
r->pfoo->check_foo(3, 4);
nvobj::delete_persistent<foo>(r->pfoo);
r->pfoo = nullptr;
});
} catch (...) {
UT_ASSERT(0);
}
UT_ASSERT(r->pfoo == nullptr);
}
/*
* test_additional_delete -- (internal) test double delete and delete rollback
*/
void
test_additional_delete(nvobj::pool<struct root> &pop)
{
nvobj::persistent_ptr<root> r = pop.root();
try {
nvobj::transaction::run(pop, [&] {
UT_ASSERT(r->pfoo == nullptr);
r->pfoo = nvobj::make_persistent<foo>();
r->pfoo->check_foo(1, 1);
});
} catch (...) {
UT_ASSERT(0);
}
bool exception_thrown = false;
try {
nvobj::transaction::run(pop, [&] {
UT_ASSERT(r->pfoo != nullptr);
nvobj::delete_persistent<foo>(r->pfoo);
r->pfoo = nullptr;
nvobj::delete_persistent<foo>(r->pfoo);
nvobj::transaction::abort(EINVAL);
});
} catch (pmem::manual_tx_abort &) {
exception_thrown = true;
} catch (...) {
UT_ASSERT(0);
}
UT_ASSERT(exception_thrown);
UT_ASSERT(r->pfoo != nullptr);
r->pfoo->check_foo(1, 1);
try {
nvobj::transaction::run(pop, [&] {
UT_ASSERT(r->pfoo != nullptr);
nvobj::delete_persistent<foo>(r->pfoo);
r->pfoo = nullptr;
});
} catch (...) {
UT_ASSERT(0);
}
UT_ASSERT(r->pfoo == nullptr);
}
/*
* test_exceptions_handling -- (internal) test proper handling of exceptions
* inside make_persistent
*/
void
test_exceptions_handling(nvobj::pool<struct root> &pop)
{
nvobj::persistent_ptr<root> r = pop.root();
bool scope_error_thrown = false;
try {
/* Run outside of a transaction, expect error */
r->pfoo = nvobj::make_persistent<foo>();
UT_ASSERT(0);
} catch (pmem::transaction_scope_error &) {
scope_error_thrown = true;
}
UT_ASSERT(scope_error_thrown);
/* OOM handling */
bool alloc_error_thrown = false;
try {
nvobj::transaction::run(pop, [&] {
UT_ASSERT(r->bstruct == nullptr);
r->bstruct = nvobj::make_persistent<big_struct>();
UT_ASSERT(0);
});
} catch (pmem::transaction_alloc_error &e) {
(void)e.what();
alloc_error_thrown = true;
}
UT_ASSERT(alloc_error_thrown);
/* OOM handling */
alloc_error_thrown = false;
try {
nvobj::transaction::run(pop, [&] {
UT_ASSERT(r->bstruct == nullptr);
r->bstruct = nvobj::make_persistent<big_struct>();
UT_ASSERT(0);
});
} catch (pmem::transaction_error &e) {
(void)e.what();
alloc_error_thrown = true;
}
UT_ASSERT(alloc_error_thrown);
/* OOM handling */
alloc_error_thrown = false;
try {
nvobj::transaction::run(pop, [&] {
UT_ASSERT(r->bstruct == nullptr);
r->bstruct = nvobj::make_persistent<big_struct>();
UT_ASSERT(0);
});
} catch (std::bad_alloc &e) {
(void)e.what();
alloc_error_thrown = true;
}
UT_ASSERT(alloc_error_thrown);
bool scope_error_delete_thrown = false;
try {
nvobj::transaction::run(pop, [&] {
UT_ASSERT(r->pfoo == nullptr);
r->pfoo = nvobj::make_persistent<foo>();
});
} catch (...) {
UT_ASSERT(0);
}
try {
/* Run outside of a transaction, expect error */
nvobj::delete_persistent<foo>(r->pfoo);
UT_ASSERT(0);
} catch (pmem::transaction_scope_error &) {
scope_error_delete_thrown = true;
}
UT_ASSERT(scope_error_delete_thrown);
try {
nvobj::transaction::run(pop, [&] {
UT_ASSERT(r->throwing == nullptr);
r->throwing = nvobj::make_persistent<struct_throwing>();
UT_ASSERT(0);
});
} catch (int &e) {
UT_ASSERT(e == struct_throwing::magic_number);
}
UT_ASSERT(r->throwing == nullptr);
try {
nvobj::transaction::run(pop, [&] {
nvobj::delete_persistent<foo>(r->pfoo);
r->pfoo = nullptr;
});
} catch (...) {
UT_ASSERT(0);
}
}
/*
* test_flags -- (internal) test proper handling of flags
*/
void
test_flags(nvobj::pool<struct root> &pop)
{
nvobj::persistent_ptr<root> r = pop.root();
auto alloc_class = pop.ctl_set<struct pobj_alloc_class_desc>(
"heap.alloc_class.new.desc",
{sizeof(foo) + 16, 0, 200, POBJ_HEADER_COMPACT, 0});
try {
nvobj::transaction::run(pop, [&] {
UT_ASSERT(r->pfoo == nullptr);
r->pfoo = nvobj::make_persistent<foo>(
nvobj::allocation_flag::class_id(
alloc_class.class_id));
});
} catch (std::exception &e) {
UT_FATALexc(e);
}
UT_ASSERTeq(pmemobj_alloc_usable_size(r->pfoo.raw()), sizeof(foo));
try {
nvobj::transaction::run(pop, [&] {
nvobj::delete_persistent<foo>(r->pfoo);
r->pfoo = nullptr;
});
} catch (std::exception &e) {
UT_FATALexc(e);
}
try {
nvobj::transaction::run(pop, [&] {
UT_ASSERT(r->pfoo == nullptr);
r->pfoo = nvobj::make_persistent<foo>(
nvobj::allocation_flag::class_id(
alloc_class.class_id),
1, 2);
});
} catch (std::exception &e) {
UT_FATALexc(e);
}
r->pfoo->check_foo(1, 2);
UT_ASSERTeq(pmemobj_alloc_usable_size(r->pfoo.raw()), sizeof(foo));
try {
nvobj::transaction::run(pop, [&] {
nvobj::delete_persistent<foo>(r->pfoo);
r->pfoo = nullptr;
});
} catch (std::exception &e) {
UT_FATALexc(e);
}
}
}
static void
test(int argc, char *argv[])
{
if (argc != 2)
UT_FATAL("usage: %s file-name", argv[0]);
const char *path = argv[1];
nvobj::pool<struct root> pop;
try {
pop = nvobj::pool<root>::create(path, LAYOUT, PMEMOBJ_MIN_POOL,
S_IWUSR | S_IRUSR);
} catch (pmem::pool_error &pe) {
UT_FATAL("!pool::create: %s %s", pe.what(), path);
}
test_make_no_args(pop);
test_make_args(pop);
test_additional_delete(pop);
test_exceptions_handling(pop);
test_flags(pop);
pop.close();
}
int
main(int argc, char *argv[])
{
return run_test([&] { test(argc, argv); });
}
|