From 3d2990f90d040b2535c0523dbc30b2956f7d74d0 Mon Sep 17 00:00:00 2001 From: Jeff Becker Date: Mon, 8 Jun 2020 09:26:53 -0400 Subject: [PATCH] use llarp::LogSilencer to shut up loging in unit tests --- llarp/util/logging/logger.cpp | 17 +++--- llarp/util/logging/logger.hpp | 14 ++++- test/main.cpp | 7 ++- test/regress/2020-06-08-key-backup-bug.cpp | 67 +++++++++++----------- 4 files changed, 59 insertions(+), 46 deletions(-) diff --git a/llarp/util/logging/logger.cpp b/llarp/util/logging/logger.cpp index 00daa34b3..ab00650ae 100644 --- a/llarp/util/logging/logger.cpp +++ b/llarp/util/logging/logger.cpp @@ -174,14 +174,17 @@ namespace llarp } } - void - SilenceLog(std::function func) + LogSilencer::LogSilencer() : LogSilencer(LogContext::Instance()) + { + } + + LogSilencer::LogSilencer(LogContext& ctx) : stream(std::move(ctx.logStream)) + { + } + + LogSilencer::~LogSilencer() { - auto& log = LogContext::Instance(); - ILogStream_ptr oldStream = std::move(log.logStream); - log.logStream = nullptr; - func(); - log.logStream = std::move(oldStream); + LogContext::Instance().logStream = std::move(stream); } } // namespace llarp diff --git a/llarp/util/logging/logger.hpp b/llarp/util/logging/logger.hpp index a01b54dbb..1b51e6b3c 100644 --- a/llarp/util/logging/logger.hpp +++ b/llarp/util/logging/logger.hpp @@ -60,9 +60,17 @@ namespace llarp std::shared_ptr threadpool); }; - // call a function where the logging is slienced - void - SilenceLog(std::function func); + /// RAII type to turn logging off + /// logging is suppressed as long as the silencer is in scope + struct LogSilencer + { + LogSilencer(); + ~LogSilencer(); + explicit LogSilencer(LogContext& ctx); + + private: + ILogStream_ptr stream; + }; void SetLogLevel(LogLevel lvl); diff --git a/test/main.cpp b/test/main.cpp index 3bef87c66..cb2449a6b 100644 --- a/test/main.cpp +++ b/test/main.cpp @@ -1,5 +1,7 @@ #include +#include + #ifdef _WIN32 #include int @@ -8,7 +10,7 @@ startWinsock() WSADATA wsockd; int err; err = ::WSAStartup(MAKEWORD(2, 2), &wsockd); - if(err) + if (err) { perror("Failed to start Windows Sockets"); return err; @@ -20,8 +22,9 @@ startWinsock() int main(int argc, char** argv) { + llarp::LogSilencer shutup; #ifdef _WIN32 - if(startWinsock()) + if (startWinsock()) return -1; #endif diff --git a/test/regress/2020-06-08-key-backup-bug.cpp b/test/regress/2020-06-08-key-backup-bug.cpp index 7763506be..396243801 100644 --- a/test/regress/2020-06-08-key-backup-bug.cpp +++ b/test/regress/2020-06-08-key-backup-bug.cpp @@ -19,45 +19,44 @@ make_context(fs::path keyfile) TEST_CASE("key backup bug regression test", "[regress]") { - llarp::SilenceLog([]() { - for (const fs::path& path : {"regress-1.private", "regress-2.private", ""}) + llarp::LogSilencer shutup; + for (const fs::path& path : {"regress-1.private", "regress-2.private", ""}) + { + llarp::service::Address endpointAddress{}; + for (size_t index = 0; index < 10; index++) { - llarp::service::Address endpointAddress{}; - for (size_t index = 0; index < 10; index++) - { - auto context = make_context(path); - REQUIRE(llarp_main_setup(context, false) == 0); - auto ctx = llarp::Context::Get(context); - ctx->CallSafe([ctx, index, &endpointAddress, &path]() { - auto ep = ctx->router->hiddenServiceContext().GetDefault(); - REQUIRE(ep != nullptr); - if (index == 0) + auto context = make_context(path); + REQUIRE(llarp_main_setup(context, false) == 0); + auto ctx = llarp::Context::Get(context); + ctx->CallSafe([ctx, index, &endpointAddress, &path]() { + auto ep = ctx->router->hiddenServiceContext().GetDefault(); + REQUIRE(ep != nullptr); + if (index == 0) + { + REQUIRE(endpointAddress.IsZero()); + // first iteration, we are getting our identity + endpointAddress = ep->GetIdentity().pub.Addr(); + REQUIRE(not endpointAddress.IsZero()); + } + else + { + REQUIRE(not endpointAddress.IsZero()); + if (path.empty()) { - REQUIRE(endpointAddress.IsZero()); - // first iteration, we are getting our identity - endpointAddress = ep->GetIdentity().pub.Addr(); - REQUIRE(not endpointAddress.IsZero()); + // we want the keys to shift + REQUIRE(endpointAddress != ep->GetIdentity().pub.Addr()); } else { - REQUIRE(not endpointAddress.IsZero()); - if (path.empty()) - { - // we want the keys to shift - REQUIRE(endpointAddress != ep->GetIdentity().pub.Addr()); - } - else - { - // after the first iteration we expect the keys to stay the same - REQUIRE(endpointAddress == ep->GetIdentity().pub.Addr()); - } + // after the first iteration we expect the keys to stay the same + REQUIRE(endpointAddress == ep->GetIdentity().pub.Addr()); } - ctx->CloseAsync(); - }); - REQUIRE(llarp_main_run(context, llarp_main_runtime_opts{}) == 0); - llarp_main_free(context); - } - fs::remove(path); + } + ctx->CloseAsync(); + }); + REQUIRE(llarp_main_run(context, llarp_main_runtime_opts{}) == 0); + llarp_main_free(context); } - }); + fs::remove(path); + } }