OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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_environment.h" | 5 #include "base/test/scoped_task_environment.h" |
6 | 6 |
7 #include "base/run_loop.h" | 7 #include "base/run_loop.h" |
8 #include "base/task_scheduler/scheduler_worker_pool_params.h" | 8 #include "base/task_scheduler/scheduler_worker_pool_params.h" |
9 #include "base/task_scheduler/task_scheduler.h" | 9 #include "base/task_scheduler/task_scheduler.h" |
10 #include "base/time/time.h" | 10 #include "base/time/time.h" |
11 | 11 |
12 namespace base { | 12 namespace base { |
13 namespace test { | 13 namespace test { |
14 | 14 |
15 ScopedTaskEnvironment::ScopedTaskEnvironment() { | 15 ScopedTaskEnvironment::ScopedTaskEnvironment(MainThreadType main_thread_type) |
| 16 : message_loop_(main_thread_type == MainThreadType::UI |
| 17 ? MessageLoop::TYPE_UI |
| 18 : MessageLoop::TYPE_IO) { |
16 DCHECK(!TaskScheduler::GetInstance()); | 19 DCHECK(!TaskScheduler::GetInstance()); |
17 | 20 |
18 // Instantiate a TaskScheduler with 1 thread in each of its 4 pools. Threads | 21 // Instantiate a TaskScheduler with 1 thread in each of its 4 pools. Threads |
19 // stay alive even when they don't have work. | 22 // stay alive even when they don't have work. |
20 constexpr int kMaxThreads = 1; | 23 constexpr int kMaxThreads = 1; |
21 const TimeDelta kSuggestedReclaimTime = TimeDelta::Max(); | 24 const TimeDelta kSuggestedReclaimTime = TimeDelta::Max(); |
22 const SchedulerWorkerPoolParams worker_pool_params( | 25 const SchedulerWorkerPoolParams worker_pool_params( |
23 SchedulerWorkerPoolParams::StandbyThreadPolicy::ONE, kMaxThreads, | 26 SchedulerWorkerPoolParams::StandbyThreadPolicy::ONE, kMaxThreads, |
24 kSuggestedReclaimTime); | 27 kSuggestedReclaimTime); |
25 TaskScheduler::CreateAndSetDefaultTaskScheduler( | 28 TaskScheduler::CreateAndSetDefaultTaskScheduler( |
26 "ScopedTaskEnvironment", {worker_pool_params, worker_pool_params, | 29 "ScopedTaskEnvironment", {worker_pool_params, worker_pool_params, |
27 worker_pool_params, worker_pool_params}); | 30 worker_pool_params, worker_pool_params}); |
28 task_scheduler_ = TaskScheduler::GetInstance(); | 31 task_scheduler_ = TaskScheduler::GetInstance(); |
29 } | 32 } |
30 | 33 |
31 ScopedTaskEnvironment::~ScopedTaskEnvironment() { | 34 ScopedTaskEnvironment::~ScopedTaskEnvironment() { |
32 RunLoop().RunUntilIdle(); | 35 RunLoop().RunUntilIdle(); |
33 | 36 |
34 DCHECK_EQ(TaskScheduler::GetInstance(), task_scheduler_); | 37 DCHECK_EQ(TaskScheduler::GetInstance(), task_scheduler_); |
35 // Without FlushForTesting(), DeleteSoon() and ReleaseSoon() tasks could be | 38 // Without FlushForTesting(), DeleteSoon() and ReleaseSoon() tasks could be |
36 // skipped, resulting in memory leaks. | 39 // skipped, resulting in memory leaks. |
37 TaskScheduler::GetInstance()->FlushForTesting(); | 40 TaskScheduler::GetInstance()->FlushForTesting(); |
38 TaskScheduler::GetInstance()->Shutdown(); | 41 TaskScheduler::GetInstance()->Shutdown(); |
39 TaskScheduler::GetInstance()->JoinForTesting(); | 42 TaskScheduler::GetInstance()->JoinForTesting(); |
40 TaskScheduler::SetInstance(nullptr); | 43 TaskScheduler::SetInstance(nullptr); |
41 } | 44 } |
42 | 45 |
43 } // namespace test | 46 } // namespace test |
44 } // namespace base | 47 } // namespace base |
OLD | NEW |