| 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> |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 78 // returned by GetWork(). | 78 // returned by GetWork(). |
| 79 EXPECT_EQ(created_sequences_.size(), num_run_tasks_); | 79 EXPECT_EQ(created_sequences_.size(), num_run_tasks_); |
| 80 } | 80 } |
| 81 | 81 |
| 82 void SetNumSequencesToCreate(size_t num_sequences_to_create) { | 82 void SetNumSequencesToCreate(size_t num_sequences_to_create) { |
| 83 AutoSchedulerLock auto_lock(lock_); | 83 AutoSchedulerLock auto_lock(lock_); |
| 84 EXPECT_EQ(0U, num_sequences_to_create_); | 84 EXPECT_EQ(0U, num_sequences_to_create_); |
| 85 num_sequences_to_create_ = num_sequences_to_create; | 85 num_sequences_to_create_ = num_sequences_to_create; |
| 86 } | 86 } |
| 87 | 87 |
| 88 size_t NumGetWork() const { | 88 void SetMaxGetWork(size_t max_get_work) { |
| 89 AutoSchedulerLock auto_lock(lock_); | 89 AutoSchedulerLock auto_lock(lock_); |
| 90 return num_get_work_; | 90 max_get_work_ = max_get_work; |
| 91 } | 91 } |
| 92 | 92 |
| 93 std::unique_ptr<SchedulerWorkerThread> worker_thread_; | 93 std::unique_ptr<SchedulerWorkerThread> worker_thread_; |
| 94 | 94 |
| 95 private: | 95 private: |
| 96 // SchedulerWorkerThread::Delegate: | 96 // SchedulerWorkerThread::Delegate: |
| 97 void OnMainEntry() override { | 97 void OnMainEntry() override { |
| 98 // Without this |auto_lock|, OnMainEntry() could be called twice without | 98 // Without this |auto_lock|, OnMainEntry() could be called twice without |
| 99 // generating an error. | 99 // generating an error. |
| 100 AutoSchedulerLock auto_lock(lock_); | 100 AutoSchedulerLock auto_lock(lock_); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 112 SchedulerWorkerThread* worker_thread) override { | 112 SchedulerWorkerThread* worker_thread) override { |
| 113 EXPECT_EQ(worker_thread_.get(), worker_thread); | 113 EXPECT_EQ(worker_thread_.get(), worker_thread); |
| 114 | 114 |
| 115 { | 115 { |
| 116 AutoSchedulerLock auto_lock(lock_); | 116 AutoSchedulerLock auto_lock(lock_); |
| 117 | 117 |
| 118 // Increment the number of times that this method has been called. | 118 // Increment the number of times that this method has been called. |
| 119 ++num_get_work_; | 119 ++num_get_work_; |
| 120 num_get_work_cv_->Signal(); | 120 num_get_work_cv_->Signal(); |
| 121 | 121 |
| 122 // Verify that this method isn't called more times than expected. |
| 123 EXPECT_LE(num_get_work_, max_get_work_); |
| 124 |
| 122 // Check if a Sequence should be returned. | 125 // Check if a Sequence should be returned. |
| 123 if (num_sequences_to_create_ == 0) | 126 if (num_sequences_to_create_ == 0) |
| 124 return nullptr; | 127 return nullptr; |
| 125 --num_sequences_to_create_; | 128 --num_sequences_to_create_; |
| 126 } | 129 } |
| 127 | 130 |
| 128 // Create a Sequence that contains 1 Task. | 131 // Create a Sequence that contains one Task. |
| 129 scoped_refptr<Sequence> sequence(new Sequence); | 132 scoped_refptr<Sequence> sequence(new Sequence); |
| 130 task_tracker_.PostTask( | 133 task_tracker_.PostTask( |
| 131 Bind(IgnoreResult(&Sequence::PushTask), Unretained(sequence.get())), | 134 Bind(IgnoreResult(&Sequence::PushTask), Unretained(sequence.get())), |
| 132 WrapUnique(new Task( | 135 WrapUnique(new Task( |
| 133 FROM_HERE, Bind(&TaskSchedulerWorkerThreadTest::RunTaskCallback, | 136 FROM_HERE, Bind(&TaskSchedulerWorkerThreadTest::RunTaskCallback, |
| 134 Unretained(this)), | 137 Unretained(this)), |
| 135 TaskTraits()))); | 138 TaskTraits()))); |
| 136 | 139 |
| 137 { | 140 { |
| 138 // Add the Sequence to the vector of created Sequences. | 141 // Add the Sequence to the vector of created Sequences. |
| 139 AutoSchedulerLock auto_lock(lock_); | 142 AutoSchedulerLock auto_lock(lock_); |
| 140 created_sequences_.push_back(sequence); | 143 created_sequences_.push_back(sequence); |
| 141 } | 144 } |
| 142 | 145 |
| 143 return sequence; | 146 return sequence; |
| 144 } | 147 } |
| 145 | 148 |
| 146 void RanTaskFromSequence(scoped_refptr<Sequence> sequence) override { | 149 void RanTaskFromSequence(scoped_refptr<Sequence> sequence) override { |
| 147 AutoSchedulerLock auto_lock(lock_); | 150 AutoSchedulerLock auto_lock(lock_); |
| 148 run_sequences_.push_back(std::move(sequence)); | 151 run_sequences_.push_back(std::move(sequence)); |
| 152 EXPECT_LE(run_sequences_.size(), created_sequences_.size()); |
| 149 run_sequences_cv_->Signal(); | 153 run_sequences_cv_->Signal(); |
| 150 } | 154 } |
| 151 | 155 |
| 152 void RunTaskCallback() { | 156 void RunTaskCallback() { |
| 153 AutoSchedulerLock auto_lock(lock_); | 157 AutoSchedulerLock auto_lock(lock_); |
| 154 ++num_run_tasks_; | 158 ++num_run_tasks_; |
| 159 EXPECT_LE(num_run_tasks_, created_sequences_.size()); |
| 155 } | 160 } |
| 156 | 161 |
| 157 TaskTracker task_tracker_; | 162 TaskTracker task_tracker_; |
| 158 | 163 |
| 159 // Synchronizes access to all members below. | 164 // Synchronizes access to all members below. |
| 160 mutable SchedulerLock lock_; | 165 mutable SchedulerLock lock_; |
| 161 | 166 |
| 162 // Signaled once OnMainEntry() has been called. | 167 // Signaled once OnMainEntry() has been called. |
| 163 WaitableEvent main_entry_called_; | 168 WaitableEvent main_entry_called_; |
| 164 | 169 |
| 165 // True once OnMainExit() has been called. | 170 // True once OnMainExit() has been called. |
| 166 bool main_exit_called_ = false; | 171 bool main_exit_called_ = false; |
| 167 | 172 |
| 168 // Number of Sequences that should be created by GetWork(). When this | 173 // Number of Sequences that should be created by GetWork(). When this |
| 169 // is 0, GetWork() returns nullptr. | 174 // is 0, GetWork() returns nullptr. |
| 170 size_t num_sequences_to_create_ = 0; | 175 size_t num_sequences_to_create_ = 0; |
| 171 | 176 |
| 172 // Number of times that GetWork() has been called. | 177 // Number of times that GetWork() has been called. |
| 173 size_t num_get_work_ = 0; | 178 size_t num_get_work_ = 0; |
| 174 | 179 |
| 180 // Maximum number of times that GetWork() can be called. |
| 181 size_t max_get_work_ = 0; |
| 182 |
| 175 // Condition variable signaled when |num_get_work_| is incremented. | 183 // Condition variable signaled when |num_get_work_| is incremented. |
| 176 std::unique_ptr<ConditionVariable> num_get_work_cv_; | 184 std::unique_ptr<ConditionVariable> num_get_work_cv_; |
| 177 | 185 |
| 178 // Sequences created by GetWork(). | 186 // Sequences created by GetWork(). |
| 179 std::vector<scoped_refptr<Sequence>> created_sequences_; | 187 std::vector<scoped_refptr<Sequence>> created_sequences_; |
| 180 | 188 |
| 181 // Sequences passed to RanTaskFromSequence(). | 189 // Sequences passed to RanTaskFromSequence(). |
| 182 std::vector<scoped_refptr<Sequence>> run_sequences_; | 190 std::vector<scoped_refptr<Sequence>> run_sequences_; |
| 183 | 191 |
| 184 // Condition variable signaled when a Sequence is added to |run_sequences_|. | 192 // Condition variable signaled when a Sequence is added to |run_sequences_|. |
| 185 std::unique_ptr<ConditionVariable> run_sequences_cv_; | 193 std::unique_ptr<ConditionVariable> run_sequences_cv_; |
| 186 | 194 |
| 187 // Number of times that RunTaskCallback() has been called. | 195 // Number of times that RunTaskCallback() has been called. |
| 188 size_t num_run_tasks_ = 0; | 196 size_t num_run_tasks_ = 0; |
| 189 | 197 |
| 190 DISALLOW_COPY_AND_ASSIGN(TaskSchedulerWorkerThreadTest); | 198 DISALLOW_COPY_AND_ASSIGN(TaskSchedulerWorkerThreadTest); |
| 191 }; | 199 }; |
| 192 | 200 |
| 193 // Verify that when GetWork() continuously returns Sequences, all Tasks in these | 201 // Verify that when GetWork() continuously returns Sequences, all Tasks in these |
| 194 // Sequences run successfully. The SchedulerWorkerThread is woken up once. | 202 // Sequences run successfully. The test wakes up the SchedulerWorkerThread once. |
| 195 TEST_F(TaskSchedulerWorkerThreadTest, ContinousWork) { | 203 TEST_F(TaskSchedulerWorkerThreadTest, ContinuousWork) { |
| 196 // Set GetWork() to return |kNumSequencesPerTest| Sequences before starting to | 204 // Set GetWork() to return |kNumSequencesPerTest| Sequences before starting to |
| 197 // return nullptr. | 205 // return nullptr. |
| 198 SetNumSequencesToCreate(kNumSequencesPerTest); | 206 SetNumSequencesToCreate(kNumSequencesPerTest); |
| 199 | 207 |
| 208 // Expect |kNumSequencesPerTest| calls to GetWork() in which it returns a |
| 209 // Sequence and one call in which its returns nullptr. |
| 210 const size_t kExpectedNumGetWork = kNumSequencesPerTest + 1; |
| 211 SetMaxGetWork(kExpectedNumGetWork); |
| 212 |
| 200 // Wake up |worker_thread_| and wait until it has run all the Tasks returned | 213 // Wake up |worker_thread_| and wait until it has run all the Tasks returned |
| 201 // by GetWork(). | 214 // by GetWork(). |
| 202 worker_thread_->WakeUp(); | 215 worker_thread_->WakeUp(); |
| 203 WaitForAllSequencesToRun(); | 216 WaitForAllSequencesToRun(); |
| 204 | 217 WaitForNumGetWork(kExpectedNumGetWork); |
| 205 // Expect |kNumSequencesPerTest| calls to GetWork() in which it returned a | |
| 206 // Sequence and 1 call in which it returned nullptr. | |
| 207 const size_t expected_num_get_work = kNumSequencesPerTest + 1; | |
| 208 WaitForNumGetWork(expected_num_get_work); | |
| 209 EXPECT_EQ(expected_num_get_work, NumGetWork()); | |
| 210 } | 218 } |
| 211 | 219 |
| 212 // Verify that when GetWork() alternates between returning a Sequence and | 220 // Verify that when GetWork() alternates between returning a Sequence and |
| 213 // returning nullptr, all Tasks in the returned Sequences run successfully. The | 221 // returning nullptr, all Tasks in the returned Sequences run successfully. The |
| 214 // SchedulerWorkerThread is woken up once for each Sequence. | 222 // test wakes up the SchedulerWorkerThread once for each Sequence. |
| 215 TEST_F(TaskSchedulerWorkerThreadTest, IntermittentWork) { | 223 TEST_F(TaskSchedulerWorkerThreadTest, IntermittentWork) { |
| 216 for (size_t i = 0; i < kNumSequencesPerTest; ++i) { | 224 for (size_t i = 0; i < kNumSequencesPerTest; ++i) { |
| 217 // Set GetWork() to return 1 Sequence before starting to return | 225 // Set GetWork() to return 1 Sequence before starting to return |
| 218 // nullptr. | 226 // nullptr. |
| 219 SetNumSequencesToCreate(1); | 227 SetNumSequencesToCreate(1); |
| 220 | 228 |
| 229 // Expect |i + 1| calls to GetWork() in which it returns a Sequence and |
| 230 // |i + 1| calls in which it returns nullptr. |
| 231 const size_t expected_num_get_work = 2 * (i + 1); |
| 232 SetMaxGetWork(expected_num_get_work); |
| 233 |
| 221 // Wake up |worker_thread_| and wait until it has run the Task returned by | 234 // Wake up |worker_thread_| and wait until it has run the Task returned by |
| 222 // GetWork(). | 235 // GetWork(). |
| 223 worker_thread_->WakeUp(); | 236 worker_thread_->WakeUp(); |
| 224 WaitForAllSequencesToRun(); | 237 WaitForAllSequencesToRun(); |
| 225 | |
| 226 // Expect |i| calls to GetWork() in which it returned a Sequence and | |
| 227 // |i| calls in which it returned nullptr. | |
| 228 const size_t expected_num_get_work = 2 * (i + 1); | |
| 229 WaitForNumGetWork(expected_num_get_work); | 238 WaitForNumGetWork(expected_num_get_work); |
| 230 EXPECT_EQ(expected_num_get_work, NumGetWork()); | |
| 231 } | 239 } |
| 232 } | 240 } |
| 233 | 241 |
| 234 } // namespace | 242 } // namespace |
| 235 } // namespace internal | 243 } // namespace internal |
| 236 } // namespace base | 244 } // namespace base |
| OLD | NEW |