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 <algorithm> | 5 #include <algorithm> |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/compiler_specific.h" | 8 #include "base/compiler_specific.h" |
9 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
11 #include "base/message_loop.h" | 11 #include "base/message_loop.h" |
12 #include "base/message_loop_proxy.h" | 12 #include "base/message_loop_proxy.h" |
13 #include "base/synchronization/condition_variable.h" | 13 #include "base/synchronization/condition_variable.h" |
14 #include "base/synchronization/lock.h" | 14 #include "base/synchronization/lock.h" |
| 15 #include "base/test/sequenced_worker_pool_owner.h" |
15 #include "base/test/task_runner_test_template.h" | 16 #include "base/test/task_runner_test_template.h" |
16 #include "base/threading/platform_thread.h" | 17 #include "base/threading/platform_thread.h" |
17 #include "base/threading/sequenced_worker_pool.h" | 18 #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 |
23 // IMPORTANT NOTE: | 24 // IMPORTANT NOTE: |
24 // | 25 // |
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
138 | 139 |
139 base::ConditionVariable cond_var_; | 140 base::ConditionVariable cond_var_; |
140 | 141 |
141 // Protected by lock_. | 142 // Protected by lock_. |
142 std::vector<int> complete_sequence_; | 143 std::vector<int> complete_sequence_; |
143 | 144 |
144 // Counter of the number of "block" workers that have started. | 145 // Counter of the number of "block" workers that have started. |
145 size_t started_events_; | 146 size_t started_events_; |
146 }; | 147 }; |
147 | 148 |
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 { | 149 class SequencedWorkerPoolTest : public testing::Test { |
215 public: | 150 public: |
216 SequencedWorkerPoolTest() | 151 SequencedWorkerPoolTest() |
217 : pool_owner_(kNumWorkerThreads, "test"), | 152 : pool_owner_(kNumWorkerThreads, "test"), |
218 tracker_(new TestTracker) {} | 153 tracker_(new TestTracker) {} |
219 | 154 |
220 ~SequencedWorkerPoolTest() {} | 155 ~SequencedWorkerPoolTest() {} |
221 | 156 |
222 virtual void SetUp() {} | 157 virtual void SetUp() {} |
223 | 158 |
(...skipping 370 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
594 scoped_ptr<SequencedWorkerPoolOwner> pool_owner_; | 529 scoped_ptr<SequencedWorkerPoolOwner> pool_owner_; |
595 }; | 530 }; |
596 | 531 |
597 INSTANTIATE_TYPED_TEST_CASE_P( | 532 INSTANTIATE_TYPED_TEST_CASE_P( |
598 SequencedWorkerPool, TaskRunnerTest, | 533 SequencedWorkerPool, TaskRunnerTest, |
599 SequencedWorkerPoolTaskRunnerTestDelegate); | 534 SequencedWorkerPoolTaskRunnerTestDelegate); |
600 | 535 |
601 } // namespace | 536 } // namespace |
602 | 537 |
603 } // namespace base | 538 } // namespace base |
OLD | NEW |