From d804bed0f5e23a03e81d6dc80bad298ea321b523 Mon Sep 17 00:00:00 2001 From: Heisenberg Date: Wed, 11 Sep 2019 10:36:09 +0800 Subject: [PATCH] add unit test Former-commit-id: 7807128ae39dd66c812fa254be840d2c4c7a364e --- cpp/src/utils/LogUtil.cpp | 4 ++-- cpp/src/utils/LogUtil.h | 1 + cpp/unittest/server/util_test.cpp | 30 ++++++++++++++++++++++++++++++ 3 files changed, 33 insertions(+), 2 deletions(-) diff --git a/cpp/src/utils/LogUtil.cpp b/cpp/src/utils/LogUtil.cpp index ec6f078500..98a9596da8 100644 --- a/cpp/src/utils/LogUtil.cpp +++ b/cpp/src/utils/LogUtil.cpp @@ -27,7 +27,7 @@ static int fatal_idx = 0; } // TODO(yzb) : change the easylogging library to get the log level from parameter rather than filename -void rolloutHandler(const char *filename, std::size_t size) { +void RolloutHandler(const char *filename, std::size_t size) { char *dirc = strdup(filename); char *basec = strdup(filename); char *dir = dirname(dirc); @@ -112,7 +112,7 @@ int32_t InitLog(const std::string &log_config_file) { el::Loggers::reconfigureAllLoggers(conf); el::Loggers::addFlag(el::LoggingFlag::StrictLogFileSizeCheck); - el::Helpers::installPreRollOutCallback(rolloutHandler); + el::Helpers::installPreRollOutCallback(RolloutHandler); return 0; } diff --git a/cpp/src/utils/LogUtil.h b/cpp/src/utils/LogUtil.h index d6c9eff252..7c8d936a6d 100644 --- a/cpp/src/utils/LogUtil.h +++ b/cpp/src/utils/LogUtil.h @@ -16,6 +16,7 @@ inline std::string GetFileName(std::string filename) { int pos = filename.find_last_of('/'); return filename.substr(pos + 1); } +void RolloutHandler(const char *filename, std::size_t size); #define SHOW_LOCATION #ifdef SHOW_LOCATION diff --git a/cpp/unittest/server/util_test.cpp b/cpp/unittest/server/util_test.cpp index 730eedbd33..6a78d29e15 100644 --- a/cpp/unittest/server/util_test.cpp +++ b/cpp/unittest/server/util_test.cpp @@ -6,6 +6,9 @@ #include #include #include +#include +#include +#include #include "utils/CommonUtil.h" #include "utils/Error.h" @@ -251,3 +254,30 @@ TEST(UtilTest, TIMERECORDER_TEST) { rc.RecordSection("end"); } } + +TEST(UtilTest, ROLLOUTHANDLER_TEST){ + std::string dir1 = "/tmp/milvus_test"; + std::string dir2 = "/tmp/milvus_test/log_test"; + std::string filename[6] = {"log_global.log", "log_debug.log", "log_warning.log", "log_trace.log", "log_error.log", "log_fatal.log"}; + + mkdir(dir1.c_str(), S_IRWXU); + mkdir(dir2.c_str(), S_IRWXU); + for (int i = 0; i < 6; ++i) { + std::string tmp = dir2 + "/" + filename[i]; + + std::ofstream file; + file.open(tmp.c_str()); + file << "zilliz" << std::endl; + + server::RolloutHandler(tmp.c_str(), 0); + + tmp.append(".1"); + std::ifstream file2; + file2.open(tmp); + + std::string tmp2; + file2 >> tmp2; + ASSERT_EQ(tmp2, "zilliz"); + } + boost::filesystem::remove_all(dir2); +} \ No newline at end of file