| 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/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 20 matching lines...) Expand all Loading... |
| 31 #include "base/win/scoped_com_initializer.h" | 31 #include "base/win/scoped_com_initializer.h" |
| 32 #endif // defined(OS_WIN) | 32 #endif // defined(OS_WIN) |
| 33 | 33 |
| 34 namespace base { | 34 namespace base { |
| 35 namespace test { | 35 namespace test { |
| 36 | 36 |
| 37 namespace { | 37 namespace { |
| 38 | 38 |
| 39 enum class ExecutionMode { PARALLEL, SEQUENCED, SINGLE_THREADED }; | 39 enum class ExecutionMode { PARALLEL, SEQUENCED, SINGLE_THREADED }; |
| 40 | 40 |
| 41 // ScopedTaskScheduler intentionally breaks the TaskScheduler contract of not |
| 42 // running tasks before Start(). This avoid having to call Start() with dummy |
| 43 // parameters. |
| 41 class TestTaskScheduler : public TaskScheduler { | 44 class TestTaskScheduler : public TaskScheduler { |
| 42 public: | 45 public: |
| 43 // |external_message_loop| is an externally provided MessageLoop on which to | 46 // |external_message_loop| is an externally provided MessageLoop on which to |
| 44 // run tasks. A MessageLoop will be created by TestTaskScheduler if | 47 // run tasks. A MessageLoop will be created by TestTaskScheduler if |
| 45 // |external_message_loop| is nullptr. | 48 // |external_message_loop| is nullptr. |
| 46 explicit TestTaskScheduler(MessageLoop* external_message_loop); | 49 explicit TestTaskScheduler(MessageLoop* external_message_loop); |
| 47 ~TestTaskScheduler() override; | 50 ~TestTaskScheduler() override; |
| 48 | 51 |
| 49 // TaskScheduler: | 52 // TaskScheduler: |
| 53 void Start(const TaskScheduler::InitParams& init_params) override; |
| 50 void PostDelayedTaskWithTraits(const tracked_objects::Location& from_here, | 54 void PostDelayedTaskWithTraits(const tracked_objects::Location& from_here, |
| 51 const TaskTraits& traits, | 55 const TaskTraits& traits, |
| 52 OnceClosure task, | 56 OnceClosure task, |
| 53 TimeDelta delay) override; | 57 TimeDelta delay) override; |
| 54 scoped_refptr<TaskRunner> CreateTaskRunnerWithTraits( | 58 scoped_refptr<TaskRunner> CreateTaskRunnerWithTraits( |
| 55 const TaskTraits& traits) override; | 59 const TaskTraits& traits) override; |
| 56 scoped_refptr<SequencedTaskRunner> CreateSequencedTaskRunnerWithTraits( | 60 scoped_refptr<SequencedTaskRunner> CreateSequencedTaskRunnerWithTraits( |
| 57 const TaskTraits& traits) override; | 61 const TaskTraits& traits) override; |
| 58 scoped_refptr<SingleThreadTaskRunner> CreateSingleThreadTaskRunnerWithTraits( | 62 scoped_refptr<SingleThreadTaskRunner> CreateSingleThreadTaskRunnerWithTraits( |
| 59 const TaskTraits& traits) override; | 63 const TaskTraits& traits) override; |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 157 : MakeUnique<MessageLoop>()), | 161 : MakeUnique<MessageLoop>()), |
| 158 message_loop_(message_loop_owned_ ? message_loop_owned_.get() | 162 message_loop_(message_loop_owned_ ? message_loop_owned_.get() |
| 159 : external_message_loop) {} | 163 : external_message_loop) {} |
| 160 | 164 |
| 161 TestTaskScheduler::~TestTaskScheduler() { | 165 TestTaskScheduler::~TestTaskScheduler() { |
| 162 // Shutdown if it hasn't already been done explicitly. | 166 // Shutdown if it hasn't already been done explicitly. |
| 163 if (!task_tracker_.HasShutdownStarted()) | 167 if (!task_tracker_.HasShutdownStarted()) |
| 164 Shutdown(); | 168 Shutdown(); |
| 165 } | 169 } |
| 166 | 170 |
| 171 void TestTaskScheduler::Start(const TaskScheduler::InitParams&) { |
| 172 NOTREACHED(); |
| 173 } |
| 174 |
| 167 void TestTaskScheduler::PostDelayedTaskWithTraits( | 175 void TestTaskScheduler::PostDelayedTaskWithTraits( |
| 168 const tracked_objects::Location& from_here, | 176 const tracked_objects::Location& from_here, |
| 169 const TaskTraits& traits, | 177 const TaskTraits& traits, |
| 170 OnceClosure task, | 178 OnceClosure task, |
| 171 TimeDelta delay) { | 179 TimeDelta delay) { |
| 172 CreateTaskRunnerWithTraits(traits)->PostDelayedTask(from_here, | 180 CreateTaskRunnerWithTraits(traits)->PostDelayedTask(from_here, |
| 173 std::move(task), delay); | 181 std::move(task), delay); |
| 174 } | 182 } |
| 175 | 183 |
| 176 scoped_refptr<TaskRunner> TestTaskScheduler::CreateTaskRunnerWithTraits( | 184 scoped_refptr<TaskRunner> TestTaskScheduler::CreateTaskRunnerWithTraits( |
| (...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 323 DCHECK_EQ(task_scheduler_, TaskScheduler::GetInstance()); | 331 DCHECK_EQ(task_scheduler_, TaskScheduler::GetInstance()); |
| 324 | 332 |
| 325 // Per contract, call JoinForTesting() before deleting the TaskScheduler. | 333 // Per contract, call JoinForTesting() before deleting the TaskScheduler. |
| 326 TaskScheduler::GetInstance()->JoinForTesting(); | 334 TaskScheduler::GetInstance()->JoinForTesting(); |
| 327 | 335 |
| 328 TaskScheduler::SetInstance(nullptr); | 336 TaskScheduler::SetInstance(nullptr); |
| 329 } | 337 } |
| 330 | 338 |
| 331 } // namespace test | 339 } // namespace test |
| 332 } // namespace base | 340 } // namespace base |
| OLD | NEW |