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 internal { |
| 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 bool ran_all_tasks_with_expected_priority() const { |
| 39 return ran_all_tasks_with_expected_priority_; |
| 40 } |
| 41 |
| 42 TaskSchedulerImpl scheduler_; |
| 43 |
| 44 private: |
| 45 void RunTask(ThreadPriority expected_priority) { |
| 46 #if !defined(OS_MACOSX) |
| 47 // PlatformThread::GetCurrentThreadPriority() isn't implemented on Mac. |
| 48 // crbug.com/554651 |
| 49 if (PlatformThread::GetCurrentThreadPriority() != expected_priority) |
| 50 ran_all_tasks_with_expected_priority_ = false; |
| 51 #endif // defined(OS_MACOSX) |
| 52 |
| 53 AutoSchedulerLock auto_lock(lock_); |
| 54 ++num_executed_tasks_; |
| 55 cv_.Signal(); |
| 56 } |
| 57 |
| 58 // Lock protecting |cv_|. |
| 59 SchedulerLock lock_; |
| 60 |
| 61 // Condition variable signaled each time a task completes its execution. |
| 62 ConditionVariable cv_; |
| 63 |
| 64 // Number of posted tasks. |
| 65 size_t num_posted_tasks_; |
| 66 |
| 67 // Number of tasks that were executed. |
| 68 size_t num_executed_tasks_; |
| 69 |
| 70 // True if all tasks ran on a thread with the expected priority. |
| 71 bool ran_all_tasks_with_expected_priority_; |
| 72 }; |
| 73 |
| 74 TEST_F(TaskSchedulerImplTest, PostTaskWithTraits) { |
| 75 scheduler_.PostTaskWithTraits( |
| 76 FROM_HERE, TaskTraits().WithPriority(TaskPriority::BACKGROUND), |
| 77 GetTaskClosure(ThreadPriority::BACKGROUND)); |
| 78 scheduler_.PostTaskWithTraits( |
| 79 FROM_HERE, TaskTraits().WithPriority(TaskPriority::USER_VISIBLE), |
| 80 GetTaskClosure(ThreadPriority::NORMAL)); |
| 81 scheduler_.PostTaskWithTraits( |
| 82 FROM_HERE, TaskTraits().WithPriority(TaskPriority::USER_BLOCKING), |
| 83 GetTaskClosure(ThreadPriority::NORMAL)); |
| 84 |
| 85 scheduler_.PostTaskWithTraits( |
| 86 FROM_HERE, |
| 87 TaskTraits().WithPriority(TaskPriority::BACKGROUND).WithFileIO(), |
| 88 GetTaskClosure(ThreadPriority::BACKGROUND)); |
| 89 scheduler_.PostTaskWithTraits( |
| 90 FROM_HERE, |
| 91 TaskTraits().WithPriority(TaskPriority::USER_VISIBLE).WithFileIO(), |
| 92 GetTaskClosure(ThreadPriority::NORMAL)); |
| 93 scheduler_.PostTaskWithTraits( |
| 94 FROM_HERE, |
| 95 TaskTraits().WithPriority(TaskPriority::USER_BLOCKING).WithFileIO(), |
| 96 GetTaskClosure(ThreadPriority::NORMAL)); |
| 97 |
| 98 WaitUntilLastPostedTaskHasRun(); |
| 99 scheduler_.ShutdownAndJoinAllThreadsForTesting(); |
| 100 |
| 101 EXPECT_TRUE(ran_all_tasks_with_expected_priority()); |
| 102 } |
| 103 |
| 104 TEST_F(TaskSchedulerImplTest, CreateTaskRunnerWithTraits) { |
| 105 scheduler_.CreateTaskRunnerWithTraits( |
| 106 TaskTraits().WithPriority(TaskPriority::BACKGROUND), |
| 107 ExecutionMode::PARALLEL) |
| 108 ->PostTask(FROM_HERE, GetTaskClosure(ThreadPriority::BACKGROUND)); |
| 109 scheduler_.CreateTaskRunnerWithTraits( |
| 110 TaskTraits().WithPriority(TaskPriority::USER_VISIBLE), |
| 111 ExecutionMode::PARALLEL) |
| 112 ->PostTask(FROM_HERE, GetTaskClosure(ThreadPriority::NORMAL)); |
| 113 scheduler_.CreateTaskRunnerWithTraits( |
| 114 TaskTraits().WithPriority(TaskPriority::USER_BLOCKING), |
| 115 ExecutionMode::PARALLEL) |
| 116 ->PostTask(FROM_HERE, GetTaskClosure(ThreadPriority::NORMAL)); |
| 117 |
| 118 scheduler_ |
| 119 .CreateTaskRunnerWithTraits( |
| 120 TaskTraits().WithPriority(TaskPriority::BACKGROUND).WithFileIO(), |
| 121 ExecutionMode::PARALLEL) |
| 122 ->PostTask(FROM_HERE, GetTaskClosure(ThreadPriority::BACKGROUND)); |
| 123 scheduler_ |
| 124 .CreateTaskRunnerWithTraits( |
| 125 TaskTraits().WithPriority(TaskPriority::USER_VISIBLE).WithFileIO(), |
| 126 ExecutionMode::PARALLEL) |
| 127 ->PostTask(FROM_HERE, GetTaskClosure(ThreadPriority::NORMAL)); |
| 128 scheduler_ |
| 129 .CreateTaskRunnerWithTraits( |
| 130 TaskTraits().WithPriority(TaskPriority::USER_BLOCKING).WithFileIO(), |
| 131 ExecutionMode::PARALLEL) |
| 132 ->PostTask(FROM_HERE, GetTaskClosure(ThreadPriority::NORMAL)); |
| 133 |
| 134 WaitUntilLastPostedTaskHasRun(); |
| 135 scheduler_.ShutdownAndJoinAllThreadsForTesting(); |
| 136 |
| 137 EXPECT_TRUE(ran_all_tasks_with_expected_priority()); |
| 138 } |
| 139 |
| 140 } // namespace internal |
| 141 } // namespace base |
OLD | NEW |