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

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

Issue 2868093002: Add base::test::ScopedTaskEnvironment::RunUntilIdle(). (Closed)
Patch Set: 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
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"
11 11
12 namespace base { 12 namespace base {
13 namespace test { 13 namespace test {
14 14
15 namespace {
16
17 class TaskObserver : public MessageLoop::TaskObserver {
18 public:
19 TaskObserver() = default;
20
21 // MessageLoop::TaskObserver:
22 void WillProcessTask(const PendingTask& pending_task) override {}
23 void DidProcessTask(const PendingTask& pending_task) override {
24 ran_task_ = true;
robliao 2017/05/09 21:52:52 It would be more accurate to call this called_did_
25 }
26
27 bool ran_task() const { return ran_task_; }
28
29 private:
30 bool ran_task_ = false;
31 DISALLOW_COPY_AND_ASSIGN(TaskObserver);
32 };
33
34 } // namespace
35
15 ScopedTaskEnvironment::ScopedTaskEnvironment(MainThreadType main_thread_type) 36 ScopedTaskEnvironment::ScopedTaskEnvironment(MainThreadType main_thread_type)
16 : message_loop_(main_thread_type == MainThreadType::DEFAULT 37 : message_loop_(main_thread_type == MainThreadType::DEFAULT
17 ? MessageLoop::TYPE_DEFAULT 38 ? MessageLoop::TYPE_DEFAULT
18 : (main_thread_type == MainThreadType::UI 39 : (main_thread_type == MainThreadType::UI
19 ? MessageLoop::TYPE_UI 40 ? MessageLoop::TYPE_UI
20 : MessageLoop::TYPE_IO)) { 41 : MessageLoop::TYPE_IO)) {
21 DCHECK(!TaskScheduler::GetInstance()); 42 DCHECK(!TaskScheduler::GetInstance());
22 43
23 // Instantiate a TaskScheduler with 1 thread in each of its 4 pools. Threads 44 // Instantiate a TaskScheduler with 1 thread in each of its 4 pools. Threads
24 // stay alive even when they don't have work. 45 // stay alive even when they don't have work.
(...skipping 13 matching lines...) Expand all
38 59
39 DCHECK_EQ(TaskScheduler::GetInstance(), task_scheduler_); 60 DCHECK_EQ(TaskScheduler::GetInstance(), task_scheduler_);
40 // Without FlushForTesting(), DeleteSoon() and ReleaseSoon() tasks could be 61 // Without FlushForTesting(), DeleteSoon() and ReleaseSoon() tasks could be
41 // skipped, resulting in memory leaks. 62 // skipped, resulting in memory leaks.
42 TaskScheduler::GetInstance()->FlushForTesting(); 63 TaskScheduler::GetInstance()->FlushForTesting();
43 TaskScheduler::GetInstance()->Shutdown(); 64 TaskScheduler::GetInstance()->Shutdown();
44 TaskScheduler::GetInstance()->JoinForTesting(); 65 TaskScheduler::GetInstance()->JoinForTesting();
45 TaskScheduler::SetInstance(nullptr); 66 TaskScheduler::SetInstance(nullptr);
46 } 67 }
47 68
69 void ScopedTaskEnvironment::RunUntilIdle() {
70 for (;;) {
71 TaskScheduler::GetInstance()->FlushForTesting();
72
73 TaskObserver task_observer;
74 MessageLoop::current()->AddTaskObserver(&task_observer);
75 RunLoop().RunUntilIdle();
76 MessageLoop::current()->RemoveTaskObserver(&task_observer);
77
78 if (!task_observer.ran_task())
79 return;
80 }
81 }
82
48 } // namespace test 83 } // namespace test
49 } // namespace base 84 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698