| 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 "cc/base/worker_pool.h" | 5 #include "cc/base/worker_pool.h" |
| 6 | 6 |
| 7 #include <vector> | |
| 8 | |
| 9 #include "cc/base/completion_event.h" | |
| 10 #include "testing/gtest/include/gtest/gtest.h" | 7 #include "testing/gtest/include/gtest/gtest.h" |
| 11 | 8 |
| 12 namespace cc { | 9 namespace cc { |
| 13 | 10 |
| 14 namespace { | 11 namespace { |
| 15 | 12 |
| 16 class FakeTaskImpl : public internal::WorkerPoolTask { | |
| 17 public: | |
| 18 FakeTaskImpl(const base::Closure& callback, | |
| 19 const base::Closure& reply, | |
| 20 internal::WorkerPoolTask::TaskVector* dependencies) | |
| 21 : internal::WorkerPoolTask(dependencies), | |
| 22 callback_(callback), | |
| 23 reply_(reply) { | |
| 24 } | |
| 25 FakeTaskImpl(const base::Closure& callback, const base::Closure& reply) | |
| 26 : callback_(callback), | |
| 27 reply_(reply) { | |
| 28 } | |
| 29 | |
| 30 // Overridden from internal::WorkerPoolTask: | |
| 31 virtual void RunOnThread(unsigned thread_index) OVERRIDE { | |
| 32 if (!callback_.is_null()) | |
| 33 callback_.Run(); | |
| 34 } | |
| 35 virtual void DispatchCompletionCallback() OVERRIDE { | |
| 36 if (!reply_.is_null()) | |
| 37 reply_.Run(); | |
| 38 } | |
| 39 | |
| 40 private: | |
| 41 virtual ~FakeTaskImpl() {} | |
| 42 | |
| 43 const base::Closure callback_; | |
| 44 const base::Closure reply_; | |
| 45 }; | |
| 46 | |
| 47 class FakeWorkerPool : public WorkerPool { | |
| 48 public: | |
| 49 FakeWorkerPool() : WorkerPool(1, base::TimeDelta::FromDays(1024), "test") {} | |
| 50 virtual ~FakeWorkerPool() {} | |
| 51 | |
| 52 static scoped_ptr<FakeWorkerPool> Create() { | |
| 53 return make_scoped_ptr(new FakeWorkerPool); | |
| 54 } | |
| 55 | |
| 56 void ScheduleTasks(const base::Closure& callback, | |
| 57 const base::Closure& reply, | |
| 58 const base::Closure& dependency, | |
| 59 int count) { | |
| 60 scoped_refptr<FakeTaskImpl> dependency_task( | |
| 61 new FakeTaskImpl(dependency, base::Closure())); | |
| 62 | |
| 63 internal::WorkerPoolTask::TaskVector tasks; | |
| 64 for (int i = 0; i < count; ++i) { | |
| 65 internal::WorkerPoolTask::TaskVector dependencies(1, dependency_task); | |
| 66 tasks.push_back(new FakeTaskImpl(callback, reply, &dependencies)); | |
| 67 } | |
| 68 scoped_refptr<FakeTaskImpl> completion_task( | |
| 69 new FakeTaskImpl(base::Bind(&FakeWorkerPool::OnTasksCompleted, | |
| 70 base::Unretained(this)), | |
| 71 base::Closure(), | |
| 72 &tasks)); | |
| 73 | |
| 74 scheduled_tasks_completion_.reset(new CompletionEvent); | |
| 75 WorkerPool::ScheduleTasks(completion_task); | |
| 76 } | |
| 77 | |
| 78 void WaitForTasksToComplete() { | |
| 79 DCHECK(scheduled_tasks_completion_); | |
| 80 scheduled_tasks_completion_->Wait(); | |
| 81 } | |
| 82 | |
| 83 private: | |
| 84 void OnTasksCompleted() { | |
| 85 DCHECK(scheduled_tasks_completion_); | |
| 86 scheduled_tasks_completion_->Signal(); | |
| 87 } | |
| 88 | |
| 89 scoped_ptr<CompletionEvent> scheduled_tasks_completion_; | |
| 90 }; | |
| 91 | |
| 92 class WorkerPoolTest : public testing::Test, | 13 class WorkerPoolTest : public testing::Test, |
| 93 public WorkerPoolClient { | 14 public WorkerPoolClient { |
| 94 public: | 15 public: |
| 95 WorkerPoolTest() : finish_dispatching_completion_callbacks_count_(0) {} | 16 WorkerPoolTest() |
| 96 virtual ~WorkerPoolTest() {} | 17 : run_task_count_(0), |
| 18 on_task_completed_count_(0), |
| 19 finish_dispatching_completion_callbacks_count_(0) { |
| 20 } |
| 21 virtual ~WorkerPoolTest() { |
| 22 } |
| 97 | 23 |
| 98 // Overridden from testing::Test: | |
| 99 virtual void SetUp() OVERRIDE { | 24 virtual void SetUp() OVERRIDE { |
| 100 Reset(); | 25 Reset(); |
| 101 } | 26 } |
| 27 |
| 102 virtual void TearDown() OVERRIDE { | 28 virtual void TearDown() OVERRIDE { |
| 103 worker_pool_->Shutdown(); | 29 worker_pool_->Shutdown(); |
| 104 } | 30 } |
| 105 | 31 |
| 106 // Overridden from WorkerPoolClient: | 32 // Overridden from WorkerPoolClient: |
| 107 virtual void DidFinishDispatchingWorkerPoolCompletionCallbacks() OVERRIDE { | 33 virtual void DidFinishDispatchingWorkerPoolCompletionCallbacks() OVERRIDE { |
| 108 ++finish_dispatching_completion_callbacks_count_; | 34 ++finish_dispatching_completion_callbacks_count_; |
| 109 } | 35 } |
| 110 | 36 |
| 111 void Reset() { | 37 void Reset() { |
| 112 worker_pool_ = FakeWorkerPool::Create(); | 38 worker_pool_ = WorkerPool::Create(1, |
| 39 base::TimeDelta::FromDays(1024), |
| 40 "test"); |
| 113 worker_pool_->SetClient(this); | 41 worker_pool_->SetClient(this); |
| 114 } | 42 } |
| 115 | 43 |
| 116 void RunAllTasksAndReset() { | 44 void RunAllTasksAndReset() { |
| 117 worker_pool_->WaitForTasksToComplete(); | |
| 118 worker_pool_->Shutdown(); | 45 worker_pool_->Shutdown(); |
| 119 Reset(); | 46 Reset(); |
| 120 } | 47 } |
| 121 | 48 |
| 122 FakeWorkerPool* worker_pool() { | 49 WorkerPool* worker_pool() { |
| 123 return worker_pool_.get(); | 50 return worker_pool_.get(); |
| 124 } | 51 } |
| 125 | 52 |
| 126 void RunTask(unsigned id) { | 53 void RunTask() { |
| 127 run_task_ids_.push_back(id); | 54 ++run_task_count_; |
| 128 } | 55 } |
| 129 | 56 |
| 130 void OnTaskCompleted(unsigned id) { | 57 void OnTaskCompleted() { |
| 131 on_task_completed_ids_.push_back(id); | 58 ++on_task_completed_count_; |
| 132 } | 59 } |
| 133 | 60 |
| 134 const std::vector<unsigned>& run_task_ids() { | 61 unsigned run_task_count() { |
| 135 return run_task_ids_; | 62 return run_task_count_; |
| 136 } | 63 } |
| 137 | 64 |
| 138 const std::vector<unsigned>& on_task_completed_ids() { | 65 unsigned on_task_completed_count() { |
| 139 return on_task_completed_ids_; | 66 return on_task_completed_count_; |
| 140 } | 67 } |
| 141 | 68 |
| 142 unsigned finish_dispatching_completion_callbacks_count() { | 69 unsigned finish_dispatching_completion_callbacks_count() { |
| 143 return finish_dispatching_completion_callbacks_count_; | 70 return finish_dispatching_completion_callbacks_count_; |
| 144 } | 71 } |
| 145 | 72 |
| 146 private: | 73 private: |
| 147 scoped_ptr<FakeWorkerPool> worker_pool_; | 74 scoped_ptr<WorkerPool> worker_pool_; |
| 148 std::vector<unsigned> run_task_ids_; | 75 unsigned run_task_count_; |
| 149 std::vector<unsigned> on_task_completed_ids_; | 76 unsigned on_task_completed_count_; |
| 150 unsigned finish_dispatching_completion_callbacks_count_; | 77 unsigned finish_dispatching_completion_callbacks_count_; |
| 151 }; | 78 }; |
| 152 | 79 |
| 153 TEST_F(WorkerPoolTest, Basic) { | 80 TEST_F(WorkerPoolTest, Basic) { |
| 154 EXPECT_EQ(0u, run_task_ids().size()); | 81 EXPECT_EQ(0u, run_task_count()); |
| 155 EXPECT_EQ(0u, on_task_completed_ids().size()); | 82 EXPECT_EQ(0u, on_task_completed_count()); |
| 156 EXPECT_EQ(0u, finish_dispatching_completion_callbacks_count()); | 83 EXPECT_EQ(0u, finish_dispatching_completion_callbacks_count()); |
| 157 | 84 |
| 158 worker_pool()->ScheduleTasks( | 85 worker_pool()->PostTaskAndReply( |
| 159 base::Bind(&WorkerPoolTest::RunTask, base::Unretained(this), 0u), | 86 base::Bind(&WorkerPoolTest::RunTask, base::Unretained(this)), |
| 160 base::Bind(&WorkerPoolTest::OnTaskCompleted, base::Unretained(this), 0u), | 87 base::Bind(&WorkerPoolTest::OnTaskCompleted, base::Unretained(this))); |
| 161 base::Closure(), | |
| 162 1); | |
| 163 RunAllTasksAndReset(); | 88 RunAllTasksAndReset(); |
| 164 | 89 |
| 165 EXPECT_EQ(1u, run_task_ids().size()); | 90 EXPECT_EQ(1u, run_task_count()); |
| 166 EXPECT_EQ(1u, on_task_completed_ids().size()); | 91 EXPECT_EQ(1u, on_task_completed_count()); |
| 167 EXPECT_EQ(1u, finish_dispatching_completion_callbacks_count()); | 92 EXPECT_EQ(1u, finish_dispatching_completion_callbacks_count()); |
| 168 | 93 |
| 169 worker_pool()->ScheduleTasks( | 94 worker_pool()->PostTaskAndReply( |
| 170 base::Bind(&WorkerPoolTest::RunTask, base::Unretained(this), 0u), | 95 base::Bind(&WorkerPoolTest::RunTask, base::Unretained(this)), |
| 171 base::Bind(&WorkerPoolTest::OnTaskCompleted, base::Unretained(this), 0u), | 96 base::Bind(&WorkerPoolTest::OnTaskCompleted, base::Unretained(this))); |
| 172 base::Closure(), | 97 worker_pool()->PostTaskAndReply( |
| 173 2); | 98 base::Bind(&WorkerPoolTest::RunTask, base::Unretained(this)), |
| 99 base::Bind(&WorkerPoolTest::OnTaskCompleted, base::Unretained(this))); |
| 174 RunAllTasksAndReset(); | 100 RunAllTasksAndReset(); |
| 175 | 101 |
| 176 EXPECT_EQ(3u, run_task_ids().size()); | 102 EXPECT_EQ(3u, run_task_count()); |
| 177 EXPECT_EQ(3u, on_task_completed_ids().size()); | 103 EXPECT_EQ(3u, on_task_completed_count()); |
| 178 EXPECT_EQ(2u, finish_dispatching_completion_callbacks_count()); | 104 EXPECT_EQ(2u, finish_dispatching_completion_callbacks_count()); |
| 179 } | 105 } |
| 180 | 106 |
| 181 TEST_F(WorkerPoolTest, Dependencies) { | |
| 182 worker_pool()->ScheduleTasks( | |
| 183 base::Bind(&WorkerPoolTest::RunTask, base::Unretained(this), 1u), | |
| 184 base::Bind(&WorkerPoolTest::OnTaskCompleted, base::Unretained(this), 1u), | |
| 185 base::Bind(&WorkerPoolTest::RunTask, base::Unretained(this), 0u), | |
| 186 1); | |
| 187 RunAllTasksAndReset(); | |
| 188 | |
| 189 // Check if dependency ran before task. | |
| 190 ASSERT_EQ(2u, run_task_ids().size()); | |
| 191 EXPECT_EQ(0u, run_task_ids()[0]); | |
| 192 EXPECT_EQ(1u, run_task_ids()[1]); | |
| 193 ASSERT_EQ(1u, on_task_completed_ids().size()); | |
| 194 EXPECT_EQ(1u, on_task_completed_ids()[0]); | |
| 195 | |
| 196 worker_pool()->ScheduleTasks( | |
| 197 base::Bind(&WorkerPoolTest::RunTask, base::Unretained(this), 1u), | |
| 198 base::Bind(&WorkerPoolTest::OnTaskCompleted, base::Unretained(this), 1u), | |
| 199 base::Bind(&WorkerPoolTest::RunTask, base::Unretained(this), 0u), | |
| 200 2); | |
| 201 RunAllTasksAndReset(); | |
| 202 | |
| 203 // Dependency should only run once. | |
| 204 ASSERT_EQ(5u, run_task_ids().size()); | |
| 205 EXPECT_EQ(0u, run_task_ids()[2]); | |
| 206 EXPECT_EQ(1u, run_task_ids()[3]); | |
| 207 EXPECT_EQ(1u, run_task_ids()[4]); | |
| 208 ASSERT_EQ(3u, on_task_completed_ids().size()); | |
| 209 EXPECT_EQ(1u, on_task_completed_ids()[1]); | |
| 210 EXPECT_EQ(1u, on_task_completed_ids()[2]); | |
| 211 } | |
| 212 | |
| 213 } // namespace | 107 } // namespace |
| 214 | 108 |
| 215 } // namespace cc | 109 } // namespace cc |
| OLD | NEW |