log_ut.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. #include "all.h"
  2. #include <library/cpp/testing/unittest/registar.h>
  3. #include <util/system/fs.h>
  4. #include <util/system/rwlock.h>
  5. #include <util/system/yield.h>
  6. #include <util/memory/blob.h>
  7. #include <util/stream/file.h>
  8. #include <util/generic/string.h>
  9. #include <util/generic/vector.h>
  10. class TLogTest: public TTestBase {
  11. UNIT_TEST_SUITE(TLogTest);
  12. UNIT_TEST(TestFile)
  13. UNIT_TEST(TestFormat)
  14. UNIT_TEST(TestWrite)
  15. UNIT_TEST(TestThreaded)
  16. UNIT_TEST(TestThreadedWithOverflow)
  17. UNIT_TEST(TestNoFlush)
  18. UNIT_TEST(TestMetaFlags)
  19. UNIT_TEST_SUITE_END();
  20. private:
  21. void TestFile();
  22. void TestFormat();
  23. void TestWrite();
  24. void TestThreaded();
  25. void TestThreadedWithOverflow();
  26. void TestNoFlush();
  27. void TestMetaFlags();
  28. void SetUp() override;
  29. void TearDown() override;
  30. };
  31. UNIT_TEST_SUITE_REGISTRATION(TLogTest);
  32. #define LOGFILE "tmplogfile"
  33. void TLogTest::TestFile() {
  34. {
  35. TLog log;
  36. {
  37. TLog filelog(LOGFILE);
  38. log = filelog;
  39. }
  40. int v1 = 12;
  41. unsigned v2 = 34;
  42. double v3 = 3.0;
  43. const char* v4 = "qwqwqw";
  44. log.ReopenLog();
  45. log.AddLog("some useful data %d, %u, %lf, %s\n", v1, v2, v3, v4);
  46. }
  47. TBlob data = TBlob::FromFileSingleThreaded(LOGFILE);
  48. UNIT_ASSERT_EQUAL(TString((const char*)data.Begin(), data.Size()), "some useful data 12, 34, 3.000000, qwqwqw\n");
  49. }
  50. void TLogTest::TestThreaded() {
  51. {
  52. TFileLogBackend fb(LOGFILE);
  53. TLog log(THolder(new TThreadedLogBackend(&fb)));
  54. int v1 = 12;
  55. unsigned v2 = 34;
  56. double v3 = 3.0;
  57. const char* v4 = "qwqwqw";
  58. log.ReopenLog();
  59. log.AddLog("some useful data %d, %u, %lf, %s\n", v1, v2, v3, v4);
  60. }
  61. TBlob data = TBlob::FromFileSingleThreaded(LOGFILE);
  62. UNIT_ASSERT_EQUAL(TString((const char*)data.Begin(), data.Size()), "some useful data 12, 34, 3.000000, qwqwqw\n");
  63. }
  64. void TLogTest::TestThreadedWithOverflow() {
  65. class TFakeLogBackend: public TLogBackend {
  66. public:
  67. TWriteGuard Guard() {
  68. return TWriteGuard(Lock_);
  69. }
  70. void WriteData(const TLogRecord&) override {
  71. TReadGuard guard(Lock_);
  72. }
  73. void ReopenLog() override {
  74. TWriteGuard guard(Lock_);
  75. }
  76. private:
  77. TRWMutex Lock_;
  78. };
  79. auto waitForFreeQueue = [](const TLog& log) {
  80. ThreadYield();
  81. while (log.BackEndQueueSize() > 0) {
  82. Sleep(TDuration::MilliSeconds(1));
  83. }
  84. };
  85. TFakeLogBackend fb;
  86. {
  87. TLog log(THolder(new TThreadedLogBackend(&fb, 2)));
  88. auto guard = fb.Guard();
  89. log.AddLog("first write");
  90. waitForFreeQueue(log);
  91. log.AddLog("second write (first in queue)");
  92. log.AddLog("third write (second in queue)");
  93. UNIT_ASSERT_EXCEPTION(log.AddLog("fourth write (queue overflow)"), yexception);
  94. }
  95. {
  96. ui32 overflows = 0;
  97. TLog log(THolder(new TThreadedLogBackend(&fb, 2, [&overflows] { ++overflows; })));
  98. auto guard = fb.Guard();
  99. log.AddLog("first write");
  100. waitForFreeQueue(log);
  101. log.AddLog("second write (first in queue)");
  102. log.AddLog("third write (second in queue)");
  103. UNIT_ASSERT_EQUAL(overflows, 0);
  104. log.AddLog("fourth write (queue overflow)");
  105. UNIT_ASSERT_EQUAL(overflows, 1);
  106. }
  107. }
  108. void TLogTest::TestNoFlush() {
  109. {
  110. TFileLogBackend fb(LOGFILE);
  111. TLog log(THolder(new TThreadedLogBackend(&fb)));
  112. int v1 = 12;
  113. unsigned v2 = 34;
  114. double v3 = 3.0;
  115. const char* v4 = "qwqwqw";
  116. log.ReopenLogNoFlush();
  117. log.AddLog("some useful data %d, %u, %lf, %s\n", v1, v2, v3, v4);
  118. }
  119. TBlob data = TBlob::FromFileSingleThreaded(LOGFILE);
  120. UNIT_ASSERT_EQUAL(TString((const char*)data.Begin(), data.Size()), "some useful data 12, 34, 3.000000, qwqwqw\n");
  121. }
  122. void TLogTest::TestFormat() {
  123. TStringStream data;
  124. {
  125. TLog log(THolder(new TStreamLogBackend(&data)));
  126. log << "qw"
  127. << " "
  128. << "1234" << 1234 << " " << 12.3 << 'q' << Endl;
  129. }
  130. UNIT_ASSERT_EQUAL(data.Str(), "qw 12341234 12.3q\n");
  131. }
  132. void TLogTest::TestWrite() {
  133. TStringStream data;
  134. TString test;
  135. {
  136. TLog log(THolder(new TStreamLogBackend(&data)));
  137. for (size_t i = 0; i < 1000; ++i) {
  138. TVector<char> buf(i, (char)i);
  139. test.append(buf.data(), buf.size());
  140. log.Write(buf.data(), buf.size());
  141. }
  142. }
  143. UNIT_ASSERT_EQUAL(data.Str(), test);
  144. }
  145. void TLogTest::TestMetaFlags() {
  146. class TTestLogBackendStub: public TLogBackend {
  147. public:
  148. TTestLogBackendStub(TLogRecord::TMetaFlags& data)
  149. : Data_(data)
  150. {
  151. }
  152. void WriteData(const TLogRecord& record) override {
  153. Data_ = record.MetaFlags;
  154. }
  155. void ReopenLog() override {
  156. }
  157. private:
  158. TLogRecord::TMetaFlags& Data_;
  159. };
  160. TLogRecord::TMetaFlags metaFlags;
  161. TLog log(MakeHolder<TTestLogBackendStub>(metaFlags));
  162. log.Write(ELogPriority::TLOG_INFO, TString("message"), {{"key", "value"}});
  163. TLogRecord::TMetaFlags expected{{"key", "value"}};
  164. UNIT_ASSERT_EQUAL(metaFlags, expected);
  165. }
  166. void TLogTest::SetUp() {
  167. TearDown();
  168. }
  169. void TLogTest::TearDown() {
  170. NFs::Remove(LOGFILE);
  171. }