nux-1.16.0
|
00001 #include <gtest/gtest.h> 00002 #include <gmock/gmock.h> 00003 00004 #include <fstream> 00005 #include <streambuf> 00006 #include <string> 00007 00008 #include <boost/filesystem.hpp> 00009 #include <boost/filesystem/fstream.hpp> 00010 00011 #include "NuxCore/RollingFileAppender.h" 00012 #include "Helpers.h" 00013 00014 namespace bf = boost::filesystem; 00015 00016 using namespace nux::logging; 00017 using namespace nux::testing; 00018 using namespace testing; 00019 00020 namespace { 00021 00035 const std::string TEST_ROOT("/tmp/nux-test-cases"); 00036 00037 class TestRollingFileAppender : public ::testing::Test 00038 { 00039 protected: 00040 virtual void SetUp() { 00041 // Make sure that the tests start with nothing there. 00042 bf::remove_all(TEST_ROOT); 00043 } 00044 00045 virtual void TearDown() { 00046 // Delete the unity test directory 00047 bf::remove_all(TEST_ROOT); 00048 } 00049 00050 bool WaitForRoll(RollingFileAppender& appender, unsigned timeout = 5) { 00051 TestCallback rolled; 00052 TestCallback timed_out; 00053 g_timeout_add_seconds(timeout, &TestCallback::glib_callback, &timed_out); 00054 appender.files_rolled.connect(rolled.sigc_callback()); 00055 00056 while (!rolled.happened && !timed_out.happened) { 00057 PumpGObjectMainLoop(); 00058 } 00059 return rolled.happened; 00060 } 00061 00062 }; 00063 00064 TEST_F(TestRollingFileAppender, NoTestRoot) { 00065 // The test root should not exist. 00066 EXPECT_FALSE(bf::exists(TEST_ROOT)); 00067 } 00068 00069 TEST_F(TestRollingFileAppender, TestLogFileRollsAtFlush) { 00070 00071 std::string logfile = TEST_ROOT + "/nux.log"; 00072 unsigned max_log_size = 20; // roll every 20 characters 00073 RollingFileAppender output(logfile, 5, max_log_size); 00074 00075 output << "Testing the rolling of the logfile" << std::endl; 00076 WaitForRoll(output); 00077 output << "Long line greater than max_log_size" << std::endl; 00078 WaitForRoll(output); 00079 00080 // Since the log files are rolled on flush, if the last thing written out 00081 // takes the filesize greater than the max_log_size, the log files are 00082 // rolled and the current file being appended to is now empty. 00083 EXPECT_THAT(ReadFile(logfile + ".1"), 00084 Eq("Long line greater than max_log_size\n")); 00085 EXPECT_THAT(ReadFile(logfile + ".2"), 00086 Eq("Testing the rolling of the logfile\n")); 00087 } 00088 00089 TEST_F(TestRollingFileAppender, TestExistingLogFileMoved) { 00090 00091 std::string logfile = TEST_ROOT + "/nux.log"; 00092 { 00093 bf::create_directories(bf::path(logfile).parent_path()); 00094 std::ofstream output(logfile); 00095 output << "Existing file."; 00096 } 00097 EXPECT_TRUE(bf::exists(logfile)); 00098 00099 RollingFileAppender output(logfile); 00100 00101 EXPECT_THAT(ReadFile(logfile + ".1"), 00102 Eq("Existing file.")); 00103 } 00104 00105 TEST_F(TestRollingFileAppender, TestDeletingOld) { 00106 00107 std::string logfile = TEST_ROOT + "/nux.log"; 00108 // Two backups, size 20 bytes. 00109 RollingFileAppender output(logfile, 2, 20); 00110 00111 // Due to the asynchronous manner in which the output is sent to the 00112 // underlying file, we explicitly wait for the roll here. Otherwise we may 00113 // just send all the logging lines to one file then it would roll. 00114 output << "Oldest line should be deleted." << std::endl; 00115 WaitForRoll(output); 00116 output << "This line will be in the last backup." << std::endl; 00117 WaitForRoll(output); 00118 output << "This is backup number 1." << std::endl; 00119 WaitForRoll(output); 00120 00121 EXPECT_THAT(ReadFile(logfile + ".1"), 00122 Eq("This is backup number 1.\n")); 00123 EXPECT_THAT(ReadFile(logfile + ".2"), 00124 Eq("This line will be in the last backup.\n")); 00125 EXPECT_FALSE(bf::exists(logfile + ".3")); 00126 } 00127 00128 TEST_F(TestRollingFileAppender, TestFullPathNeeded) { 00129 EXPECT_THROW(RollingFileAppender("nux.log"), std::runtime_error); 00130 EXPECT_THROW(RollingFileAppender("relative/nux.log"), std::runtime_error); 00131 } 00132 00133 TEST_F(TestRollingFileAppender, TestFileNeeded) { 00134 // For some obscure reason, EXPECT_THROW won't accept: 00135 // RollingFileAppender(logfile) 00136 // as its first arg. 00137 std::string directory_path = TEST_ROOT + "/somedir"; 00138 bf::create_directories(directory_path); 00139 EXPECT_THROW(RollingFileAppender appender(directory_path), std::runtime_error); 00140 00141 std::string symlink_path = TEST_ROOT + "/somelink"; 00142 bf::create_symlink(directory_path, symlink_path); 00143 EXPECT_THROW(RollingFileAppender appender(symlink_path), std::runtime_error); 00144 } 00145 00146 00147 } // anon namespace