OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "base/task_scheduler/task_scheduler_impl.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/callback_forward.h" |
| 9 #include "base/synchronization/condition_variable.h" |
| 10 #include "base/task_scheduler/scheduler_lock.h" |
| 11 #include "base/threading/platform_thread.h" |
| 12 #include "build/build_config.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 |
| 15 namespace base { |
| 16 namespace task_scheduler { |
| 17 |
| 18 class TaskSchedulerImplTest : public testing::Test { |
| 19 protected: |
| 20 TaskSchedulerImplTest() |
| 21 : cv_(lock_.RawLockForConditionVariable()), |
| 22 num_posted_tasks_(0), |
| 23 num_executed_tasks_(0), |
| 24 ran_all_tasks_with_expected_priority_(true) {} |
| 25 |
| 26 Closure GetTaskClosure(ThreadPriority expected_priority) { |
| 27 ++num_posted_tasks_; |
| 28 return Bind(&TaskSchedulerImplTest::RunTask, Unretained(this), |
| 29 expected_priority); |
| 30 } |
| 31 |
| 32 void WaitUntilLastPostedTaskHasRun() { |
| 33 AutoSchedulerLock auto_lock(lock_); |
| 34 while (num_posted_tasks_ != num_executed_tasks_) |
| 35 cv_.Wait(); |
| 36 } |
| 37 |
| 38 void Shutdown() { |
| 39 scheduler_.Shutdown(); |
| 40 scheduler_.JoinAllThreadsForTesting(); |
| 41 } |
| 42 |
| 43 bool ran_all_tasks_with_expected_priority() const { |
| 44 return ran_all_tasks_with_expected_priority_; |
| 45 } |
| 46 |
| 47 TaskSchedulerImpl scheduler_; |
| 48 |
| 49 private: |
| 50 void RunTask(ThreadPriority expected_priority) { |
| 51 #if !defined(OS_MACOSX) |
| 52 // PlatformThread::GetCurrentThreadPriority() isn't implemented on Mac. |
| 53 // crbug.com/554651 |
| 54 if (PlatformThread::GetCurrentThreadPriority() != expected_priority) |
| 55 ran_all_tasks_with_expected_priority_ = false; |
| 56 #endif // defined(OS_MACOSX) |
| 57 |
| 58 AutoSchedulerLock auto_lock(lock_); |
| 59 ++num_executed_tasks_; |
| 60 cv_.Signal(); |
| 61 } |
| 62 |
| 63 // Lock protecting |cv_|. |
| 64 SchedulerLock lock_; |
| 65 |
| 66 // Condition variable signaled each time a task completes its execution. |
| 67 ConditionVariable cv_; |
| 68 |
| 69 // Number of posted tasks. |
| 70 size_t num_posted_tasks_; |
| 71 |
| 72 // Number of tasks that were executed. |
| 73 size_t num_executed_tasks_; |
| 74 |
| 75 // True if all tasks ran on a thread with the expected priority. |
| 76 bool ran_all_tasks_with_expected_priority_; |
| 77 }; |
| 78 |
| 79 TEST_F(TaskSchedulerImplTest, PostTaskWithTraits) { |
| 80 scheduler_.PostTaskWithTraits( |
| 81 FROM_HERE, TaskTraits().WithPriority(TaskPriority::BACKGROUND), |
| 82 GetTaskClosure(ThreadPriority::BACKGROUND)); |
| 83 scheduler_.PostTaskWithTraits( |
| 84 FROM_HERE, TaskTraits().WithPriority(TaskPriority::USER_VISIBLE), |
| 85 GetTaskClosure(ThreadPriority::NORMAL)); |
| 86 scheduler_.PostTaskWithTraits( |
| 87 FROM_HERE, TaskTraits().WithPriority(TaskPriority::USER_BLOCKING), |
| 88 GetTaskClosure(ThreadPriority::NORMAL)); |
| 89 |
| 90 scheduler_.PostTaskWithTraits( |
| 91 FROM_HERE, |
| 92 TaskTraits().WithPriority(TaskPriority::BACKGROUND).WithFileIO(), |
| 93 GetTaskClosure(ThreadPriority::BACKGROUND)); |
| 94 scheduler_.PostTaskWithTraits( |
| 95 FROM_HERE, |
| 96 TaskTraits().WithPriority(TaskPriority::USER_VISIBLE).WithFileIO(), |
| 97 GetTaskClosure(ThreadPriority::NORMAL)); |
| 98 scheduler_.PostTaskWithTraits( |
| 99 FROM_HERE, |
| 100 TaskTraits().WithPriority(TaskPriority::USER_BLOCKING).WithFileIO(), |
| 101 GetTaskClosure(ThreadPriority::NORMAL)); |
| 102 |
| 103 WaitUntilLastPostedTaskHasRun(); |
| 104 Shutdown(); |
| 105 |
| 106 EXPECT_TRUE(ran_all_tasks_with_expected_priority()); |
| 107 } |
| 108 |
| 109 TEST_F(TaskSchedulerImplTest, CreateTaskRunnerWithTraits) { |
| 110 scheduler_.CreateTaskRunnerWithTraits( |
| 111 TaskTraits().WithPriority(TaskPriority::BACKGROUND), |
| 112 ExecutionMode::PARALLEL) |
| 113 ->PostTask(FROM_HERE, GetTaskClosure(ThreadPriority::BACKGROUND)); |
| 114 scheduler_.CreateTaskRunnerWithTraits( |
| 115 TaskTraits().WithPriority(TaskPriority::USER_VISIBLE), |
| 116 ExecutionMode::PARALLEL) |
| 117 ->PostTask(FROM_HERE, GetTaskClosure(ThreadPriority::NORMAL)); |
| 118 scheduler_.CreateTaskRunnerWithTraits( |
| 119 TaskTraits().WithPriority(TaskPriority::USER_BLOCKING), |
| 120 ExecutionMode::PARALLEL) |
| 121 ->PostTask(FROM_HERE, GetTaskClosure(ThreadPriority::NORMAL)); |
| 122 |
| 123 scheduler_ |
| 124 .CreateTaskRunnerWithTraits( |
| 125 TaskTraits().WithPriority(TaskPriority::BACKGROUND).WithFileIO(), |
| 126 ExecutionMode::PARALLEL) |
| 127 ->PostTask(FROM_HERE, GetTaskClosure(ThreadPriority::BACKGROUND)); |
| 128 scheduler_ |
| 129 .CreateTaskRunnerWithTraits( |
| 130 TaskTraits().WithPriority(TaskPriority::USER_VISIBLE).WithFileIO(), |
| 131 ExecutionMode::PARALLEL) |
| 132 ->PostTask(FROM_HERE, GetTaskClosure(ThreadPriority::NORMAL)); |
| 133 scheduler_ |
| 134 .CreateTaskRunnerWithTraits( |
| 135 TaskTraits().WithPriority(TaskPriority::USER_BLOCKING).WithFileIO(), |
| 136 ExecutionMode::PARALLEL) |
| 137 ->PostTask(FROM_HERE, GetTaskClosure(ThreadPriority::NORMAL)); |
| 138 |
| 139 WaitUntilLastPostedTaskHasRun(); |
| 140 Shutdown(); |
| 141 |
| 142 EXPECT_TRUE(ran_all_tasks_with_expected_priority()); |
| 143 } |
| 144 |
| 145 } // namespace task_scheduler |
| 146 } // namespace base |
OLD | NEW |