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 { |
84 ++count_; | 84 ++count_; |
85 } | 85 } |
86 | 86 |
87 int count() const { return count_; } | 87 int count() const { return count_; } |
88 | 88 |
89 private: | 89 private: |
90 int count_; | 90 int count_; |
91 }; | 91 }; |
92 | 92 |
93 class LoggingObserver : public NetLog::ThreadSafeObserver { | 93 class LoggingObserver : public NetLog::ThreadSafeObserver { |
94 public: | 94 public: |
95 LoggingObserver() {} | 95 LoggingObserver() {} |
96 | 96 |
97 virtual ~LoggingObserver() { | 97 virtual ~LoggingObserver() { |
98 if (net_log()) | 98 if (net_log()) |
99 net_log()->RemoveThreadSafeObserver(this); | 99 net_log()->RemoveThreadSafeObserver(this); |
100 } | 100 } |
101 | 101 |
102 virtual void OnAddEntry(const NetLog::Entry& entry) OVERRIDE { | 102 virtual void OnAddEntry(const NetLog::Entry& entry) override { |
103 base::Value* value = entry.ToValue(); | 103 base::Value* value = entry.ToValue(); |
104 base::DictionaryValue* dict = NULL; | 104 base::DictionaryValue* dict = NULL; |
105 ASSERT_TRUE(value->GetAsDictionary(&dict)); | 105 ASSERT_TRUE(value->GetAsDictionary(&dict)); |
106 values_.push_back(dict); | 106 values_.push_back(dict); |
107 } | 107 } |
108 | 108 |
109 size_t GetNumValues() const { return values_.size(); } | 109 size_t GetNumValues() const { return values_.size(); } |
110 base::DictionaryValue* GetValue(size_t index) const { return values_[index]; } | 110 base::DictionaryValue* GetValue(size_t index) const { return values_[index]; } |
111 | 111 |
112 private: | 112 private: |
(...skipping 18 matching lines...) Expand all Loading... |
131 start_event_(NULL) { | 131 start_event_(NULL) { |
132 } | 132 } |
133 | 133 |
134 // We'll wait for |start_event| to be triggered before calling a subclass's | 134 // We'll wait for |start_event| to be triggered before calling a subclass's |
135 // subclass's RunTestThread() function. | 135 // subclass's RunTestThread() function. |
136 void Init(NetLog* net_log, base::WaitableEvent* start_event) { | 136 void Init(NetLog* net_log, base::WaitableEvent* start_event) { |
137 start_event_ = start_event; | 137 start_event_ = start_event; |
138 net_log_ = net_log; | 138 net_log_ = net_log; |
139 } | 139 } |
140 | 140 |
141 virtual void Run() OVERRIDE { | 141 virtual void Run() override { |
142 start_event_->Wait(); | 142 start_event_->Wait(); |
143 RunTestThread(); | 143 RunTestThread(); |
144 } | 144 } |
145 | 145 |
146 // Subclasses must override this with the code they want to run on their | 146 // Subclasses must override this with the code they want to run on their |
147 // thread. | 147 // thread. |
148 virtual void RunTestThread() = 0; | 148 virtual void RunTestThread() = 0; |
149 | 149 |
150 protected: | 150 protected: |
151 NetLog* net_log_; | 151 NetLog* net_log_; |
152 | 152 |
153 private: | 153 private: |
154 // Only triggered once all threads have been created, to make it less likely | 154 // Only triggered once all threads have been created, to make it less likely |
155 // each thread completes before the next one starts. | 155 // each thread completes before the next one starts. |
156 base::WaitableEvent* start_event_; | 156 base::WaitableEvent* start_event_; |
157 | 157 |
158 DISALLOW_COPY_AND_ASSIGN(NetLogTestThread); | 158 DISALLOW_COPY_AND_ASSIGN(NetLogTestThread); |
159 }; | 159 }; |
160 | 160 |
161 // A thread that adds a bunch of events to the NetLog. | 161 // A thread that adds a bunch of events to the NetLog. |
162 class AddEventsTestThread : public NetLogTestThread { | 162 class AddEventsTestThread : public NetLogTestThread { |
163 public: | 163 public: |
164 AddEventsTestThread() {} | 164 AddEventsTestThread() {} |
165 virtual ~AddEventsTestThread() {} | 165 virtual ~AddEventsTestThread() {} |
166 | 166 |
167 private: | 167 private: |
168 virtual void RunTestThread() OVERRIDE { | 168 virtual void RunTestThread() override { |
169 for (int i = 0; i < kEvents; ++i) | 169 for (int i = 0; i < kEvents; ++i) |
170 AddEvent(net_log_); | 170 AddEvent(net_log_); |
171 } | 171 } |
172 | 172 |
173 DISALLOW_COPY_AND_ASSIGN(AddEventsTestThread); | 173 DISALLOW_COPY_AND_ASSIGN(AddEventsTestThread); |
174 }; | 174 }; |
175 | 175 |
176 // A thread that adds and removes an observer from the NetLog repeatedly. | 176 // A thread that adds and removes an observer from the NetLog repeatedly. |
177 class AddRemoveObserverTestThread : public NetLogTestThread { | 177 class AddRemoveObserverTestThread : public NetLogTestThread { |
178 public: | 178 public: |
179 AddRemoveObserverTestThread() {} | 179 AddRemoveObserverTestThread() {} |
180 | 180 |
181 virtual ~AddRemoveObserverTestThread() { | 181 virtual ~AddRemoveObserverTestThread() { |
182 EXPECT_TRUE(!observer_.net_log()); | 182 EXPECT_TRUE(!observer_.net_log()); |
183 } | 183 } |
184 | 184 |
185 private: | 185 private: |
186 virtual void RunTestThread() OVERRIDE { | 186 virtual void RunTestThread() override { |
187 for (int i = 0; i < kEvents; ++i) { | 187 for (int i = 0; i < kEvents; ++i) { |
188 ASSERT_FALSE(observer_.net_log()); | 188 ASSERT_FALSE(observer_.net_log()); |
189 | 189 |
190 net_log_->AddThreadSafeObserver(&observer_, NetLog::LOG_ALL_BUT_BYTES); | 190 net_log_->AddThreadSafeObserver(&observer_, NetLog::LOG_ALL_BUT_BYTES); |
191 ASSERT_EQ(net_log_, observer_.net_log()); | 191 ASSERT_EQ(net_log_, observer_.net_log()); |
192 ASSERT_EQ(NetLog::LOG_ALL_BUT_BYTES, observer_.log_level()); | 192 ASSERT_EQ(NetLog::LOG_ALL_BUT_BYTES, observer_.log_level()); |
193 ASSERT_LE(net_log_->GetLogLevel(), NetLog::LOG_ALL_BUT_BYTES); | 193 ASSERT_LE(net_log_->GetLogLevel(), NetLog::LOG_ALL_BUT_BYTES); |
194 | 194 |
195 net_log_->SetObserverLogLevel(&observer_, NetLog::LOG_ALL); | 195 net_log_->SetObserverLogLevel(&observer_, NetLog::LOG_ALL); |
196 ASSERT_EQ(net_log_, observer_.net_log()); | 196 ASSERT_EQ(net_log_, observer_.net_log()); |
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
352 NetLog net_log; | 352 NetLog net_log; |
353 | 353 |
354 // Run a bunch of threads to completion, each of which will repeatedly add | 354 // Run a bunch of threads to completion, each of which will repeatedly add |
355 // and remove an observer, and set its logging level. | 355 // and remove an observer, and set its logging level. |
356 RunTestThreads<AddRemoveObserverTestThread>(&net_log); | 356 RunTestThreads<AddRemoveObserverTestThread>(&net_log); |
357 } | 357 } |
358 | 358 |
359 } // namespace | 359 } // namespace |
360 | 360 |
361 } // namespace net | 361 } // namespace net |
OLD | NEW |