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(MainThreadType main_thread_type) | 15 ScopedTaskEnvironment::ScopedTaskEnvironment(MainThreadType main_thread_type) |
16 : message_loop_(main_thread_type == MainThreadType::DEFAULT | 16 : message_loop_(main_thread_type == MainThreadType::DEFAULT |
17 ? MessageLoop::TYPE_DEFAULT | 17 ? MessageLoop::TYPE_DEFAULT |
18 : (main_thread_type == MainThreadType::UI | 18 : (main_thread_type == MainThreadType::UI |
19 ? MessageLoop::TYPE_UI | 19 ? MessageLoop::TYPE_UI |
20 : MessageLoop::TYPE_IO)) { | 20 : MessageLoop::TYPE_IO)) { |
21 DCHECK(!TaskScheduler::GetInstance()); | 21 DCHECK(!TaskScheduler::GetInstance()); |
22 | 22 |
23 // Instantiate a TaskScheduler with 1 thread in each of its 4 pools. Threads | 23 // Instantiate a TaskScheduler with 1 thread in each of its 4 pools. Threads |
24 // stay alive even when they don't have work. | 24 // stay alive even when they don't have work. |
25 constexpr int kMaxThreads = 1; | 25 constexpr int kMaxThreads = 1; |
26 const TimeDelta kSuggestedReclaimTime = TimeDelta::Max(); | 26 const TimeDelta kSuggestedReclaimTime = TimeDelta::Max(); |
27 const SchedulerWorkerPoolParams worker_pool_params( | 27 const SchedulerWorkerPoolParams worker_pool_params( |
28 SchedulerWorkerPoolParams::StandbyThreadPolicy::ONE, kMaxThreads, | 28 SchedulerWorkerPoolParams::StandbyThreadPolicy::ONE, kMaxThreads, |
29 kSuggestedReclaimTime); | 29 kSuggestedReclaimTime); |
30 TaskScheduler::CreateAndSetDefaultTaskScheduler( | 30 TaskScheduler::Create("ScopedTaskEnvironment"); |
31 "ScopedTaskEnvironment", {worker_pool_params, worker_pool_params, | |
32 worker_pool_params, worker_pool_params}); | |
33 task_scheduler_ = TaskScheduler::GetInstance(); | 31 task_scheduler_ = TaskScheduler::GetInstance(); |
| 32 TaskScheduler::GetInstance()->Start({worker_pool_params, worker_pool_params, |
| 33 worker_pool_params, worker_pool_params}); |
34 } | 34 } |
35 | 35 |
36 ScopedTaskEnvironment::~ScopedTaskEnvironment() { | 36 ScopedTaskEnvironment::~ScopedTaskEnvironment() { |
37 RunLoop().RunUntilIdle(); | 37 RunLoop().RunUntilIdle(); |
38 | 38 |
39 DCHECK_EQ(TaskScheduler::GetInstance(), task_scheduler_); | 39 DCHECK_EQ(TaskScheduler::GetInstance(), task_scheduler_); |
40 // Without FlushForTesting(), DeleteSoon() and ReleaseSoon() tasks could be | 40 // Without FlushForTesting(), DeleteSoon() and ReleaseSoon() tasks could be |
41 // skipped, resulting in memory leaks. | 41 // skipped, resulting in memory leaks. |
42 TaskScheduler::GetInstance()->FlushForTesting(); | 42 TaskScheduler::GetInstance()->FlushForTesting(); |
43 TaskScheduler::GetInstance()->Shutdown(); | 43 TaskScheduler::GetInstance()->Shutdown(); |
44 TaskScheduler::GetInstance()->JoinForTesting(); | 44 TaskScheduler::GetInstance()->JoinForTesting(); |
45 TaskScheduler::SetInstance(nullptr); | 45 TaskScheduler::SetInstance(nullptr); |
46 } | 46 } |
47 | 47 |
48 } // namespace test | 48 } // namespace test |
49 } // namespace base | 49 } // namespace base |
OLD | NEW |