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