| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 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/resources/worker_pool.h" | 5 #include "cc/resources/worker_pool.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "cc/base/completion_event.h" | 9 #include "cc/base/completion_event.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
| 11 | 11 |
| 12 namespace cc { | 12 namespace cc { |
| 13 | 13 |
| 14 namespace { | 14 namespace { |
| 15 | 15 |
| 16 class FakeTaskImpl : public internal::WorkerPoolTask { | 16 class FakeWorkerPoolTaskImpl : public internal::WorkerPoolTask { |
| 17 public: | 17 public: |
| 18 FakeTaskImpl(const base::Closure& callback, | 18 FakeWorkerPoolTaskImpl(const base::Closure& callback, |
| 19 const base::Closure& reply, | 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), | 20 : callback_(callback), |
| 27 reply_(reply) { | 21 reply_(reply) { |
| 28 } | 22 } |
| 29 | 23 |
| 30 // Overridden from internal::WorkerPoolTask: | 24 // Overridden from internal::WorkerPoolTask: |
| 31 virtual void RunOnThread(unsigned thread_index) OVERRIDE { | 25 virtual void RunOnThread(unsigned thread_index) OVERRIDE { |
| 32 if (!callback_.is_null()) | 26 if (!callback_.is_null()) |
| 33 callback_.Run(); | 27 callback_.Run(); |
| 34 } | 28 } |
| 35 virtual void DispatchCompletionCallback() OVERRIDE { | 29 virtual void DispatchCompletionCallback() OVERRIDE { |
| 36 if (!reply_.is_null()) | 30 if (!reply_.is_null()) |
| 37 reply_.Run(); | 31 reply_.Run(); |
| 38 } | 32 } |
| 39 | 33 |
| 40 private: | 34 private: |
| 41 virtual ~FakeTaskImpl() {} | 35 virtual ~FakeWorkerPoolTaskImpl() {} |
| 42 | 36 |
| 43 const base::Closure callback_; | 37 const base::Closure callback_; |
| 44 const base::Closure reply_; | 38 const base::Closure reply_; |
| 39 |
| 40 DISALLOW_COPY_AND_ASSIGN(FakeWorkerPoolTaskImpl); |
| 45 }; | 41 }; |
| 46 | 42 |
| 47 class FakeWorkerPool : public WorkerPool { | 43 class FakeWorkerPool : public WorkerPool { |
| 48 public: | 44 public: |
| 49 FakeWorkerPool() : WorkerPool(1, "test") {} | 45 FakeWorkerPool() : WorkerPool(1, "test") {} |
| 50 virtual ~FakeWorkerPool() {} | 46 virtual ~FakeWorkerPool() {} |
| 51 | 47 |
| 52 static scoped_ptr<FakeWorkerPool> Create() { | 48 static scoped_ptr<FakeWorkerPool> Create() { |
| 53 return make_scoped_ptr(new FakeWorkerPool); | 49 return make_scoped_ptr(new FakeWorkerPool); |
| 54 } | 50 } |
| 55 | 51 |
| 56 void ScheduleTasks(const base::Closure& callback, | 52 void ScheduleTasks(const base::Closure& callback, |
| 57 const base::Closure& reply, | 53 const base::Closure& reply, |
| 58 const base::Closure& dependency, | 54 const base::Closure& dependency, |
| 59 int count) { | 55 int count) { |
| 60 scoped_refptr<FakeTaskImpl> dependency_task( | 56 unsigned priority = 0u; |
| 61 new FakeTaskImpl(dependency, base::Closure())); | 57 TaskGraph graph; |
| 62 | 58 |
| 63 internal::WorkerPoolTask::TaskVector tasks; | 59 scoped_refptr<FakeWorkerPoolTaskImpl> completion_task( |
| 60 new FakeWorkerPoolTaskImpl( |
| 61 base::Bind(&FakeWorkerPool::OnTasksCompleted, |
| 62 base::Unretained(this)), |
| 63 base::Closure())); |
| 64 scoped_ptr<GraphNode> completion_node(new GraphNode); |
| 65 completion_node->set_task(completion_task.get()); |
| 66 |
| 67 scoped_refptr<FakeWorkerPoolTaskImpl> dependency_task( |
| 68 new FakeWorkerPoolTaskImpl(dependency, base::Closure())); |
| 69 scoped_ptr<GraphNode> dependency_node(new GraphNode); |
| 70 dependency_node->set_task(dependency_task.get()); |
| 71 |
| 72 TaskVector tasks; |
| 64 for (int i = 0; i < count; ++i) { | 73 for (int i = 0; i < count; ++i) { |
| 65 internal::WorkerPoolTask::TaskVector dependencies(1, dependency_task); | 74 scoped_refptr<FakeWorkerPoolTaskImpl> task( |
| 66 tasks.push_back(new FakeTaskImpl(callback, reply, &dependencies)); | 75 new FakeWorkerPoolTaskImpl(callback, reply)); |
| 76 scoped_ptr<GraphNode> node(new GraphNode); |
| 77 node->set_task(task.get()); |
| 78 node->add_dependent(completion_node.get()); |
| 79 completion_node->add_dependency(); |
| 80 dependency_node->add_dependent(node.get()); |
| 81 node->add_dependency(); |
| 82 node->set_priority(priority++); |
| 83 graph.set(task.get(), node.Pass()); |
| 84 tasks.push_back(task.get()); |
| 67 } | 85 } |
| 68 scoped_refptr<FakeTaskImpl> completion_task( | 86 |
| 69 new FakeTaskImpl(base::Bind(&FakeWorkerPool::OnTasksCompleted, | 87 completion_node->set_priority(priority++); |
| 70 base::Unretained(this)), | 88 graph.set(completion_task.get(), completion_node.Pass()); |
| 71 base::Closure(), | 89 dependency_node->set_priority(priority++); |
| 72 &tasks)); | 90 graph.set(dependency_task.get(), dependency_node.Pass()); |
| 73 | 91 |
| 74 scheduled_tasks_completion_.reset(new CompletionEvent); | 92 scheduled_tasks_completion_.reset(new CompletionEvent); |
| 75 | 93 |
| 76 TaskGraph graph; | 94 SetTaskGraph(&graph); |
| 77 BuildTaskGraph(completion_task.get(), &graph); | 95 |
| 78 WorkerPool::SetTaskGraph(&graph); | 96 tasks_.swap(tasks); |
| 79 root_.swap(completion_task); | 97 completion_task_.swap(completion_task); |
| 98 dependency_task_.swap(dependency_task); |
| 80 } | 99 } |
| 81 | 100 |
| 82 void WaitForTasksToComplete() { | 101 void WaitForTasksToComplete() { |
| 83 DCHECK(scheduled_tasks_completion_); | 102 DCHECK(scheduled_tasks_completion_); |
| 84 scheduled_tasks_completion_->Wait(); | 103 scheduled_tasks_completion_->Wait(); |
| 85 } | 104 } |
| 86 | 105 |
| 87 private: | 106 private: |
| 107 typedef std::vector<scoped_refptr<internal::WorkerPoolTask> > TaskVector; |
| 108 |
| 88 void OnTasksCompleted() { | 109 void OnTasksCompleted() { |
| 89 DCHECK(scheduled_tasks_completion_); | 110 DCHECK(scheduled_tasks_completion_); |
| 90 scheduled_tasks_completion_->Signal(); | 111 scheduled_tasks_completion_->Signal(); |
| 91 } | 112 } |
| 92 | 113 |
| 93 scoped_refptr<FakeTaskImpl> root_; | 114 TaskVector tasks_; |
| 115 scoped_refptr<FakeWorkerPoolTaskImpl> completion_task_; |
| 116 scoped_refptr<FakeWorkerPoolTaskImpl> dependency_task_; |
| 94 scoped_ptr<CompletionEvent> scheduled_tasks_completion_; | 117 scoped_ptr<CompletionEvent> scheduled_tasks_completion_; |
| 118 |
| 119 DISALLOW_COPY_AND_ASSIGN(FakeWorkerPool); |
| 95 }; | 120 }; |
| 96 | 121 |
| 97 class WorkerPoolTest : public testing::Test { | 122 class WorkerPoolTest : public testing::Test { |
| 98 public: | 123 public: |
| 99 WorkerPoolTest() {} | 124 WorkerPoolTest() {} |
| 100 virtual ~WorkerPoolTest() {} | 125 virtual ~WorkerPoolTest() {} |
| 101 | 126 |
| 102 // Overridden from testing::Test: | 127 // Overridden from testing::Test: |
| 103 virtual void SetUp() OVERRIDE { | 128 virtual void SetUp() OVERRIDE { |
| 104 Reset(); | 129 Reset(); |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 197 EXPECT_EQ(1u, run_task_ids()[3]); | 222 EXPECT_EQ(1u, run_task_ids()[3]); |
| 198 EXPECT_EQ(1u, run_task_ids()[4]); | 223 EXPECT_EQ(1u, run_task_ids()[4]); |
| 199 ASSERT_EQ(3u, on_task_completed_ids().size()); | 224 ASSERT_EQ(3u, on_task_completed_ids().size()); |
| 200 EXPECT_EQ(1u, on_task_completed_ids()[1]); | 225 EXPECT_EQ(1u, on_task_completed_ids()[1]); |
| 201 EXPECT_EQ(1u, on_task_completed_ids()[2]); | 226 EXPECT_EQ(1u, on_task_completed_ids()[2]); |
| 202 } | 227 } |
| 203 | 228 |
| 204 } // namespace | 229 } // namespace |
| 205 | 230 |
| 206 } // namespace cc | 231 } // namespace cc |
| OLD | NEW |