2013-07-14 01:57:26 +02:00
|
|
|
|
|
|
|
#include <gtest/gtest.h>
|
2013-08-19 07:17:04 +02:00
|
|
|
#include <iostream>
|
2013-07-14 01:57:26 +02:00
|
|
|
#include "testing_helpers.h"
|
2013-10-05 06:14:35 +02:00
|
|
|
#include "g2log.hpp"
|
|
|
|
#include "g2logworker.hpp"
|
2013-07-14 03:33:00 +02:00
|
|
|
#include "std2_make_unique.hpp"
|
2013-11-07 09:56:48 +01:00
|
|
|
#include <fstream>
|
2013-07-14 01:57:26 +02:00
|
|
|
|
|
|
|
using namespace std;
|
2013-08-19 07:17:04 +02:00
|
|
|
namespace testing_helpers {
|
|
|
|
|
2013-11-07 09:56:48 +01:00
|
|
|
std::string g_mockFatal_message = {};
|
|
|
|
int g_mockFatal_signal = -1;
|
|
|
|
bool g_mockFatalWasCalled = false;
|
|
|
|
|
|
|
|
|
|
|
|
std::string mockFatalMessage() { return g_mockFatal_message; }
|
|
|
|
int mockFatalSignal() { return g_mockFatal_signal; }
|
|
|
|
bool mockFatalWasCalled() { return g_mockFatalWasCalled; }
|
|
|
|
|
|
|
|
|
|
|
|
void mockFatalCall(g2::FatalMessage fatal_message) {
|
|
|
|
g_mockFatal_message = fatal_message.toString();
|
|
|
|
g_mockFatal_signal = fatal_message.signal_id_;
|
|
|
|
g_mockFatalWasCalled = true;
|
|
|
|
}
|
|
|
|
void clearMockFatal() {
|
|
|
|
g_mockFatal_message = {};
|
|
|
|
g_mockFatal_signal = -1;
|
|
|
|
g_mockFatalWasCalled = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-08-19 07:17:04 +02:00
|
|
|
bool removeFile(std::string path_to_file) {
|
|
|
|
return (0 == std::remove(path_to_file.c_str()));
|
|
|
|
}
|
|
|
|
|
2013-11-07 09:56:48 +01:00
|
|
|
bool verifyContent(const std::string &total_text, std::string msg_to_find) {
|
|
|
|
std::string content(total_text);
|
|
|
|
size_t location = content.find(msg_to_find);
|
|
|
|
return (location != std::string::npos);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string readFileToText(std::string filename) {
|
|
|
|
std::ifstream in;
|
|
|
|
in.open(filename.c_str(), std::ios_base::in);
|
|
|
|
if (!in.is_open()) {
|
|
|
|
return
|
|
|
|
{
|
|
|
|
}; // error just return empty string - test will 'fault'
|
|
|
|
}
|
|
|
|
std::ostringstream oss;
|
|
|
|
oss << in.rdbuf();
|
|
|
|
return oss.str();
|
|
|
|
}
|
2013-10-05 06:14:35 +02:00
|
|
|
size_t LogFileCleaner::size() {
|
2013-08-19 07:17:04 +02:00
|
|
|
return logs_to_clean_.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
LogFileCleaner::~LogFileCleaner() {
|
|
|
|
std::lock_guard<std::mutex> lock(g_mutex);
|
|
|
|
{
|
|
|
|
for (const auto& file : logs_to_clean_) {
|
|
|
|
if (!removeFile(file)) {
|
|
|
|
ADD_FAILURE() << "UNABLE to remove: " << file << std::endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
logs_to_clean_.clear();
|
|
|
|
} // mutex
|
|
|
|
}
|
|
|
|
|
|
|
|
void LogFileCleaner::addLogToClean(std::string path_to_log) {
|
|
|
|
std::lock_guard<std::mutex> lock(g_mutex);
|
|
|
|
{
|
|
|
|
if (std::find(logs_to_clean_.begin(), logs_to_clean_.end(), path_to_log.c_str()) == logs_to_clean_.end())
|
|
|
|
logs_to_clean_.push_back(path_to_log);
|
2013-07-14 03:33:00 +02:00
|
|
|
}
|
2013-08-19 07:17:04 +02:00
|
|
|
}
|
2013-10-05 06:14:35 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
2013-08-21 07:40:39 +02:00
|
|
|
ScopedLogger::ScopedLogger()
|
2013-11-02 17:01:18 +01:00
|
|
|
: _previousWorker(g2::internal::shutDownLogging())
|
2013-08-21 07:40:39 +02:00
|
|
|
, _currentWorker(g2LogWorker::createWithNoSink()) {
|
|
|
|
g2::initializeLogging(_currentWorker.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
ScopedLogger::~ScopedLogger() {
|
2013-11-02 17:01:18 +01:00
|
|
|
auto* current = g2::internal::shutDownLogging();
|
2013-10-05 06:14:35 +02:00
|
|
|
CHECK(current == _currentWorker.get());
|
|
|
|
if (nullptr != _previousWorker) {
|
|
|
|
g2::initializeLogging(_previousWorker);
|
|
|
|
}
|
2013-08-21 07:40:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
g2LogWorker* ScopedLogger::get() {
|
2013-10-05 06:14:35 +02:00
|
|
|
return _currentWorker.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
RestoreFileLogger::RestoreFileLogger(std::string directory)
|
2013-11-07 09:56:48 +01:00
|
|
|
: _scope(new ScopedLogger), _handle(_scope->get()->addSink(std2::make_unique<g2::FileSink>("UNIT_TEST_LOGGER", directory), &g2::FileSink::fileWrite))
|
2013-10-05 06:14:35 +02:00
|
|
|
{
|
|
|
|
using namespace g2;
|
2013-11-07 09:56:48 +01:00
|
|
|
internal::changeFatalInitHandlerForUnitTesting(&mockFatalCall);
|
2013-10-05 06:14:35 +02:00
|
|
|
|
|
|
|
LOG(INFO) << "Restore logger test ";
|
2013-11-07 09:56:48 +01:00
|
|
|
auto filename = _handle->call(&FileSink::fileName);
|
2013-10-05 06:14:35 +02:00
|
|
|
if (!filename.valid()) ADD_FAILURE();
|
2013-11-07 09:56:48 +01:00
|
|
|
_log_file = filename.get();
|
2013-08-21 07:40:39 +02:00
|
|
|
}
|
|
|
|
|
2013-10-05 06:14:35 +02:00
|
|
|
RestoreFileLogger::~RestoreFileLogger() {
|
2013-11-07 09:56:48 +01:00
|
|
|
_scope.reset();
|
|
|
|
if (!removeFile(_log_file))
|
2013-10-05 06:14:35 +02:00
|
|
|
ADD_FAILURE();
|
|
|
|
}
|
|
|
|
|
2013-11-07 09:56:48 +01:00
|
|
|
std::string RestoreFileLogger::contentSoFar() {
|
|
|
|
std::future<std::string> filename = _handle->call(&g2::FileSink::fileName);
|
|
|
|
EXPECT_TRUE(filename.valid());
|
|
|
|
auto file = filename.get();
|
|
|
|
return readFileToText(file);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-10-05 06:14:35 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-08-21 07:40:39 +02:00
|
|
|
|
2013-07-14 03:33:00 +02:00
|
|
|
|
2013-08-19 07:17:04 +02:00
|
|
|
} // testing_helpers
|