rotating_file_ut.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #include "rotating_file.h"
  2. #include "record.h"
  3. #include <util/generic/string.h>
  4. #include <util/system/fstat.h>
  5. #include <util/system/fs.h>
  6. #include <library/cpp/testing/unittest/registar.h>
  7. #include <library/cpp/testing/unittest/tests_data.h>
  8. Y_UNIT_TEST_SUITE(NewRotatingFileSuite) {
  9. const TString PATH = GetWorkPath() + "/my.log";
  10. Y_UNIT_TEST(TestFileWrite) {
  11. TRotatingFileLogBackend log(PATH, 4000, 2);
  12. TString data = "my data";
  13. log.WriteData(TLogRecord(ELogPriority::TLOG_INFO, data.data(), data.size()));
  14. UNIT_ASSERT_C(TFileStat(PATH).Size > 0, "file " << PATH << " has zero size");
  15. }
  16. Y_UNIT_TEST(TestFileRotate) {
  17. const ui64 maxSize = 40;
  18. TRotatingFileLogBackend log(PATH, maxSize, 2);
  19. TStringBuilder data;
  20. for (size_t i = 0; i < 10; ++i)
  21. data << "data\n";
  22. log.WriteData(TLogRecord(ELogPriority::TLOG_INFO, data.data(), data.size()));
  23. UNIT_ASSERT_C(TFileStat(PATH).Size > 0, "file " << PATH << " has zero size");
  24. data.clear();
  25. data << "more data";
  26. log.WriteData(TLogRecord(ELogPriority::TLOG_INFO, data.data(), data.size()));
  27. UNIT_ASSERT_C(TFileStat(PATH).Size > 0, "file " << PATH << " has zero size");
  28. UNIT_ASSERT_C(TFileStat(TStringBuilder{} << PATH << ".1").Size > 0, "file " << PATH << ".1 has zero size");
  29. UNIT_ASSERT_C(TFileStat(PATH).Size < maxSize, "size of file " << PATH << " is greater than the size limit of " << maxSize << " bytes");
  30. }
  31. Y_UNIT_TEST(TestDoubleFileRotate) {
  32. const ui64 maxSize = 40;
  33. TRotatingFileLogBackend log(PATH, maxSize, 2);
  34. TStringBuilder data;
  35. for (size_t i = 0; i < 10; ++i)
  36. data << "data\n";
  37. log.WriteData(TLogRecord(ELogPriority::TLOG_INFO, data.data(), data.size()));
  38. UNIT_ASSERT_C(TFileStat(PATH).Size > 0, "file " << PATH << " has zero size");
  39. log.WriteData(TLogRecord(ELogPriority::TLOG_INFO, data.data(), data.size()));
  40. UNIT_ASSERT_C(TFileStat(PATH).Size > 0, "file " << PATH << " has zero size");
  41. UNIT_ASSERT_C(TFileStat(TStringBuilder{} << PATH << ".1").Size > 0, "file " << PATH << ".1 has zero size");
  42. UNIT_ASSERT_C(TFileStat(PATH).Size > maxSize, "size of file " << PATH << " is lesser than was written");
  43. data.clear();
  44. data << "more data";
  45. log.WriteData(TLogRecord(ELogPriority::TLOG_INFO, data.data(), data.size()));
  46. UNIT_ASSERT_C(TFileStat(PATH).Size > 0, "file " << PATH << " has zero size");
  47. UNIT_ASSERT_C(TFileStat(TStringBuilder{} << PATH << ".1").Size > 0, "file " << PATH << ".1 has zero size");
  48. UNIT_ASSERT_C(TFileStat(TStringBuilder{} << PATH << ".2").Size > 0, "file " << PATH << ".2 has zero size");
  49. UNIT_ASSERT_C(TFileStat(PATH).Size < maxSize, "size of file " << PATH << " is greater than the size limit of " << maxSize << " bytes");
  50. }
  51. }