test: Add assert mock to avoid death tests (#1947)

Fixes #1750
This commit is contained in:
Sergey Kuznetsov
2025-03-07 18:11:52 +00:00
committed by GitHub
parent 8a08c5e6ce
commit c57fe1e6e4
30 changed files with 411 additions and 295 deletions

67
src/util/Assert.cpp Normal file
View File

@@ -0,0 +1,67 @@
//------------------------------------------------------------------------------
/*
This file is part of clio: https://github.com/XRPLF/clio
Copyright (c) 2025, the clio developers.
Permission to use, copy, modify, and distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================
#include "util/Assert.hpp"
#include "util/log/Logger.hpp"
#include <boost/log/core/core.hpp>
#include <cstdlib>
#include <iostream>
#include <string_view>
#include <utility>
namespace util::impl {
OnAssert::ActionType OnAssert::action;
void
OnAssert::call(std::string_view message)
{
if (not OnAssert::action) {
resetAction();
}
OnAssert::action(message);
}
void
OnAssert::setAction(ActionType newAction)
{
OnAssert::action = std::move(newAction);
}
void
OnAssert::resetAction()
{
OnAssert::action = [](std::string_view m) { OnAssert::defaultAction(m); };
}
void
OnAssert::defaultAction(std::string_view message)
{
if (LogService::enabled()) {
LOG(LogService::fatal()) << message;
} else {
std::cerr << message;
}
std::exit(EXIT_FAILURE); // std::abort does not flush gcovr output and causes uncovered lines
}
} // namespace util::impl

View File

@@ -20,9 +20,11 @@
#pragma once
#include "util/SourceLocation.hpp"
#include "util/log/Logger.hpp"
#include <boost/log/core/core.hpp>
#include <functional>
#include <string_view>
#ifndef CLIO_WITHOUT_STACKTRACE
#include <boost/stacktrace.hpp>
#include <boost/stacktrace/stacktrace.hpp>
@@ -31,9 +33,30 @@
#include <fmt/format.h>
#include <cstdlib>
#include <iostream>
namespace util {
namespace util::impl {
class OnAssert {
public:
using ActionType = std::function<void(std::string_view)>;
private:
static ActionType action;
public:
static void
call(std::string_view message);
static void
setAction(ActionType newAction);
static void
resetAction();
private:
static void
defaultAction(std::string_view message);
};
/**
* @brief Assert that a condition is true
@@ -75,16 +98,12 @@ assertImpl(
fmt::format(format, std::forward<Args>(args)...)
);
#endif
if (boost::log::core::get()->get_logging_enabled()) {
LOG(LogService::fatal()) << resultMessage;
} else {
std::cerr << resultMessage;
}
std::exit(EXIT_FAILURE); // std::abort does not flush gcovr output and causes uncovered lines
OnAssert::call(resultMessage);
}
}
} // namespace util
} // namespace util::impl
#define ASSERT(condition, ...) \
util::assertImpl(CURRENT_SRC_LOCATION, #condition, static_cast<bool>(condition), __VA_ARGS__)
util::impl::assertImpl(CURRENT_SRC_LOCATION, #condition, static_cast<bool>(condition), __VA_ARGS__)

View File

@@ -2,7 +2,8 @@ add_library(clio_util)
target_sources(
clio_util
PRIVATE build/Build.cpp
PRIVATE Assert.cpp
build/Build.cpp
config/Config.cpp
CoroutineGroup.cpp
log/Logger.cpp

View File

@@ -77,7 +77,7 @@ public:
* Used to cancel the timer for scheduled operations and request the operation to be stopped as soon as possible
*/
void
abort() noexcept
abort()
{
operation_.abort();
}

View File

@@ -199,6 +199,12 @@ LogService::init(config::ClioConfigDefinition const& config)
return {};
}
bool
LogService::enabled()
{
return boost::log::core::get()->get_logging_enabled();
}
Logger::Pump
Logger::trace(SourceLocationType const& loc) const
{

View File

@@ -367,6 +367,14 @@ public:
{
return alertLog.warn(loc);
}
/**
* @brief Whether the LogService is enabled or not
*
* @return true if the LogService is enabled, false otherwise
*/
[[nodiscard]] static bool
enabled();
};
}; // namespace util