From 2726e88cbe8371eb0b66b6baf0f061554ee74b3a Mon Sep 17 00:00:00 2001 From: Brian Szmyd Date: Wed, 4 Oct 2023 09:24:23 -0700 Subject: [PATCH] Reduce reliance on test_package --- src/include/nuraft_mesg/grpc_factory.hpp | 1 + src/lib/grpc_factory.cpp | 16 +++++++++---- src/lib/logger.hpp | 4 ++-- test_package/CMakeLists.txt | 29 +++++++----------------- test_package/conanfile.py | 25 +++++++------------- test_package/example_client.cpp | 2 +- test_package/example_server.cpp | 14 +++++++----- 7 files changed, 39 insertions(+), 52 deletions(-) diff --git a/src/include/nuraft_mesg/grpc_factory.hpp b/src/include/nuraft_mesg/grpc_factory.hpp index 3ef6d88..4a282e7 100644 --- a/src/include/nuraft_mesg/grpc_factory.hpp +++ b/src/include/nuraft_mesg/grpc_factory.hpp @@ -44,6 +44,7 @@ class grpc_factory : public nuraft::rpc_client_factory, public std::enable_share std::string const& workerName() const { return _worker_name; } nuraft::ptr< nuraft::rpc_client > create_client(const std::string& client) override; + nuraft::ptr< nuraft::rpc_client > create_client(peer_id_t const& client); virtual std::error_condition create_client(peer_id_t const& client, nuraft::ptr< nuraft::rpc_client >&) = 0; diff --git a/src/lib/grpc_factory.cpp b/src/lib/grpc_factory.cpp index 7370d5e..a2026ee 100644 --- a/src/lib/grpc_factory.cpp +++ b/src/lib/grpc_factory.cpp @@ -130,16 +130,22 @@ class grpc_error_client : public grpc_base_client { } }; -nuraft::ptr< nuraft::rpc_client > grpc_factory::create_client(const std::string& client) { +nuraft::ptr< nuraft::rpc_client > grpc_factory::create_client(std::string const& client) { + try { + return create_client(boost::uuids::string_generator()(client)); + } catch (std::runtime_error const& e) { LOGCRITICAL("Client Endpoint Invalid! [{}]", client); } + return nullptr; +} + +nuraft::ptr< nuraft::rpc_client > grpc_factory::create_client(peer_id_t const& client) { nuraft::ptr< nuraft::rpc_client > new_client; - auto client_uuid = boost::uuids::string_generator()(client); std::unique_lock< client_factory_lock_type > lk(_client_lock); - auto [it, happened] = _clients.emplace(client_uuid, nullptr); + auto [it, happened] = _clients.emplace(client, nullptr); if (_clients.end() != it) { if (!happened) { LOGDEBUGMOD(nuraft_mesg, "Re-creating client for {}", client); - if (auto err = reinit_client(client_uuid, it->second); err) { + if (auto err = reinit_client(client, it->second); err) { LOGERROR("Failed to re-initialize client {}: {}", client, err.message()); new_client = std::make_shared< grpc_error_client >(); } else { @@ -147,7 +153,7 @@ nuraft::ptr< nuraft::rpc_client > grpc_factory::create_client(const std::string& } } else { LOGDEBUGMOD(nuraft_mesg, "Creating client for {}", client); - if (auto err = create_client(client_uuid, it->second); err) { + if (auto err = create_client(client, it->second); err) { LOGERROR("Failed to create client for {}: {}", client, err.message()); new_client = std::make_shared< grpc_error_client >(); } else { diff --git a/src/lib/logger.hpp b/src/lib/logger.hpp index d7081db..c886335 100644 --- a/src/lib/logger.hpp +++ b/src/lib/logger.hpp @@ -41,11 +41,11 @@ class nuraft_mesg_logger : public ::nuraft::logger { case 1: [[fallthrough]]; case 2: { - LOGERRORMOD_USING_LOGGER(nuraft, _custom_logger, "ERROR {}", mesg); + LOGERRORMOD(nuraft, "{}", mesg); } break; ; case 3: { - LOGWARNMOD_USING_LOGGER(nuraft, _custom_logger, "WARNING {}", mesg); + LOGWARNMOD(nuraft, "{}", mesg); } break; ; case 4: { diff --git a/test_package/CMakeLists.txt b/test_package/CMakeLists.txt index 0b155b5..f41a8ac 100644 --- a/test_package/CMakeLists.txt +++ b/test_package/CMakeLists.txt @@ -1,29 +1,16 @@ +cmake_minimum_required(VERSION 3.11) project(test_package) -cmake_minimum_required(VERSION 2.8.11) include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake) -conan_basic_setup() +conan_basic_setup(TARGETS) -if (${CMAKE_BUILD_TYPE} STREQUAL Debug) - if (NOT ${CONAN_SETTINGS_COMPILER} STREQUAL "clang" AND NOT ${CONAN_SETTINGS_COMPILER} STREQUAL "apple-clang") - include (../cmake/debug_flags.cmake) - endif() -endif() -if (${MEMORY_SANITIZER_ON}) - include (../cmake/mem_sanitizer.cmake) -endif () +find_package(nuraft_mesg QUIET REQUIRED) -set(CMAKE_CXX_STANDARD 17) +set(CMAKE_CXX_STANDARD 20) set(CPP_WARNINGS "-Wall -Wextra -Werror") -find_program(CCACHE_FOUND ccache) -if (CCACHE_FOUND) - set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE ccache) - set_property(GLOBAL PROPERTY RULE_LAUNCH_LINK ccache) -endif () +add_executable(example_server example_server.cpp example_state_manager.cpp in_memory_log_store.cpp) +target_link_libraries(example_server nuraft_mesg::nuraft_mesg) -add_executable(raft_server example_server.cpp example_state_manager.cpp in_memory_log_store.cpp) -add_executable(raft_client example_client.cpp) - -conan_target_link_libraries(raft_server) -conan_target_link_libraries(raft_client) +add_executable(example_client example_client.cpp) +target_link_libraries(example_client nuraft_mesg::nuraft_mesg) diff --git a/test_package/conanfile.py b/test_package/conanfile.py index 5f31704..ce34dc6 100644 --- a/test_package/conanfile.py +++ b/test_package/conanfile.py @@ -1,29 +1,20 @@ -from conans import ConanFile, CMake, tools, RunEnvironment +from conans import ConanFile +from conan.tools.build import cross_building +from conans import CMake import os class TestPackageConan(ConanFile): settings = "os", "compiler", "build_type", "arch" generators = "cmake", "cmake_find_package" - fail_timeout = '30s' - abort_timeout = '35s' - def build(self): cmake = CMake(self) cmake.configure(defs={'CONAN_CMAKE_SILENT_OUTPUT': 'ON'}) cmake.build() def test(self): - with tools.environment_append(RunEnvironment(self).vars): - # TODO: Temporarily restricting tests to run for one build_type only, since running multiple - # at the same time cause the tests and builds to fail - if self.settings.build_type == 'Debug': - self.run("echo $(pwd)") - bin_path = os.path.join("../../", "run_tests.sh") - if self.settings.os == "Windows": - self.run(bin_path) - elif self.settings.os == "Macos": - self.run("DYLD_LIBRARY_PATH=%s %s" % (os.environ.get('DYLD_LIBRARY_PATH', ''), bin_path)) - else: - bin_path = "timeout -k {} {} {}".format(self.abort_timeout, self.fail_timeout, bin_path) - self.run("LD_LIBRARY_PATH=%s %s" % (os.environ.get('LD_LIBRARY_PATH', ''), bin_path)) + if not cross_building(self): + sbin_path = os.path.join("bin", "example_server") + cbin_path = os.path.join("bin", "example_client") + self.run(sbin_path, run_environment=True) + self.run(cbin_path, run_environment=True) diff --git a/test_package/example_client.cpp b/test_package/example_client.cpp index 83bde8a..ad0f397 100644 --- a/test_package/example_client.cpp +++ b/test_package/example_client.cpp @@ -12,7 +12,7 @@ SISL_OPTION_GROUP(client, (add, "a", "add", "Add a server to the cluster", cxxopts::value< uint32_t >(), "id"), (clean, "", "clean", "Reset all persistence", cxxopts::value< bool >(), ""), - (group, "g", "group", "Group ID", cxxopts::value< uint32_t >(), ""), + (group, "g", "group", "Group ID", cxxopts::value< uint32_t >()->default_value("0"), ""), (server, "", "server", "Server to send message to", cxxopts::value< uint32_t >()->default_value("0"), "id"), (echo, "m", "echo", "Send message to echo service", cxxopts::value< std::string >(), "message"), diff --git a/test_package/example_server.cpp b/test_package/example_server.cpp index 6888497..31b97ca 100644 --- a/test_package/example_server.cpp +++ b/test_package/example_server.cpp @@ -12,7 +12,9 @@ #include "example_state_manager.h" #include "uuids.h" -SISL_OPTION_GROUP(server, (server_id, "", "server_id", "Servers ID (0-9)", cxxopts::value< uint32_t >(), ""), +SISL_OPTION_GROUP(server, + (server_id, "", "server_id", "Servers ID (0-9)", cxxopts::value< uint32_t >()->default_value("0"), + ""), (start_group, "", "create", "Group to create", cxxopts::value< uint32_t >(), "")) SISL_OPTIONS_ENABLE(logging, server, nuraft_mesg) @@ -124,10 +126,10 @@ int main(int argc, char** argv) { } // Just prevent main() from exiting, require a SIGNAL - { - std::unique_lock< std::mutex > ulock(k_stop_cv_lock); - k_stop_cv.wait(ulock, []() { return k_stop; }); - } - LOGERROR("Stopping Service!"); + //{ + // std::unique_lock< std::mutex > ulock(k_stop_cv_lock); + // k_stop_cv.wait(ulock, []() { return k_stop; }); + //} + LOGWARN("Stopping Service!"); return 0; }