1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "net/log/net_log_unittest.h"
8 #include "base/memory/scoped_vector.h"
9 #include "base/synchronization/waitable_event.h"
10 #include "base/threading/simple_thread.h"
11 #include "base/values.h"
12 #include "net/base/net_errors.h"
18 const int kThreads
= 10;
19 const int kEvents
= 100;
21 base::Value
* NetLogLevelCallback(NetLog::LogLevel log_level
) {
22 base::DictionaryValue
* dict
= new base::DictionaryValue();
23 dict
->SetInteger("log_level", log_level
);
27 TEST(NetLogTest
, Basic
) {
28 CapturingNetLog net_log
;
29 CapturingNetLog::CapturedEntryList entries
;
30 net_log
.GetEntries(&entries
);
31 EXPECT_EQ(0u, entries
.size());
33 net_log
.AddGlobalEntry(NetLog::TYPE_CANCELLED
);
35 net_log
.GetEntries(&entries
);
36 ASSERT_EQ(1u, entries
.size());
37 EXPECT_EQ(NetLog::TYPE_CANCELLED
, entries
[0].type
);
38 EXPECT_EQ(NetLog::SOURCE_NONE
, entries
[0].source
.type
);
39 EXPECT_NE(NetLog::Source::kInvalidId
, entries
[0].source
.id
);
40 EXPECT_EQ(NetLog::PHASE_NONE
, entries
[0].phase
);
41 EXPECT_GE(base::TimeTicks::Now(), entries
[0].time
);
42 EXPECT_FALSE(entries
[0].params
);
45 // Check that the correct LogLevel is sent to NetLog Value callbacks.
46 TEST(NetLogTest
, LogLevels
) {
47 CapturingNetLog net_log
;
48 for (int log_level
= NetLog::LOG_ALL
; log_level
< NetLog::LOG_NONE
;
50 net_log
.SetLogLevel(static_cast<NetLog::LogLevel
>(log_level
));
51 EXPECT_EQ(log_level
, net_log
.GetLogLevel());
53 net_log
.AddGlobalEntry(NetLog::TYPE_SOCKET_ALIVE
,
54 base::Bind(NetLogLevelCallback
));
56 CapturingNetLog::CapturedEntryList entries
;
57 net_log
.GetEntries(&entries
);
59 ASSERT_EQ(1u, entries
.size());
60 EXPECT_EQ(NetLog::TYPE_SOCKET_ALIVE
, entries
[0].type
);
61 EXPECT_EQ(NetLog::SOURCE_NONE
, entries
[0].source
.type
);
62 EXPECT_NE(NetLog::Source::kInvalidId
, entries
[0].source
.id
);
63 EXPECT_EQ(NetLog::PHASE_NONE
, entries
[0].phase
);
64 EXPECT_GE(base::TimeTicks::Now(), entries
[0].time
);
67 ASSERT_TRUE(entries
[0].GetIntegerValue("log_level", &logged_log_level
));
68 EXPECT_EQ(log_level
, logged_log_level
);
74 class CountingObserver
: public NetLog::ThreadSafeObserver
{
76 CountingObserver() : count_(0) {}
78 ~CountingObserver() override
{
80 net_log()->DeprecatedRemoveObserver(this);
83 void OnAddEntry(const NetLog::Entry
& entry
) override
{ ++count_
; }
85 int count() const { return count_
; }
91 class LoggingObserver
: public NetLog::ThreadSafeObserver
{
95 ~LoggingObserver() override
{
97 net_log()->DeprecatedRemoveObserver(this);
100 void OnAddEntry(const NetLog::Entry
& entry
) override
{
101 base::Value
* value
= entry
.ToValue();
102 base::DictionaryValue
* dict
= NULL
;
103 ASSERT_TRUE(value
->GetAsDictionary(&dict
));
104 values_
.push_back(dict
);
107 size_t GetNumValues() const { return values_
.size(); }
108 base::DictionaryValue
* GetValue(size_t index
) const { return values_
[index
]; }
111 ScopedVector
<base::DictionaryValue
> values_
;
114 base::Value
* LogLevelToValue(NetLog::LogLevel log_level
) {
115 return new base::FundamentalValue(log_level
);
118 void AddEvent(NetLog
* net_log
) {
119 net_log
->AddGlobalEntry(NetLog::TYPE_CANCELLED
, base::Bind(LogLevelToValue
));
122 // A thread that waits until an event has been signalled before calling
124 class NetLogTestThread
: public base::SimpleThread
{
127 : base::SimpleThread("NetLogTest"), net_log_(NULL
), start_event_(NULL
) {}
129 // We'll wait for |start_event| to be triggered before calling a subclass's
130 // subclass's RunTestThread() function.
131 void Init(NetLog
* net_log
, base::WaitableEvent
* start_event
) {
132 start_event_
= start_event
;
136 void Run() override
{
137 start_event_
->Wait();
141 // Subclasses must override this with the code they want to run on their
143 virtual void RunTestThread() = 0;
149 // Only triggered once all threads have been created, to make it less likely
150 // each thread completes before the next one starts.
151 base::WaitableEvent
* start_event_
;
153 DISALLOW_COPY_AND_ASSIGN(NetLogTestThread
);
156 // A thread that adds a bunch of events to the NetLog.
157 class AddEventsTestThread
: public NetLogTestThread
{
159 AddEventsTestThread() {}
160 ~AddEventsTestThread() override
{}
163 void RunTestThread() override
{
164 for (int i
= 0; i
< kEvents
; ++i
)
168 DISALLOW_COPY_AND_ASSIGN(AddEventsTestThread
);
171 // A thread that adds and removes an observer from the NetLog repeatedly.
172 class AddRemoveObserverTestThread
: public NetLogTestThread
{
174 AddRemoveObserverTestThread() {}
176 ~AddRemoveObserverTestThread() override
{ EXPECT_TRUE(!observer_
.net_log()); }
179 void RunTestThread() override
{
180 for (int i
= 0; i
< kEvents
; ++i
) {
181 ASSERT_FALSE(observer_
.net_log());
183 net_log_
->DeprecatedAddObserver(&observer_
, NetLog::LOG_ALL_BUT_BYTES
);
184 ASSERT_EQ(net_log_
, observer_
.net_log());
185 ASSERT_EQ(NetLog::LOG_ALL_BUT_BYTES
, observer_
.log_level());
186 ASSERT_LE(net_log_
->GetLogLevel(), NetLog::LOG_ALL_BUT_BYTES
);
188 net_log_
->SetObserverLogLevel(&observer_
, NetLog::LOG_ALL
);
189 ASSERT_EQ(net_log_
, observer_
.net_log());
190 ASSERT_EQ(NetLog::LOG_ALL
, observer_
.log_level());
191 ASSERT_LE(net_log_
->GetLogLevel(), NetLog::LOG_ALL
);
193 net_log_
->DeprecatedRemoveObserver(&observer_
);
194 ASSERT_TRUE(!observer_
.net_log());
198 CountingObserver observer_
;
200 DISALLOW_COPY_AND_ASSIGN(AddRemoveObserverTestThread
);
203 // Creates |kThreads| threads of type |ThreadType| and then runs them all
205 template <class ThreadType
>
206 void RunTestThreads(NetLog
* net_log
) {
207 ThreadType threads
[kThreads
];
208 base::WaitableEvent
start_event(true, false);
210 for (size_t i
= 0; i
< arraysize(threads
); ++i
) {
211 threads
[i
].Init(net_log
, &start_event
);
215 start_event
.Signal();
217 for (size_t i
= 0; i
< arraysize(threads
); ++i
)
221 // Makes sure that events on multiple threads are dispatched to all observers.
222 TEST(NetLogTest
, NetLogEventThreads
) {
225 // Attach some observers. Since they're created after |net_log|, they'll
226 // safely detach themselves on destruction.
227 CountingObserver observers
[3];
228 for (size_t i
= 0; i
< arraysize(observers
); ++i
)
229 net_log
.DeprecatedAddObserver(&observers
[i
], NetLog::LOG_ALL
);
231 // Run a bunch of threads to completion, each of which will emit events to
233 RunTestThreads
<AddEventsTestThread
>(&net_log
);
235 // Check that each observer saw the emitted events.
236 const int kTotalEvents
= kThreads
* kEvents
;
237 for (size_t i
= 0; i
< arraysize(observers
); ++i
)
238 EXPECT_EQ(kTotalEvents
, observers
[i
].count());
241 // Test adding and removing a single observer.
242 TEST(NetLogTest
, NetLogAddRemoveObserver
) {
244 CountingObserver observer
;
247 EXPECT_EQ(0, observer
.count());
248 EXPECT_EQ(NULL
, observer
.net_log());
249 EXPECT_EQ(NetLog::LOG_NONE
, net_log
.GetLogLevel());
251 // Add the observer and add an event.
252 net_log
.DeprecatedAddObserver(&observer
, NetLog::LOG_ALL_BUT_BYTES
);
253 EXPECT_EQ(&net_log
, observer
.net_log());
254 EXPECT_EQ(NetLog::LOG_ALL_BUT_BYTES
, observer
.log_level());
255 EXPECT_EQ(NetLog::LOG_ALL_BUT_BYTES
, net_log
.GetLogLevel());
258 EXPECT_EQ(1, observer
.count());
260 // Change the observer's logging level and add an event.
261 net_log
.SetObserverLogLevel(&observer
, NetLog::LOG_ALL
);
262 EXPECT_EQ(&net_log
, observer
.net_log());
263 EXPECT_EQ(NetLog::LOG_ALL
, observer
.log_level());
264 EXPECT_EQ(NetLog::LOG_ALL
, net_log
.GetLogLevel());
267 EXPECT_EQ(2, observer
.count());
269 // Remove observer and add an event.
270 net_log
.DeprecatedRemoveObserver(&observer
);
271 EXPECT_EQ(NULL
, observer
.net_log());
272 EXPECT_EQ(NetLog::LOG_NONE
, net_log
.GetLogLevel());
275 EXPECT_EQ(2, observer
.count());
277 // Add the observer a final time, and add an event.
278 net_log
.DeprecatedAddObserver(&observer
, NetLog::LOG_ALL
);
279 EXPECT_EQ(&net_log
, observer
.net_log());
280 EXPECT_EQ(NetLog::LOG_ALL
, observer
.log_level());
281 EXPECT_EQ(NetLog::LOG_ALL
, net_log
.GetLogLevel());
284 EXPECT_EQ(3, observer
.count());
287 // Test adding and removing two observers at different log levels.
288 TEST(NetLogTest
, NetLogTwoObservers
) {
290 LoggingObserver observer
[2];
292 // Add first observer.
293 net_log
.DeprecatedAddObserver(&observer
[0], NetLog::LOG_ALL_BUT_BYTES
);
294 EXPECT_EQ(&net_log
, observer
[0].net_log());
295 EXPECT_EQ(NULL
, observer
[1].net_log());
296 EXPECT_EQ(NetLog::LOG_ALL_BUT_BYTES
, observer
[0].log_level());
297 EXPECT_EQ(NetLog::LOG_ALL_BUT_BYTES
, net_log
.GetLogLevel());
299 // Add second observer observer.
300 net_log
.DeprecatedAddObserver(&observer
[1], NetLog::LOG_ALL
);
301 EXPECT_EQ(&net_log
, observer
[0].net_log());
302 EXPECT_EQ(&net_log
, observer
[1].net_log());
303 EXPECT_EQ(NetLog::LOG_ALL_BUT_BYTES
, observer
[0].log_level());
304 EXPECT_EQ(NetLog::LOG_ALL
, observer
[1].log_level());
305 EXPECT_EQ(NetLog::LOG_ALL
, net_log
.GetLogLevel());
307 // Add event and make sure both observers receive it at their respective log
311 ASSERT_EQ(1U, observer
[0].GetNumValues());
312 ASSERT_TRUE(observer
[0].GetValue(0)->GetInteger("params", ¶m
));
313 EXPECT_EQ(observer
[0].log_level(), param
);
314 ASSERT_EQ(1U, observer
[1].GetNumValues());
315 ASSERT_TRUE(observer
[1].GetValue(0)->GetInteger("params", ¶m
));
316 EXPECT_EQ(observer
[1].log_level(), param
);
318 // Remove second observer.
319 net_log
.DeprecatedRemoveObserver(&observer
[1]);
320 EXPECT_EQ(&net_log
, observer
[0].net_log());
321 EXPECT_EQ(NULL
, observer
[1].net_log());
322 EXPECT_EQ(NetLog::LOG_ALL_BUT_BYTES
, observer
[0].log_level());
323 EXPECT_EQ(NetLog::LOG_ALL_BUT_BYTES
, net_log
.GetLogLevel());
325 // Add event and make sure only second observer gets it.
327 EXPECT_EQ(2U, observer
[0].GetNumValues());
328 EXPECT_EQ(1U, observer
[1].GetNumValues());
330 // Remove first observer.
331 net_log
.DeprecatedRemoveObserver(&observer
[0]);
332 EXPECT_EQ(NULL
, observer
[0].net_log());
333 EXPECT_EQ(NULL
, observer
[1].net_log());
334 EXPECT_EQ(NetLog::LOG_NONE
, net_log
.GetLogLevel());
336 // Add event and make sure neither observer gets it.
338 EXPECT_EQ(2U, observer
[0].GetNumValues());
339 EXPECT_EQ(1U, observer
[1].GetNumValues());
342 // Makes sure that adding and removing observers simultaneously on different
344 TEST(NetLogTest
, NetLogAddRemoveObserverThreads
) {
347 // Run a bunch of threads to completion, each of which will repeatedly add
348 // and remove an observer, and set its logging level.
349 RunTestThreads
<AddRemoveObserverTestThread
>(&net_log
);