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" |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
48 const SchedulerWorkerPoolParams worker_pool_params( | 48 const SchedulerWorkerPoolParams worker_pool_params( |
49 SchedulerWorkerPoolParams::StandbyThreadPolicy::ONE, kMaxThreads, | 49 SchedulerWorkerPoolParams::StandbyThreadPolicy::ONE, kMaxThreads, |
50 kSuggestedReclaimTime); | 50 kSuggestedReclaimTime); |
51 TaskScheduler::Create("ScopedTaskEnvironment"); | 51 TaskScheduler::Create("ScopedTaskEnvironment"); |
52 task_scheduler_ = TaskScheduler::GetInstance(); | 52 task_scheduler_ = TaskScheduler::GetInstance(); |
53 TaskScheduler::GetInstance()->Start({worker_pool_params, worker_pool_params, | 53 TaskScheduler::GetInstance()->Start({worker_pool_params, worker_pool_params, |
54 worker_pool_params, worker_pool_params}); | 54 worker_pool_params, worker_pool_params}); |
55 } | 55 } |
56 | 56 |
57 ScopedTaskEnvironment::~ScopedTaskEnvironment() { | 57 ScopedTaskEnvironment::~ScopedTaskEnvironment() { |
58 RunLoop().RunUntilIdle(); | |
gab
2017/05/10 15:46:49
Replace with:
// Intentionally do not RunLoop().R
fdoray
2017/05/10 16:01:46
Done.
| |
59 | |
60 DCHECK_EQ(TaskScheduler::GetInstance(), task_scheduler_); | 58 DCHECK_EQ(TaskScheduler::GetInstance(), task_scheduler_); |
61 // Without FlushForTesting(), DeleteSoon() and ReleaseSoon() tasks could be | 59 // Without FlushForTesting(), DeleteSoon() and ReleaseSoon() tasks could be |
62 // skipped, resulting in memory leaks. | 60 // skipped, resulting in memory leaks. |
63 TaskScheduler::GetInstance()->FlushForTesting(); | 61 TaskScheduler::GetInstance()->FlushForTesting(); |
64 TaskScheduler::GetInstance()->Shutdown(); | 62 TaskScheduler::GetInstance()->Shutdown(); |
65 TaskScheduler::GetInstance()->JoinForTesting(); | 63 TaskScheduler::GetInstance()->JoinForTesting(); |
66 TaskScheduler::SetInstance(nullptr); | 64 TaskScheduler::SetInstance(nullptr); |
67 } | 65 } |
68 | 66 |
69 void ScopedTaskEnvironment::RunUntilIdle() { | 67 void ScopedTaskEnvironment::RunUntilIdle() { |
70 for (;;) { | 68 for (;;) { |
71 TaskScheduler::GetInstance()->FlushForTesting(); | 69 TaskScheduler::GetInstance()->FlushForTesting(); |
72 | 70 |
73 TaskObserver task_observer; | 71 TaskObserver task_observer; |
74 MessageLoop::current()->AddTaskObserver(&task_observer); | 72 MessageLoop::current()->AddTaskObserver(&task_observer); |
75 RunLoop().RunUntilIdle(); | 73 RunLoop().RunUntilIdle(); |
76 MessageLoop::current()->RemoveTaskObserver(&task_observer); | 74 MessageLoop::current()->RemoveTaskObserver(&task_observer); |
77 | 75 |
78 if (!task_observer.ran_task()) | 76 if (!task_observer.ran_task()) |
79 return; | 77 return; |
80 } | 78 } |
81 } | 79 } |
82 | 80 |
83 } // namespace test | 81 } // namespace test |
84 } // namespace base | 82 } // namespace base |
OLD | NEW |