| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "chrome/browser/net/chrome_net_log.h" | 5 #include "chrome/browser/net/chrome_net_log.h" |
| 6 | 6 |
| 7 #include "base/synchronization/waitable_event.h" | 7 #include "base/synchronization/waitable_event.h" |
| 8 #include "base/threading/simple_thread.h" | 8 #include "base/threading/simple_thread.h" |
| 9 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
| 10 | 10 |
| 11 namespace { | 11 namespace { |
| 12 | 12 |
| 13 const int kThreads = 10; | 13 const int kThreads = 10; |
| 14 const int kEvents = 100; | 14 const int kEvents = 100; |
| 15 | 15 |
| 16 class CountingObserver : public net::NetLog::ThreadSafeObserver { | 16 class CountingObserver : public net::NetLog::ThreadSafeObserver { |
| 17 public: | 17 public: |
| 18 CountingObserver() : count_(0) {} | 18 CountingObserver() : count_(0) {} |
| 19 | 19 |
| 20 ~CountingObserver() { | 20 virtual ~CountingObserver() { |
| 21 if (net_log()) | 21 if (net_log()) |
| 22 net_log()->RemoveThreadSafeObserver(this); | 22 net_log()->RemoveThreadSafeObserver(this); |
| 23 } | 23 } |
| 24 | 24 |
| 25 virtual void OnAddEntry(const net::NetLog::Entry& entry) OVERRIDE { | 25 virtual void OnAddEntry(const net::NetLog::Entry& entry) OVERRIDE { |
| 26 ++count_; | 26 ++count_; |
| 27 } | 27 } |
| 28 | 28 |
| 29 int count() const { return count_; } | 29 int count() const { return count_; } |
| 30 | 30 |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 69 // each thread completes before the next one starts. | 69 // each thread completes before the next one starts. |
| 70 base::WaitableEvent* start_event_; | 70 base::WaitableEvent* start_event_; |
| 71 | 71 |
| 72 DISALLOW_COPY_AND_ASSIGN(ChromeNetLogTestThread); | 72 DISALLOW_COPY_AND_ASSIGN(ChromeNetLogTestThread); |
| 73 }; | 73 }; |
| 74 | 74 |
| 75 // A thread that adds a bunch of events to the NetLog. | 75 // A thread that adds a bunch of events to the NetLog. |
| 76 class AddEventsTestThread : public ChromeNetLogTestThread { | 76 class AddEventsTestThread : public ChromeNetLogTestThread { |
| 77 public: | 77 public: |
| 78 AddEventsTestThread() {} | 78 AddEventsTestThread() {} |
| 79 ~AddEventsTestThread() {} | 79 virtual ~AddEventsTestThread() {} |
| 80 | 80 |
| 81 private: | 81 private: |
| 82 virtual void RunTestThread() OVERRIDE { | 82 virtual void RunTestThread() OVERRIDE { |
| 83 for (int i = 0; i < kEvents; ++i) | 83 for (int i = 0; i < kEvents; ++i) |
| 84 AddEvent(net_log_); | 84 AddEvent(net_log_); |
| 85 } | 85 } |
| 86 | 86 |
| 87 DISALLOW_COPY_AND_ASSIGN(AddEventsTestThread); | 87 DISALLOW_COPY_AND_ASSIGN(AddEventsTestThread); |
| 88 }; | 88 }; |
| 89 | 89 |
| 90 // A thread that adds and removes an observer from the NetLog repeatedly. | 90 // A thread that adds and removes an observer from the NetLog repeatedly. |
| 91 class AddRemoveObserverTestThread : public ChromeNetLogTestThread { | 91 class AddRemoveObserverTestThread : public ChromeNetLogTestThread { |
| 92 public: | 92 public: |
| 93 AddRemoveObserverTestThread() {} | 93 AddRemoveObserverTestThread() {} |
| 94 | 94 |
| 95 ~AddRemoveObserverTestThread() { | 95 virtual ~AddRemoveObserverTestThread() { |
| 96 EXPECT_TRUE(!observer_.net_log()); | 96 EXPECT_TRUE(!observer_.net_log()); |
| 97 } | 97 } |
| 98 | 98 |
| 99 private: | 99 private: |
| 100 virtual void RunTestThread() OVERRIDE { | 100 virtual void RunTestThread() OVERRIDE { |
| 101 for (int i = 0; i < kEvents; ++i) { | 101 for (int i = 0; i < kEvents; ++i) { |
| 102 ASSERT_FALSE(observer_.net_log()); | 102 ASSERT_FALSE(observer_.net_log()); |
| 103 | 103 |
| 104 net_log_->AddThreadSafeObserver(&observer_, net::NetLog::LOG_BASIC); | 104 net_log_->AddThreadSafeObserver(&observer_, net::NetLog::LOG_BASIC); |
| 105 ASSERT_EQ(net_log_, observer_.net_log()); | 105 ASSERT_EQ(net_log_, observer_.net_log()); |
| (...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 262 // threads works. | 262 // threads works. |
| 263 TEST(ChromeNetLogTest, NetLogAddRemoveObserverThreads) { | 263 TEST(ChromeNetLogTest, NetLogAddRemoveObserverThreads) { |
| 264 ChromeNetLog net_log; | 264 ChromeNetLog net_log; |
| 265 | 265 |
| 266 // Run a bunch of threads to completion, each of which will repeatedly add | 266 // Run a bunch of threads to completion, each of which will repeatedly add |
| 267 // and remove an observer, and set its logging level. | 267 // and remove an observer, and set its logging level. |
| 268 RunTestThreads<AddRemoveObserverTestThread>(&net_log); | 268 RunTestThreads<AddRemoveObserverTestThread>(&net_log); |
| 269 } | 269 } |
| 270 | 270 |
| 271 } // namespace | 271 } // namespace |
| OLD | NEW |