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

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

Issue 2713513003: Use the saved task runner if TestTaskScheduler is re-entered from a task executed by RunTask. (Closed)
Patch Set: Unit test 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
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 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
63 const SequenceToken& sequence_token); 63 const SequenceToken& sequence_token);
64 64
65 // Runs |task| with |sequence_token| using this TaskScheduler's TaskTracker. 65 // Runs |task| with |sequence_token| using this TaskScheduler's TaskTracker.
66 void RunTask(std::unique_ptr<internal::Task> task, 66 void RunTask(std::unique_ptr<internal::Task> task,
67 const SequenceToken& sequence_token); 67 const SequenceToken& sequence_token);
68 68
69 // Returns true if this TaskScheduler runs its tasks on the current thread. 69 // Returns true if this TaskScheduler runs its tasks on the current thread.
70 bool RunsTasksOnCurrentThread() const; 70 bool RunsTasksOnCurrentThread() const;
71 71
72 private: 72 private:
73 // Returns |saved_task_runner_| if called from inside RunTask.
fdoray 2017/02/22 16:23:52 Returns the TaskRunner to which this TaskScheduler
Joe Mason 2017/02/22 20:23:30 Done.
74 scoped_refptr<SingleThreadTaskRunner> CurrentTaskRunner() const {
fdoray 2017/02/22 16:23:52 s/CurrentTaskRunner/MessageLoopTaskRunner/ "Curre
Joe Mason 2017/02/22 20:23:30 Done.
75 if (saved_task_runner_)
76 return saved_task_runner_;
77 DCHECK(message_loop_->task_runner());
78 return message_loop_->task_runner();
79 }
80
73 // |message_loop_owned_| will be non-null if this TestTaskScheduler owns the 81 // |message_loop_owned_| will be non-null if this TestTaskScheduler owns the
74 // MessageLoop (wasn't provided an external one at construction). 82 // MessageLoop (wasn't provided an external one at construction).
75 // |message_loop_| will always be set and is used by this TestTaskScheduler to 83 // |message_loop_| will always be set and is used by this TestTaskScheduler to
76 // run tasks. 84 // run tasks.
77 std::unique_ptr<MessageLoop> message_loop_owned_; 85 std::unique_ptr<MessageLoop> message_loop_owned_;
78 MessageLoop* message_loop_; 86 MessageLoop* message_loop_;
79 87
88 // A copy of |message_loop_|'s task_runner that is saved on entry to RunTask.
fdoray 2017/02/22 16:23:51 A reference to |message_loop_->task_runner()| save
Joe Mason 2017/02/22 20:23:30 Done.
89 // It will be used if the task posted by RunTask calls any TestTaskScheduler
90 // methods.
91 scoped_refptr<SingleThreadTaskRunner> saved_task_runner_;
92
80 // Handles shutdown behaviors and sets up the environment to run a task. 93 // Handles shutdown behaviors and sets up the environment to run a task.
81 internal::TaskTracker task_tracker_; 94 internal::TaskTracker task_tracker_;
82 95
83 DISALLOW_COPY_AND_ASSIGN(TestTaskScheduler); 96 DISALLOW_COPY_AND_ASSIGN(TestTaskScheduler);
84 }; 97 };
85 98
86 class TestTaskSchedulerTaskRunner : public SingleThreadTaskRunner { 99 class TestTaskSchedulerTaskRunner : public SingleThreadTaskRunner {
87 public: 100 public:
88 TestTaskSchedulerTaskRunner(TestTaskScheduler* task_scheduler, 101 TestTaskSchedulerTaskRunner(TestTaskScheduler* task_scheduler,
89 ExecutionMode execution_mode, 102 ExecutionMode execution_mode,
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
175 void TestTaskScheduler::JoinForTesting() { 188 void TestTaskScheduler::JoinForTesting() {
176 // TestTaskScheduler doesn't create threads so this does nothing. 189 // TestTaskScheduler doesn't create threads so this does nothing.
177 } 190 }
178 191
179 bool TestTaskScheduler::PostTask(std::unique_ptr<internal::Task> task, 192 bool TestTaskScheduler::PostTask(std::unique_ptr<internal::Task> task,
180 const SequenceToken& sequence_token) { 193 const SequenceToken& sequence_token) {
181 DCHECK(task); 194 DCHECK(task);
182 if (!task_tracker_.WillPostTask(task.get())) 195 if (!task_tracker_.WillPostTask(task.get()))
183 return false; 196 return false;
184 internal::Task* const task_ptr = task.get(); 197 internal::Task* const task_ptr = task.get();
185 return message_loop_->task_runner()->PostDelayedTask( 198 return CurrentTaskRunner()->PostDelayedTask(
186 task_ptr->posted_from, Bind(&TestTaskScheduler::RunTask, Unretained(this), 199 task_ptr->posted_from, Bind(&TestTaskScheduler::RunTask, Unretained(this),
187 Passed(&task), sequence_token), 200 Passed(&task), sequence_token),
188 task_ptr->delay); 201 task_ptr->delay);
189 } 202 }
190 203
191 void TestTaskScheduler::RunTask(std::unique_ptr<internal::Task> task, 204 void TestTaskScheduler::RunTask(std::unique_ptr<internal::Task> task,
192 const SequenceToken& sequence_token) { 205 const SequenceToken& sequence_token) {
193 // Clear the MessageLoop TaskRunner to allow TaskTracker to register its own 206 // Clear the MessageLoop TaskRunner to allow TaskTracker to register its own
194 // Thread/SequencedTaskRunnerHandle as appropriate. 207 // Thread/SequencedTaskRunnerHandle as appropriate.
195 scoped_refptr<SingleThreadTaskRunner> saved_task_runner = 208 DCHECK(!saved_task_runner_);
fdoray 2017/02/22 16:23:51 Move these 2 lines before the comment. Add separat
Joe Mason 2017/02/22 20:23:30 Done. I don't think it needs a comment because as
196 MessageLoop::current()->task_runner(); 209 saved_task_runner_ = MessageLoop::current()->task_runner();
197 MessageLoop::current()->ClearTaskRunnerForTesting(); 210 MessageLoop::current()->ClearTaskRunnerForTesting();
198 211
199 // Run the task. 212 // Run the task.
200 task_tracker_.RunTask(std::move(task), sequence_token.IsValid() 213 task_tracker_.RunTask(std::move(task), sequence_token.IsValid()
201 ? sequence_token 214 ? sequence_token
202 : SequenceToken::Create()); 215 : SequenceToken::Create());
203 216
204 // Restore the MessageLoop TaskRunner. 217 // Restore the MessageLoop TaskRunner.
fdoray 2017/02/22 16:23:52 DCHECK(!MessageLoop::current()->task_runner());
Joe Mason 2017/02/22 20:23:30 Done.
205 MessageLoop::current()->SetTaskRunner(saved_task_runner); 218 MessageLoop::current()->SetTaskRunner(saved_task_runner_);
219 saved_task_runner_ = nullptr;
206 } 220 }
207 221
208 bool TestTaskScheduler::RunsTasksOnCurrentThread() const { 222 bool TestTaskScheduler::RunsTasksOnCurrentThread() const {
209 return message_loop_->task_runner()->RunsTasksOnCurrentThread(); 223 return CurrentTaskRunner()->RunsTasksOnCurrentThread();
210 } 224 }
211 225
212 TestTaskSchedulerTaskRunner::TestTaskSchedulerTaskRunner( 226 TestTaskSchedulerTaskRunner::TestTaskSchedulerTaskRunner(
213 TestTaskScheduler* task_scheduler, 227 TestTaskScheduler* task_scheduler,
214 ExecutionMode execution_mode, 228 ExecutionMode execution_mode,
215 TaskTraits traits) 229 TaskTraits traits)
216 : task_scheduler_(task_scheduler), 230 : task_scheduler_(task_scheduler),
217 execution_mode_(execution_mode), 231 execution_mode_(execution_mode),
218 sequence_token_(execution_mode == ExecutionMode::PARALLEL 232 sequence_token_(execution_mode == ExecutionMode::PARALLEL
219 ? SequenceToken() 233 ? SequenceToken()
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
263 DCHECK_EQ(task_scheduler_, TaskScheduler::GetInstance()); 277 DCHECK_EQ(task_scheduler_, TaskScheduler::GetInstance());
264 278
265 // Per contract, call JoinForTesting() before deleting the TaskScheduler. 279 // Per contract, call JoinForTesting() before deleting the TaskScheduler.
266 TaskScheduler::GetInstance()->JoinForTesting(); 280 TaskScheduler::GetInstance()->JoinForTesting();
267 281
268 TaskScheduler::SetInstance(nullptr); 282 TaskScheduler::SetInstance(nullptr);
269 } 283 }
270 284
271 } // namespace test 285 } // namespace test
272 } // namespace base 286 } // namespace base
OLDNEW
« no previous file with comments | « no previous file | base/test/scoped_task_scheduler_unittest.cc » ('j') | base/test/scoped_task_scheduler_unittest.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698