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 and the task scheduler will |
13 // be backed by a single shared MessageLoop. If a test truly needs separate | 13 // be backed by a single shared MessageLoop. If a test truly needs separate |
14 // threads, it can do so by passing the appropriate combination of option values | 14 // threads, it can do so by passing the appropriate combination of option values |
15 // during the TestBrowserThreadBundle construction. | 15 // during the TestBrowserThreadBundle construction. |
16 // | 16 // |
17 // To synchronously run tasks posted to task scheduler or to TestBrowserThreads | 17 // To synchronously run tasks posted to task scheduler or to TestBrowserThreads |
18 // that use the shared MessageLoop, call RunLoop::Run/RunUntilIdle() on the | 18 // that use the shared MessageLoop, call RunLoop::Run/RunUntilIdle() on the |
19 // thread where the TestBrowserThreadBundle lives. The destructor of | 19 // thread where the TestBrowserThreadBundle lives. The destructor of |
20 // TestBrowserThreadBundle runs remaining TestBrowserThreads tasks, remaining | 20 // TestBrowserThreadBundle runs remaining TestBrowserThreads tasks, remaining |
21 // blocking pool tasks, and remaining BLOCK_SHUTDOWN task scheduler tasks. | 21 // blocking pool tasks, and remaining BLOCK_SHUTDOWN task scheduler tasks. |
22 // | 22 // |
23 // If a test needs a MessageLoopForIO on the main thread, it should use the | 23 // 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 | 24 // IO_MAINLOOP option. This also allows task scheduler tasks to use |
25 // FileDescriptorWatcher. 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. | 26 // REAL_IO_THREAD. |
27 // | 27 // |
28 // If a test needs a TaskScheduler that doesn't run its tasks on the main | |
robliao
2017/01/12 20:15:02
Nit: s/that doesn't run its tasks on the main thre
fdoray
2017/01/12 21:25:56
Done. (dedicated thread instead of threadpool beca
| |
29 // thread, it should use REAL_TASK_SCHEDULER. Usage of this option should be | |
30 // justified as it is easier to understand and debug a single-threaded test. | |
gab
2017/01/12 19:50:27
s/test/unit test/
fdoray
2017/01/12 21:25:56
Done.
| |
31 // | |
28 // For some tests it is important to emulate real browser startup. During real | 32 // For some tests it is important to emulate real browser startup. During real |
29 // browser startup, the main MessageLoop is created before other threads. | 33 // browser startup, the main MessageLoop is created before other threads. |
30 // Passing DONT_START_THREADS to constructor will delay creating other threads | 34 // Passing DONT_START_THREADS to constructor will delay creating other threads |
31 // until the test explicitly calls Start(). | 35 // until the test explicitly calls Start(). |
32 // | 36 // |
33 // DONT_START_THREADS should only be used when the options specify at least | 37 // DONT_START_THREADS should only be used when the options specify at least |
34 // one real thread other than the main thread. | 38 // one real thread other than the main thread. |
35 | 39 |
36 #ifndef CONTENT_PUBLIC_TEST_TEST_BROWSER_THREAD_BUNDLE_H_ | 40 #ifndef CONTENT_PUBLIC_TEST_TEST_BROWSER_THREAD_BUNDLE_H_ |
37 #define CONTENT_PUBLIC_TEST_TEST_BROWSER_THREAD_BUNDLE_H_ | 41 #define CONTENT_PUBLIC_TEST_TEST_BROWSER_THREAD_BUNDLE_H_ |
38 | 42 |
39 #include <memory> | 43 #include <memory> |
40 | 44 |
41 #include "base/macros.h" | 45 #include "base/macros.h" |
42 | 46 |
43 namespace base { | 47 namespace base { |
44 class MessageLoop; | 48 class MessageLoop; |
45 namespace test { | 49 namespace test { |
50 class ScopedAsyncTaskScheduler; | |
46 class ScopedTaskScheduler; | 51 class ScopedTaskScheduler; |
47 } // namespace test | 52 } // namespace test |
48 } // namespace base | 53 } // namespace base |
49 | 54 |
50 namespace content { | 55 namespace content { |
51 | 56 |
52 class TestBrowserThread; | 57 class TestBrowserThread; |
53 | 58 |
54 class TestBrowserThreadBundle { | 59 class TestBrowserThreadBundle { |
55 public: | 60 public: |
56 // Used to specify the type of MessageLoop that backs the UI thread, and | 61 // Used to specify the type of MessageLoop that backs the UI thread, and |
57 // which of the named BrowserThreads should be backed by a real | 62 // which of the named BrowserThreads should be backed by a real |
58 // threads. The UI thread is always the main thread in a unit test. | 63 // threads. The UI thread is always the main thread in a unit test. |
59 enum Options { | 64 enum Options { |
60 DEFAULT = 0x00, | 65 DEFAULT = 0x00, |
61 IO_MAINLOOP = 0x01, | 66 IO_MAINLOOP = 0x01, |
62 REAL_DB_THREAD = 0x02, | 67 REAL_DB_THREAD = 0x02, |
63 REAL_FILE_THREAD = 0x08, | 68 REAL_FILE_THREAD = 0x04, |
64 REAL_IO_THREAD = 0x10, | 69 REAL_IO_THREAD = 0x08, |
70 REAL_TASK_SCHEDULER = 0x10, | |
65 DONT_CREATE_THREADS = 0x20, | 71 DONT_CREATE_THREADS = 0x20, |
gab
2017/01/12 19:50:27
Change 0xFF syntax to
1 << 0
1 << 1
1 << 2
1 <<
robliao
2017/01/12 20:15:02
+1
fdoray
2017/01/12 21:25:56
Done.
| |
66 }; | 72 }; |
67 | 73 |
68 TestBrowserThreadBundle(); | 74 TestBrowserThreadBundle(); |
69 explicit TestBrowserThreadBundle(int options); | 75 explicit TestBrowserThreadBundle(int options); |
70 | 76 |
71 // Creates threads; should only be called from other classes if the | 77 // Creates threads; should only be called from other classes if the |
72 // DONT_START_THREADS option was used when the bundle was created. | 78 // DONT_START_THREADS option was used when the bundle was created. |
73 void CreateThreads(); | 79 void CreateThreads(); |
74 | 80 |
75 ~TestBrowserThreadBundle(); | 81 ~TestBrowserThreadBundle(); |
76 | 82 |
77 private: | 83 private: |
78 void Init(); | 84 void Init(); |
79 | 85 |
80 std::unique_ptr<base::MessageLoop> message_loop_; | 86 std::unique_ptr<base::MessageLoop> message_loop_; |
81 std::unique_ptr<base::test::ScopedTaskScheduler> task_scheduler_; | 87 std::unique_ptr<base::test::ScopedAsyncTaskScheduler> |
88 scoped_async_task_scheduler_; | |
89 std::unique_ptr<base::test::ScopedTaskScheduler> scoped_task_scheduler_; | |
82 std::unique_ptr<TestBrowserThread> ui_thread_; | 90 std::unique_ptr<TestBrowserThread> ui_thread_; |
83 std::unique_ptr<TestBrowserThread> db_thread_; | 91 std::unique_ptr<TestBrowserThread> db_thread_; |
84 std::unique_ptr<TestBrowserThread> file_thread_; | 92 std::unique_ptr<TestBrowserThread> file_thread_; |
85 std::unique_ptr<TestBrowserThread> file_user_blocking_thread_; | 93 std::unique_ptr<TestBrowserThread> file_user_blocking_thread_; |
86 std::unique_ptr<TestBrowserThread> process_launcher_thread_; | 94 std::unique_ptr<TestBrowserThread> process_launcher_thread_; |
87 std::unique_ptr<TestBrowserThread> cache_thread_; | 95 std::unique_ptr<TestBrowserThread> cache_thread_; |
88 std::unique_ptr<TestBrowserThread> io_thread_; | 96 std::unique_ptr<TestBrowserThread> io_thread_; |
89 | 97 |
90 int options_; | 98 int options_; |
91 bool threads_created_; | 99 bool threads_created_; |
92 | 100 |
93 DISALLOW_COPY_AND_ASSIGN(TestBrowserThreadBundle); | 101 DISALLOW_COPY_AND_ASSIGN(TestBrowserThreadBundle); |
94 }; | 102 }; |
95 | 103 |
96 } // namespace content | 104 } // namespace content |
97 | 105 |
98 #endif // CONTENT_PUBLIC_TEST_TEST_BROWSER_THREAD_BUNDLE_H_ | 106 #endif // CONTENT_PUBLIC_TEST_TEST_BROWSER_THREAD_BUNDLE_H_ |
OLD | NEW |