Skip to content

Commit

Permalink
Reduce reliance on test_package
Browse files Browse the repository at this point in the history
  • Loading branch information
szmyd committed Oct 4, 2023
1 parent 54f0d75 commit 2726e88
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 52 deletions.
1 change: 1 addition & 0 deletions src/include/nuraft_mesg/grpc_factory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
16 changes: 11 additions & 5 deletions src/lib/grpc_factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,24 +130,30 @@ 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 {
new_client = it->second;
}
} 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 {
Expand Down
4 changes: 2 additions & 2 deletions src/lib/logger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
29 changes: 8 additions & 21 deletions test_package/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
25 changes: 8 additions & 17 deletions test_package/conanfile.py
Original file line number Diff line number Diff line change
@@ -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)
2 changes: 1 addition & 1 deletion test_package/example_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
14 changes: 8 additions & 6 deletions test_package/example_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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;
}

0 comments on commit 2726e88

Please sign in to comment.