Chromium Code Reviews| 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: |
|
robliao
2017/04/03 17:53:05
Should there be this many ways to flush tasks or w
gab
2017/04/03 18:14:14
I think this distinction matters (and I'm making i
robliao
2017/04/03 18:17:27
Presented that way, I might be able to buy it. We
fdoray
2017/04/03 19:18:32
I expect most that most of the comments added in t
| |
| 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 // RegisterCallbackToRunOnMainThreadWhenConditionIsMet( | |
|
gab
2017/04/03 18:20:30
I'm no clear on what RegisterCallbackToRunOnMainTh
fdoray
2017/04/03 19:18:32
Done.
| |
| 31 // base::Bind(&base::RunLoop::Quit, base::Unretained(&run_loop))); | |
|
gab
2017/04/03 18:20:30
QuitClosure
fdoray
2017/04/03 19:18:33
Done.
| |
| 32 // run_loop.Run(); | |
| 33 // | |
| 34 // To wait until there are no pending undelayed tasks in TaskScheduler or in the | |
| 35 // blocking pool, without running tasks from the shared MessageLoop: | |
| 36 // base::TaskScheduler()->FlushForTesting(); | |
|
gab
2017/04/03 18:20:30
// Note: base::SequencedWorkerPool::FlushForTestin
fdoray
2017/04/03 19:18:33
Done.
| |
| 37 // | |
| 38 // The destructor of TestBrowserThreadBundle runs remaining TestBrowserThreads | |
| 39 // tasks, remaining blocking pool tasks, and remaining BLOCK_SHUTDOWN task | |
| 40 // scheduler tasks. | |
| 22 // | 41 // |
| 23 // If a test needs a MessageLoopForIO on the main thread, it should use the | 42 // 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 | 43 // 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. | 44 // REAL_IO_THREAD. |
| 27 // | 45 // |
| 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 | 46 // For some tests it is important to emulate real browser startup. During real |
| 33 // browser startup, the main MessageLoop is created before other threads. | 47 // browser startup, the main MessageLoop is created before other threads. |
| 34 // Passing DONT_CREATE_THREADS to constructor will delay creating other threads | 48 // Passing DONT_CREATE_THREADS to constructor will delay creating other threads |
| 35 // until the test explicitly calls CreateThreads(). | 49 // until the test explicitly calls CreateThreads(). |
| 36 // | 50 // |
| 37 // DONT_CREATE_THREADS should only be used when the options specify at least | 51 // DONT_CREATE_THREADS should only be used when the options specify at least |
| 38 // one real thread other than the main thread. | 52 // one real thread other than the main thread. |
| 39 | 53 |
| 40 #ifndef CONTENT_PUBLIC_TEST_TEST_BROWSER_THREAD_BUNDLE_H_ | 54 #ifndef CONTENT_PUBLIC_TEST_TEST_BROWSER_THREAD_BUNDLE_H_ |
| 41 #define CONTENT_PUBLIC_TEST_TEST_BROWSER_THREAD_BUNDLE_H_ | 55 #define CONTENT_PUBLIC_TEST_TEST_BROWSER_THREAD_BUNDLE_H_ |
| 42 | 56 |
| 43 #include <memory> | 57 #include <memory> |
| 44 | 58 |
| 45 #include "base/macros.h" | 59 #include "base/macros.h" |
| 46 | 60 |
| 47 namespace base { | 61 namespace base { |
| 48 class MessageLoop; | 62 class MessageLoop; |
| 49 namespace test { | 63 namespace test { |
| 50 class ScopedAsyncTaskScheduler; | 64 class ScopedAsyncTaskScheduler; |
| 51 class ScopedTaskScheduler; | |
| 52 } // namespace test | 65 } // namespace test |
| 53 } // namespace base | 66 } // namespace base |
| 54 | 67 |
| 55 namespace content { | 68 namespace content { |
| 56 | 69 |
| 57 class TestBrowserThread; | 70 class TestBrowserThread; |
| 58 | 71 |
| 59 class TestBrowserThreadBundle { | 72 class TestBrowserThreadBundle { |
| 60 public: | 73 public: |
| 61 // Used to specify the type of MessageLoop that backs the UI thread, and | 74 // 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 | 75 // 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. | 76 // threads. The UI thread is always the main thread in a unit test. |
| 64 enum Options { | 77 enum Options { |
| 65 DEFAULT = 0, | 78 DEFAULT = 0, |
| 66 IO_MAINLOOP = 1 << 0, | 79 IO_MAINLOOP = 1 << 0, |
| 67 REAL_DB_THREAD = 1 << 1, | 80 REAL_DB_THREAD = 1 << 1, |
| 68 REAL_FILE_THREAD = 1 << 2, | 81 REAL_FILE_THREAD = 1 << 2, |
| 69 REAL_IO_THREAD = 1 << 3, | 82 REAL_IO_THREAD = 1 << 3, |
| 70 REAL_TASK_SCHEDULER = 1 << 4, | 83 DONT_CREATE_THREADS = 1 << 4, |
| 71 DONT_CREATE_THREADS = 1 << 5, | |
| 72 }; | 84 }; |
| 73 | 85 |
| 74 TestBrowserThreadBundle(); | 86 TestBrowserThreadBundle(); |
| 75 explicit TestBrowserThreadBundle(int options); | 87 explicit TestBrowserThreadBundle(int options); |
| 76 | 88 |
| 77 // Creates threads; should only be called from other classes if the | 89 // Creates threads; should only be called from other classes if the |
| 78 // DONT_CREATE_THREADS option was used when the bundle was created. | 90 // DONT_CREATE_THREADS option was used when the bundle was created. |
| 79 void CreateThreads(); | 91 void CreateThreads(); |
| 80 | 92 |
| 81 ~TestBrowserThreadBundle(); | 93 ~TestBrowserThreadBundle(); |
| 82 | 94 |
| 83 private: | 95 private: |
| 84 void Init(); | 96 void Init(); |
| 85 | 97 |
| 86 std::unique_ptr<base::MessageLoop> message_loop_; | 98 std::unique_ptr<base::MessageLoop> message_loop_; |
| 87 std::unique_ptr<base::test::ScopedAsyncTaskScheduler> | 99 std::unique_ptr<base::test::ScopedAsyncTaskScheduler> |
| 88 scoped_async_task_scheduler_; | 100 scoped_async_task_scheduler_; |
| 89 std::unique_ptr<base::test::ScopedTaskScheduler> scoped_task_scheduler_; | |
| 90 std::unique_ptr<TestBrowserThread> ui_thread_; | 101 std::unique_ptr<TestBrowserThread> ui_thread_; |
| 91 std::unique_ptr<TestBrowserThread> db_thread_; | 102 std::unique_ptr<TestBrowserThread> db_thread_; |
| 92 std::unique_ptr<TestBrowserThread> file_thread_; | 103 std::unique_ptr<TestBrowserThread> file_thread_; |
| 93 std::unique_ptr<TestBrowserThread> file_user_blocking_thread_; | 104 std::unique_ptr<TestBrowserThread> file_user_blocking_thread_; |
| 94 std::unique_ptr<TestBrowserThread> process_launcher_thread_; | 105 std::unique_ptr<TestBrowserThread> process_launcher_thread_; |
| 95 std::unique_ptr<TestBrowserThread> cache_thread_; | 106 std::unique_ptr<TestBrowserThread> cache_thread_; |
| 96 std::unique_ptr<TestBrowserThread> io_thread_; | 107 std::unique_ptr<TestBrowserThread> io_thread_; |
| 97 | 108 |
| 98 int options_; | 109 int options_; |
| 99 bool threads_created_; | 110 bool threads_created_; |
| 100 | 111 |
| 101 DISALLOW_COPY_AND_ASSIGN(TestBrowserThreadBundle); | 112 DISALLOW_COPY_AND_ASSIGN(TestBrowserThreadBundle); |
| 102 }; | 113 }; |
| 103 | 114 |
| 104 } // namespace content | 115 } // namespace content |
| 105 | 116 |
| 106 #endif // CONTENT_PUBLIC_TEST_TEST_BROWSER_THREAD_BUNDLE_H_ | 117 #endif // CONTENT_PUBLIC_TEST_TEST_BROWSER_THREAD_BUNDLE_H_ |
| OLD | NEW |