2013-08-02 07:06:12 +02:00
|
|
|
/** ==========================================================================
|
|
|
|
* 2012 by KjellKod.cc. This is PUBLIC DOMAIN to use at your own risk and comes
|
|
|
|
* with no warranties. This code is yours to share, use and modify with no
|
|
|
|
* strings attached and no restrictions or obligations.
|
|
|
|
* ============================================================================*/
|
|
|
|
|
|
|
|
|
|
|
|
#include <gtest/gtest.h>
|
|
|
|
#include <memory>
|
|
|
|
#include <fstream>
|
|
|
|
#include <string>
|
|
|
|
#include <memory>
|
|
|
|
#include <future>
|
|
|
|
#include <queue>
|
|
|
|
|
|
|
|
|
|
|
|
#include <thread>
|
2013-10-05 06:14:35 +02:00
|
|
|
#include "g2log.hpp"
|
|
|
|
#include "g2logworker.hpp"
|
2013-08-02 07:06:12 +02:00
|
|
|
#include "testing_helpers.h"
|
|
|
|
|
2013-08-19 07:18:18 +02:00
|
|
|
using namespace testing_helpers;
|
2013-08-02 07:06:12 +02:00
|
|
|
|
|
|
|
|
|
|
|
namespace { // anonymous
|
|
|
|
const char* name_path_1 = "./some_fake_DirectoryOrName_1_";
|
2013-11-12 10:21:40 +01:00
|
|
|
g2::LogWorker* g_logger_ptr = nullptr;
|
2013-10-05 06:14:35 +02:00
|
|
|
g2::SinkHandle<g2::FileSink>* g_filesink_handler = nullptr;
|
2013-08-02 07:06:12 +02:00
|
|
|
LogFileCleaner* g_cleaner_ptr = nullptr;
|
2013-08-19 07:18:18 +02:00
|
|
|
|
2013-08-02 07:06:12 +02:00
|
|
|
bool isTextAvailableInContent(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();
|
|
|
|
std::string content(oss.str());
|
|
|
|
return content;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string setLogNameAndAddCount(std::string new_file_to_create) {
|
|
|
|
static std::mutex m;
|
|
|
|
static int count;
|
|
|
|
std::string add_count;
|
|
|
|
std::lock_guard<std::mutex> lock(m);
|
|
|
|
{
|
|
|
|
add_count = std::to_string(++count) + "_";
|
2013-10-05 06:14:35 +02:00
|
|
|
auto future_new_log = g_filesink_handler->call(&g2::FileSink::changeLogFile, new_file_to_create + add_count);
|
2013-08-02 07:06:12 +02:00
|
|
|
auto new_log = future_new_log.get();
|
|
|
|
if (!new_log.empty()) g_cleaner_ptr->addLogToClean(new_log);
|
|
|
|
return new_log;
|
|
|
|
}
|
|
|
|
return add_count;
|
|
|
|
}
|
|
|
|
|
2013-08-19 07:18:18 +02:00
|
|
|
std::string setLogName(std::string new_file_to_create) {
|
2013-10-05 06:14:35 +02:00
|
|
|
auto future_new_log = g_filesink_handler->call(&g2::FileSink::changeLogFile, new_file_to_create);
|
2013-08-02 07:06:12 +02:00
|
|
|
auto new_log = future_new_log.get();
|
|
|
|
if (!new_log.empty()) g_cleaner_ptr->addLogToClean(new_log);
|
|
|
|
return new_log;
|
|
|
|
}
|
2013-08-19 07:18:18 +02:00
|
|
|
|
2013-08-02 07:06:12 +02:00
|
|
|
std::string getLogName() {
|
2013-10-05 06:14:35 +02:00
|
|
|
return g_filesink_handler->call(&g2::FileSink::fileName).get();
|
2013-08-02 07:06:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
} // anonymous
|
|
|
|
|
|
|
|
TEST(TestOf_GetFileName, Expecting_ValidLogFile) {
|
|
|
|
|
|
|
|
LOG(INFO) << "test_filechange, Retrieving file name: ";
|
|
|
|
ASSERT_NE(g_logger_ptr, nullptr);
|
|
|
|
ASSERT_FALSE(getLogName().empty());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(TestOf_ChangingLogFile, Expecting_NewLogFileUsed) {
|
2013-08-19 07:18:18 +02:00
|
|
|
auto old_log = getLogName();
|
2013-08-02 07:06:12 +02:00
|
|
|
std::string name = setLogNameAndAddCount(name_path_1);
|
|
|
|
auto new_log = setLogName(name);
|
|
|
|
ASSERT_NE(old_log, new_log);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(TestOf_ManyThreadsChangingLogFileName, Expecting_EqualNumberLogsCreated) {
|
2013-10-05 06:14:35 +02:00
|
|
|
auto old_log = g_filesink_handler->call(&g2::FileSink::fileName).get();
|
2013-08-02 07:06:12 +02:00
|
|
|
if (!old_log.empty()) g_cleaner_ptr->addLogToClean(old_log);
|
|
|
|
|
|
|
|
LOG(INFO) << "SoManyThreadsAllDoingChangeFileName";
|
|
|
|
std::vector<std::thread> threads;
|
|
|
|
auto max = 2;
|
|
|
|
auto size = g_cleaner_ptr->size();
|
|
|
|
for (auto count = 0; count < max; ++count) {
|
|
|
|
std::string drive = ((count % 2) == 0) ? "./_threadEven_" : "./_threaOdd_";
|
|
|
|
threads.push_back(std::thread(setLogNameAndAddCount, drive));
|
|
|
|
}
|
|
|
|
for (auto& thread : threads)
|
|
|
|
thread.join();
|
|
|
|
|
|
|
|
// check that all logs were created
|
|
|
|
ASSERT_EQ(size + max, g_cleaner_ptr->size());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(TestOf_IllegalLogFileName, Expecting_NoChangeToOriginalFileName) {
|
|
|
|
std::string original = getLogName();
|
|
|
|
auto perhaps_a_name = setLogName("XY:/"); // does not exist
|
|
|
|
ASSERT_TRUE(perhaps_a_name.empty());
|
|
|
|
std::string post_illegal = getLogName();
|
|
|
|
ASSERT_STREQ(original.c_str(), post_illegal.c_str());
|
|
|
|
}
|
|
|
|
|
2013-08-19 07:18:18 +02:00
|
|
|
|
2013-08-02 07:06:12 +02:00
|
|
|
int main(int argc, char *argv[]) {
|
|
|
|
LogFileCleaner cleaner;
|
|
|
|
g_cleaner_ptr = &cleaner;
|
|
|
|
int return_value = 1;
|
2013-08-19 07:18:18 +02:00
|
|
|
std::stringstream cerrDump;
|
|
|
|
|
2013-08-02 07:06:12 +02:00
|
|
|
std::string last_log_file;
|
|
|
|
{
|
2013-08-19 07:18:18 +02:00
|
|
|
|
|
|
|
testing_helpers::ScopedOut scopedCerr(std::cerr, &cerrDump);
|
|
|
|
|
2013-11-12 10:21:40 +01:00
|
|
|
auto logger = g2::LogWorker::createWithDefaultLogger("ReplaceLogFile", name_path_1);
|
2013-09-01 08:44:30 +02:00
|
|
|
g_logger_ptr = logger.worker.get();
|
|
|
|
g_filesink_handler = logger.sink.get();
|
2013-10-05 06:14:35 +02:00
|
|
|
last_log_file = g_filesink_handler->call(&g2::FileSink::fileName).get();
|
2013-09-01 08:44:30 +02:00
|
|
|
cleaner.addLogToClean(last_log_file);
|
|
|
|
|
|
|
|
|
2013-08-02 07:06:12 +02:00
|
|
|
g2::initializeLogging(g_logger_ptr);
|
2013-08-19 07:18:18 +02:00
|
|
|
LOG(INFO) << "test_filechange demo*" << std::endl;
|
|
|
|
|
|
|
|
testing::InitGoogleTest(&argc, argv);
|
2013-08-02 07:06:12 +02:00
|
|
|
return_value = RUN_ALL_TESTS();
|
2013-08-19 07:18:18 +02:00
|
|
|
|
2013-10-05 06:14:35 +02:00
|
|
|
last_log_file = g_filesink_handler->call(&g2::FileSink::fileName).get();
|
2013-08-19 07:18:18 +02:00
|
|
|
std::cout << "log file at: " << last_log_file << std::endl;
|
2013-08-02 07:06:12 +02:00
|
|
|
//g2::shutDownLogging();
|
|
|
|
}
|
|
|
|
std::cout << "FINISHED WITH THE TESTING" << std::endl;
|
|
|
|
// cleaning up
|
|
|
|
cleaner.addLogToClean(last_log_file);
|
|
|
|
return return_value;
|
|
|
|
}
|