OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/base/net_log_unittest.h" | 5 #include "net/base/net_log_unittest.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/memory/scoped_vector.h" | 8 #include "base/memory/scoped_vector.h" |
9 #include "base/synchronization/waitable_event.h" | 9 #include "base/synchronization/waitable_event.h" |
10 #include "base/threading/simple_thread.h" | 10 #include "base/threading/simple_thread.h" |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
73 | 73 |
74 class CountingObserver : public NetLog::ThreadSafeObserver { | 74 class CountingObserver : public NetLog::ThreadSafeObserver { |
75 public: | 75 public: |
76 CountingObserver() : count_(0) {} | 76 CountingObserver() : count_(0) {} |
77 | 77 |
78 virtual ~CountingObserver() { | 78 virtual ~CountingObserver() { |
79 if (net_log()) | 79 if (net_log()) |
80 net_log()->RemoveThreadSafeObserver(this); | 80 net_log()->RemoveThreadSafeObserver(this); |
81 } | 81 } |
82 | 82 |
83 virtual void OnAddEntry(const NetLog::Entry& entry) OVERRIDE { | 83 virtual void OnAddEntry(const NetLog::Entry& entry) OVERRIDE { ++count_; } |
84 ++count_; | |
85 } | |
86 | 84 |
87 int count() const { return count_; } | 85 int count() const { return count_; } |
88 | 86 |
89 private: | 87 private: |
90 int count_; | 88 int count_; |
91 }; | 89 }; |
92 | 90 |
93 class LoggingObserver : public NetLog::ThreadSafeObserver { | 91 class LoggingObserver : public NetLog::ThreadSafeObserver { |
94 public: | 92 public: |
95 LoggingObserver() {} | 93 LoggingObserver() {} |
(...skipping 23 matching lines...) Expand all Loading... |
119 | 117 |
120 void AddEvent(NetLog* net_log) { | 118 void AddEvent(NetLog* net_log) { |
121 net_log->AddGlobalEntry(NetLog::TYPE_CANCELLED, base::Bind(LogLevelToValue)); | 119 net_log->AddGlobalEntry(NetLog::TYPE_CANCELLED, base::Bind(LogLevelToValue)); |
122 } | 120 } |
123 | 121 |
124 // A thread that waits until an event has been signalled before calling | 122 // A thread that waits until an event has been signalled before calling |
125 // RunTestThread. | 123 // RunTestThread. |
126 class NetLogTestThread : public base::SimpleThread { | 124 class NetLogTestThread : public base::SimpleThread { |
127 public: | 125 public: |
128 NetLogTestThread() | 126 NetLogTestThread() |
129 : base::SimpleThread("NetLogTest"), | 127 : base::SimpleThread("NetLogTest"), net_log_(NULL), start_event_(NULL) {} |
130 net_log_(NULL), | |
131 start_event_(NULL) { | |
132 } | |
133 | 128 |
134 // We'll wait for |start_event| to be triggered before calling a subclass's | 129 // We'll wait for |start_event| to be triggered before calling a subclass's |
135 // subclass's RunTestThread() function. | 130 // subclass's RunTestThread() function. |
136 void Init(NetLog* net_log, base::WaitableEvent* start_event) { | 131 void Init(NetLog* net_log, base::WaitableEvent* start_event) { |
137 start_event_ = start_event; | 132 start_event_ = start_event; |
138 net_log_ = net_log; | 133 net_log_ = net_log; |
139 } | 134 } |
140 | 135 |
141 virtual void Run() OVERRIDE { | 136 virtual void Run() OVERRIDE { |
142 start_event_->Wait(); | 137 start_event_->Wait(); |
(...skipping 28 matching lines...) Expand all Loading... |
171 } | 166 } |
172 | 167 |
173 DISALLOW_COPY_AND_ASSIGN(AddEventsTestThread); | 168 DISALLOW_COPY_AND_ASSIGN(AddEventsTestThread); |
174 }; | 169 }; |
175 | 170 |
176 // A thread that adds and removes an observer from the NetLog repeatedly. | 171 // A thread that adds and removes an observer from the NetLog repeatedly. |
177 class AddRemoveObserverTestThread : public NetLogTestThread { | 172 class AddRemoveObserverTestThread : public NetLogTestThread { |
178 public: | 173 public: |
179 AddRemoveObserverTestThread() {} | 174 AddRemoveObserverTestThread() {} |
180 | 175 |
181 virtual ~AddRemoveObserverTestThread() { | 176 virtual ~AddRemoveObserverTestThread() { EXPECT_TRUE(!observer_.net_log()); } |
182 EXPECT_TRUE(!observer_.net_log()); | |
183 } | |
184 | 177 |
185 private: | 178 private: |
186 virtual void RunTestThread() OVERRIDE { | 179 virtual void RunTestThread() OVERRIDE { |
187 for (int i = 0; i < kEvents; ++i) { | 180 for (int i = 0; i < kEvents; ++i) { |
188 ASSERT_FALSE(observer_.net_log()); | 181 ASSERT_FALSE(observer_.net_log()); |
189 | 182 |
190 net_log_->AddThreadSafeObserver(&observer_, NetLog::LOG_ALL_BUT_BYTES); | 183 net_log_->AddThreadSafeObserver(&observer_, NetLog::LOG_ALL_BUT_BYTES); |
191 ASSERT_EQ(net_log_, observer_.net_log()); | 184 ASSERT_EQ(net_log_, observer_.net_log()); |
192 ASSERT_EQ(NetLog::LOG_ALL_BUT_BYTES, observer_.log_level()); | 185 ASSERT_EQ(NetLog::LOG_ALL_BUT_BYTES, observer_.log_level()); |
193 ASSERT_LE(net_log_->GetLogLevel(), NetLog::LOG_ALL_BUT_BYTES); | 186 ASSERT_LE(net_log_->GetLogLevel(), NetLog::LOG_ALL_BUT_BYTES); |
194 | 187 |
195 net_log_->SetObserverLogLevel(&observer_, NetLog::LOG_ALL); | 188 net_log_->SetObserverLogLevel(&observer_, NetLog::LOG_ALL); |
196 ASSERT_EQ(net_log_, observer_.net_log()); | 189 ASSERT_EQ(net_log_, observer_.net_log()); |
197 ASSERT_EQ(NetLog::LOG_ALL, observer_.log_level()); | 190 ASSERT_EQ(NetLog::LOG_ALL, observer_.log_level()); |
198 ASSERT_LE(net_log_->GetLogLevel(), NetLog::LOG_ALL); | 191 ASSERT_LE(net_log_->GetLogLevel(), NetLog::LOG_ALL); |
199 | 192 |
200 net_log_->RemoveThreadSafeObserver(&observer_); | 193 net_log_->RemoveThreadSafeObserver(&observer_); |
201 ASSERT_TRUE(!observer_.net_log()); | 194 ASSERT_TRUE(!observer_.net_log()); |
202 } | 195 } |
203 } | 196 } |
204 | 197 |
205 CountingObserver observer_; | 198 CountingObserver observer_; |
206 | 199 |
207 DISALLOW_COPY_AND_ASSIGN(AddRemoveObserverTestThread); | 200 DISALLOW_COPY_AND_ASSIGN(AddRemoveObserverTestThread); |
208 }; | 201 }; |
209 | 202 |
210 // Creates |kThreads| threads of type |ThreadType| and then runs them all | 203 // Creates |kThreads| threads of type |ThreadType| and then runs them all |
211 // to completion. | 204 // to completion. |
212 template<class ThreadType> | 205 template <class ThreadType> |
213 void RunTestThreads(NetLog* net_log) { | 206 void RunTestThreads(NetLog* net_log) { |
214 ThreadType threads[kThreads]; | 207 ThreadType threads[kThreads]; |
215 base::WaitableEvent start_event(true, false); | 208 base::WaitableEvent start_event(true, false); |
216 | 209 |
217 for (size_t i = 0; i < arraysize(threads); ++i) { | 210 for (size_t i = 0; i < arraysize(threads); ++i) { |
218 threads[i].Init(net_log, &start_event); | 211 threads[i].Init(net_log, &start_event); |
219 threads[i].Start(); | 212 threads[i].Start(); |
220 } | 213 } |
221 | 214 |
222 start_event.Signal(); | 215 start_event.Signal(); |
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
352 NetLog net_log; | 345 NetLog net_log; |
353 | 346 |
354 // Run a bunch of threads to completion, each of which will repeatedly add | 347 // Run a bunch of threads to completion, each of which will repeatedly add |
355 // and remove an observer, and set its logging level. | 348 // and remove an observer, and set its logging level. |
356 RunTestThreads<AddRemoveObserverTestThread>(&net_log); | 349 RunTestThreads<AddRemoveObserverTestThread>(&net_log); |
357 } | 350 } |
358 | 351 |
359 } // namespace | 352 } // namespace |
360 | 353 |
361 } // namespace net | 354 } // namespace net |
OLD | NEW |