Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(101)

Side by Side Diff: base/test/scoped_task_scheduler.cc

Issue 2684273002: Allow ScopedTaskScheduler to be created before a class that overrides MessageLoop::current()'s task… (Closed)
Patch Set: Get rid of extra files in patch Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | base/test/scoped_task_scheduler_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/test/scoped_task_scheduler.h" 5 #include "base/test/scoped_task_scheduler.h"
6 6
7 #include <memory> 7 #include <memory>
8 #include <utility> 8 #include <utility>
9 #include <vector> 9 #include <vector>
10 10
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
70 bool RunsTasksOnCurrentThread() const; 70 bool RunsTasksOnCurrentThread() const;
71 71
72 private: 72 private:
73 // |message_loop_owned_| will be non-null if this TestTaskScheduler owns the 73 // |message_loop_owned_| will be non-null if this TestTaskScheduler owns the
74 // MessageLoop (wasn't provided an external one at construction). 74 // MessageLoop (wasn't provided an external one at construction).
75 // |message_loop_| will always be set and is used by this TestTaskScheduler to 75 // |message_loop_| will always be set and is used by this TestTaskScheduler to
76 // run tasks. 76 // run tasks.
77 std::unique_ptr<MessageLoop> message_loop_owned_; 77 std::unique_ptr<MessageLoop> message_loop_owned_;
78 MessageLoop* message_loop_; 78 MessageLoop* message_loop_;
79 79
80 // The SingleThreadTaskRunner associated with |message_loop_|.
81 const scoped_refptr<SingleThreadTaskRunner> message_loop_task_runner_ =
82 message_loop_->task_runner();
83
84 // Handles shutdown behaviors and sets up the environment to run a task. 80 // Handles shutdown behaviors and sets up the environment to run a task.
85 internal::TaskTracker task_tracker_; 81 internal::TaskTracker task_tracker_;
86 82
87 DISALLOW_COPY_AND_ASSIGN(TestTaskScheduler); 83 DISALLOW_COPY_AND_ASSIGN(TestTaskScheduler);
88 }; 84 };
89 85
90 class TestTaskSchedulerTaskRunner : public SingleThreadTaskRunner { 86 class TestTaskSchedulerTaskRunner : public SingleThreadTaskRunner {
91 public: 87 public:
92 TestTaskSchedulerTaskRunner(TestTaskScheduler* task_scheduler, 88 TestTaskSchedulerTaskRunner(TestTaskScheduler* task_scheduler,
93 ExecutionMode execution_mode, 89 ExecutionMode execution_mode,
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
179 void TestTaskScheduler::JoinForTesting() { 175 void TestTaskScheduler::JoinForTesting() {
180 // TestTaskScheduler doesn't create threads so this does nothing. 176 // TestTaskScheduler doesn't create threads so this does nothing.
181 } 177 }
182 178
183 bool TestTaskScheduler::PostTask(std::unique_ptr<internal::Task> task, 179 bool TestTaskScheduler::PostTask(std::unique_ptr<internal::Task> task,
184 const SequenceToken& sequence_token) { 180 const SequenceToken& sequence_token) {
185 DCHECK(task); 181 DCHECK(task);
186 if (!task_tracker_.WillPostTask(task.get())) 182 if (!task_tracker_.WillPostTask(task.get()))
187 return false; 183 return false;
188 internal::Task* const task_ptr = task.get(); 184 internal::Task* const task_ptr = task.get();
189 return message_loop_task_runner_->PostDelayedTask( 185 return message_loop_->task_runner()->PostDelayedTask(
190 task_ptr->posted_from, Bind(&TestTaskScheduler::RunTask, Unretained(this), 186 task_ptr->posted_from, Bind(&TestTaskScheduler::RunTask, Unretained(this),
191 Passed(&task), sequence_token), 187 Passed(&task), sequence_token),
192 task_ptr->delay); 188 task_ptr->delay);
193 } 189 }
194 190
195 void TestTaskScheduler::RunTask(std::unique_ptr<internal::Task> task, 191 void TestTaskScheduler::RunTask(std::unique_ptr<internal::Task> task,
196 const SequenceToken& sequence_token) { 192 const SequenceToken& sequence_token) {
197 // Clear the MessageLoop TaskRunner to allow TaskTracker to register its own 193 // Clear the MessageLoop TaskRunner to allow TaskTracker to register its own
198 // Thread/SequencedTaskRunnerHandle as appropriate. 194 // Thread/SequencedTaskRunnerHandle as appropriate.
195 scoped_refptr<SingleThreadTaskRunner> saved_task_runner =
196 MessageLoop::current()->task_runner();
199 MessageLoop::current()->ClearTaskRunnerForTesting(); 197 MessageLoop::current()->ClearTaskRunnerForTesting();
200 198
201 // Run the task. 199 // Run the task.
202 task_tracker_.RunTask(std::move(task), sequence_token.IsValid() 200 task_tracker_.RunTask(std::move(task), sequence_token.IsValid()
203 ? sequence_token 201 ? sequence_token
204 : SequenceToken::Create()); 202 : SequenceToken::Create());
205 203
206 // Restore the MessageLoop TaskRunner. 204 // Restore the MessageLoop TaskRunner.
207 MessageLoop::current()->SetTaskRunner(message_loop_task_runner_); 205 MessageLoop::current()->SetTaskRunner(saved_task_runner);
208 } 206 }
209 207
210 bool TestTaskScheduler::RunsTasksOnCurrentThread() const { 208 bool TestTaskScheduler::RunsTasksOnCurrentThread() const {
211 return message_loop_task_runner_->RunsTasksOnCurrentThread(); 209 return message_loop_->task_runner()->RunsTasksOnCurrentThread();
212 } 210 }
213 211
214 TestTaskSchedulerTaskRunner::TestTaskSchedulerTaskRunner( 212 TestTaskSchedulerTaskRunner::TestTaskSchedulerTaskRunner(
215 TestTaskScheduler* task_scheduler, 213 TestTaskScheduler* task_scheduler,
216 ExecutionMode execution_mode, 214 ExecutionMode execution_mode,
217 TaskTraits traits) 215 TaskTraits traits)
218 : task_scheduler_(task_scheduler), 216 : task_scheduler_(task_scheduler),
219 execution_mode_(execution_mode), 217 execution_mode_(execution_mode),
220 sequence_token_(execution_mode == ExecutionMode::PARALLEL 218 sequence_token_(execution_mode == ExecutionMode::PARALLEL
221 ? SequenceToken() 219 ? SequenceToken()
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
265 DCHECK_EQ(task_scheduler_, TaskScheduler::GetInstance()); 263 DCHECK_EQ(task_scheduler_, TaskScheduler::GetInstance());
266 264
267 // Per contract, call JoinForTesting() before deleting the TaskScheduler. 265 // Per contract, call JoinForTesting() before deleting the TaskScheduler.
268 TaskScheduler::GetInstance()->JoinForTesting(); 266 TaskScheduler::GetInstance()->JoinForTesting();
269 267
270 TaskScheduler::SetInstance(nullptr); 268 TaskScheduler::SetInstance(nullptr);
271 } 269 }
272 270
273 } // namespace test 271 } // namespace test
274 } // namespace base 272 } // namespace base
OLDNEW
« no previous file with comments | « no previous file | base/test/scoped_task_scheduler_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698