Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/task_scheduler/scheduler_worker_thread.h" | 5 #include "base/task_scheduler/scheduler_worker_thread.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <memory> | 9 #include <memory> |
| 10 #include <vector> | 10 #include <vector> |
| 11 | 11 |
| 12 #include "base/bind.h" | 12 #include "base/bind.h" |
| 13 #include "base/bind_helpers.h" | 13 #include "base/bind_helpers.h" |
| 14 #include "base/macros.h" | 14 #include "base/macros.h" |
| 15 #include "base/memory/ptr_util.h" | 15 #include "base/memory/ptr_util.h" |
| 16 #include "base/synchronization/condition_variable.h" | 16 #include "base/synchronization/condition_variable.h" |
| 17 #include "base/task_scheduler/scheduler_lock.h" | 17 #include "base/task_scheduler/scheduler_lock.h" |
| 18 #include "base/task_scheduler/scheduler_worker_thread_delegate.h" | |
| 18 #include "base/task_scheduler/sequence.h" | 19 #include "base/task_scheduler/sequence.h" |
| 19 #include "base/task_scheduler/task.h" | 20 #include "base/task_scheduler/task.h" |
| 20 #include "base/task_scheduler/task_tracker.h" | 21 #include "base/task_scheduler/task_tracker.h" |
| 21 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
| 22 | 23 |
| 23 namespace base { | 24 namespace base { |
| 24 namespace internal { | 25 namespace internal { |
| 25 namespace { | 26 namespace { |
| 26 | 27 |
| 27 const size_t kNumSequencesPerTest = 150; | 28 const size_t kNumSequencesPerTest = 150; |
| 28 | 29 |
| 29 class TaskSchedulerWorkerThreadTest : public testing::Test { | 30 class TaskSchedulerWorkerThreadTest : public testing::Test, |
| 31 public SchedulerWorkerThreadDelegate { | |
| 30 protected: | 32 protected: |
| 31 TaskSchedulerWorkerThreadTest() | 33 TaskSchedulerWorkerThreadTest() |
| 32 : num_main_entry_callback_cv_(lock_.CreateConditionVariable()), | 34 : num_main_entry_cv_(lock_.CreateConditionVariable()), |
| 33 num_get_work_callback_cv_(lock_.CreateConditionVariable()), | 35 num_get_work_callback_cv_(lock_.CreateConditionVariable()), |
|
robliao
2016/04/06 21:28:20
Nit: Remove callback?
Review for remaining uses.
fdoray
2016/04/07 13:53:41
Done.
| |
| 34 run_sequences_cv_(lock_.CreateConditionVariable()) {} | 36 run_sequences_cv_(lock_.CreateConditionVariable()) {} |
| 35 | 37 |
| 36 void SetUp() override { | 38 void SetUp() override { |
| 37 worker_thread_ = SchedulerWorkerThread::CreateSchedulerWorkerThread( | 39 worker_thread_ = SchedulerWorkerThread::CreateSchedulerWorkerThread( |
| 38 ThreadPriority::NORMAL, | 40 ThreadPriority::NORMAL, this, &task_tracker_); |
| 39 Bind(&TaskSchedulerWorkerThreadTest::MainEntryCallback, | |
| 40 Unretained(this)), | |
| 41 Bind(&TaskSchedulerWorkerThreadTest::GetWorkCallback, Unretained(this)), | |
| 42 Bind(&TaskSchedulerWorkerThreadTest::RanTaskFromSequenceCallback, | |
| 43 Unretained(this)), | |
| 44 &task_tracker_); | |
| 45 ASSERT_TRUE(worker_thread_); | 41 ASSERT_TRUE(worker_thread_); |
| 46 WaitForNumMainEntryCallback(1); | 42 WaitForMainEntry(); |
| 47 } | 43 } |
| 48 | 44 |
| 49 void TearDown() override { | 45 void TearDown() override { |
| 46 { | |
| 47 AutoSchedulerLock auto_lock(lock_); | |
| 48 EXPECT_EQ(0U, num_main_exit_); | |
| 49 } | |
| 50 | |
| 50 worker_thread_->JoinForTesting(); | 51 worker_thread_->JoinForTesting(); |
| 51 | 52 |
| 52 AutoSchedulerLock auto_lock(lock_); | 53 AutoSchedulerLock auto_lock(lock_); |
| 53 EXPECT_EQ(1U, num_main_entry_callback_); | 54 EXPECT_EQ(1U, num_main_entry_); |
| 55 EXPECT_EQ(1U, num_main_exit_); | |
| 54 } | 56 } |
| 55 | 57 |
| 56 // Wait until MainEntryCallback() has been called |num_main_entry_callback| | 58 // Wait until OnMainEntry() has been called once. |
| 57 // times. | 59 void WaitForMainEntry() { |
| 58 void WaitForNumMainEntryCallback(size_t num_main_entry_callback) { | |
| 59 AutoSchedulerLock auto_lock(lock_); | 60 AutoSchedulerLock auto_lock(lock_); |
| 60 while (num_main_entry_callback_ < num_main_entry_callback) | 61 while (num_main_entry_ < 1) |
| 61 num_main_entry_callback_cv_->Wait(); | 62 num_main_entry_cv_->Wait(); |
| 62 } | 63 } |
| 63 | 64 |
| 64 // Wait until GetWorkCallback() has been called |num_get_work_callback| times. | 65 // Wait until GetWork() has been called |num_get_work_callback| times. |
| 65 void WaitForNumGetWorkCallback(size_t num_get_work_callback) { | 66 void WaitForNumGetWork(size_t num_get_work_callback) { |
| 66 AutoSchedulerLock auto_lock(lock_); | 67 AutoSchedulerLock auto_lock(lock_); |
| 67 while (num_get_work_callback_ < num_get_work_callback) | 68 while (num_get_work_callback_ < num_get_work_callback) |
| 68 num_get_work_callback_cv_->Wait(); | 69 num_get_work_callback_cv_->Wait(); |
| 69 } | 70 } |
| 70 | 71 |
| 71 // Wait until there is no more Sequences to create and | 72 // Wait until there are no more Sequences to create and |
| 72 // RanTaskFromSequenceCallback() has been invoked once for each Sequence | 73 // RanTaskFromSequenceCallback() has been invoked once for each Sequence |
| 73 // returned by GetWorkCallback(). | 74 // returned by GetWork(). |
| 74 void WaitForAllSequencesToRun() { | 75 void WaitForAllSequencesToRun() { |
| 75 AutoSchedulerLock auto_lock(lock_); | 76 AutoSchedulerLock auto_lock(lock_); |
| 76 | 77 |
| 77 while (num_sequences_to_create_ > 0 || | 78 while (num_sequences_to_create_ > 0 || |
| 78 run_sequences_.size() < created_sequences_.size()) { | 79 run_sequences_.size() < created_sequences_.size()) { |
| 79 run_sequences_cv_->Wait(); | 80 run_sequences_cv_->Wait(); |
| 80 } | 81 } |
| 81 | 82 |
| 82 // Verify that RanTaskFromSequenceCallback() has been invoked with the | 83 // Verify that RanTaskFromSequenceCallback() has been invoked with the |
| 83 // same Sequences that were returned by GetWorkCallback(). | 84 // same Sequences that were returned by GetWork(). |
| 84 EXPECT_EQ(created_sequences_, run_sequences_); | 85 EXPECT_EQ(created_sequences_, run_sequences_); |
| 85 | 86 |
| 86 // Verify that RunTaskCallback() has been invoked once for each Sequence | 87 // Verify that RunTaskCallback() has been invoked once for each Sequence |
| 87 // returned by GetWorkCallback(). | 88 // returned by GetWork(). |
| 88 EXPECT_EQ(created_sequences_.size(), num_run_tasks_); | 89 EXPECT_EQ(created_sequences_.size(), num_run_tasks_); |
| 89 } | 90 } |
| 90 | 91 |
| 91 void SetNumSequencesToCreate(size_t num_sequences_to_create) { | 92 void SetNumSequencesToCreate(size_t num_sequences_to_create) { |
| 92 AutoSchedulerLock auto_lock(lock_); | 93 AutoSchedulerLock auto_lock(lock_); |
| 93 EXPECT_EQ(0U, num_sequences_to_create_); | 94 EXPECT_EQ(0U, num_sequences_to_create_); |
| 94 num_sequences_to_create_ = num_sequences_to_create; | 95 num_sequences_to_create_ = num_sequences_to_create; |
| 95 } | 96 } |
| 96 | 97 |
| 97 size_t NumGetWorkCallback() const { | 98 size_t NumGetWork() const { |
| 98 AutoSchedulerLock auto_lock(lock_); | 99 AutoSchedulerLock auto_lock(lock_); |
| 99 return num_get_work_callback_; | 100 return num_get_work_callback_; |
| 100 } | 101 } |
| 101 | 102 |
| 102 std::unique_ptr<SchedulerWorkerThread> worker_thread_; | 103 std::unique_ptr<SchedulerWorkerThread> worker_thread_; |
| 103 | 104 |
| 104 private: | 105 private: |
| 105 void MainEntryCallback() { | 106 // SchedulerWorkerThreadDelegate: |
| 107 void OnMainEntry() override { | |
| 106 AutoSchedulerLock auto_lock(lock_); | 108 AutoSchedulerLock auto_lock(lock_); |
| 107 ++num_main_entry_callback_; | 109 ++num_main_entry_; |
| 108 num_main_entry_callback_cv_->Signal(); | 110 num_main_entry_cv_->Signal(); |
| 109 } | 111 } |
| 110 | 112 |
| 111 // Returns a Sequence that contains 1 Task if |num_sequences_to_create_| is | 113 void OnMainExit() override { |
| 112 // greater than 0. | 114 AutoSchedulerLock auto_lock(lock_); |
| 113 scoped_refptr<Sequence> GetWorkCallback( | 115 ++num_main_exit_; |
| 114 SchedulerWorkerThread* worker_thread) { | 116 } |
| 115 EXPECT_EQ(worker_thread_.get(), worker_thread); | |
| 116 | 117 |
| 118 scoped_refptr<Sequence> GetWork() override { | |
| 117 { | 119 { |
| 118 AutoSchedulerLock auto_lock(lock_); | 120 AutoSchedulerLock auto_lock(lock_); |
| 119 | 121 |
| 120 // Increment the number of times that this callback has been invoked. | 122 // Increment the number of times that this callback has been invoked. |
| 121 ++num_get_work_callback_; | 123 ++num_get_work_callback_; |
| 122 num_get_work_callback_cv_->Signal(); | 124 num_get_work_callback_cv_->Signal(); |
| 123 | 125 |
| 124 // Check if a Sequence should be returned. | 126 // Check if a Sequence should be returned. |
| 125 if (num_sequences_to_create_ == 0) | 127 if (num_sequences_to_create_ == 0) |
| 126 return nullptr; | 128 return nullptr; |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 138 | 140 |
| 139 { | 141 { |
| 140 // Add the Sequence to the vector of created Sequences. | 142 // Add the Sequence to the vector of created Sequences. |
| 141 AutoSchedulerLock auto_lock(lock_); | 143 AutoSchedulerLock auto_lock(lock_); |
| 142 created_sequences_.push_back(sequence); | 144 created_sequences_.push_back(sequence); |
| 143 } | 145 } |
| 144 | 146 |
| 145 return sequence; | 147 return sequence; |
| 146 } | 148 } |
| 147 | 149 |
| 148 void RanTaskFromSequenceCallback(const SchedulerWorkerThread* worker_thread, | 150 // Called after the SchedulerWorkerThread has tried to run a Task from |
| 149 scoped_refptr<Sequence> sequence) { | 151 // |sequence| (a TaskTracker might have prevented the Task from running). |
| 150 EXPECT_EQ(worker_thread_.get(), worker_thread); | 152 void RanTaskFromSequence(scoped_refptr<Sequence> sequence) override { |
| 151 | |
| 152 AutoSchedulerLock auto_lock(lock_); | 153 AutoSchedulerLock auto_lock(lock_); |
| 153 run_sequences_.push_back(std::move(sequence)); | 154 run_sequences_.push_back(std::move(sequence)); |
| 154 run_sequences_cv_->Signal(); | 155 run_sequences_cv_->Signal(); |
| 155 } | 156 } |
| 156 | 157 |
| 157 void RunTaskCallback() { | 158 void RunTaskCallback() { |
| 158 AutoSchedulerLock auto_lock(lock_); | 159 AutoSchedulerLock auto_lock(lock_); |
| 159 ++num_run_tasks_; | 160 ++num_run_tasks_; |
| 160 } | 161 } |
| 161 | 162 |
| 162 TaskTracker task_tracker_; | 163 TaskTracker task_tracker_; |
| 163 | 164 |
| 164 // Synchronizes access to all members below. | 165 // Synchronizes access to all members below. |
| 165 mutable SchedulerLock lock_; | 166 mutable SchedulerLock lock_; |
| 166 | 167 |
| 167 // Number of times that MainEntryCallback() has been called. | 168 // Number of times that OnMainEntry() has been called. |
| 168 size_t num_main_entry_callback_ = 0; | 169 size_t num_main_entry_ = 0; |
| 169 | 170 |
| 170 // Condition variable signaled when |num_main_entry_callback_| is incremented. | 171 // Condition variable signaled when |num_main_entry_| is incremented. |
| 171 std::unique_ptr<ConditionVariable> num_main_entry_callback_cv_; | 172 std::unique_ptr<ConditionVariable> num_main_entry_cv_; |
| 172 | 173 |
| 173 // Number of Sequences that should be created by GetWorkCallback(). When this | 174 // Number of times that OnMainExit() has been called. |
| 174 // is 0, GetWorkCallback() returns nullptr. | 175 size_t num_main_exit_ = 0; |
| 176 | |
| 177 // Number of Sequences that should be created by GetWork(). When this | |
| 178 // is 0, GetWork() returns nullptr. | |
| 175 size_t num_sequences_to_create_ = 0; | 179 size_t num_sequences_to_create_ = 0; |
| 176 | 180 |
| 177 // Number of times that GetWorkCallback() has been called. | 181 // Number of times that GetWork() has been called. |
| 178 size_t num_get_work_callback_ = 0; | 182 size_t num_get_work_callback_ = 0; |
| 179 | 183 |
| 180 // Condition variable signaled when |num_get_work_callback_| is incremented. | 184 // Condition variable signaled when |num_get_work_callback_| is incremented. |
| 181 std::unique_ptr<ConditionVariable> num_get_work_callback_cv_; | 185 std::unique_ptr<ConditionVariable> num_get_work_callback_cv_; |
| 182 | 186 |
| 183 // Sequences created by GetWorkCallback(). | 187 // Sequences created by GetWork(). |
| 184 std::vector<scoped_refptr<Sequence>> created_sequences_; | 188 std::vector<scoped_refptr<Sequence>> created_sequences_; |
| 185 | 189 |
| 186 // Sequences passed to RanTaskFromSequenceCallback(). | 190 // Sequences passed to RanTaskFromSequenceCallback(). |
| 187 std::vector<scoped_refptr<Sequence>> run_sequences_; | 191 std::vector<scoped_refptr<Sequence>> run_sequences_; |
| 188 | 192 |
| 189 // Condition variable signaled when a Sequence is added to |run_sequences_|. | 193 // Condition variable signaled when a Sequence is added to |run_sequences_|. |
| 190 std::unique_ptr<ConditionVariable> run_sequences_cv_; | 194 std::unique_ptr<ConditionVariable> run_sequences_cv_; |
| 191 | 195 |
| 192 // Number of times that RunTaskCallback() has been called. | 196 // Number of times that RunTaskCallback() has been called. |
| 193 size_t num_run_tasks_ = 0; | 197 size_t num_run_tasks_ = 0; |
| 194 | 198 |
| 195 DISALLOW_COPY_AND_ASSIGN(TaskSchedulerWorkerThreadTest); | 199 DISALLOW_COPY_AND_ASSIGN(TaskSchedulerWorkerThreadTest); |
| 196 }; | 200 }; |
| 197 | 201 |
| 198 // Verify that when GetWorkCallback() continuously returns Sequences, all Tasks | 202 // Verify that when GetWork() continuously returns Sequences, all Tasks in these |
| 199 // in these Sequences run successfully. The SchedulerWorkerThread is woken up | 203 // Sequences run successfully. The SchedulerWorkerThread is woken up once. |
| 200 // once. | |
| 201 TEST_F(TaskSchedulerWorkerThreadTest, ContinousWork) { | 204 TEST_F(TaskSchedulerWorkerThreadTest, ContinousWork) { |
| 202 // Set GetWorkCallback() to return |kNumSequencesPerTest| Sequences before | 205 // Set GetWork() to return |kNumSequencesPerTest| Sequences before starting to |
| 203 // starting to return nullptr. | 206 // return nullptr. |
| 204 SetNumSequencesToCreate(kNumSequencesPerTest); | 207 SetNumSequencesToCreate(kNumSequencesPerTest); |
| 205 | 208 |
| 206 // Wake up |worker_thread_| and wait until it has run all the Tasks returned | 209 // Wake up |worker_thread_| and wait until it has run all the Tasks returned |
| 207 // by GetWorkCallback(). | 210 // by GetWork(). |
| 208 worker_thread_->WakeUp(); | 211 worker_thread_->WakeUp(); |
| 209 WaitForAllSequencesToRun(); | 212 WaitForAllSequencesToRun(); |
| 210 | 213 |
| 211 // Expect |kNumSequencesPerTest| calls to GetWorkCallback() in which it | 214 // Expect |kNumSequencesPerTest| calls to GetWork() in which it returned a |
| 212 // returned a Sequence and 1 call in which it returned nullptr. | 215 // Sequence and 1 call in which it returned nullptr. |
| 213 const size_t expected_num_get_work_callback = kNumSequencesPerTest + 1; | 216 const size_t expected_num_get_work_callback = kNumSequencesPerTest + 1; |
| 214 WaitForNumGetWorkCallback(expected_num_get_work_callback); | 217 WaitForNumGetWork(expected_num_get_work_callback); |
| 215 EXPECT_EQ(expected_num_get_work_callback, NumGetWorkCallback()); | 218 EXPECT_EQ(expected_num_get_work_callback, NumGetWork()); |
| 216 } | 219 } |
| 217 | 220 |
| 218 // Verify that when GetWorkCallback() alternates between returning a Sequence | 221 // Verify that when GetWork() alternates between returning a Sequence and |
| 219 // and returning nullptr, all Tasks in the returned Sequences run successfully. | 222 // returning nullptr, all Tasks in the returned Sequences run successfully. The |
| 220 // The SchedulerWorkerThread is woken up once for each Sequence. | 223 // SchedulerWorkerThread is woken up once for each Sequence. |
| 221 TEST_F(TaskSchedulerWorkerThreadTest, IntermittentWork) { | 224 TEST_F(TaskSchedulerWorkerThreadTest, IntermittentWork) { |
| 222 for (size_t i = 0; i < kNumSequencesPerTest; ++i) { | 225 for (size_t i = 0; i < kNumSequencesPerTest; ++i) { |
| 223 // Set GetWorkCallback() to return 1 Sequence before starting to return | 226 // Set GetWork() to return 1 Sequence before starting to return |
| 224 // nullptr. | 227 // nullptr. |
| 225 SetNumSequencesToCreate(1); | 228 SetNumSequencesToCreate(1); |
| 226 | 229 |
| 227 // Wake up |worker_thread_| and wait until it has run all the Tasks returned | 230 // Wake up |worker_thread_| and wait until it has run all the Tasks returned |
| 228 // by GetWorkCallback(). | 231 // by GetWork(). |
| 229 worker_thread_->WakeUp(); | 232 worker_thread_->WakeUp(); |
| 230 WaitForAllSequencesToRun(); | 233 WaitForAllSequencesToRun(); |
| 231 | 234 |
| 232 // Expect |i| calls to GetWorkCallback() in which it returned a Sequence and | 235 // Expect |i| calls to GetWork() in which it returned a Sequence and |
| 233 // |i| calls in which it returned nullptr. | 236 // |i| calls in which it returned nullptr. |
| 234 const size_t expected_num_get_work_callback = 2 * (i + 1); | 237 const size_t expected_num_get_work_callback = 2 * (i + 1); |
| 235 WaitForNumGetWorkCallback(expected_num_get_work_callback); | 238 WaitForNumGetWork(expected_num_get_work_callback); |
| 236 EXPECT_EQ(expected_num_get_work_callback, NumGetWorkCallback()); | 239 EXPECT_EQ(expected_num_get_work_callback, NumGetWork()); |
| 237 } | 240 } |
| 238 } | 241 } |
| 239 | 242 |
| 240 } // namespace | 243 } // namespace |
| 241 } // namespace internal | 244 } // namespace internal |
| 242 } // namespace base | 245 } // namespace base |
| OLD | NEW |