| 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 "base/threading/sequenced_worker_pool_unittest.h" |
| 6 |
| 5 #include <algorithm> | 7 #include <algorithm> |
| 6 | 8 |
| 7 #include "base/bind.h" | 9 #include "base/bind.h" |
| 8 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
| 9 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
| 10 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
| 11 #include "base/message_loop.h" | 13 #include "base/message_loop.h" |
| 12 #include "base/message_loop_proxy.h" | 14 #include "base/message_loop_proxy.h" |
| 13 #include "base/synchronization/condition_variable.h" | 15 #include "base/synchronization/condition_variable.h" |
| 14 #include "base/synchronization/lock.h" | 16 #include "base/synchronization/lock.h" |
| 15 #include "base/test/task_runner_test_template.h" | 17 #include "base/test/task_runner_test_template.h" |
| 16 #include "base/threading/platform_thread.h" | 18 #include "base/threading/platform_thread.h" |
| 17 #include "base/threading/sequenced_worker_pool.h" | |
| 18 #include "base/tracked_objects.h" | 19 #include "base/tracked_objects.h" |
| 19 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
| 20 | 21 |
| 21 namespace base { | 22 namespace base { |
| 22 | 23 |
| 24 SequencedWorkerPoolOwner::SequencedWorkerPoolOwner( |
| 25 size_t max_threads, |
| 26 const std::string& thread_name_prefix) |
| 27 : constructor_message_loop_(MessageLoop::current()), |
| 28 pool_(new SequencedWorkerPool(max_threads, thread_name_prefix)) { |
| 29 pool_->SetTestingObserver(this); |
| 30 } |
| 31 |
| 32 SequencedWorkerPoolOwner::~SequencedWorkerPoolOwner() { |
| 33 pool_ = NULL; |
| 34 MessageLoop::current()->Run(); |
| 35 } |
| 36 |
| 37 const scoped_refptr<SequencedWorkerPool>& SequencedWorkerPoolOwner::pool() { |
| 38 return pool_; |
| 39 } |
| 40 |
| 41 void SequencedWorkerPoolOwner::SetWillWaitForShutdownCallback( |
| 42 const Closure& callback) { |
| 43 will_wait_for_shutdown_callback_ = callback; |
| 44 } |
| 45 |
| 46 void SequencedWorkerPoolOwner::WillWaitForShutdown() { |
| 47 if (!will_wait_for_shutdown_callback_.is_null()) { |
| 48 will_wait_for_shutdown_callback_.Run(); |
| 49 } |
| 50 } |
| 51 |
| 52 void SequencedWorkerPoolOwner::OnDestruct() { |
| 53 constructor_message_loop_->PostTask( |
| 54 FROM_HERE, |
| 55 constructor_message_loop_->QuitClosure()); |
| 56 } |
| 57 |
| 23 // IMPORTANT NOTE: | 58 // IMPORTANT NOTE: |
| 24 // | 59 // |
| 25 // Many of these tests have failure modes where they'll hang forever. These | 60 // Many of these tests have failure modes where they'll hang forever. These |
| 26 // tests should not be flaky, and hangling indicates a type of failure. Do not | 61 // tests should not be flaky, and hangling indicates a type of failure. Do not |
| 27 // mark as flaky if they're hanging, it's likely an actual bug. | 62 // mark as flaky if they're hanging, it's likely an actual bug. |
| 28 | 63 |
| 29 namespace { | 64 namespace { |
| 30 | 65 |
| 31 const size_t kNumWorkerThreads = 3; | 66 const size_t kNumWorkerThreads = 3; |
| 32 | 67 |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 138 | 173 |
| 139 base::ConditionVariable cond_var_; | 174 base::ConditionVariable cond_var_; |
| 140 | 175 |
| 141 // Protected by lock_. | 176 // Protected by lock_. |
| 142 std::vector<int> complete_sequence_; | 177 std::vector<int> complete_sequence_; |
| 143 | 178 |
| 144 // Counter of the number of "block" workers that have started. | 179 // Counter of the number of "block" workers that have started. |
| 145 size_t started_events_; | 180 size_t started_events_; |
| 146 }; | 181 }; |
| 147 | 182 |
| 148 // Wrapper around SequencedWorkerPool that blocks destruction until | |
| 149 // the pool is actually destroyed. This is so that a | |
| 150 // SequencedWorkerPool from one test doesn't outlive its test and | |
| 151 // cause strange races with other tests that touch global stuff (like | |
| 152 // histograms and logging). However, this requires that nothing else | |
| 153 // on this thread holds a ref to the pool when the | |
| 154 // SequencedWorkerPoolOwner is destroyed. | |
| 155 class SequencedWorkerPoolOwner : public SequencedWorkerPool::TestingObserver { | |
| 156 public: | |
| 157 SequencedWorkerPoolOwner(size_t max_threads, | |
| 158 const std::string& thread_name_prefix) | |
| 159 : constructor_message_loop_(MessageLoop::current()), | |
| 160 pool_(new SequencedWorkerPool(max_threads, thread_name_prefix)) { | |
| 161 pool_->SetTestingObserver(this); | |
| 162 } | |
| 163 | |
| 164 virtual ~SequencedWorkerPoolOwner() { | |
| 165 pool_ = NULL; | |
| 166 MessageLoop::current()->Run(); | |
| 167 } | |
| 168 | |
| 169 // Don't change the return pool's testing observer. | |
| 170 const scoped_refptr<SequencedWorkerPool>& pool() { | |
| 171 return pool_; | |
| 172 } | |
| 173 | |
| 174 // The given callback will be called on WillWaitForShutdown(). | |
| 175 void SetWillWaitForShutdownCallback(const Closure& callback) { | |
| 176 will_wait_for_shutdown_callback_ = callback; | |
| 177 } | |
| 178 | |
| 179 private: | |
| 180 // SequencedWorkerPool::TestingObserver implementation. | |
| 181 virtual void WillWaitForShutdown() OVERRIDE { | |
| 182 if (!will_wait_for_shutdown_callback_.is_null()) { | |
| 183 will_wait_for_shutdown_callback_.Run(); | |
| 184 } | |
| 185 } | |
| 186 | |
| 187 virtual void OnDestruct() OVERRIDE { | |
| 188 constructor_message_loop_->PostTask( | |
| 189 FROM_HERE, | |
| 190 constructor_message_loop_->QuitClosure()); | |
| 191 } | |
| 192 | |
| 193 MessageLoop* const constructor_message_loop_; | |
| 194 scoped_refptr<SequencedWorkerPool> pool_; | |
| 195 Closure will_wait_for_shutdown_callback_; | |
| 196 | |
| 197 DISALLOW_COPY_AND_ASSIGN(SequencedWorkerPoolOwner); | |
| 198 }; | |
| 199 | |
| 200 class SequencedWorkerPoolTest : public testing::Test { | 183 class SequencedWorkerPoolTest : public testing::Test { |
| 201 public: | 184 public: |
| 202 SequencedWorkerPoolTest() | 185 SequencedWorkerPoolTest() |
| 203 : pool_owner_(kNumWorkerThreads, "test"), | 186 : pool_owner_(kNumWorkerThreads, "test"), |
| 204 tracker_(new TestTracker) {} | 187 tracker_(new TestTracker) {} |
| 205 | 188 |
| 206 ~SequencedWorkerPoolTest() {} | 189 ~SequencedWorkerPoolTest() {} |
| 207 | 190 |
| 208 virtual void SetUp() {} | 191 virtual void SetUp() {} |
| 209 | 192 |
| (...skipping 314 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 524 scoped_ptr<SequencedWorkerPoolOwner> pool_owner_; | 507 scoped_ptr<SequencedWorkerPoolOwner> pool_owner_; |
| 525 }; | 508 }; |
| 526 | 509 |
| 527 INSTANTIATE_TYPED_TEST_CASE_P( | 510 INSTANTIATE_TYPED_TEST_CASE_P( |
| 528 SequencedWorkerPool, TaskRunnerTest, | 511 SequencedWorkerPool, TaskRunnerTest, |
| 529 SequencedWorkerPoolTaskRunnerTestDelegate); | 512 SequencedWorkerPoolTaskRunnerTestDelegate); |
| 530 | 513 |
| 531 } // namespace | 514 } // namespace |
| 532 | 515 |
| 533 } // namespace base | 516 } // namespace base |
| OLD | NEW |