use llarp::LogSilencer to shut up loging in unit tests

pull/1301/head
Jeff Becker 4 years ago
parent a73335579a
commit 3d2990f90d
No known key found for this signature in database
GPG Key ID: F357B3B42F6F9B05

@ -174,14 +174,17 @@ namespace llarp
}
}
void
SilenceLog(std::function<void(void)> 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

@ -60,9 +60,17 @@ namespace llarp
std::shared_ptr<thread::ThreadPool> threadpool);
};
// call a function where the logging is slienced
void
SilenceLog(std::function<void(void)> 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);

@ -1,5 +1,7 @@
#include <gtest/gtest.h>
#include <util/logging/logger.hpp>
#ifdef _WIN32
#include <winsock2.h>
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

@ -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);
}
}

Loading…
Cancel
Save