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 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
70 return num_run_tasks_; | 70 return num_run_tasks_; |
71 } | 71 } |
72 | 72 |
73 std::vector<scoped_refptr<Sequence>> CreatedSequences() { | 73 std::vector<scoped_refptr<Sequence>> CreatedSequences() { |
74 AutoSchedulerLock auto_lock(lock_); | 74 AutoSchedulerLock auto_lock(lock_); |
75 return created_sequences_; | 75 return created_sequences_; |
76 } | 76 } |
77 | 77 |
78 std::vector<scoped_refptr<Sequence>> EnqueuedSequences() { | 78 std::vector<scoped_refptr<Sequence>> EnqueuedSequences() { |
79 AutoSchedulerLock auto_lock(lock_); | 79 AutoSchedulerLock auto_lock(lock_); |
80 return enqueued_sequences_; | 80 return re_enqueued_sequences_; |
81 } | 81 } |
82 | 82 |
83 std::unique_ptr<SchedulerWorkerThread> worker_thread_; | 83 std::unique_ptr<SchedulerWorkerThread> worker_thread_; |
84 | 84 |
85 private: | 85 private: |
86 // SchedulerWorkerThread::Delegate: | 86 // SchedulerWorkerThread::Delegate: |
87 void OnMainEntry() override { | 87 void OnMainEntry() override { |
88 // Without this |auto_lock|, OnMainEntry() could be called twice without | 88 // Without this |auto_lock|, OnMainEntry() could be called twice without |
89 // generating an error. | 89 // generating an error. |
90 AutoSchedulerLock auto_lock(lock_); | 90 AutoSchedulerLock auto_lock(lock_); |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
126 { | 126 { |
127 // Add the Sequence to the vector of created Sequences. | 127 // Add the Sequence to the vector of created Sequences. |
128 AutoSchedulerLock auto_lock(lock_); | 128 AutoSchedulerLock auto_lock(lock_); |
129 created_sequences_.push_back(sequence); | 129 created_sequences_.push_back(sequence); |
130 } | 130 } |
131 | 131 |
132 return sequence; | 132 return sequence; |
133 } | 133 } |
134 | 134 |
135 // This override verifies that |sequence| contains the expected number of | 135 // This override verifies that |sequence| contains the expected number of |
136 // Tasks and adds it to |enqueued_sequences_|. Unlike a normal EnqueueSequence | 136 // Tasks and adds it to |re_re_enqueued_sequences_|. Unlike a normal |
gab
2016/04/25 19:28:07
s/re_re_/re_/ :-)
fdoray
2016/04/25 19:35:09
Done.
| |
137 // ReEnqueueSequence | |
137 // implementation, it doesn't reinsert |sequence| into a queue for further | 138 // implementation, it doesn't reinsert |sequence| into a queue for further |
138 // execution. | 139 // execution. |
139 void EnqueueSequence(scoped_refptr<Sequence> sequence) override { | 140 void ReEnqueueSequence(scoped_refptr<Sequence> sequence) override { |
140 EXPECT_GT(TasksPerSequence(), 1U); | 141 EXPECT_GT(TasksPerSequence(), 1U); |
141 | 142 |
142 // Verify that |sequence| contains TasksPerSequence() - 1 Tasks. | 143 // Verify that |sequence| contains TasksPerSequence() - 1 Tasks. |
143 for (size_t i = 0; i < TasksPerSequence() - 1; ++i) { | 144 for (size_t i = 0; i < TasksPerSequence() - 1; ++i) { |
144 EXPECT_TRUE(sequence->PeekTask()); | 145 EXPECT_TRUE(sequence->PeekTask()); |
145 sequence->PopTask(); | 146 sequence->PopTask(); |
146 } | 147 } |
147 EXPECT_FALSE(sequence->PeekTask()); | 148 EXPECT_FALSE(sequence->PeekTask()); |
148 | 149 |
149 // Add |sequence| to |enqueued_sequences_|. | 150 // Add |sequence| to |re_enqueued_sequences_|. |
150 AutoSchedulerLock auto_lock(lock_); | 151 AutoSchedulerLock auto_lock(lock_); |
151 enqueued_sequences_.push_back(std::move(sequence)); | 152 re_enqueued_sequences_.push_back(std::move(sequence)); |
152 EXPECT_LE(enqueued_sequences_.size(), created_sequences_.size()); | 153 EXPECT_LE(re_enqueued_sequences_.size(), created_sequences_.size()); |
153 } | 154 } |
154 | 155 |
155 void RunTaskCallback() { | 156 void RunTaskCallback() { |
156 AutoSchedulerLock auto_lock(lock_); | 157 AutoSchedulerLock auto_lock(lock_); |
157 ++num_run_tasks_; | 158 ++num_run_tasks_; |
158 EXPECT_LE(num_run_tasks_, created_sequences_.size()); | 159 EXPECT_LE(num_run_tasks_, created_sequences_.size()); |
159 } | 160 } |
160 | 161 |
161 TaskTracker task_tracker_; | 162 TaskTracker task_tracker_; |
162 | 163 |
(...skipping 13 matching lines...) Expand all Loading... | |
176 // Maximum number of times that GetWork() can be called. | 177 // Maximum number of times that GetWork() can be called. |
177 size_t max_get_work_ = 0; | 178 size_t max_get_work_ = 0; |
178 | 179 |
179 // Condition variable signaled when |num_get_work_| is incremented. | 180 // Condition variable signaled when |num_get_work_| is incremented. |
180 std::unique_ptr<ConditionVariable> num_get_work_cv_; | 181 std::unique_ptr<ConditionVariable> num_get_work_cv_; |
181 | 182 |
182 // Sequences created by GetWork(). | 183 // Sequences created by GetWork(). |
183 std::vector<scoped_refptr<Sequence>> created_sequences_; | 184 std::vector<scoped_refptr<Sequence>> created_sequences_; |
184 | 185 |
185 // Sequences passed to EnqueueSequence(). | 186 // Sequences passed to EnqueueSequence(). |
186 std::vector<scoped_refptr<Sequence>> enqueued_sequences_; | 187 std::vector<scoped_refptr<Sequence>> re_enqueued_sequences_; |
187 | 188 |
188 // Number of times that RunTaskCallback() has been called. | 189 // Number of times that RunTaskCallback() has been called. |
189 size_t num_run_tasks_ = 0; | 190 size_t num_run_tasks_ = 0; |
190 | 191 |
191 DISALLOW_COPY_AND_ASSIGN(TaskSchedulerWorkerThreadTest); | 192 DISALLOW_COPY_AND_ASSIGN(TaskSchedulerWorkerThreadTest); |
192 }; | 193 }; |
193 | 194 |
194 // Verify that when GetWork() continuously returns Sequences, all Tasks in these | 195 // Verify that when GetWork() continuously returns Sequences, all Tasks in these |
195 // Sequences run successfully. The test wakes up the SchedulerWorkerThread once. | 196 // Sequences run successfully. The test wakes up the SchedulerWorkerThread once. |
196 TEST_P(TaskSchedulerWorkerThreadTest, ContinuousWork) { | 197 TEST_P(TaskSchedulerWorkerThreadTest, ContinuousWork) { |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
255 INSTANTIATE_TEST_CASE_P(OneTaskPerSequence, | 256 INSTANTIATE_TEST_CASE_P(OneTaskPerSequence, |
256 TaskSchedulerWorkerThreadTest, | 257 TaskSchedulerWorkerThreadTest, |
257 ::testing::Values(1)); | 258 ::testing::Values(1)); |
258 INSTANTIATE_TEST_CASE_P(TwoTasksPerSequence, | 259 INSTANTIATE_TEST_CASE_P(TwoTasksPerSequence, |
259 TaskSchedulerWorkerThreadTest, | 260 TaskSchedulerWorkerThreadTest, |
260 ::testing::Values(2)); | 261 ::testing::Values(2)); |
261 | 262 |
262 } // namespace | 263 } // namespace |
263 } // namespace internal | 264 } // namespace internal |
264 } // namespace base | 265 } // namespace base |
OLD | NEW |