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

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

Issue 2876583002: Do not flush main thread tasks in ~ScopedTaskEnvironment. (Closed)
Patch Set: CR-gab-5-add-comment Created 3 years, 7 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
« no previous file with comments | « base/test/scoped_task_environment.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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(); 58 // Intentionally do not RunLoop().RunUntilIdle() here as this simulates the
59 59 // replaced base::~MessageLoop() behaviour better.
60 DCHECK_EQ(TaskScheduler::GetInstance(), task_scheduler_); 60 DCHECK_EQ(TaskScheduler::GetInstance(), task_scheduler_);
61 // Without FlushForTesting(), DeleteSoon() and ReleaseSoon() tasks could be 61 // Without FlushForTesting(), DeleteSoon() and ReleaseSoon() tasks could be
62 // skipped, resulting in memory leaks. 62 // skipped, resulting in memory leaks.
63 TaskScheduler::GetInstance()->FlushForTesting(); 63 TaskScheduler::GetInstance()->FlushForTesting();
64 TaskScheduler::GetInstance()->Shutdown(); 64 TaskScheduler::GetInstance()->Shutdown();
65 TaskScheduler::GetInstance()->JoinForTesting(); 65 TaskScheduler::GetInstance()->JoinForTesting();
66 TaskScheduler::SetInstance(nullptr); 66 TaskScheduler::SetInstance(nullptr);
67 } 67 }
68 68
69 void ScopedTaskEnvironment::RunUntilIdle() { 69 void ScopedTaskEnvironment::RunUntilIdle() {
70 for (;;) { 70 for (;;) {
71 TaskScheduler::GetInstance()->FlushForTesting(); 71 TaskScheduler::GetInstance()->FlushForTesting();
72 72
73 TaskObserver task_observer; 73 TaskObserver task_observer;
74 MessageLoop::current()->AddTaskObserver(&task_observer); 74 MessageLoop::current()->AddTaskObserver(&task_observer);
75 RunLoop().RunUntilIdle(); 75 RunLoop().RunUntilIdle();
76 MessageLoop::current()->RemoveTaskObserver(&task_observer); 76 MessageLoop::current()->RemoveTaskObserver(&task_observer);
77 77
78 if (!task_observer.ran_task()) 78 if (!task_observer.ran_task())
79 return; 79 return;
80 } 80 }
81 } 81 }
82 82
83 } // namespace test 83 } // namespace test
84 } // namespace base 84 } // namespace base
OLDNEW
« no previous file with comments | « base/test/scoped_task_environment.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698