| Index: cc/base/worker_pool_unittest.cc
|
| diff --git a/cc/base/worker_pool_unittest.cc b/cc/base/worker_pool_unittest.cc
|
| index c032e2cea79aa8d7060be54dc414bc6078d7d917..de904b16412590b181676bc9a6019eca5fb42ef4 100644
|
| --- a/cc/base/worker_pool_unittest.cc
|
| +++ b/cc/base/worker_pool_unittest.cc
|
| @@ -4,27 +4,101 @@
|
|
|
| #include "cc/base/worker_pool.h"
|
|
|
| +#include <vector>
|
| +
|
| +#include "cc/base/completion_event.h"
|
| #include "testing/gtest/include/gtest/gtest.h"
|
|
|
| namespace cc {
|
|
|
| namespace {
|
|
|
| -class WorkerPoolTest : public testing::Test,
|
| - public WorkerPoolClient {
|
| +class FakeTaskImpl : public internal::WorkerPoolTask {
|
| public:
|
| - WorkerPoolTest()
|
| - : run_task_count_(0),
|
| - on_task_completed_count_(0),
|
| - finish_dispatching_completion_callbacks_count_(0) {
|
| + FakeTaskImpl(const base::Closure& callback,
|
| + const base::Closure& reply,
|
| + internal::WorkerPoolTask::TaskVector* dependencies)
|
| + : internal::WorkerPoolTask(dependencies),
|
| + callback_(callback),
|
| + reply_(reply) {
|
| + }
|
| + FakeTaskImpl(const base::Closure& callback, const base::Closure& reply)
|
| + : callback_(callback),
|
| + reply_(reply) {
|
| + }
|
| +
|
| + // Overridden from internal::WorkerPoolTask:
|
| + virtual void RunOnThread(unsigned thread_index) OVERRIDE {
|
| + if (!callback_.is_null())
|
| + callback_.Run();
|
| + }
|
| + virtual void DispatchCompletionCallback() OVERRIDE {
|
| + if (!reply_.is_null())
|
| + reply_.Run();
|
| + }
|
| +
|
| + private:
|
| + virtual ~FakeTaskImpl() {}
|
| +
|
| + const base::Closure callback_;
|
| + const base::Closure reply_;
|
| +};
|
| +
|
| +class FakeWorkerPool : public WorkerPool {
|
| + public:
|
| + FakeWorkerPool() : WorkerPool(1, base::TimeDelta::FromDays(1024), "test") {}
|
| + virtual ~FakeWorkerPool() {}
|
| +
|
| + static scoped_ptr<FakeWorkerPool> Create() {
|
| + return make_scoped_ptr(new FakeWorkerPool);
|
| + }
|
| +
|
| + void ScheduleTasks(const base::Closure& callback,
|
| + const base::Closure& reply,
|
| + const base::Closure& dependency,
|
| + int count) {
|
| + scoped_refptr<FakeTaskImpl> dependency_task(
|
| + new FakeTaskImpl(dependency, base::Closure()));
|
| +
|
| + internal::WorkerPoolTask::TaskVector tasks;
|
| + for (int i = 0; i < count; ++i) {
|
| + internal::WorkerPoolTask::TaskVector dependencies(1, dependency_task);
|
| + tasks.push_back(new FakeTaskImpl(callback, reply, &dependencies));
|
| + }
|
| + scoped_refptr<FakeTaskImpl> completion_task(
|
| + new FakeTaskImpl(base::Bind(&FakeWorkerPool::OnTasksCompleted,
|
| + base::Unretained(this)),
|
| + base::Closure(),
|
| + &tasks));
|
| +
|
| + scheduled_tasks_completion_.reset(new CompletionEvent);
|
| + WorkerPool::ScheduleTasks(completion_task);
|
| }
|
| - virtual ~WorkerPoolTest() {
|
| +
|
| + void WaitForTasksToComplete() {
|
| + DCHECK(scheduled_tasks_completion_);
|
| + scheduled_tasks_completion_->Wait();
|
| }
|
|
|
| + private:
|
| + void OnTasksCompleted() {
|
| + DCHECK(scheduled_tasks_completion_);
|
| + scheduled_tasks_completion_->Signal();
|
| + }
|
| +
|
| + scoped_ptr<CompletionEvent> scheduled_tasks_completion_;
|
| +};
|
| +
|
| +class WorkerPoolTest : public testing::Test,
|
| + public WorkerPoolClient {
|
| + public:
|
| + WorkerPoolTest() : finish_dispatching_completion_callbacks_count_(0) {}
|
| + virtual ~WorkerPoolTest() {}
|
| +
|
| + // Overridden from testing::Test:
|
| virtual void SetUp() OVERRIDE {
|
| Reset();
|
| }
|
| -
|
| virtual void TearDown() OVERRIDE {
|
| worker_pool_->Shutdown();
|
| }
|
| @@ -35,35 +109,34 @@ class WorkerPoolTest : public testing::Test,
|
| }
|
|
|
| void Reset() {
|
| - worker_pool_ = WorkerPool::Create(1,
|
| - base::TimeDelta::FromDays(1024),
|
| - "test");
|
| + worker_pool_ = FakeWorkerPool::Create();
|
| worker_pool_->SetClient(this);
|
| }
|
|
|
| void RunAllTasksAndReset() {
|
| + worker_pool_->WaitForTasksToComplete();
|
| worker_pool_->Shutdown();
|
| Reset();
|
| }
|
|
|
| - WorkerPool* worker_pool() {
|
| + FakeWorkerPool* worker_pool() {
|
| return worker_pool_.get();
|
| }
|
|
|
| - void RunTask() {
|
| - ++run_task_count_;
|
| + void RunTask(unsigned id) {
|
| + run_task_ids_.push_back(id);
|
| }
|
|
|
| - void OnTaskCompleted() {
|
| - ++on_task_completed_count_;
|
| + void OnTaskCompleted(unsigned id) {
|
| + on_task_completed_ids_.push_back(id);
|
| }
|
|
|
| - unsigned run_task_count() {
|
| - return run_task_count_;
|
| + const std::vector<unsigned>& run_task_ids() {
|
| + return run_task_ids_;
|
| }
|
|
|
| - unsigned on_task_completed_count() {
|
| - return on_task_completed_count_;
|
| + const std::vector<unsigned>& on_task_completed_ids() {
|
| + return on_task_completed_ids_;
|
| }
|
|
|
| unsigned finish_dispatching_completion_callbacks_count() {
|
| @@ -71,39 +144,72 @@ class WorkerPoolTest : public testing::Test,
|
| }
|
|
|
| private:
|
| - scoped_ptr<WorkerPool> worker_pool_;
|
| - unsigned run_task_count_;
|
| - unsigned on_task_completed_count_;
|
| + scoped_ptr<FakeWorkerPool> worker_pool_;
|
| + std::vector<unsigned> run_task_ids_;
|
| + std::vector<unsigned> on_task_completed_ids_;
|
| unsigned finish_dispatching_completion_callbacks_count_;
|
| };
|
|
|
| TEST_F(WorkerPoolTest, Basic) {
|
| - EXPECT_EQ(0u, run_task_count());
|
| - EXPECT_EQ(0u, on_task_completed_count());
|
| + EXPECT_EQ(0u, run_task_ids().size());
|
| + EXPECT_EQ(0u, on_task_completed_ids().size());
|
| EXPECT_EQ(0u, finish_dispatching_completion_callbacks_count());
|
|
|
| - worker_pool()->PostTaskAndReply(
|
| - base::Bind(&WorkerPoolTest::RunTask, base::Unretained(this)),
|
| - base::Bind(&WorkerPoolTest::OnTaskCompleted, base::Unretained(this)));
|
| + worker_pool()->ScheduleTasks(
|
| + base::Bind(&WorkerPoolTest::RunTask, base::Unretained(this), 0u),
|
| + base::Bind(&WorkerPoolTest::OnTaskCompleted, base::Unretained(this), 0u),
|
| + base::Closure(),
|
| + 1);
|
| RunAllTasksAndReset();
|
|
|
| - EXPECT_EQ(1u, run_task_count());
|
| - EXPECT_EQ(1u, on_task_completed_count());
|
| + EXPECT_EQ(1u, run_task_ids().size());
|
| + EXPECT_EQ(1u, on_task_completed_ids().size());
|
| EXPECT_EQ(1u, finish_dispatching_completion_callbacks_count());
|
|
|
| - worker_pool()->PostTaskAndReply(
|
| - base::Bind(&WorkerPoolTest::RunTask, base::Unretained(this)),
|
| - base::Bind(&WorkerPoolTest::OnTaskCompleted, base::Unretained(this)));
|
| - worker_pool()->PostTaskAndReply(
|
| - base::Bind(&WorkerPoolTest::RunTask, base::Unretained(this)),
|
| - base::Bind(&WorkerPoolTest::OnTaskCompleted, base::Unretained(this)));
|
| + worker_pool()->ScheduleTasks(
|
| + base::Bind(&WorkerPoolTest::RunTask, base::Unretained(this), 0u),
|
| + base::Bind(&WorkerPoolTest::OnTaskCompleted, base::Unretained(this), 0u),
|
| + base::Closure(),
|
| + 2);
|
| RunAllTasksAndReset();
|
|
|
| - EXPECT_EQ(3u, run_task_count());
|
| - EXPECT_EQ(3u, on_task_completed_count());
|
| + EXPECT_EQ(3u, run_task_ids().size());
|
| + EXPECT_EQ(3u, on_task_completed_ids().size());
|
| EXPECT_EQ(2u, finish_dispatching_completion_callbacks_count());
|
| }
|
|
|
| +TEST_F(WorkerPoolTest, Dependencies) {
|
| + worker_pool()->ScheduleTasks(
|
| + base::Bind(&WorkerPoolTest::RunTask, base::Unretained(this), 1u),
|
| + base::Bind(&WorkerPoolTest::OnTaskCompleted, base::Unretained(this), 1u),
|
| + base::Bind(&WorkerPoolTest::RunTask, base::Unretained(this), 0u),
|
| + 1);
|
| + RunAllTasksAndReset();
|
| +
|
| + // Check if dependency ran before task.
|
| + ASSERT_EQ(2u, run_task_ids().size());
|
| + EXPECT_EQ(0u, run_task_ids()[0]);
|
| + EXPECT_EQ(1u, run_task_ids()[1]);
|
| + ASSERT_EQ(1u, on_task_completed_ids().size());
|
| + EXPECT_EQ(1u, on_task_completed_ids()[0]);
|
| +
|
| + worker_pool()->ScheduleTasks(
|
| + base::Bind(&WorkerPoolTest::RunTask, base::Unretained(this), 1u),
|
| + base::Bind(&WorkerPoolTest::OnTaskCompleted, base::Unretained(this), 1u),
|
| + base::Bind(&WorkerPoolTest::RunTask, base::Unretained(this), 0u),
|
| + 2);
|
| + RunAllTasksAndReset();
|
| +
|
| + // Dependency should only run once.
|
| + ASSERT_EQ(5u, run_task_ids().size());
|
| + EXPECT_EQ(0u, run_task_ids()[2]);
|
| + EXPECT_EQ(1u, run_task_ids()[3]);
|
| + EXPECT_EQ(1u, run_task_ids()[4]);
|
| + ASSERT_EQ(3u, on_task_completed_ids().size());
|
| + EXPECT_EQ(1u, on_task_completed_ids()[1]);
|
| + EXPECT_EQ(1u, on_task_completed_ids()[2]);
|
| +}
|
| +
|
| } // namespace
|
|
|
| } // namespace cc
|
|
|