From ee0c8bb249396da61812eec6e996b50f36d9f1b4 Mon Sep 17 00:00:00 2001 From: Leonid Yuriev Date: Sun, 4 Nov 2018 18:57:36 +0300 Subject: [PATCH] mdbx: backport - add db-copy testcase. Change-Id: Ib554880ebbabcb5dfc55bdb3c71767d0fa1630fd --- libmdbx.files | 1 + mdbx.h | 3 +++ test/CMakeLists.txt | 51 ++++++++++++++++++++++---------------------- test/cases.cc | 3 ++- test/config.h | 5 +++-- test/copy.cc | 26 ++++++++++++++++++++++ test/main.cc | 4 ++++ test/osal-unix.cc | 6 +++++- test/osal-windows.cc | 6 +++++- test/osal.h | 3 ++- test/test.cc | 7 +++++- test/test.h | 11 ++++++++++ test/test.vcxproj | 1 + 13 files changed, 95 insertions(+), 32 deletions(-) create mode 100644 test/copy.cc diff --git a/libmdbx.files b/libmdbx.files index 38125146..653b0397 100644 --- a/libmdbx.files +++ b/libmdbx.files @@ -3,6 +3,7 @@ README-RU.md pcrf_test/CMakeLists.txt src/tools/CMakeLists.txt test/CMakeLists.txt +test/copy.cc tutorial/CMakeLists.txt tutorial/sample-mdbx.c AUTHORS diff --git a/mdbx.h b/mdbx.h index 64aaeec8..cedcbc11 100644 --- a/mdbx.h +++ b/mdbx.h @@ -100,6 +100,7 @@ typedef DWORD mdbx_tid_t; #define MDBX_EIO ERROR_WRITE_FAULT #define MDBX_EPERM ERROR_INVALID_FUNCTION #define MDBX_EINTR ERROR_CANCELLED +#define MDBX_ENOFILE ERROR_FILE_NOT_FOUND #else @@ -120,6 +121,8 @@ typedef pthread_t mdbx_tid_t; #define MDBX_EIO EIO #define MDBX_EPERM EPERM #define MDBX_EINTR EINTR +#define MDBX_ENOFILE ENOENT + #endif #ifdef _MSC_VER diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 3ed01bdd..ca7dd794 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -1,34 +1,35 @@ -set(TARGET mdbx_test) +set(TARGET mdbx_test) project(${TARGET}) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-missing-declarations") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-cast-qual") add_executable(${TARGET} - base.h - cases.cc - chrono.cc - chrono.h - config.cc - config.h - dead.cc - hill.cc - jitter.cc - keygen.cc - keygen.h - log.cc - log.h - main.cc - osal.h - osal-unix.cc - test.cc - test.h - try.cc - utils.cc - utils.h -) + base.h + cases.cc + chrono.cc + chrono.h + config.cc + config.h + copy.cc + dead.cc + hill.cc + jitter.cc + keygen.cc + keygen.h + log.cc + log.h + main.cc + osal.h + osal-unix.cc + test.cc + test.h + try.cc + utils.cc + utils.h + ) target_link_libraries(${TARGET} - mdbx - ) + mdbx + ) diff --git a/test/cases.cc b/test/cases.cc index 4f4306d5..13d47576 100644 --- a/test/cases.cc +++ b/test/cases.cc @@ -1,4 +1,4 @@ -/* +/* * Copyright 2017-2018 Leonid Yuriev * and other libmdbx authors: please see AUTHORS file. * All rights reserved. @@ -68,6 +68,7 @@ void testcase_setup(const char *casename, actor_params ¶ms, configure_actor(last_space_id, ac_jitter, nullptr, params); configure_actor(last_space_id, ac_hill, nullptr, params); configure_actor(last_space_id, ac_try, nullptr, params); + configure_actor(last_space_id, ac_copy, nullptr, params); log_notice("<<< testcase_setup(%s): done", casename); } else { failure("unknown testcase `%s`", casename); diff --git a/test/config.h b/test/config.h index 2d0fede0..1886a8ea 100644 --- a/test/config.h +++ b/test/config.h @@ -1,4 +1,4 @@ -/* +/* * Copyright 2017-2018 Leonid Yuriev * and other libmdbx authors: please see AUTHORS file. * All rights reserved. @@ -26,7 +26,8 @@ enum actor_testcase { ac_deadread, ac_deadwrite, ac_jitter, - ac_try + ac_try, + ac_copy }; enum actor_status { diff --git a/test/copy.cc b/test/copy.cc new file mode 100644 index 00000000..e239d41e --- /dev/null +++ b/test/copy.cc @@ -0,0 +1,26 @@ +#include "test.h" + +void testcase_copy::copy_db(const bool with_compaction) { + int err = osal_removefile(copy_pathname); + if (err != MDBX_SUCCESS && err != MDBX_ENOFILE) + failure_perror("mdbx_removefile()", err); + + err = mdbx_env_copy(db_guard.get(), copy_pathname.c_str(), + with_compaction ? MDBX_CP_COMPACT : 0); + if (unlikely(err != MDBX_SUCCESS)) + failure_perror(with_compaction ? "mdbx_env_copy(MDBX_CP_COMPACT)" + : "mdbx_env_copy(MDBX_CP_ASIS)", + err); +} + +bool testcase_copy::run() { + jitter_delay(); + db_open(); + assert(!txn_guard); + const bool order = flipcoin(); + jitter_delay(); + copy_db(order); + jitter_delay(); + copy_db(!order); + return true; +} diff --git a/test/main.cc b/test/main.cc index 3384311b..275b7b13 100644 --- a/test/main.cc +++ b/test/main.cc @@ -337,6 +337,10 @@ int main(int argc, char *const argv[]) { configure_actor(last_space_id, ac_deadwrite, value, params); continue; } + if (config::parse_option(argc, argv, narg, "copy", nullptr)) { + configure_actor(last_space_id, ac_copy, value, params); + continue; + } if (config::parse_option(argc, argv, narg, "failfast", global::config::failfast)) continue; diff --git a/test/osal-unix.cc b/test/osal-unix.cc index 6661ae42..1856d0f8 100644 --- a/test/osal-unix.cc +++ b/test/osal-unix.cc @@ -1,4 +1,4 @@ -/* +/* * Copyright 2017-2018 Leonid Yuriev * and other libmdbx authors: please see AUTHORS file. * All rights reserved. @@ -301,3 +301,7 @@ std::string osal_tempdir(void) { return "/dev/shm/"; return ""; } + +int osal_removefile(const std::string &pathname) { + return unlink(pathname.c_str()) ? errno : MDBX_SUCCESS; +} diff --git a/test/osal-windows.cc b/test/osal-windows.cc index 7d59f657..f7f1de56 100644 --- a/test/osal-windows.cc +++ b/test/osal-windows.cc @@ -1,4 +1,4 @@ -/* +/* * Copyright 2017-2018 Leonid Yuriev * and other libmdbx authors: please see AUTHORS file. * All rights reserved. @@ -405,3 +405,7 @@ std::string osal_tempdir(void) { DWORD len = GetTempPathA(sizeof(buf), buf); return std::string(buf, len); } + +int osal_removefile(const std::string &pathname) { + return DeleteFileA(pathname.c_str()) ? MDBX_SUCCESS : GetLastError(); +} diff --git a/test/osal.h b/test/osal.h index c27282a6..3ccc7bbe 100644 --- a/test/osal.h +++ b/test/osal.h @@ -1,4 +1,4 @@ -/* +/* * Copyright 2017-2018 Leonid Yuriev * and other libmdbx authors: please see AUTHORS file. * All rights reserved. @@ -32,6 +32,7 @@ void osal_udelay(unsigned us); void osal_yield(void); bool osal_istty(int fd); std::string osal_tempdir(void); +int osal_removefile(const std::string &pathname); #ifdef _MSC_VER #ifndef STDIN_FILENO diff --git a/test/test.cc b/test/test.cc index c28bbd22..445c4c8d 100644 --- a/test/test.cc +++ b/test/test.cc @@ -1,4 +1,4 @@ -/* +/* * Copyright 2017-2018 Leonid Yuriev * and other libmdbx authors: please see AUTHORS file. * All rights reserved. @@ -31,6 +31,8 @@ const char *testcase2str(const actor_testcase testcase) { return "jitter"; case ac_try: return "try"; + case ac_copy: + return "copy"; } } @@ -443,6 +445,9 @@ bool test_execute(const actor_config &config) { case ac_try: test.reset(new testcase_try(config, pid)); break; + case ac_copy: + test.reset(new testcase_copy(config, pid)); + break; default: test.reset(new testcase(config, pid)); break; diff --git a/test/test.h b/test/test.h index ef1c4caa..765940ce 100644 --- a/test/test.h +++ b/test/test.h @@ -203,3 +203,14 @@ public: bool run(); bool teardown(); }; + +class testcase_copy : public testcase { + const std::string copy_pathname; + void copy_db(const bool with_compaction); + +public: + testcase_copy(const actor_config &config, const mdbx_pid_t pid) + : testcase(config, pid), + copy_pathname(config.params.pathname_db + "-copy") {} + bool run(); +}; diff --git a/test/test.vcxproj b/test/test.vcxproj index a8c21d38..98ff7f49 100644 --- a/test/test.vcxproj +++ b/test/test.vcxproj @@ -184,6 +184,7 @@ +