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( |
| 29 max_threads, thread_name_prefix, |
| 30 ALLOW_THIS_IN_INITIALIZER_LIST(this))), |
| 31 has_work_call_count_(0) {} |
| 32 |
| 33 SequencedWorkerPoolOwner::~SequencedWorkerPoolOwner() { |
| 34 pool_ = NULL; |
| 35 MessageLoop::current()->Run(); |
| 36 } |
| 37 |
| 38 const scoped_refptr<SequencedWorkerPool>& SequencedWorkerPoolOwner::pool() { |
| 39 return pool_; |
| 40 } |
| 41 |
| 42 void SequencedWorkerPoolOwner::SetWillWaitForShutdownCallback( |
| 43 const Closure& callback) { |
| 44 will_wait_for_shutdown_callback_ = callback; |
| 45 } |
| 46 |
| 47 int SequencedWorkerPoolOwner::has_work_call_count() const { |
| 48 AutoLock lock(has_work_lock_); |
| 49 return has_work_call_count_; |
| 50 } |
| 51 |
| 52 void SequencedWorkerPoolOwner::OnHasWork() { |
| 53 AutoLock lock(has_work_lock_); |
| 54 ++has_work_call_count_; |
| 55 } |
| 56 |
| 57 void SequencedWorkerPoolOwner::WillWaitForShutdown() { |
| 58 if (!will_wait_for_shutdown_callback_.is_null()) { |
| 59 will_wait_for_shutdown_callback_.Run(); |
| 60 } |
| 61 } |
| 62 |
| 63 void SequencedWorkerPoolOwner::OnDestruct() { |
| 64 constructor_message_loop_->PostTask( |
| 65 FROM_HERE, |
| 66 constructor_message_loop_->QuitClosure()); |
| 67 } |
| 68 |
23 // IMPORTANT NOTE: | 69 // IMPORTANT NOTE: |
24 // | 70 // |
25 // Many of these tests have failure modes where they'll hang forever. These | 71 // 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 | 72 // 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. | 73 // mark as flaky if they're hanging, it's likely an actual bug. |
28 | 74 |
29 namespace { | 75 namespace { |
30 | 76 |
31 const size_t kNumWorkerThreads = 3; | 77 const size_t kNumWorkerThreads = 3; |
32 | 78 |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
138 | 184 |
139 base::ConditionVariable cond_var_; | 185 base::ConditionVariable cond_var_; |
140 | 186 |
141 // Protected by lock_. | 187 // Protected by lock_. |
142 std::vector<int> complete_sequence_; | 188 std::vector<int> complete_sequence_; |
143 | 189 |
144 // Counter of the number of "block" workers that have started. | 190 // Counter of the number of "block" workers that have started. |
145 size_t started_events_; | 191 size_t started_events_; |
146 }; | 192 }; |
147 | 193 |
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( | |
161 max_threads, thread_name_prefix, | |
162 ALLOW_THIS_IN_INITIALIZER_LIST(this))), | |
163 has_work_call_count_(0) {} | |
164 | |
165 virtual ~SequencedWorkerPoolOwner() { | |
166 pool_ = NULL; | |
167 MessageLoop::current()->Run(); | |
168 } | |
169 | |
170 // Don't change the return pool's testing observer. | |
171 const scoped_refptr<SequencedWorkerPool>& pool() { | |
172 return pool_; | |
173 } | |
174 | |
175 // The given callback will be called on WillWaitForShutdown(). | |
176 void SetWillWaitForShutdownCallback(const Closure& callback) { | |
177 will_wait_for_shutdown_callback_ = callback; | |
178 } | |
179 | |
180 int has_work_call_count() const { | |
181 AutoLock lock(has_work_lock_); | |
182 return has_work_call_count_; | |
183 } | |
184 | |
185 private: | |
186 // SequencedWorkerPool::TestingObserver implementation. | |
187 virtual void OnHasWork() OVERRIDE { | |
188 AutoLock lock(has_work_lock_); | |
189 ++has_work_call_count_; | |
190 } | |
191 | |
192 virtual void WillWaitForShutdown() OVERRIDE { | |
193 if (!will_wait_for_shutdown_callback_.is_null()) { | |
194 will_wait_for_shutdown_callback_.Run(); | |
195 } | |
196 } | |
197 | |
198 virtual void OnDestruct() OVERRIDE { | |
199 constructor_message_loop_->PostTask( | |
200 FROM_HERE, | |
201 constructor_message_loop_->QuitClosure()); | |
202 } | |
203 | |
204 MessageLoop* const constructor_message_loop_; | |
205 scoped_refptr<SequencedWorkerPool> pool_; | |
206 Closure will_wait_for_shutdown_callback_; | |
207 | |
208 mutable Lock has_work_lock_; | |
209 int has_work_call_count_; | |
210 | |
211 DISALLOW_COPY_AND_ASSIGN(SequencedWorkerPoolOwner); | |
212 }; | |
213 | |
214 class SequencedWorkerPoolTest : public testing::Test { | 194 class SequencedWorkerPoolTest : public testing::Test { |
215 public: | 195 public: |
216 SequencedWorkerPoolTest() | 196 SequencedWorkerPoolTest() |
217 : pool_owner_(kNumWorkerThreads, "test"), | 197 : pool_owner_(kNumWorkerThreads, "test"), |
218 tracker_(new TestTracker) {} | 198 tracker_(new TestTracker) {} |
219 | 199 |
220 ~SequencedWorkerPoolTest() {} | 200 ~SequencedWorkerPoolTest() {} |
221 | 201 |
222 virtual void SetUp() {} | 202 virtual void SetUp() {} |
223 | 203 |
(...skipping 318 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
542 scoped_ptr<SequencedWorkerPoolOwner> pool_owner_; | 522 scoped_ptr<SequencedWorkerPoolOwner> pool_owner_; |
543 }; | 523 }; |
544 | 524 |
545 INSTANTIATE_TYPED_TEST_CASE_P( | 525 INSTANTIATE_TYPED_TEST_CASE_P( |
546 SequencedWorkerPool, TaskRunnerTest, | 526 SequencedWorkerPool, TaskRunnerTest, |
547 SequencedWorkerPoolTaskRunnerTestDelegate); | 527 SequencedWorkerPoolTaskRunnerTestDelegate); |
548 | 528 |
549 } // namespace | 529 } // namespace |
550 | 530 |
551 } // namespace base | 531 } // namespace base |
OLD | NEW |