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