| Index: trunk/src/cc/base/worker_pool_unittest.cc
|
| ===================================================================
|
| --- trunk/src/cc/base/worker_pool_unittest.cc (revision 202735)
|
| +++ trunk/src/cc/base/worker_pool_unittest.cc (working copy)
|
| @@ -4,101 +4,27 @@
|
|
|
| #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 FakeTaskImpl : public internal::WorkerPoolTask {
|
| +class WorkerPoolTest : public testing::Test,
|
| + public WorkerPoolClient {
|
| public:
|
| - FakeTaskImpl(const base::Closure& callback,
|
| - const base::Closure& reply,
|
| - internal::WorkerPoolTask::TaskVector* dependencies)
|
| - : internal::WorkerPoolTask(dependencies),
|
| - callback_(callback),
|
| - reply_(reply) {
|
| + 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)
|
| - : callback_(callback),
|
| - reply_(reply) {
|
| + virtual ~WorkerPoolTest() {
|
| }
|
|
|
| - // 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);
|
| - }
|
| -
|
| - 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();
|
| }
|
| @@ -109,34 +35,35 @@
|
| }
|
|
|
| void Reset() {
|
| - worker_pool_ = FakeWorkerPool::Create();
|
| + worker_pool_ = WorkerPool::Create(1,
|
| + base::TimeDelta::FromDays(1024),
|
| + "test");
|
| worker_pool_->SetClient(this);
|
| }
|
|
|
| void RunAllTasksAndReset() {
|
| - worker_pool_->WaitForTasksToComplete();
|
| worker_pool_->Shutdown();
|
| Reset();
|
| }
|
|
|
| - FakeWorkerPool* worker_pool() {
|
| + WorkerPool* worker_pool() {
|
| return worker_pool_.get();
|
| }
|
|
|
| - void RunTask(unsigned id) {
|
| - run_task_ids_.push_back(id);
|
| + void RunTask() {
|
| + ++run_task_count_;
|
| }
|
|
|
| - void OnTaskCompleted(unsigned id) {
|
| - on_task_completed_ids_.push_back(id);
|
| + void OnTaskCompleted() {
|
| + ++on_task_completed_count_;
|
| }
|
|
|
| - const std::vector<unsigned>& run_task_ids() {
|
| - return run_task_ids_;
|
| + unsigned run_task_count() {
|
| + return run_task_count_;
|
| }
|
|
|
| - const std::vector<unsigned>& on_task_completed_ids() {
|
| - return on_task_completed_ids_;
|
| + unsigned on_task_completed_count() {
|
| + return on_task_completed_count_;
|
| }
|
|
|
| unsigned finish_dispatching_completion_callbacks_count() {
|
| @@ -144,72 +71,39 @@
|
| }
|
|
|
| private:
|
| - scoped_ptr<FakeWorkerPool> worker_pool_;
|
| - std::vector<unsigned> run_task_ids_;
|
| - std::vector<unsigned> on_task_completed_ids_;
|
| + scoped_ptr<WorkerPool> worker_pool_;
|
| + unsigned run_task_count_;
|
| + unsigned on_task_completed_count_;
|
| unsigned finish_dispatching_completion_callbacks_count_;
|
| };
|
|
|
| TEST_F(WorkerPoolTest, Basic) {
|
| - EXPECT_EQ(0u, run_task_ids().size());
|
| - EXPECT_EQ(0u, on_task_completed_ids().size());
|
| + EXPECT_EQ(0u, run_task_count());
|
| + EXPECT_EQ(0u, on_task_completed_count());
|
| EXPECT_EQ(0u, finish_dispatching_completion_callbacks_count());
|
|
|
| - worker_pool()->ScheduleTasks(
|
| - base::Bind(&WorkerPoolTest::RunTask, base::Unretained(this), 0u),
|
| - base::Bind(&WorkerPoolTest::OnTaskCompleted, base::Unretained(this), 0u),
|
| - base::Closure(),
|
| - 1);
|
| + worker_pool()->PostTaskAndReply(
|
| + base::Bind(&WorkerPoolTest::RunTask, base::Unretained(this)),
|
| + base::Bind(&WorkerPoolTest::OnTaskCompleted, base::Unretained(this)));
|
| RunAllTasksAndReset();
|
|
|
| - EXPECT_EQ(1u, run_task_ids().size());
|
| - EXPECT_EQ(1u, on_task_completed_ids().size());
|
| + EXPECT_EQ(1u, run_task_count());
|
| + EXPECT_EQ(1u, on_task_completed_count());
|
| EXPECT_EQ(1u, finish_dispatching_completion_callbacks_count());
|
|
|
| - worker_pool()->ScheduleTasks(
|
| - base::Bind(&WorkerPoolTest::RunTask, base::Unretained(this), 0u),
|
| - base::Bind(&WorkerPoolTest::OnTaskCompleted, base::Unretained(this), 0u),
|
| - base::Closure(),
|
| - 2);
|
| + 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)));
|
| RunAllTasksAndReset();
|
|
|
| - EXPECT_EQ(3u, run_task_ids().size());
|
| - EXPECT_EQ(3u, on_task_completed_ids().size());
|
| + EXPECT_EQ(3u, run_task_count());
|
| + EXPECT_EQ(3u, on_task_completed_count());
|
| 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
|
|
|