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