env_logger_test.cc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. // Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
  2. // This source code is licensed under both the GPLv2 (found in the
  3. // COPYING file in the root directory) and Apache 2.0 License
  4. // (found in the LICENSE.Apache file in the root directory).
  5. //
  6. #include "logging/env_logger.h"
  7. #include "env/mock_env.h"
  8. #include "test_util/testharness.h"
  9. #include "test_util/testutil.h"
  10. namespace ROCKSDB_NAMESPACE {
  11. namespace {
  12. // In this test we only want to Log some simple log message with
  13. // no format.
  14. void LogMessage(std::shared_ptr<Logger> logger, const std::string& message) {
  15. Log(logger, "%s", message.c_str());
  16. }
  17. // Helper method to write the message num_times in the given logger.
  18. void WriteLogs(std::shared_ptr<Logger> logger, const std::string& message,
  19. int num_times) {
  20. for (int ii = 0; ii < num_times; ++ii) {
  21. LogMessage(logger, message);
  22. }
  23. }
  24. } // namespace
  25. class EnvLoggerTest : public testing::Test {
  26. public:
  27. Env* env_;
  28. EnvLoggerTest() : env_(Env::Default()) {}
  29. ~EnvLoggerTest() = default;
  30. std::shared_ptr<Logger> CreateLogger() {
  31. std::shared_ptr<Logger> result;
  32. assert(NewEnvLogger(kLogFile, env_, &result).ok());
  33. assert(result);
  34. result->SetInfoLogLevel(InfoLogLevel::INFO_LEVEL);
  35. return result;
  36. }
  37. void DeleteLogFile() { ASSERT_OK(env_->DeleteFile(kLogFile)); }
  38. static const std::string kSampleMessage;
  39. static const std::string kTestDir;
  40. static const std::string kLogFile;
  41. };
  42. const std::string EnvLoggerTest::kSampleMessage =
  43. "this is the message to be written to the log file!!";
  44. const std::string EnvLoggerTest::kLogFile = test::PerThreadDBPath("log_file");
  45. TEST_F(EnvLoggerTest, EmptyLogFile) {
  46. auto logger = CreateLogger();
  47. ASSERT_EQ(logger->Close(), Status::OK());
  48. // Check the size of the log file.
  49. uint64_t file_size;
  50. ASSERT_EQ(env_->GetFileSize(kLogFile, &file_size), Status::OK());
  51. ASSERT_EQ(file_size, 0);
  52. DeleteLogFile();
  53. }
  54. TEST_F(EnvLoggerTest, LogMultipleLines) {
  55. auto logger = CreateLogger();
  56. // Write multiple lines.
  57. const int kNumIter = 10;
  58. WriteLogs(logger, kSampleMessage, kNumIter);
  59. // Flush the logs.
  60. logger->Flush();
  61. ASSERT_EQ(logger->Close(), Status::OK());
  62. // Validate whether the log file has 'kNumIter' number of lines.
  63. ASSERT_EQ(test::GetLinesCount(kLogFile, kSampleMessage), kNumIter);
  64. DeleteLogFile();
  65. }
  66. TEST_F(EnvLoggerTest, Overwrite) {
  67. {
  68. auto logger = CreateLogger();
  69. // Write multiple lines.
  70. const int kNumIter = 10;
  71. WriteLogs(logger, kSampleMessage, kNumIter);
  72. ASSERT_EQ(logger->Close(), Status::OK());
  73. // Validate whether the log file has 'kNumIter' number of lines.
  74. ASSERT_EQ(test::GetLinesCount(kLogFile, kSampleMessage), kNumIter);
  75. }
  76. // Now reopen the file again.
  77. {
  78. auto logger = CreateLogger();
  79. // File should be empty.
  80. uint64_t file_size;
  81. ASSERT_EQ(env_->GetFileSize(kLogFile, &file_size), Status::OK());
  82. ASSERT_EQ(file_size, 0);
  83. ASSERT_EQ(logger->GetLogFileSize(), 0);
  84. ASSERT_EQ(logger->Close(), Status::OK());
  85. }
  86. DeleteLogFile();
  87. }
  88. TEST_F(EnvLoggerTest, Close) {
  89. auto logger = CreateLogger();
  90. // Write multiple lines.
  91. const int kNumIter = 10;
  92. WriteLogs(logger, kSampleMessage, kNumIter);
  93. ASSERT_EQ(logger->Close(), Status::OK());
  94. // Validate whether the log file has 'kNumIter' number of lines.
  95. ASSERT_EQ(test::GetLinesCount(kLogFile, kSampleMessage), kNumIter);
  96. DeleteLogFile();
  97. }
  98. TEST_F(EnvLoggerTest, ConcurrentLogging) {
  99. auto logger = CreateLogger();
  100. const int kNumIter = 20;
  101. std::function<void()> cb = [&]() {
  102. WriteLogs(logger, kSampleMessage, kNumIter);
  103. logger->Flush();
  104. };
  105. // Write to the logs from multiple threads.
  106. std::vector<port::Thread> threads;
  107. const int kNumThreads = 5;
  108. // Create threads.
  109. for (int ii = 0; ii < kNumThreads; ++ii) {
  110. threads.push_back(port::Thread(cb));
  111. }
  112. // Wait for them to complete.
  113. for (auto& th : threads) {
  114. th.join();
  115. }
  116. ASSERT_EQ(logger->Close(), Status::OK());
  117. // Verfiy the log file.
  118. ASSERT_EQ(test::GetLinesCount(kLogFile, kSampleMessage),
  119. kNumIter * kNumThreads);
  120. DeleteLogFile();
  121. }
  122. } // namespace ROCKSDB_NAMESPACE
  123. int main(int argc, char** argv) {
  124. ::testing::InitGoogleTest(&argc, argv);
  125. return RUN_ALL_TESTS();
  126. }