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/base/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 virtual ~CountingObserver() {
80 net_log()->RemoveThreadSafeObserver(this);
83 virtual void OnAddEntry(const NetLog::Entry
& entry
) OVERRIDE
{
87 int count() const { return count_
; }
93 class LoggingObserver
: public NetLog::ThreadSafeObserver
{
97 virtual ~LoggingObserver() {
99 net_log()->RemoveThreadSafeObserver(this);
102 virtual void OnAddEntry(const NetLog::Entry
& entry
) OVERRIDE
{
103 base::Value
* value
= entry
.ToValue();
104 base::DictionaryValue
* dict
= NULL
;
105 ASSERT_TRUE(value
->GetAsDictionary(&dict
));
106 values_
.push_back(dict
);
109 size_t GetNumValues() const { return values_
.size(); }
110 base::DictionaryValue
* GetValue(size_t index
) const { return values_
[index
]; }
113 ScopedVector
<base::DictionaryValue
> values_
;
116 base::Value
* LogLevelToValue(NetLog::LogLevel log_level
) {
117 return new base::FundamentalValue(log_level
);
120 void AddEvent(NetLog
* net_log
) {
121 net_log
->AddGlobalEntry(NetLog::TYPE_CANCELLED
, base::Bind(LogLevelToValue
));
124 // A thread that waits until an event has been signalled before calling
126 class NetLogTestThread
: public base::SimpleThread
{
129 : base::SimpleThread("NetLogTest"),
134 // We'll wait for |start_event| to be triggered before calling a subclass's
135 // subclass's RunTestThread() function.
136 void Init(NetLog
* net_log
, base::WaitableEvent
* start_event
) {
137 start_event_
= start_event
;
141 virtual void Run() OVERRIDE
{
142 start_event_
->Wait();
146 // Subclasses must override this with the code they want to run on their
148 virtual void RunTestThread() = 0;
154 // Only triggered once all threads have been created, to make it less likely
155 // each thread completes before the next one starts.
156 base::WaitableEvent
* start_event_
;
158 DISALLOW_COPY_AND_ASSIGN(NetLogTestThread
);
161 // A thread that adds a bunch of events to the NetLog.
162 class AddEventsTestThread
: public NetLogTestThread
{
164 AddEventsTestThread() {}
165 virtual ~AddEventsTestThread() {}
168 virtual void RunTestThread() OVERRIDE
{
169 for (int i
= 0; i
< kEvents
; ++i
)
173 DISALLOW_COPY_AND_ASSIGN(AddEventsTestThread
);
176 // A thread that adds and removes an observer from the NetLog repeatedly.
177 class AddRemoveObserverTestThread
: public NetLogTestThread
{
179 AddRemoveObserverTestThread() {}
181 virtual ~AddRemoveObserverTestThread() {
182 EXPECT_TRUE(!observer_
.net_log());
186 virtual void RunTestThread() OVERRIDE
{
187 for (int i
= 0; i
< kEvents
; ++i
) {
188 ASSERT_FALSE(observer_
.net_log());
190 net_log_
->AddThreadSafeObserver(&observer_
, NetLog::LOG_ALL_BUT_BYTES
);
191 ASSERT_EQ(net_log_
, observer_
.net_log());
192 ASSERT_EQ(NetLog::LOG_ALL_BUT_BYTES
, observer_
.log_level());
193 ASSERT_LE(net_log_
->GetLogLevel(), NetLog::LOG_ALL_BUT_BYTES
);
195 net_log_
->SetObserverLogLevel(&observer_
, NetLog::LOG_ALL
);
196 ASSERT_EQ(net_log_
, observer_
.net_log());
197 ASSERT_EQ(NetLog::LOG_ALL
, observer_
.log_level());
198 ASSERT_LE(net_log_
->GetLogLevel(), NetLog::LOG_ALL
);
200 net_log_
->RemoveThreadSafeObserver(&observer_
);
201 ASSERT_TRUE(!observer_
.net_log());
205 CountingObserver observer_
;
207 DISALLOW_COPY_AND_ASSIGN(AddRemoveObserverTestThread
);
210 // Creates |kThreads| threads of type |ThreadType| and then runs them all
212 template<class ThreadType
>
213 void RunTestThreads(NetLog
* net_log
) {
214 ThreadType threads
[kThreads
];
215 base::WaitableEvent
start_event(true, false);
217 for (size_t i
= 0; i
< arraysize(threads
); ++i
) {
218 threads
[i
].Init(net_log
, &start_event
);
222 start_event
.Signal();
224 for (size_t i
= 0; i
< arraysize(threads
); ++i
)
228 // Makes sure that events on multiple threads are dispatched to all observers.
229 TEST(NetLogTest
, NetLogEventThreads
) {
232 // Attach some observers. Since they're created after |net_log|, they'll
233 // safely detach themselves on destruction.
234 CountingObserver observers
[3];
235 for (size_t i
= 0; i
< arraysize(observers
); ++i
)
236 net_log
.AddThreadSafeObserver(&observers
[i
], NetLog::LOG_ALL
);
238 // Run a bunch of threads to completion, each of which will emit events to
240 RunTestThreads
<AddEventsTestThread
>(&net_log
);
242 // Check that each observer saw the emitted events.
243 const int kTotalEvents
= kThreads
* kEvents
;
244 for (size_t i
= 0; i
< arraysize(observers
); ++i
)
245 EXPECT_EQ(kTotalEvents
, observers
[i
].count());
248 // Test adding and removing a single observer.
249 TEST(NetLogTest
, NetLogAddRemoveObserver
) {
251 CountingObserver observer
;
254 EXPECT_EQ(0, observer
.count());
255 EXPECT_EQ(NULL
, observer
.net_log());
256 EXPECT_EQ(NetLog::LOG_NONE
, net_log
.GetLogLevel());
258 // Add the observer and add an event.
259 net_log
.AddThreadSafeObserver(&observer
, NetLog::LOG_ALL_BUT_BYTES
);
260 EXPECT_EQ(&net_log
, observer
.net_log());
261 EXPECT_EQ(NetLog::LOG_ALL_BUT_BYTES
, observer
.log_level());
262 EXPECT_EQ(NetLog::LOG_ALL_BUT_BYTES
, net_log
.GetLogLevel());
265 EXPECT_EQ(1, observer
.count());
267 // Change the observer's logging level and add an event.
268 net_log
.SetObserverLogLevel(&observer
, NetLog::LOG_ALL
);
269 EXPECT_EQ(&net_log
, observer
.net_log());
270 EXPECT_EQ(NetLog::LOG_ALL
, observer
.log_level());
271 EXPECT_EQ(NetLog::LOG_ALL
, net_log
.GetLogLevel());
274 EXPECT_EQ(2, observer
.count());
276 // Remove observer and add an event.
277 net_log
.RemoveThreadSafeObserver(&observer
);
278 EXPECT_EQ(NULL
, observer
.net_log());
279 EXPECT_EQ(NetLog::LOG_NONE
, net_log
.GetLogLevel());
282 EXPECT_EQ(2, observer
.count());
284 // Add the observer a final time, and add an event.
285 net_log
.AddThreadSafeObserver(&observer
, NetLog::LOG_ALL
);
286 EXPECT_EQ(&net_log
, observer
.net_log());
287 EXPECT_EQ(NetLog::LOG_ALL
, observer
.log_level());
288 EXPECT_EQ(NetLog::LOG_ALL
, net_log
.GetLogLevel());
291 EXPECT_EQ(3, observer
.count());
294 // Test adding and removing two observers at different log levels.
295 TEST(NetLogTest
, NetLogTwoObservers
) {
297 LoggingObserver observer
[2];
299 // Add first observer.
300 net_log
.AddThreadSafeObserver(&observer
[0], NetLog::LOG_ALL_BUT_BYTES
);
301 EXPECT_EQ(&net_log
, observer
[0].net_log());
302 EXPECT_EQ(NULL
, observer
[1].net_log());
303 EXPECT_EQ(NetLog::LOG_ALL_BUT_BYTES
, observer
[0].log_level());
304 EXPECT_EQ(NetLog::LOG_ALL_BUT_BYTES
, net_log
.GetLogLevel());
306 // Add second observer observer.
307 net_log
.AddThreadSafeObserver(&observer
[1], NetLog::LOG_ALL
);
308 EXPECT_EQ(&net_log
, observer
[0].net_log());
309 EXPECT_EQ(&net_log
, observer
[1].net_log());
310 EXPECT_EQ(NetLog::LOG_ALL_BUT_BYTES
, observer
[0].log_level());
311 EXPECT_EQ(NetLog::LOG_ALL
, observer
[1].log_level());
312 EXPECT_EQ(NetLog::LOG_ALL
, net_log
.GetLogLevel());
314 // Add event and make sure both observers receive it at their respective log
318 ASSERT_EQ(1U, observer
[0].GetNumValues());
319 ASSERT_TRUE(observer
[0].GetValue(0)->GetInteger("params", ¶m
));
320 EXPECT_EQ(observer
[0].log_level(), param
);
321 ASSERT_EQ(1U, observer
[1].GetNumValues());
322 ASSERT_TRUE(observer
[1].GetValue(0)->GetInteger("params", ¶m
));
323 EXPECT_EQ(observer
[1].log_level(), param
);
325 // Remove second observer.
326 net_log
.RemoveThreadSafeObserver(&observer
[1]);
327 EXPECT_EQ(&net_log
, observer
[0].net_log());
328 EXPECT_EQ(NULL
, observer
[1].net_log());
329 EXPECT_EQ(NetLog::LOG_ALL_BUT_BYTES
, observer
[0].log_level());
330 EXPECT_EQ(NetLog::LOG_ALL_BUT_BYTES
, net_log
.GetLogLevel());
332 // Add event and make sure only second observer gets it.
334 EXPECT_EQ(2U, observer
[0].GetNumValues());
335 EXPECT_EQ(1U, observer
[1].GetNumValues());
337 // Remove first observer.
338 net_log
.RemoveThreadSafeObserver(&observer
[0]);
339 EXPECT_EQ(NULL
, observer
[0].net_log());
340 EXPECT_EQ(NULL
, observer
[1].net_log());
341 EXPECT_EQ(NetLog::LOG_NONE
, net_log
.GetLogLevel());
343 // Add event and make sure neither observer gets it.
345 EXPECT_EQ(2U, observer
[0].GetNumValues());
346 EXPECT_EQ(1U, observer
[1].GetNumValues());
349 // Makes sure that adding and removing observers simultaneously on different
351 TEST(NetLogTest
, NetLogAddRemoveObserverThreads
) {
354 // Run a bunch of threads to completion, each of which will repeatedly add
355 // and remove an observer, and set its logging level.
356 RunTestThreads
<AddRemoveObserverTestThread
>(&net_log
);