OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 // TestBrowserThreadBundle is a convenience class for creating a set of | 5 // TestBrowserThreadBundle is a convenience class for creating a set of |
6 // TestBrowserThreads, a blocking pool, and a task scheduler in unit tests. For | 6 // TestBrowserThreads, a blocking pool, and a task scheduler in unit tests. For |
7 // most tests, it is sufficient to just instantiate the TestBrowserThreadBundle | 7 // most tests, it is sufficient to just instantiate the TestBrowserThreadBundle |
8 // as a member variable. It is a good idea to put the TestBrowserThreadBundle as | 8 // as a member variable. It is a good idea to put the TestBrowserThreadBundle as |
9 // the first member variable in test classes, so it is destroyed last, and the | 9 // the first member variable in test classes, so it is destroyed last, and the |
10 // test threads always exist from the perspective of other classes. | 10 // test threads always exist from the perspective of other classes. |
11 // | 11 // |
12 // By default, all of the created TestBrowserThreads and the task scheduler will | 12 // By default, all of the created TestBrowserThreads will be backed by a single |
13 // be backed by a single shared MessageLoop. If a test truly needs separate | 13 // shared MessageLoop. If a test truly needs separate threads, it can do so by |
14 // threads, it can do so by passing the appropriate combination of option values | 14 // passing the appropriate combination of option values during the |
15 // during the TestBrowserThreadBundle construction. | 15 // TestBrowserThreadBundle construction. TaskScheduler and blocking pool tasks |
| 16 // always run on dedicated threads. |
16 // | 17 // |
17 // To synchronously run tasks posted to task scheduler or to TestBrowserThreads | 18 // To synchronously run tasks from the shared MessageLoop: |
18 // that use the shared MessageLoop, call RunLoop::Run/RunUntilIdle() on the | 19 // |
19 // thread where the TestBrowserThreadBundle lives. The destructor of | 20 // ... until there are no undelayed tasks in the shared MessageLoop: |
20 // TestBrowserThreadBundle runs remaining TestBrowserThreads tasks, remaining | 21 // base::RunLoop::RunUntilIdle(); |
21 // blocking pool tasks, and remaining BLOCK_SHUTDOWN task scheduler tasks. | 22 // |
| 23 // ... until there are no undelayed tasks in the shared MessageLoop, in |
| 24 // TaskScheduler or in the blocking pool (excluding tasks not posted from the |
| 25 // shared MessageLoop's thread, TaskScheduler or the blocking pool): |
| 26 // content::RunAllBlockingPoolTasksUntilIdle(); |
| 27 // |
| 28 // ... until a condition is met: |
| 29 // base::RunLoop run_loop; |
| 30 // // Runs until a task running in the shared MessageLoop calls |
| 31 // // run_loop.Quit() or runs run_loop.QuitClosure() (&run_loop or |
| 32 // // run_loop.QuitClosure() must be kept somewhere accessible by that task). |
| 33 // run_loop.Run(); |
| 34 // |
| 35 // To wait until there are no pending undelayed tasks in TaskScheduler or in the |
| 36 // blocking pool, without running tasks from the shared MessageLoop: |
| 37 // base::TaskScheduler::GetInstance()->FlushForTesting(); |
| 38 // // Note: content::BrowserThread::GetBlockingPool()->FlushForTesting() is |
| 39 // // equivalent but deprecated. |
| 40 // |
| 41 // The destructor of TestBrowserThreadBundle runs remaining TestBrowserThreads |
| 42 // tasks, remaining blocking pool tasks, and remaining BLOCK_SHUTDOWN task |
| 43 // scheduler tasks. |
22 // | 44 // |
23 // If a test needs a MessageLoopForIO on the main thread, it should use the | 45 // If a test needs a MessageLoopForIO on the main thread, it should use the |
24 // IO_MAINLOOP option. This also allows task scheduler tasks to use | 46 // IO_MAINLOOP option. Most of the time, IO_MAINLOOP avoids needing to use a |
25 // FileDescriptorWatcher. Most of the time, IO_MAINLOOP avoids needing to use a | |
26 // REAL_IO_THREAD. | 47 // REAL_IO_THREAD. |
27 // | 48 // |
28 // If a test needs a TaskScheduler that runs tasks on a dedicated thread, it | |
29 // should use REAL_TASK_SCHEDULER. Usage of this option should be justified as | |
30 // it is easier to understand and debug a single-threaded unit test. | |
31 // | |
32 // For some tests it is important to emulate real browser startup. During real | 49 // For some tests it is important to emulate real browser startup. During real |
33 // browser startup, the main MessageLoop is created before other threads. | 50 // browser startup, the main MessageLoop is created before other threads. |
34 // Passing DONT_CREATE_THREADS to constructor will delay creating other threads | 51 // Passing DONT_CREATE_THREADS to constructor will delay creating other threads |
35 // until the test explicitly calls CreateThreads(). | 52 // until the test explicitly calls CreateThreads(). |
36 // | 53 // |
37 // DONT_CREATE_THREADS should only be used when the options specify at least | 54 // DONT_CREATE_THREADS should only be used when the options specify at least |
38 // one real thread other than the main thread. | 55 // one real thread other than the main thread. |
39 | 56 |
40 #ifndef CONTENT_PUBLIC_TEST_TEST_BROWSER_THREAD_BUNDLE_H_ | 57 #ifndef CONTENT_PUBLIC_TEST_TEST_BROWSER_THREAD_BUNDLE_H_ |
41 #define CONTENT_PUBLIC_TEST_TEST_BROWSER_THREAD_BUNDLE_H_ | 58 #define CONTENT_PUBLIC_TEST_TEST_BROWSER_THREAD_BUNDLE_H_ |
42 | 59 |
43 #include <memory> | 60 #include <memory> |
44 | 61 |
45 #include "base/macros.h" | 62 #include "base/macros.h" |
46 | 63 |
47 namespace base { | 64 namespace base { |
48 class MessageLoop; | 65 class MessageLoop; |
49 namespace test { | 66 namespace test { |
50 class ScopedAsyncTaskScheduler; | 67 class ScopedAsyncTaskScheduler; |
51 class ScopedTaskScheduler; | |
52 } // namespace test | 68 } // namespace test |
53 } // namespace base | 69 } // namespace base |
54 | 70 |
55 namespace content { | 71 namespace content { |
56 | 72 |
57 class TestBrowserThread; | 73 class TestBrowserThread; |
58 | 74 |
59 class TestBrowserThreadBundle { | 75 class TestBrowserThreadBundle { |
60 public: | 76 public: |
61 // Used to specify the type of MessageLoop that backs the UI thread, and | 77 // Used to specify the type of MessageLoop that backs the UI thread, and |
62 // which of the named BrowserThreads should be backed by a real | 78 // which of the named BrowserThreads should be backed by a real |
63 // threads. The UI thread is always the main thread in a unit test. | 79 // threads. The UI thread is always the main thread in a unit test. |
64 enum Options { | 80 enum Options { |
65 DEFAULT = 0, | 81 DEFAULT = 0, |
66 IO_MAINLOOP = 1 << 0, | 82 IO_MAINLOOP = 1 << 0, |
67 REAL_DB_THREAD = 1 << 1, | 83 REAL_DB_THREAD = 1 << 1, |
68 REAL_FILE_THREAD = 1 << 2, | 84 REAL_FILE_THREAD = 1 << 2, |
69 REAL_IO_THREAD = 1 << 3, | 85 REAL_IO_THREAD = 1 << 3, |
70 REAL_TASK_SCHEDULER = 1 << 4, | 86 DONT_CREATE_THREADS = 1 << 4, |
71 DONT_CREATE_THREADS = 1 << 5, | |
72 }; | 87 }; |
73 | 88 |
74 TestBrowserThreadBundle(); | 89 TestBrowserThreadBundle(); |
75 explicit TestBrowserThreadBundle(int options); | 90 explicit TestBrowserThreadBundle(int options); |
76 | 91 |
77 // Creates threads; should only be called from other classes if the | 92 // Creates threads; should only be called from other classes if the |
78 // DONT_CREATE_THREADS option was used when the bundle was created. | 93 // DONT_CREATE_THREADS option was used when the bundle was created. |
79 void CreateThreads(); | 94 void CreateThreads(); |
80 | 95 |
81 ~TestBrowserThreadBundle(); | 96 ~TestBrowserThreadBundle(); |
82 | 97 |
83 private: | 98 private: |
84 void Init(); | 99 void Init(); |
85 | 100 |
86 std::unique_ptr<base::MessageLoop> message_loop_; | 101 std::unique_ptr<base::MessageLoop> message_loop_; |
87 std::unique_ptr<base::test::ScopedAsyncTaskScheduler> | 102 std::unique_ptr<base::test::ScopedAsyncTaskScheduler> |
88 scoped_async_task_scheduler_; | 103 scoped_async_task_scheduler_; |
89 std::unique_ptr<base::test::ScopedTaskScheduler> scoped_task_scheduler_; | |
90 std::unique_ptr<TestBrowserThread> ui_thread_; | 104 std::unique_ptr<TestBrowserThread> ui_thread_; |
91 std::unique_ptr<TestBrowserThread> db_thread_; | 105 std::unique_ptr<TestBrowserThread> db_thread_; |
92 std::unique_ptr<TestBrowserThread> file_thread_; | 106 std::unique_ptr<TestBrowserThread> file_thread_; |
93 std::unique_ptr<TestBrowserThread> file_user_blocking_thread_; | 107 std::unique_ptr<TestBrowserThread> file_user_blocking_thread_; |
94 std::unique_ptr<TestBrowserThread> process_launcher_thread_; | 108 std::unique_ptr<TestBrowserThread> process_launcher_thread_; |
95 std::unique_ptr<TestBrowserThread> cache_thread_; | 109 std::unique_ptr<TestBrowserThread> cache_thread_; |
96 std::unique_ptr<TestBrowserThread> io_thread_; | 110 std::unique_ptr<TestBrowserThread> io_thread_; |
97 | 111 |
98 int options_; | 112 int options_; |
99 bool threads_created_; | 113 bool threads_created_; |
100 | 114 |
101 DISALLOW_COPY_AND_ASSIGN(TestBrowserThreadBundle); | 115 DISALLOW_COPY_AND_ASSIGN(TestBrowserThreadBundle); |
102 }; | 116 }; |
103 | 117 |
104 } // namespace content | 118 } // namespace content |
105 | 119 |
106 #endif // CONTENT_PUBLIC_TEST_TEST_BROWSER_THREAD_BUNDLE_H_ | 120 #endif // CONTENT_PUBLIC_TEST_TEST_BROWSER_THREAD_BUNDLE_H_ |
OLD | NEW |