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 LogSilencer::LogSilencer() : LogSilencer(LogContext::Instance())
SilenceLog(std::function<void(void)> func) {
}
LogSilencer::LogSilencer(LogContext& ctx) : stream(std::move(ctx.logStream))
{
}
LogSilencer::~LogSilencer()
{ {
auto& log = LogContext::Instance(); LogContext::Instance().logStream = std::move(stream);
ILogStream_ptr oldStream = std::move(log.logStream);
log.logStream = nullptr;
func();
log.logStream = std::move(oldStream);
} }
} // namespace llarp } // namespace llarp

@ -60,9 +60,17 @@ namespace llarp
std::shared_ptr<thread::ThreadPool> threadpool); std::shared_ptr<thread::ThreadPool> threadpool);
}; };
// call a function where the logging is slienced /// RAII type to turn logging off
void /// logging is suppressed as long as the silencer is in scope
SilenceLog(std::function<void(void)> func); struct LogSilencer
{
LogSilencer();
~LogSilencer();
explicit LogSilencer(LogContext& ctx);
private:
ILogStream_ptr stream;
};
void void
SetLogLevel(LogLevel lvl); SetLogLevel(LogLevel lvl);

@ -1,5 +1,7 @@
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include <util/logging/logger.hpp>
#ifdef _WIN32 #ifdef _WIN32
#include <winsock2.h> #include <winsock2.h>
int int
@ -8,7 +10,7 @@ startWinsock()
WSADATA wsockd; WSADATA wsockd;
int err; int err;
err = ::WSAStartup(MAKEWORD(2, 2), &wsockd); err = ::WSAStartup(MAKEWORD(2, 2), &wsockd);
if(err) if (err)
{ {
perror("Failed to start Windows Sockets"); perror("Failed to start Windows Sockets");
return err; return err;
@ -20,8 +22,9 @@ startWinsock()
int int
main(int argc, char** argv) main(int argc, char** argv)
{ {
llarp::LogSilencer shutup;
#ifdef _WIN32 #ifdef _WIN32
if(startWinsock()) if (startWinsock())
return -1; return -1;
#endif #endif

@ -19,45 +19,44 @@ make_context(fs::path keyfile)
TEST_CASE("key backup bug regression test", "[regress]") TEST_CASE("key backup bug regression test", "[regress]")
{ {
llarp::SilenceLog([]() { llarp::LogSilencer shutup;
for (const fs::path& path : {"regress-1.private", "regress-2.private", ""}) 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{}; auto context = make_context(path);
for (size_t index = 0; index < 10; index++) REQUIRE(llarp_main_setup(context, false) == 0);
{ auto ctx = llarp::Context::Get(context);
auto context = make_context(path); ctx->CallSafe([ctx, index, &endpointAddress, &path]() {
REQUIRE(llarp_main_setup(context, false) == 0); auto ep = ctx->router->hiddenServiceContext().GetDefault();
auto ctx = llarp::Context::Get(context); REQUIRE(ep != nullptr);
ctx->CallSafe([ctx, index, &endpointAddress, &path]() { if (index == 0)
auto ep = ctx->router->hiddenServiceContext().GetDefault(); {
REQUIRE(ep != nullptr); REQUIRE(endpointAddress.IsZero());
if (index == 0) // 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()); // we want the keys to shift
// first iteration, we are getting our identity REQUIRE(endpointAddress != ep->GetIdentity().pub.Addr());
endpointAddress = ep->GetIdentity().pub.Addr();
REQUIRE(not endpointAddress.IsZero());
} }
else else
{ {
REQUIRE(not endpointAddress.IsZero()); // after the first iteration we expect the keys to stay the same
if (path.empty()) REQUIRE(endpointAddress == ep->GetIdentity().pub.Addr());
{
// 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());
}
} }
ctx->CloseAsync(); }
}); ctx->CloseAsync();
REQUIRE(llarp_main_run(context, llarp_main_runtime_opts{}) == 0); });
llarp_main_free(context); REQUIRE(llarp_main_run(context, llarp_main_runtime_opts{}) == 0);
} llarp_main_free(context);
fs::remove(path);
} }
}); fs::remove(path);
}
} }

Loading…
Cancel
Save