Chromium Code Reviews| Index: base/task_scheduler/task_tracker_unittest.cc |
| diff --git a/base/task_scheduler/task_tracker_unittest.cc b/base/task_scheduler/task_tracker_unittest.cc |
| index 2a08bc189eddfce1283cbf199996551be723a525..d6bee7cf0d7b7a6a82cbbf3a49403e4353427ec4 100644 |
| --- a/base/task_scheduler/task_tracker_unittest.cc |
| +++ b/base/task_scheduler/task_tracker_unittest.cc |
| @@ -5,7 +5,6 @@ |
| #include "base/task_scheduler/task_tracker.h" |
| #include <memory> |
| -#include <queue> |
| #include "base/bind.h" |
| #include "base/logging.h" |
| @@ -75,21 +74,6 @@ class TaskSchedulerTaskTrackerTest |
| TaskTraits().WithShutdownBehavior(shutdown_behavior))); |
| } |
| - // Tries to post |task| via |tracker_|. If |tracker_| approves the operation, |
| - // |task| is added to |posted_tasks_|. |
| - void PostTaskViaTracker(std::unique_ptr<Task> task) { |
| - tracker_.PostTask( |
| - Bind(&TaskSchedulerTaskTrackerTest::PostTaskCallback, Unretained(this)), |
| - std::move(task)); |
| - } |
| - |
| - // Tries to run the next task in |posted_tasks_| via |tracker_|. |
| - void RunNextPostedTaskViaTracker() { |
| - ASSERT_FALSE(posted_tasks_.empty()); |
| - tracker_.RunTask(posted_tasks_.front().get()); |
| - posted_tasks_.pop(); |
| - } |
| - |
| // Calls tracker_->Shutdown() on a new thread. When this returns, Shutdown() |
| // method has been entered on the new thread, but it hasn't necessarily |
| // returned. |
| @@ -119,13 +103,8 @@ class TaskSchedulerTaskTrackerTest |
| TaskTracker tracker_; |
| size_t num_tasks_executed_ = 0; |
| - std::queue<std::unique_ptr<Task>> posted_tasks_; |
| private: |
| - void PostTaskCallback(std::unique_ptr<Task> task) { |
| - posted_tasks_.push(std::move(task)); |
| - } |
| - |
| void RunTaskCallback() { ++num_tasks_executed_; } |
| std::unique_ptr<ThreadCallingShutdown> thread_calling_shutdown_; |
| @@ -148,18 +127,14 @@ class TaskSchedulerTaskTrackerTest |
| } // namespace |
| TEST_P(TaskSchedulerTaskTrackerTest, PostAndRunBeforeShutdown) { |
| - std::unique_ptr<Task> task_to_post(CreateTask(GetParam())); |
| - const Task* task_to_post_raw = task_to_post.get(); |
| + std::unique_ptr<Task> task(CreateTask(GetParam())); |
| // Post the task. |
|
danakj
2016/04/06 19:38:30
update comment
robliao
2016/04/06 19:42:33
Maybe update the test names too.
fdoray
2016/04/06 20:59:27
Done.
|
| - EXPECT_TRUE(posted_tasks_.empty()); |
| - PostTaskViaTracker(std::move(task_to_post)); |
| - EXPECT_EQ(1U, posted_tasks_.size()); |
| - EXPECT_EQ(task_to_post_raw, posted_tasks_.front().get()); |
| + EXPECT_TRUE(tracker_.WillPostTask(task.get())); |
| // Run the posted task. |
| EXPECT_EQ(0U, num_tasks_executed_); |
| - RunNextPostedTaskViaTracker(); |
| + tracker_.RunTask(task.get()); |
| EXPECT_EQ(1U, num_tasks_executed_); |
| // Shutdown() shouldn't block. |
| @@ -168,15 +143,14 @@ TEST_P(TaskSchedulerTaskTrackerTest, PostAndRunBeforeShutdown) { |
| TEST_P(TaskSchedulerTaskTrackerTest, PostAndRunLongTaskBeforeShutdown) { |
| // Post a task that will block until |event| is signaled. |
| - EXPECT_TRUE(posted_tasks_.empty()); |
| WaitableEvent event(false, false); |
| - PostTaskViaTracker(WrapUnique( |
| + std::unique_ptr<Task> blocked_task( |
| new Task(FROM_HERE, Bind(&WaitableEvent::Wait, Unretained(&event)), |
| - TaskTraits().WithShutdownBehavior(GetParam())))); |
| - EXPECT_EQ(1U, posted_tasks_.size()); |
| + TaskTraits().WithShutdownBehavior(GetParam()))); |
| + EXPECT_TRUE(tracker_.WillPostTask(blocked_task.get())); |
| // Run the task asynchronouly. |
| - ThreadRunningTask thread_running_task(&tracker_, posted_tasks_.front().get()); |
| + ThreadRunningTask thread_running_task(&tracker_, blocked_task.get()); |
| thread_running_task.Start(); |
| // Initiate shutdown while the task is running. |
| @@ -200,17 +174,14 @@ TEST_P(TaskSchedulerTaskTrackerTest, PostAndRunLongTaskBeforeShutdown) { |
| } |
| TEST_P(TaskSchedulerTaskTrackerTest, PostBeforeShutdownRunDuringShutdown) { |
| - std::unique_ptr<Task> task_to_post(CreateTask(GetParam())); |
| - const Task* task_to_post_raw = task_to_post.get(); |
| - |
| - // Post the task. |
| - EXPECT_TRUE(posted_tasks_.empty()); |
| - PostTaskViaTracker(std::move(task_to_post)); |
| - EXPECT_EQ(1U, posted_tasks_.size()); |
| - EXPECT_EQ(task_to_post_raw, posted_tasks_.front().get()); |
| + // Post a task. |
|
danakj
2016/04/06 19:38:30
update comment?
fdoray
2016/04/06 20:59:27
Done.
|
| + std::unique_ptr<Task> task(CreateTask(GetParam())); |
| + EXPECT_TRUE(tracker_.WillPostTask(task.get())); |
| // Post a BLOCK_SHUTDOWN task just to block shutdown. |
|
danakj
2016/04/06 19:38:30
and here, "Post" isn't strictly right.
fdoray
2016/04/06 20:59:27
Done.
|
| - PostTaskViaTracker(CreateTask(TaskShutdownBehavior::BLOCK_SHUTDOWN)); |
| + std::unique_ptr<Task> block_shutdown_task( |
| + CreateTask(TaskShutdownBehavior::BLOCK_SHUTDOWN)); |
| + EXPECT_TRUE(tracker_.WillPostTask(block_shutdown_task.get())); |
| // Call Shutdown() asynchronously. |
| CallShutdownAsync(); |
| @@ -219,27 +190,22 @@ TEST_P(TaskSchedulerTaskTrackerTest, PostBeforeShutdownRunDuringShutdown) { |
| // Try to run the first posted task. Only BLOCK_SHUTDOWN tasks should run, |
| // others should be discarded. |
| EXPECT_EQ(0U, num_tasks_executed_); |
| - RunNextPostedTaskViaTracker(); |
| + tracker_.RunTask(task.get()); |
| EXPECT_EQ(GetParam() == TaskShutdownBehavior::BLOCK_SHUTDOWN ? 1U : 0U, |
| num_tasks_executed_); |
| VERIFY_ASYNC_SHUTDOWN_IN_PROGRESS(); |
| // Unblock shutdown by running the remaining BLOCK_SHUTDOWN task. |
| - RunNextPostedTaskViaTracker(); |
| + tracker_.RunTask(block_shutdown_task.get()); |
| EXPECT_EQ(GetParam() == TaskShutdownBehavior::BLOCK_SHUTDOWN ? 2U : 1U, |
| num_tasks_executed_); |
| WAIT_FOR_ASYNC_SHUTDOWN_COMPLETED(); |
| } |
| TEST_P(TaskSchedulerTaskTrackerTest, PostBeforeShutdownRunAfterShutdown) { |
| - std::unique_ptr<Task> task_to_post(CreateTask(GetParam())); |
| - const Task* task_to_post_raw = task_to_post.get(); |
| - |
| - // Post the task. |
| - EXPECT_TRUE(posted_tasks_.empty()); |
| - PostTaskViaTracker(std::move(task_to_post)); |
| - EXPECT_EQ(1U, posted_tasks_.size()); |
| - EXPECT_EQ(task_to_post_raw, posted_tasks_.front().get()); |
| + // Post a task. |
|
robliao
2016/04/06 19:42:33
Same here.
fdoray
2016/04/06 20:59:27
Done.
|
| + std::unique_ptr<Task> task(CreateTask(GetParam())); |
| + EXPECT_TRUE(tracker_.WillPostTask(task.get())); |
| // Call Shutdown() asynchronously. |
| CallShutdownAsync(); |
| @@ -249,7 +215,7 @@ TEST_P(TaskSchedulerTaskTrackerTest, PostBeforeShutdownRunAfterShutdown) { |
| VERIFY_ASYNC_SHUTDOWN_IN_PROGRESS(); |
| // Run the task to unblock shutdown. |
| - RunNextPostedTaskViaTracker(); |
| + tracker_.RunTask(task.get()); |
| EXPECT_EQ(1U, num_tasks_executed_); |
| WAIT_FOR_ASYNC_SHUTDOWN_COMPLETED(); |
| @@ -260,16 +226,16 @@ TEST_P(TaskSchedulerTaskTrackerTest, PostBeforeShutdownRunAfterShutdown) { |
| WAIT_FOR_ASYNC_SHUTDOWN_COMPLETED(); |
| // The task shouldn't be allowed to run after shutdown. |
| - RunNextPostedTaskViaTracker(); |
| + tracker_.RunTask(task.get()); |
| EXPECT_EQ(0U, num_tasks_executed_); |
| } |
| } |
| TEST_P(TaskSchedulerTaskTrackerTest, PostAndRunDuringShutdown) { |
| // Post a BLOCK_SHUTDOWN task just to block shutdown. |
| - PostTaskViaTracker(CreateTask(TaskShutdownBehavior::BLOCK_SHUTDOWN)); |
| - std::unique_ptr<Task> block_shutdown_task = std::move(posted_tasks_.front()); |
| - posted_tasks_.pop(); |
| + std::unique_ptr<Task> block_shutdown_task( |
| + CreateTask(TaskShutdownBehavior::BLOCK_SHUTDOWN)); |
| + EXPECT_TRUE(tracker_.WillPostTask(block_shutdown_task.get())); |
| // Call Shutdown() asynchronously. |
| CallShutdownAsync(); |
| @@ -277,18 +243,17 @@ TEST_P(TaskSchedulerTaskTrackerTest, PostAndRunDuringShutdown) { |
| if (GetParam() == TaskShutdownBehavior::BLOCK_SHUTDOWN) { |
| // Post a BLOCK_SHUTDOWN task. This should succeed. |
| - EXPECT_TRUE(posted_tasks_.empty()); |
| - PostTaskViaTracker(CreateTask(GetParam())); |
| - EXPECT_EQ(1U, posted_tasks_.size()); |
| + std::unique_ptr<Task> task(CreateTask(GetParam())); |
| + EXPECT_TRUE(tracker_.WillPostTask(task.get())); |
| // Run the BLOCK_SHUTDOWN task. This should succeed. |
| EXPECT_EQ(0U, num_tasks_executed_); |
| - RunNextPostedTaskViaTracker(); |
| + tracker_.RunTask(task.get()); |
| EXPECT_EQ(1U, num_tasks_executed_); |
| } else { |
| // It shouldn't be possible to post a non BLOCK_SHUTDOWN task. |
| - PostTaskViaTracker(CreateTask(GetParam())); |
| - EXPECT_TRUE(posted_tasks_.empty()); |
| + std::unique_ptr<Task> task(CreateTask(GetParam())); |
| + EXPECT_FALSE(tracker_.WillPostTask(task.get())); |
| // Don't try to run the task, because it hasn't been posted successfully. |
| } |
| @@ -305,15 +270,14 @@ TEST_P(TaskSchedulerTaskTrackerTest, PostAndRunDuringShutdown) { |
| TEST_P(TaskSchedulerTaskTrackerTest, PostAfterShutdown) { |
| // It is not possible to post a task after shutdown. |
| tracker_.Shutdown(); |
| - EXPECT_TRUE(posted_tasks_.empty()); |
| + |
| + std::unique_ptr<Task> task(CreateTask(GetParam())); |
| if (GetParam() == TaskShutdownBehavior::BLOCK_SHUTDOWN) { |
| - EXPECT_DCHECK_DEATH({ PostTaskViaTracker(CreateTask(GetParam())); }, ""); |
| + EXPECT_DCHECK_DEATH({ tracker_.WillPostTask(task.get()); }, ""); |
| } else { |
| - PostTaskViaTracker(CreateTask(GetParam())); |
| + EXPECT_FALSE(tracker_.WillPostTask(task.get())); |
| } |
| - |
| - EXPECT_TRUE(posted_tasks_.empty()); |
| } |
| INSTANTIATE_TEST_CASE_P( |