From 3b3c262d0ea53f8d0ffd2d11c8d910ccd0a51e50 Mon Sep 17 00:00:00 2001 From: Stephane Janel Date: Sat, 22 Apr 2023 17:51:27 +0200 Subject: [PATCH] Clean-up code related to settings::RunMode. Also add unit tests of LoggingInfo and ProcessCommandsFromCLI --- src/api-objects/include/apikeysprovider.hpp | 5 +- src/api-objects/src/apikeysprovider.cpp | 6 ++- src/api/common/include/cryptowatchapi.hpp | 6 +-- src/api/common/test/cryptowatchapi_test.cpp | 10 ++-- .../common/test/exchangeprivateapi_test.cpp | 4 +- .../common/test/exchangepublicapi_test.cpp | 5 +- src/api/exchanges/test/commonapi_test.hpp | 5 +- src/engine/include/coincentercommands.hpp | 6 +++ src/engine/src/coincentercommands.cpp | 4 ++ src/engine/test/exchangedata_test.hpp | 5 +- .../include/proxy.hpp} | 0 src/iotools/src/curlhandle.cpp | 2 +- src/iotools/test/curlhandle_test.cpp | 2 +- src/main/CMakeLists.txt | 8 ++++ src/main/src/main.cpp | 6 +-- src/main/src/processcommandsfromcli.cpp | 20 ++++---- src/main/src/processcommandsfromcli.hpp | 3 +- src/main/test/processcommandsfromcli_test.cpp | 26 ++++++++++ src/objects/CMakeLists.txt | 7 +++ src/objects/include/coincenterinfo.hpp | 3 +- src/objects/include/logginginfo.hpp | 2 +- src/objects/src/logginginfo.cpp | 13 +---- src/objects/test/coincenterinfo_test.cpp | 2 +- src/objects/test/fiatconverter_test.cpp | 10 ++-- src/objects/test/logginginfo_test.cpp | 48 +++++++++++++++++++ src/tech/include/runmodes.hpp | 31 ++++++++++-- 26 files changed, 174 insertions(+), 65 deletions(-) rename src/{tech/include/cct_proxy.hpp => iotools/include/proxy.hpp} (100%) create mode 100644 src/main/test/processcommandsfromcli_test.cpp create mode 100644 src/objects/test/logginginfo_test.cpp diff --git a/src/api-objects/include/apikeysprovider.hpp b/src/api-objects/include/apikeysprovider.hpp index e76083f4..ba4da3b3 100644 --- a/src/api-objects/include/apikeysprovider.hpp +++ b/src/api-objects/include/apikeysprovider.hpp @@ -18,11 +18,10 @@ class APIKeysProvider { public: using KeyNames = SmallVector; - explicit APIKeysProvider(std::string_view dataDir, settings::RunMode runMode = settings::RunMode::kProd) + APIKeysProvider(std::string_view dataDir, settings::RunMode runMode) : APIKeysProvider(dataDir, ExchangeSecretsInfo(), runMode) {} - APIKeysProvider(std::string_view dataDir, const ExchangeSecretsInfo &exchangeSecretsInfo, - settings::RunMode runMode = settings::RunMode::kProd); + APIKeysProvider(std::string_view dataDir, const ExchangeSecretsInfo &exchangeSecretsInfo, settings::RunMode runMode); KeyNames getKeyNames(std::string_view platform) const; diff --git a/src/api-objects/src/apikeysprovider.cpp b/src/api-objects/src/apikeysprovider.cpp index bb8901fe..faadcee6 100644 --- a/src/api-objects/src/apikeysprovider.cpp +++ b/src/api-objects/src/apikeysprovider.cpp @@ -12,7 +12,9 @@ namespace { std::string_view GetSecretFileName(settings::RunMode runMode) { switch (runMode) { - case settings::RunMode::kTest: + case settings::RunMode::kTestKeys: + [[fallthrough]]; + case settings::RunMode::kTestKeysWithProxy: log::info("Test mode activated, shifting to secret_test.json file."); return "secret_test.json"; default: @@ -73,7 +75,7 @@ APIKeysProvider::APIKeysMap APIKeysProvider::ParseAPIKeys(std::string_view dataD } else { std::string_view secretFileName = GetSecretFileName(runMode); File secretsFile(dataDir, File::Type::kSecret, secretFileName, - runMode == settings::RunMode::kProd ? File::IfError::kNoThrow : File::IfError::kThrow); + AreTestKeysRequested(runMode) ? File::IfError::kThrow : File::IfError::kNoThrow); json jsonData = secretsFile.readAllJson(); for (auto& [publicExchangeName, keyObj] : jsonData.items()) { const auto& exchangesWithoutSecrets = exchangeSecretsInfo.exchangesWithoutSecrets(); diff --git a/src/api/common/include/cryptowatchapi.hpp b/src/api/common/include/cryptowatchapi.hpp index d24e1794..3b20b5b1 100644 --- a/src/api/common/include/cryptowatchapi.hpp +++ b/src/api/common/include/cryptowatchapi.hpp @@ -22,8 +22,8 @@ class CryptowatchAPI : public ExchangeBase { public: using Fiats = FlatSet; - explicit CryptowatchAPI(const CoincenterInfo &config, settings::RunMode runMode = settings::RunMode::kProd, - Duration fiatsUpdateFrequency = std::chrono::hours(96), bool loadFromFileCacheAtInit = true); + CryptowatchAPI(const CoincenterInfo &config, settings::RunMode runMode, + Duration fiatsUpdateFrequency = std::chrono::hours(96), bool loadFromFileCacheAtInit = true); /// Tells whether given exchange is supported by Cryptowatch. bool queryIsExchangeSupported(std::string_view exchangeName) { @@ -42,7 +42,7 @@ class CryptowatchAPI : public ExchangeBase { } /// Tells whether given currency code is a fiat currency or not. - /// Fiat currencies are traditionnal currencies, such as EUR, USD, GBP, KRW, etc. + /// Fiat currencies are traditional currencies, such as EUR, USD, GBP, KRW, etc. /// Information here: https://en.wikipedia.org/wiki/Fiat_money bool queryIsCurrencyCodeFiat(CurrencyCode currencyCode) { std::lock_guard guard(_fiatsMutex); diff --git a/src/api/common/test/cryptowatchapi_test.cpp b/src/api/common/test/cryptowatchapi_test.cpp index 9291cdfe..624b1575 100644 --- a/src/api/common/test/cryptowatchapi_test.cpp +++ b/src/api/common/test/cryptowatchapi_test.cpp @@ -9,13 +9,9 @@ namespace cct::api { class CryptowatchAPITest : public ::testing::Test { protected: - CryptowatchAPITest() : cryptowatchAPI(config) {} - - void SetUp() override {} - void TearDown() override {} - - CoincenterInfo config; - CryptowatchAPI cryptowatchAPI; + settings::RunMode runMode = settings::RunMode::kTestKeys; + CoincenterInfo config{runMode}; + CryptowatchAPI cryptowatchAPI{config, runMode}; }; TEST_F(CryptowatchAPITest, Prices) { diff --git a/src/api/common/test/exchangeprivateapi_test.cpp b/src/api/common/test/exchangeprivateapi_test.cpp index 8cf0d91b..f823a9d6 100644 --- a/src/api/common/test/exchangeprivateapi_test.cpp +++ b/src/api/common/test/exchangeprivateapi_test.cpp @@ -47,8 +47,8 @@ class ExchangePrivateTest : public ::testing::Test { } LoadConfiguration loadConfiguration{kDefaultDataDir, LoadConfiguration::ExchangeConfigFileType::kTest}; - CoincenterInfo coincenterInfo{settings::RunMode::kProd, loadConfiguration}; - CryptowatchAPI cryptowatchAPI{coincenterInfo, settings::RunMode::kProd, Duration::max(), true}; + CoincenterInfo coincenterInfo{settings::RunMode::kTestKeys, loadConfiguration}; + CryptowatchAPI cryptowatchAPI{coincenterInfo, settings::RunMode::kTestKeys, Duration::max(), true}; FiatConverter fiatConverter{coincenterInfo, Duration::max()}; // max to avoid real Fiat converter queries MockExchangePublic exchangePublic{kSupportedExchanges[0], fiatConverter, cryptowatchAPI, coincenterInfo}; diff --git a/src/api/common/test/exchangepublicapi_test.cpp b/src/api/common/test/exchangepublicapi_test.cpp index 6c7bf7f5..20c71543 100644 --- a/src/api/common/test/exchangepublicapi_test.cpp +++ b/src/api/common/test/exchangepublicapi_test.cpp @@ -17,9 +17,10 @@ class ExchangePublicTest : public ::testing::Test { void SetUp() override {} void TearDown() override {} + settings::RunMode runMode = settings::RunMode::kTestKeys; LoadConfiguration loadConfiguration{kDefaultDataDir, LoadConfiguration::ExchangeConfigFileType::kTest}; - CoincenterInfo coincenterInfo{settings::RunMode::kProd, loadConfiguration}; - CryptowatchAPI cryptowatchAPI{coincenterInfo}; + CoincenterInfo coincenterInfo{runMode, loadConfiguration}; + CryptowatchAPI cryptowatchAPI{coincenterInfo, runMode}; FiatConverter fiatConverter{coincenterInfo, Duration::max()}; // max to avoid real Fiat converter queries MockExchangePublic exchangePublic{kSupportedExchanges[0], fiatConverter, cryptowatchAPI, coincenterInfo}; }; diff --git a/src/api/exchanges/test/commonapi_test.hpp b/src/api/exchanges/test/commonapi_test.hpp index e3e3a5e4..3bdf9fb1 100644 --- a/src/api/exchanges/test/commonapi_test.hpp +++ b/src/api/exchanges/test/commonapi_test.hpp @@ -46,11 +46,12 @@ class TestAPI { return sample; } + settings::RunMode runMode = settings::RunMode::kProd; LoadConfiguration loadConfig{kDefaultDataDir, LoadConfiguration::ExchangeConfigFileType::kTest}; - CoincenterInfo coincenterInfo{settings::RunMode::kProd, loadConfig}; + CoincenterInfo coincenterInfo{runMode, loadConfig}; APIKeysProvider apiKeysProvider{coincenterInfo.dataDir(), coincenterInfo.getRunMode()}; FiatConverter fiatConverter{coincenterInfo, Duration::max()}; // max to avoid real Fiat converter queries - CryptowatchAPI cryptowatchAPI{coincenterInfo}; + CryptowatchAPI cryptowatchAPI{coincenterInfo, runMode}; PublicExchangeT exchangePublic{coincenterInfo, fiatConverter, cryptowatchAPI}; std::unique_ptr exchangePrivatePtr{ CreatePrivateExchangeIfKeyPresent(exchangePublic, coincenterInfo, apiKeysProvider)}; diff --git a/src/engine/include/coincentercommands.hpp b/src/engine/include/coincentercommands.hpp index 4b1d9aef..01fe6549 100644 --- a/src/engine/include/coincentercommands.hpp +++ b/src/engine/include/coincentercommands.hpp @@ -13,13 +13,19 @@ namespace cct { class CoincenterCommands { public: + // Builds a CoincenterCommands without any commands. CoincenterCommands() noexcept(std::is_nothrow_default_constructible_v) = default; + // Builds a CoincenterCommands and add commands from given command line options. + explicit CoincenterCommands(const CoincenterCmdLineOptions &cmdLineOptions); + static CoincenterCmdLineOptions ParseOptions(int argc, const char *argv[]); static MonitoringInfo CreateMonitoringInfo(std::string_view programName, const CoincenterCmdLineOptions &cmdLineOptions); + /// @brief Set this CoincenterCommands from given command line options. + /// @return false if only help or version is asked, true otherwise bool setFromOptions(const CoincenterCmdLineOptions &cmdLineOptions); std::span commands() const { return _commands; } diff --git a/src/engine/src/coincentercommands.cpp b/src/engine/src/coincentercommands.cpp index cc75ad56..67ec7be8 100644 --- a/src/engine/src/coincentercommands.cpp +++ b/src/engine/src/coincentercommands.cpp @@ -55,6 +55,10 @@ std::pair ParseOrderRequest(const CoincenterCm } } // namespace +CoincenterCommands::CoincenterCommands(const CoincenterCmdLineOptions &cmdLineOptions) { + setFromOptions(cmdLineOptions); +} + bool CoincenterCommands::setFromOptions(const CoincenterCmdLineOptions &cmdLineOptions) { if (cmdLineOptions.help || cmdLineOptions.version) { return false; diff --git a/src/engine/test/exchangedata_test.hpp b/src/engine/test/exchangedata_test.hpp index b3c0abb1..5c0ad926 100644 --- a/src/engine/test/exchangedata_test.hpp +++ b/src/engine/test/exchangedata_test.hpp @@ -23,8 +23,9 @@ class ExchangesBaseTest : public ::testing::Test { void TearDown() override {} LoadConfiguration loadConfiguration{kDefaultDataDir, LoadConfiguration::ExchangeConfigFileType::kTest}; - CoincenterInfo coincenterInfo{settings::RunMode::kProd, loadConfiguration}; - api::CryptowatchAPI cryptowatchAPI{coincenterInfo, settings::RunMode::kProd, Duration::max(), true}; + settings::RunMode runMode = settings::RunMode::kTestKeys; + CoincenterInfo coincenterInfo{runMode, loadConfiguration}; + api::CryptowatchAPI cryptowatchAPI{coincenterInfo, runMode, Duration::max(), true}; FiatConverter fiatConverter{coincenterInfo, Duration::max()}; // max to avoid real Fiat converter queries api::MockExchangePublic exchangePublic1{kSupportedExchanges[0], fiatConverter, cryptowatchAPI, coincenterInfo}; api::MockExchangePublic exchangePublic2{kSupportedExchanges[1], fiatConverter, cryptowatchAPI, coincenterInfo}; diff --git a/src/tech/include/cct_proxy.hpp b/src/iotools/include/proxy.hpp similarity index 100% rename from src/tech/include/cct_proxy.hpp rename to src/iotools/include/proxy.hpp diff --git a/src/iotools/src/curlhandle.cpp b/src/iotools/src/curlhandle.cpp index 4df0b7aa..ce8908ee 100644 --- a/src/iotools/src/curlhandle.cpp +++ b/src/iotools/src/curlhandle.cpp @@ -12,9 +12,9 @@ #include "abstractmetricgateway.hpp" #include "cct_exception.hpp" #include "cct_log.hpp" -#include "cct_proxy.hpp" #include "curlmetrics.hpp" #include "curloptions.hpp" +#include "proxy.hpp" #include "stringhelpers.hpp" namespace cct { diff --git a/src/iotools/test/curlhandle_test.cpp b/src/iotools/test/curlhandle_test.cpp index c95c8471..455c0318 100644 --- a/src/iotools/test/curlhandle_test.cpp +++ b/src/iotools/test/curlhandle_test.cpp @@ -2,8 +2,8 @@ #include -#include "cct_proxy.hpp" #include "curloptions.hpp" +#include "proxy.hpp" /* URL available to test HTTPS, cf * https://support.nmi.com/hc/en-gb/articles/360021544791-How-to-Check-If-the-Correct-Certificates-Are-Installed-on-Linux diff --git a/src/main/CMakeLists.txt b/src/main/CMakeLists.txt index 437a4ec0..d97cde5b 100644 --- a/src/main/CMakeLists.txt +++ b/src/main/CMakeLists.txt @@ -19,4 +19,12 @@ set_target_properties(coincenter PROPERTIES VERSION ${PROJECT_VERSION} COMPILE_DEFINITIONS_DEBUG "JSON_DEBUG;JSON_SAFE;JSON_ISO_STRICT" RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR} +) + +add_unit_test( + processcommandsfromcli_test + src/processcommandsfromcli.cpp + test/processcommandsfromcli_test.cpp + LIBRARIES + coincenter_engine ) \ No newline at end of file diff --git a/src/main/src/main.cpp b/src/main/src/main.cpp index 9290efa6..8d9776ff 100644 --- a/src/main/src/main.cpp +++ b/src/main/src/main.cpp @@ -11,11 +11,11 @@ int main(int argc, const char* argv[]) { try { auto cmdLineOptions = cct::CoincenterCommands::ParseOptions(argc, argv); - cct::CoincenterCommands coincenterCommands; - if (coincenterCommands.setFromOptions(cmdLineOptions)) { + if (!cmdLineOptions.help && !cmdLineOptions.version) { + cct::CoincenterCommands coincenterCommands(cmdLineOptions); auto programName = std::filesystem::path(argv[0]).filename().string(); - cct::ProcessCommandsFromCLI(programName, coincenterCommands, cmdLineOptions); + cct::ProcessCommandsFromCLI(programName, coincenterCommands, cmdLineOptions, cct::settings::RunMode::kProd); } } catch (const cct::invalid_argument& e) { cct::log::critical("Invalid argument: {}", e.what()); diff --git a/src/main/src/processcommandsfromcli.cpp b/src/main/src/processcommandsfromcli.cpp index 6f3a9a59..480ecf1f 100644 --- a/src/main/src/processcommandsfromcli.cpp +++ b/src/main/src/processcommandsfromcli.cpp @@ -32,17 +32,17 @@ json LoadGeneralConfigAndOverrideOptionsFromCLI(const CoincenterCmdLineOptions & } // namespace void ProcessCommandsFromCLI(std::string_view programName, const CoincenterCommands &coincenterCommands, - const CoincenterCmdLineOptions &cmdLineOptions) { + const CoincenterCmdLineOptions &cmdLineOptions, settings::RunMode runMode) { json generalConfigData = LoadGeneralConfigAndOverrideOptionsFromCLI(cmdLineOptions); + Duration fiatConversionQueryRate = ParseDuration(generalConfigData["fiatConversion"]["rate"].get()); + ApiOutputType apiOutputType = ApiOutputTypeFromString(generalConfigData["apiOutputType"].get()); + // Create LoggingInfo first as it is a RAII structure re-initializing spdlog loggers. // It will be held by GeneralConfig and then itself by CoincenterInfo though. LoggingInfo loggingInfo(static_cast(generalConfigData["log"])); - Duration fiatConversionQueryRate = ParseDuration(generalConfigData["fiatConversion"]["rate"].get()); - - GeneralConfig generalConfig(std::move(loggingInfo), fiatConversionQueryRate, - ApiOutputTypeFromString(generalConfigData["apiOutputType"].get())); + GeneralConfig generalConfig(std::move(loggingInfo), fiatConversionQueryRate, apiOutputType); LoadConfiguration loadConfiguration(cmdLineOptions.dataDir, LoadConfiguration::ExchangeConfigFileType::kProd); @@ -55,7 +55,7 @@ void ProcessCommandsFromCLI(std::string_view programName, const CoincenterComman File::IfError::kThrow); // Should be outside the try / catch as it holds the RAII object managing the Logging (LoggingInfo) - CoincenterInfo coincenterInfo(settings::RunMode::kProd, loadConfiguration, std::move(generalConfig), + CoincenterInfo coincenterInfo(runMode, loadConfiguration, std::move(generalConfig), CoincenterCommands::CreateMonitoringInfo(programName, cmdLineOptions), currencyAcronymsTranslatorFile, stableCoinsFile, currencyPrefixesTranslatorFile); @@ -73,10 +73,12 @@ void ProcessCommandsFromCLI(std::string_view programName, const CoincenterComman int nbCommandsProcessed = coincenter.process(coincenterCommands); - // Write potentially updated cache data on disk at end of program - coincenter.updateFileCaches(); + if (nbCommandsProcessed > 0) { + // Write potentially updated cache data on disk at end of program + coincenter.updateFileCaches(); + } - log::debug("normal termination after {} command(s) processed", nbCommandsProcessed); + log::info("normal termination after {} command(s) processed", nbCommandsProcessed); } catch (const exception &e) { // Log exception here as LoggingInfo is still configured at this point (will be destroyed immediately afterwards) log::critical("Exception: {}", e.what()); diff --git a/src/main/src/processcommandsfromcli.hpp b/src/main/src/processcommandsfromcli.hpp index f4859b87..b58992c1 100644 --- a/src/main/src/processcommandsfromcli.hpp +++ b/src/main/src/processcommandsfromcli.hpp @@ -4,8 +4,9 @@ #include "coincentercommands.hpp" #include "coincenteroptions.hpp" +#include "runmodes.hpp" namespace cct { void ProcessCommandsFromCLI(std::string_view programName, const CoincenterCommands &coincenterCommands, - const CoincenterCmdLineOptions &cmdLineOptions); + const CoincenterCmdLineOptions &cmdLineOptions, settings::RunMode runMode); } \ No newline at end of file diff --git a/src/main/test/processcommandsfromcli_test.cpp b/src/main/test/processcommandsfromcli_test.cpp new file mode 100644 index 00000000..4a3240c1 --- /dev/null +++ b/src/main/test/processcommandsfromcli_test.cpp @@ -0,0 +1,26 @@ +#include "processcommandsfromcli.hpp" + +#include + +namespace cct { +namespace { +constexpr settings::RunMode kRunMode = settings::RunMode::kTestKeys; +constexpr std::string_view kProgramName = "coincenter"; +} // namespace + +TEST(ProcessCommandsFromCLI, TestNoArguments) { + CoincenterCmdLineOptions cmdLineOptions; + CoincenterCommands coincenterCommands{cmdLineOptions}; + + EXPECT_NO_THROW(ProcessCommandsFromCLI(kProgramName, coincenterCommands, cmdLineOptions, kRunMode)); +} + +TEST(ProcessCommandsFromCLI, TestIncorrectArgument) { + CoincenterCmdLineOptions cmdLineOptions; + cmdLineOptions.apiOutputType = "invalid"; + CoincenterCommands coincenterCommands{cmdLineOptions}; + + EXPECT_THROW(ProcessCommandsFromCLI(kProgramName, coincenterCommands, cmdLineOptions, kRunMode), invalid_argument); +} + +} // namespace cct \ No newline at end of file diff --git a/src/objects/CMakeLists.txt b/src/objects/CMakeLists.txt index 6995faf4..28b69315 100644 --- a/src/objects/CMakeLists.txt +++ b/src/objects/CMakeLists.txt @@ -60,6 +60,13 @@ add_unit_test( CCT_DISABLE_SPDLOG ) +add_unit_test( + logginginfo_test + test/logginginfo_test.cpp + LIBRARIES + coincenter_objects +) + add_unit_test( marketorderbook_test test/marketorderbook_test.cpp diff --git a/src/objects/include/coincenterinfo.hpp b/src/objects/include/coincenterinfo.hpp index b20045c2..8f47161a 100644 --- a/src/objects/include/coincenterinfo.hpp +++ b/src/objects/include/coincenterinfo.hpp @@ -28,8 +28,7 @@ class CoincenterInfo { using CurrencyPrefixAcronymMap = std::map>; using StableCoinsMap = std::unordered_map; - explicit CoincenterInfo(settings::RunMode runMode = settings::RunMode::kProd, - const LoadConfiguration &loadConfiguration = LoadConfiguration(), + explicit CoincenterInfo(settings::RunMode runMode, const LoadConfiguration &loadConfiguration = LoadConfiguration(), GeneralConfig &&generalConfig = GeneralConfig(), MonitoringInfo &&monitoringInfo = MonitoringInfo(), const Reader ¤cyAcronymsReader = Reader(), const Reader &stableCoinsReader = Reader(), diff --git a/src/objects/include/logginginfo.hpp b/src/objects/include/logginginfo.hpp index 977342f9..ebd58cc6 100644 --- a/src/objects/include/logginginfo.hpp +++ b/src/objects/include/logginginfo.hpp @@ -24,7 +24,7 @@ class LoggingInfo { LoggingInfo(const LoggingInfo &) = delete; LoggingInfo(LoggingInfo &&loggingInfo) noexcept; LoggingInfo &operator=(const LoggingInfo &) = delete; - LoggingInfo &operator=(LoggingInfo &&loggingInfo) noexcept; + LoggingInfo &operator=(LoggingInfo &&loggingInfo) = delete; ~LoggingInfo(); diff --git a/src/objects/src/logginginfo.cpp b/src/objects/src/logginginfo.cpp index 9763184a..7d4b79e9 100644 --- a/src/objects/src/logginginfo.cpp +++ b/src/objects/src/logginginfo.cpp @@ -81,20 +81,9 @@ LoggingInfo::LoggingInfo(LoggingInfo &&loggingInfo) noexcept _logLevelFilePos(loggingInfo._logLevelFilePos), _destroyLoggers(std::exchange(loggingInfo._destroyLoggers, false)) {} -LoggingInfo &LoggingInfo::operator=(LoggingInfo &&loggingInfo) noexcept { - if (&loggingInfo != this) { - _maxFileSizeInBytes = loggingInfo._maxFileSizeInBytes; - _maxNbFiles = loggingInfo._maxNbFiles; - _logLevelConsolePos = loggingInfo._logLevelConsolePos; - _logLevelFilePos = loggingInfo._logLevelFilePos; - _destroyLoggers = std::exchange(loggingInfo._destroyLoggers, false); - } - return *this; -} - LoggingInfo::~LoggingInfo() { if (_destroyLoggers) { - log::drop_all(); + log::drop(kOutputLoggerName); } } diff --git a/src/objects/test/coincenterinfo_test.cpp b/src/objects/test/coincenterinfo_test.cpp index f8b35f8c..38ae3a6f 100644 --- a/src/objects/test/coincenterinfo_test.cpp +++ b/src/objects/test/coincenterinfo_test.cpp @@ -27,7 +27,7 @@ class CoincenterInfoTest : public ::testing::Test { MockReader currencyPrefixesReader; CoincenterInfo createCoincenterInfo() const { - return CoincenterInfo(settings::RunMode::kTest, loadConfiguration, GeneralConfig(), MonitoringInfo(), + return CoincenterInfo(settings::RunMode::kTestKeysWithProxy, loadConfiguration, GeneralConfig(), MonitoringInfo(), currencyAcronymsReader, stableCoinsReader, currencyPrefixesReader); } }; diff --git a/src/objects/test/fiatconverter_test.cpp b/src/objects/test/fiatconverter_test.cpp index ce9fc981..d139819c 100644 --- a/src/objects/test/fiatconverter_test.cpp +++ b/src/objects/test/fiatconverter_test.cpp @@ -76,13 +76,9 @@ CurlHandle::~CurlHandle() {} // NOLINT class FiatConverterTest : public ::testing::Test { protected: - FiatConverterTest() : converter(coincenterInfo, std::chrono::milliseconds(1)) {} - - void SetUp() override {} - void TearDown() override {} - - CoincenterInfo coincenterInfo; - FiatConverter converter; + settings::RunMode runMode = settings::RunMode::kTestKeys; + CoincenterInfo coincenterInfo{runMode}; + FiatConverter converter{coincenterInfo, std::chrono::milliseconds(1)}; }; TEST_F(FiatConverterTest, DirectConversion) { diff --git a/src/objects/test/logginginfo_test.cpp b/src/objects/test/logginginfo_test.cpp new file mode 100644 index 00000000..f8facca7 --- /dev/null +++ b/src/objects/test/logginginfo_test.cpp @@ -0,0 +1,48 @@ +#include "logginginfo.hpp" + +#include + +namespace cct { +TEST(LoggingInfo, DefaultConstructor) { + LoggingInfo loggingInfo; + + log::info("test"); +} + +TEST(LoggingInfo, ConstructorFromJson) { + json generalConfigJsonLogPart; + + generalConfigJsonLogPart.emplace("maxFileSize", "1Mi"); + generalConfigJsonLogPart.emplace("maxNbFiles", 42); + generalConfigJsonLogPart.emplace("console", "debug"); + generalConfigJsonLogPart.emplace("file", "trace"); + + LoggingInfo loggingInfo(generalConfigJsonLogPart); + + log::info("test"); +} + +TEST(LoggingInfo, ReentrantTest) { + { + LoggingInfo loggingInfo; + + log::info("test1"); + } + + { + LoggingInfo loggingInfo; + + log::info("test2"); + } +} + +TEST(LoggingInfo, MoveConstructor) { + LoggingInfo loggingInfo; + + log::info("test1"); + + LoggingInfo loggingInfo2(std::move(loggingInfo)); + + log::info("test2"); +} +} // namespace cct \ No newline at end of file diff --git a/src/tech/include/runmodes.hpp b/src/tech/include/runmodes.hpp index 9e51f13d..403e2d4f 100644 --- a/src/tech/include/runmodes.hpp +++ b/src/tech/include/runmodes.hpp @@ -1,14 +1,37 @@ #pragma once +#include + namespace cct { namespace settings { -enum class RunMode { +enum class RunMode : int8_t { kProd, - kProxy, // proxy : capture & match requests from proxy - kTest, // proxy + use test keys + kTestKeys, + kProxy, // proxy : capture & match requests from proxy + kTestKeysWithProxy, // proxy + use test keys }; } // namespace settings -inline bool IsProxyRequested(settings::RunMode mode) { return mode >= settings::RunMode::kProxy; } +constexpr bool IsProxyRequested(settings::RunMode runMode) { + switch (runMode) { + case settings::RunMode::kProxy: + [[fallthrough]]; + case settings::RunMode::kTestKeysWithProxy: + return true; + default: + return false; + } +} + +constexpr bool AreTestKeysRequested(settings::RunMode runMode) { + switch (runMode) { + case settings::RunMode::kTestKeys: + [[fallthrough]]; + case settings::RunMode::kTestKeysWithProxy: + return true; + default: + return false; + } +} } // namespace cct