forked from abseil/abseil-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scoped_mock_log_test.cc
290 lines (235 loc) · 8.66 KB
/
scoped_mock_log_test.cc
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
//
// Copyright 2022 The Abseil Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "absl/log/scoped_mock_log.h"
#include <memory>
#include <thread> // NOLINT(build/c++11)
#include "gmock/gmock.h"
#include "gtest/gtest-spi.h"
#include "gtest/gtest.h"
#include "absl/base/attributes.h"
#include "absl/base/log_severity.h"
#include "absl/log/globals.h"
#include "absl/log/internal/test_helpers.h"
#include "absl/log/internal/test_matchers.h"
#include "absl/log/log.h"
#include "absl/memory/memory.h"
#include "absl/strings/match.h"
#include "absl/strings/string_view.h"
#include "absl/synchronization/barrier.h"
#include "absl/synchronization/notification.h"
namespace {
using ::testing::_;
using ::testing::AnyNumber;
using ::testing::Eq;
using ::testing::HasSubstr;
using ::testing::InSequence;
using ::testing::Lt;
using ::testing::Truly;
using absl::log_internal::SourceBasename;
using absl::log_internal::SourceFilename;
using absl::log_internal::SourceLine;
using absl::log_internal::TextMessageWithPrefix;
using absl::log_internal::ThreadID;
auto* test_env ABSL_ATTRIBUTE_UNUSED = ::testing::AddGlobalTestEnvironment(
new absl::log_internal::LogTestEnvironment);
#if GTEST_HAS_DEATH_TEST
TEST(ScopedMockLogDeathTest,
StartCapturingLogsCannotBeCalledWhenAlreadyCapturing) {
EXPECT_DEATH(
{
absl::ScopedMockLog log;
log.StartCapturingLogs();
log.StartCapturingLogs();
},
"StartCapturingLogs");
}
TEST(ScopedMockLogDeathTest, StopCapturingLogsCannotBeCalledWhenNotCapturing) {
EXPECT_DEATH(
{
absl::ScopedMockLog log;
log.StopCapturingLogs();
},
"StopCapturingLogs");
}
#endif
// Tests that ScopedMockLog intercepts LOG()s when it's alive.
TEST(ScopedMockLogTest, LogMockCatchAndMatchStrictExpectations) {
absl::ScopedMockLog log;
// The following expectations must match in the order they appear.
InSequence s;
EXPECT_CALL(log,
Log(absl::LogSeverity::kWarning, HasSubstr(__FILE__), "Danger."));
EXPECT_CALL(log, Log(absl::LogSeverity::kInfo, _, "Working...")).Times(2);
EXPECT_CALL(log, Log(absl::LogSeverity::kError, _, "Bad!!"));
log.StartCapturingLogs();
LOG(WARNING) << "Danger.";
LOG(INFO) << "Working...";
LOG(INFO) << "Working...";
LOG(ERROR) << "Bad!!";
}
TEST(ScopedMockLogTest, LogMockCatchAndMatchSendExpectations) {
absl::ScopedMockLog log;
EXPECT_CALL(
log,
Send(AllOf(SourceFilename(Eq("/my/very/very/very_long_source_file.cc")),
SourceBasename(Eq("very_long_source_file.cc")),
SourceLine(Eq(777)), ThreadID(Eq(absl::LogEntry::tid_t{1234})),
TextMessageWithPrefix(Truly([](absl::string_view msg) {
return absl::EndsWith(
msg, " very_long_source_file.cc:777] Info message");
})))));
log.StartCapturingLogs();
LOG(INFO)
.AtLocation("/my/very/very/very_long_source_file.cc", 777)
.WithThreadID(1234)
<< "Info message";
}
TEST(ScopedMockLogTest, ScopedMockLogCanBeNice) {
absl::ScopedMockLog log;
InSequence s;
EXPECT_CALL(log,
Log(absl::LogSeverity::kWarning, HasSubstr(__FILE__), "Danger."));
EXPECT_CALL(log, Log(absl::LogSeverity::kInfo, _, "Working...")).Times(2);
EXPECT_CALL(log, Log(absl::LogSeverity::kError, _, "Bad!!"));
log.StartCapturingLogs();
// Any number of these are OK.
LOG(INFO) << "Info message.";
// Any number of these are OK.
LOG(WARNING).AtLocation("SomeOtherFile.cc", 100) << "Danger ";
LOG(WARNING) << "Danger.";
// Any number of these are OK.
LOG(INFO) << "Info message.";
// Any number of these are OK.
LOG(WARNING).AtLocation("SomeOtherFile.cc", 100) << "Danger ";
LOG(INFO) << "Working...";
// Any number of these are OK.
LOG(INFO) << "Info message.";
// Any number of these are OK.
LOG(WARNING).AtLocation("SomeOtherFile.cc", 100) << "Danger ";
LOG(INFO) << "Working...";
// Any number of these are OK.
LOG(INFO) << "Info message.";
// Any number of these are OK.
LOG(WARNING).AtLocation("SomeOtherFile.cc", 100) << "Danger ";
LOG(ERROR) << "Bad!!";
// Any number of these are OK.
LOG(INFO) << "Info message.";
// Any number of these are OK.
LOG(WARNING).AtLocation("SomeOtherFile.cc", 100) << "Danger ";
}
// Tests that ScopedMockLog generates a test failure if a message is logged
// that is not expected (here, that means ERROR or FATAL).
TEST(ScopedMockLogTest, RejectsUnexpectedLogs) {
EXPECT_NONFATAL_FAILURE(
{
absl::ScopedMockLog log(absl::MockLogDefault::kDisallowUnexpected);
// Any INFO and WARNING messages are permitted.
EXPECT_CALL(log, Log(Lt(absl::LogSeverity::kError), _, _))
.Times(AnyNumber());
log.StartCapturingLogs();
LOG(INFO) << "Ignored";
LOG(WARNING) << "Ignored";
LOG(ERROR) << "Should not be ignored";
},
"Should not be ignored");
}
TEST(ScopedMockLogTest, CapturesLogsAfterStartCapturingLogs) {
absl::SetStderrThreshold(absl::LogSeverityAtLeast::kInfinity);
absl::ScopedMockLog log;
// The ScopedMockLog object shouldn't see these LOGs, as it hasn't
// started capturing LOGs yet.
LOG(INFO) << "Ignored info";
LOG(WARNING) << "Ignored warning";
LOG(ERROR) << "Ignored error";
EXPECT_CALL(log, Log(absl::LogSeverity::kInfo, _, "Expected info"));
log.StartCapturingLogs();
// Only this LOG will be seen by the ScopedMockLog.
LOG(INFO) << "Expected info";
}
TEST(ScopedMockLogTest, DoesNotCaptureLogsAfterStopCapturingLogs) {
absl::ScopedMockLog log;
EXPECT_CALL(log, Log(absl::LogSeverity::kInfo, _, "Expected info"));
log.StartCapturingLogs();
// This LOG should be seen by the ScopedMockLog.
LOG(INFO) << "Expected info";
log.StopCapturingLogs();
// The ScopedMockLog object shouldn't see these LOGs, as it has
// stopped capturing LOGs.
LOG(INFO) << "Ignored info";
LOG(WARNING) << "Ignored warning";
LOG(ERROR) << "Ignored error";
}
// Tests that all messages are intercepted regardless of issuing thread. The
// purpose of this test is NOT to exercise thread-safety.
TEST(ScopedMockLogTest, LogFromMultipleThreads) {
absl::ScopedMockLog log;
// We don't establish an order to expectations here, since the threads may
// execute their log statements in different order.
EXPECT_CALL(log, Log(absl::LogSeverity::kInfo, __FILE__, "Thread 1"));
EXPECT_CALL(log, Log(absl::LogSeverity::kInfo, __FILE__, "Thread 2"));
log.StartCapturingLogs();
absl::Barrier barrier(2);
std::thread thread1([&barrier]() {
barrier.Block();
LOG(INFO) << "Thread 1";
});
std::thread thread2([&barrier]() {
barrier.Block();
LOG(INFO) << "Thread 2";
});
thread1.join();
thread2.join();
}
// Tests that no sequence will be imposed on two LOG message expectations from
// different threads. This test would actually deadlock if replaced to two LOG
// statements from the same thread.
TEST(ScopedMockLogTest, NoSequenceWithMultipleThreads) {
absl::ScopedMockLog log;
absl::Barrier barrier(2);
EXPECT_CALL(log, Log(absl::LogSeverity::kInfo, _, _))
.Times(2)
.WillRepeatedly([&barrier]() { barrier.Block(); });
log.StartCapturingLogs();
std::thread thread1([]() { LOG(INFO) << "Thread 1"; });
std::thread thread2([]() { LOG(INFO) << "Thread 2"; });
thread1.join();
thread2.join();
}
TEST(ScopedMockLogTsanTest,
ScopedMockLogCanBeDeletedWhenAnotherThreadIsLogging) {
auto log = absl::make_unique<absl::ScopedMockLog>();
EXPECT_CALL(*log, Log(absl::LogSeverity::kInfo, __FILE__, "Thread log"))
.Times(AnyNumber());
log->StartCapturingLogs();
absl::Notification logging_started;
std::thread thread([&logging_started]() {
for (int i = 0; i < 100; ++i) {
if (i == 50) logging_started.Notify();
LOG(INFO) << "Thread log";
}
});
logging_started.WaitForNotification();
log.reset();
thread.join();
}
TEST(ScopedMockLogTest, AsLocalSink) {
absl::ScopedMockLog log(absl::MockLogDefault::kDisallowUnexpected);
EXPECT_CALL(log, Log(_, _, "two"));
EXPECT_CALL(log, Log(_, _, "three"));
LOG(INFO) << "one";
LOG(INFO).ToSinkOnly(&log.UseAsLocalSink()) << "two";
LOG(INFO).ToSinkAlso(&log.UseAsLocalSink()) << "three";
}
} // namespace