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

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

Issue 2891363005: Add ScopedTaskEnvironment::ExecutionControlMode. (Closed)
Patch Set: self-review 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 #ifndef BASE_TEST_SCOPED_TASK_ENVIRONMENT_H_ 5 #ifndef BASE_TEST_SCOPED_TASK_ENVIRONMENT_H_
6 #define BASE_TEST_SCOPED_TASK_ENVIRONMENT_H_ 6 #define BASE_TEST_SCOPED_TASK_ENVIRONMENT_H_
7 7
8 #include "base/macros.h" 8 #include "base/macros.h"
9 #include "base/message_loop/message_loop.h" 9 #include "base/message_loop/message_loop.h"
10 #include "base/single_thread_task_runner.h" 10 #include "base/single_thread_task_runner.h"
11 11
12 namespace base { 12 namespace base {
13 13
14 class TaskScheduler; 14 class TaskScheduler;
15 15
16 namespace test { 16 namespace test {
17 17
18 // ScopedTaskEnvironment allows usage of these APIs within its scope: 18 // ScopedTaskEnvironment allows usage of these APIs within its scope:
19 // - (Thread|Sequenced)TaskRunnerHandle, on the thread where it lives 19 // - (Thread|Sequenced)TaskRunnerHandle, on the thread where it lives
20 // - base/task_scheduler/post_task.h, on any thread 20 // - base/task_scheduler/post_task.h, on any thread
21 // 21 //
22 // Tests that need either of these APIs should instantiate a 22 // Tests that need either of these APIs should instantiate a
23 // ScopedTaskEnvironment. 23 // ScopedTaskEnvironment.
24 // 24 //
25 // Tasks posted to the (Thread|Sequenced)TaskRunnerHandle run synchronously when 25 // Tasks posted to the (Thread|Sequenced)TaskRunnerHandle run synchronously when
26 // RunLoop::Run(UntilIdle) or ScopedTaskEnvironment::RunUntilIdle is called on 26 // RunLoop::Run(UntilIdle) or ScopedTaskEnvironment::RunUntilIdle is called on
27 // the thread where the ScopedTaskEnvironment lives. 27 // the thread where the ScopedTaskEnvironment lives.
28 // 28 //
29 // Tasks posted through base/task_scheduler/post_task.h run on dedicated threads 29 // Tasks posted through base/task_scheduler/post_task.h run on dedicated
30 // threads. If ExecutionControlMode is QUEUED, they run when RunUntilIdle() or
31 // ~ScopedTaskEnvironment is called. If ExecutionControlMode is ASYNC, they run
30 // as they are posted. 32 // as they are posted.
31 // 33 //
32 // All methods of ScopedTaskEnvironment must be called from the same thread. 34 // All methods of ScopedTaskEnvironment must be called from the same thread.
33 // 35 //
34 // Usage: 36 // Usage:
35 // 37 //
36 // class MyTestFixture : public testing::Test { 38 // class MyTestFixture : public testing::Test {
37 // public: 39 // public:
38 // (...) 40 // (...)
39 // 41 //
(...skipping 14 matching lines...) Expand all
54 public: 56 public:
55 enum class MainThreadType { 57 enum class MainThreadType {
56 // The main thread doesn't pump system messages. 58 // The main thread doesn't pump system messages.
57 DEFAULT, 59 DEFAULT,
58 // The main thread pumps UI messages. 60 // The main thread pumps UI messages.
59 UI, 61 UI,
60 // The main thread pumps asynchronous IO messages. 62 // The main thread pumps asynchronous IO messages.
61 IO, 63 IO,
62 }; 64 };
63 65
66 enum class ExecutionControlMode {
gab 2017/05/23 15:26:13 I'd say "ExecutionMode" is enough (especially beca
fdoray 2017/05/25 19:17:34 Done.
67 // Tasks are queued and executed when RunUntilIdle() is called.
gab 2017/05/23 15:26:13 only executed ^^^^ is explicitly called ^^^^^^
fdoray 2017/05/25 19:17:34 Done.
68 QUEUED,
69 // Tasks run as they are posted.
gab 2017/05/23 15:26:13 + "RunUntilIdle() can still be used to block until
fdoray 2017/05/25 19:17:33 Done.
70 ASYNC,
71 };
72
64 ScopedTaskEnvironment( 73 ScopedTaskEnvironment(
65 MainThreadType main_thread_type = MainThreadType::DEFAULT); 74 MainThreadType main_thread_type = MainThreadType::DEFAULT,
75 ExecutionControlMode execution_control_mode =
76 ExecutionControlMode::ASYNC);
66 77
67 // Waits until no undelayed TaskScheduler tasks remain. Then, unregisters the 78 // Waits until no undelayed TaskScheduler tasks remain. Then, unregisters the
68 // TaskScheduler and the (Thread|Sequenced)TaskRunnerHandle. 79 // TaskScheduler and the (Thread|Sequenced)TaskRunnerHandle.
69 ~ScopedTaskEnvironment(); 80 ~ScopedTaskEnvironment();
70 81
71 // Returns a TaskRunner that schedules tasks on the main thread. 82 // Returns a TaskRunner that schedules tasks on the main thread.
72 scoped_refptr<base::SingleThreadTaskRunner> GetMainThreadTaskRunner(); 83 scoped_refptr<base::SingleThreadTaskRunner> GetMainThreadTaskRunner();
73 84
74 // Synchronously runs (Thread|Sequenced)TaskRunnerHandle tasks until no 85 // Runs tasks until both the (Thread|Sequenced)TaskRunnerHandle and the
75 // undelayed (Thread|Sequenced)TaskRunnerHandle or TaskScheduler tasks remain. 86 // TaskScheduler queues are empty.
76 void RunUntilIdle(); 87 void RunUntilIdle();
77 88
78 private: 89 private:
90 class TestTaskTracker;
91
92 const ExecutionControlMode execution_control_mode_;
93
79 // Note: |message_loop_| is an implementation detail and will be replaced in 94 // Note: |message_loop_| is an implementation detail and will be replaced in
80 // the future, do NOT rely on the presence of a MessageLoop beyond 95 // the future, do NOT rely on the presence of a MessageLoop beyond
81 // (Thread|Sequenced)TaskRunnerHandle and RunLoop. 96 // (Thread|Sequenced)TaskRunnerHandle and RunLoop.
82 MessageLoop message_loop_; 97 MessageLoop message_loop_;
83 98
84 const TaskScheduler* task_scheduler_ = nullptr; 99 const TaskScheduler* task_scheduler_ = nullptr;
85 100
101 // Owned by |task_scheduler_|.
102 TestTaskTracker* const task_tracker_;
103
86 DISALLOW_COPY_AND_ASSIGN(ScopedTaskEnvironment); 104 DISALLOW_COPY_AND_ASSIGN(ScopedTaskEnvironment);
87 }; 105 };
88 106
89 } // namespace test 107 } // namespace test
90 } // namespace base 108 } // namespace base
91 109
92 #endif // BASE_TEST_SCOPED_ASYNC_TASK_SCHEDULER_H_ 110 #endif // BASE_TEST_SCOPED_ASYNC_TASK_SCHEDULER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698