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

Side by Side Diff: content/public/test/test_browser_thread_bundle.cc

Issue 2860533002: Support base::test::ScopedTaskEnvironment and content::TestBrowserThreadBundle in the same scope. (Closed)
Patch Set: remove check failed 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 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 #include "content/public/test/test_browser_thread_bundle.h" 5 #include "content/public/test/test_browser_thread_bundle.h"
6 6
7 #include "base/logging.h" 7 #include "base/logging.h"
8 #include "base/memory/ptr_util.h" 8 #include "base/memory/ptr_util.h"
9 #include "base/message_loop/message_loop.h" 9 #include "base/message_loop/message_loop.h"
10 #include "base/run_loop.h" 10 #include "base/run_loop.h"
11 #include "base/task_scheduler/task_scheduler.h"
11 #include "base/test/scoped_async_task_scheduler.h" 12 #include "base/test/scoped_async_task_scheduler.h"
12 #include "content/browser/browser_thread_impl.h" 13 #include "content/browser/browser_thread_impl.h"
14 #include "content/public/browser/browser_thread.h"
13 #include "content/public/test/test_browser_thread.h" 15 #include "content/public/test/test_browser_thread.h"
14 16
15 namespace content { 17 namespace content {
16 18
17 TestBrowserThreadBundle::TestBrowserThreadBundle() 19 TestBrowserThreadBundle::TestBrowserThreadBundle()
18 : TestBrowserThreadBundle(DEFAULT) {} 20 : TestBrowserThreadBundle(DEFAULT) {}
19 21
20 TestBrowserThreadBundle::TestBrowserThreadBundle(int options) 22 TestBrowserThreadBundle::TestBrowserThreadBundle(int options)
21 : options_(options), threads_created_(false) { 23 : options_(options), threads_created_(false) {
22 Init(); 24 Init();
23 } 25 }
24 26
25 TestBrowserThreadBundle::~TestBrowserThreadBundle() { 27 TestBrowserThreadBundle::~TestBrowserThreadBundle() {
26 DCHECK(threads_created_); 28 CHECK(threads_created_);
27 29
28 // To avoid memory leaks, we must ensure that any tasks posted to the blocking 30 // To avoid memory leaks, we must ensure that any tasks posted to the blocking
29 // pool via PostTaskAndReply are able to reply back to the originating thread. 31 // pool via PostTaskAndReply are able to reply back to the originating thread.
30 // Thus we must flush the blocking pool while the browser threads still exist. 32 // Thus we must flush the blocking pool while the browser threads still exist.
31 base::RunLoop().RunUntilIdle(); 33 base::RunLoop().RunUntilIdle();
32 BrowserThreadImpl::FlushThreadPoolHelperForTesting(); 34 BrowserThreadImpl::FlushThreadPoolHelperForTesting();
33 35
34 // To ensure a clean teardown, each thread's message loop must be flushed 36 // To ensure a clean teardown, each thread's message loop must be flushed
35 // just before the thread is destroyed. But stopping a fake thread does not 37 // just before the thread is destroyed. But stopping a fake thread does not
36 // automatically flush the message loop, so we have to do it manually. 38 // automatically flush the message loop, so we have to do it manually.
(...skipping 15 matching lines...) Expand all
52 // it again in case any shutdown tasks have been posted to the pool from the 54 // it again in case any shutdown tasks have been posted to the pool from the
53 // threads above. 55 // threads above.
54 BrowserThreadImpl::FlushThreadPoolHelperForTesting(); 56 BrowserThreadImpl::FlushThreadPoolHelperForTesting();
55 base::RunLoop().RunUntilIdle(); 57 base::RunLoop().RunUntilIdle();
56 ui_thread_->Stop(); 58 ui_thread_->Stop();
57 base::RunLoop().RunUntilIdle(); 59 base::RunLoop().RunUntilIdle();
58 60
59 scoped_async_task_scheduler_.reset(); 61 scoped_async_task_scheduler_.reset();
60 62
61 base::RunLoop().RunUntilIdle(); 63 base::RunLoop().RunUntilIdle();
64 CHECK(base::MessageLoop::current()->IsIdleForTesting());
62 65
63 // |message_loop_| needs to explicitly go away before fake threads in order 66 // |message_loop_| needs to explicitly go away before fake threads in order
64 // for DestructionObservers hooked to |message_loop_| to be able to invoke 67 // for DestructionObservers hooked to |message_loop_| to be able to invoke
65 // BrowserThread::CurrentlyOn() -- ref. ~TestBrowserThread(). 68 // BrowserThread::CurrentlyOn() -- ref. ~TestBrowserThread().
66 CHECK(message_loop_->IsIdleForTesting());
67 message_loop_.reset(); 69 message_loop_.reset();
68 } 70 }
69 71
70 void TestBrowserThreadBundle::Init() { 72 void TestBrowserThreadBundle::Init() {
73 // Check that the UI thread hasn't already been initialized. This will fail if
74 // multiple TestBrowserThreadBundles are initialized in the same scope.
75 CHECK(!BrowserThread::IsThreadInitialized(BrowserThread::UI));
76
71 // Check for conflicting options can't have two IO threads. 77 // Check for conflicting options can't have two IO threads.
72 CHECK(!(options_ & IO_MAINLOOP) || !(options_ & REAL_IO_THREAD)); 78 CHECK(!(options_ & IO_MAINLOOP) || !(options_ & REAL_IO_THREAD));
73 // There must be a thread to start to use DONT_CREATE_THREADS 79 // There must be a thread to start to use DONT_CREATE_THREADS
74 CHECK((options_ & ~IO_MAINLOOP) != DONT_CREATE_THREADS); 80 CHECK((options_ & ~IO_MAINLOOP) != DONT_CREATE_THREADS);
75 81
76 // Create the UI thread. In production, this work is done in 82 // Create the main MessageLoop, if it doesn't already exist, and set the
77 // BrowserMainLoop::MainMessageLoopStart(). 83 // current thread as the UI thread. In production, this work is done in
78 if (options_ & IO_MAINLOOP) { 84 // BrowserMainLoop::MainMessageLoopStart(). The main MessageLoop may already
79 message_loop_.reset(new base::MessageLoopForIO()); 85 // exist if this TestBrowserThreadBundle is instantiated in a test whose
80 } else { 86 // parent fixture provides a base::test::ScopedTaskEnvironment.
81 message_loop_.reset(new base::MessageLoopForUI()); 87 const base::MessageLoop::Type message_loop_type =
82 } 88 options_ & IO_MAINLOOP ? base::MessageLoop::TYPE_IO
89 : base::MessageLoop::TYPE_UI;
90 if (!base::MessageLoop::current())
91 message_loop_ = base::MakeUnique<base::MessageLoop>(message_loop_type);
92 CHECK(base::MessageLoop::current()->IsType(message_loop_type));
83 93
84 ui_thread_.reset( 94 ui_thread_ = base::MakeUnique<TestBrowserThread>(
85 new TestBrowserThread(BrowserThread::UI, message_loop_.get())); 95 BrowserThread::UI, base::MessageLoop::current());
86 96
87 if (!(options_ & DONT_CREATE_THREADS)) 97 if (!(options_ & DONT_CREATE_THREADS))
88 CreateThreads(); 98 CreateThreads();
89 } 99 }
90 100
91 // This method mimics the work done in BrowserMainLoop::CreateThreads(). 101 // This method mimics the work done in BrowserMainLoop::CreateThreads().
92 void TestBrowserThreadBundle::CreateThreads() { 102 void TestBrowserThreadBundle::CreateThreads() {
93 DCHECK(!threads_created_); 103 CHECK(!threads_created_);
94 104
95 scoped_async_task_scheduler_ = 105 // TaskScheduler can sometimes be externally provided by a
96 base::MakeUnique<base::test::ScopedAsyncTaskScheduler>(); 106 // base::test::ScopedTaskEnvironment in a parent fixture. In that case it's
107 // expected to have provided the MessageLoop as well (in which case
108 // |message_loop_| remains null in Init()).
109 CHECK(!base::TaskScheduler::GetInstance() || !message_loop_);
110 if (!base::TaskScheduler::GetInstance()) {
111 scoped_async_task_scheduler_ =
112 base::MakeUnique<base::test::ScopedAsyncTaskScheduler>();
113 }
97 114
98 if (options_ & REAL_DB_THREAD) { 115 if (options_ & REAL_DB_THREAD) {
99 db_thread_.reset(new TestBrowserThread(BrowserThread::DB)); 116 db_thread_ = base::MakeUnique<TestBrowserThread>(BrowserThread::DB);
100 db_thread_->Start(); 117 db_thread_->Start();
101 } else { 118 } else {
102 db_thread_.reset( 119 db_thread_ = base::MakeUnique<TestBrowserThread>(
103 new TestBrowserThread(BrowserThread::DB, message_loop_.get())); 120 BrowserThread::DB, base::MessageLoop::current());
104 } 121 }
105 122
106 if (options_ & REAL_FILE_THREAD) { 123 if (options_ & REAL_FILE_THREAD) {
107 file_thread_.reset(new TestBrowserThread(BrowserThread::FILE)); 124 file_thread_ = base::MakeUnique<TestBrowserThread>(BrowserThread::FILE);
108 file_thread_->Start(); 125 file_thread_->Start();
109 } else { 126 } else {
110 file_thread_.reset( 127 file_thread_ = base::MakeUnique<TestBrowserThread>(
111 new TestBrowserThread(BrowserThread::FILE, message_loop_.get())); 128 BrowserThread::FILE, base::MessageLoop::current());
112 } 129 }
113 130
114 file_user_blocking_thread_.reset(new TestBrowserThread( 131 file_user_blocking_thread_ = base::MakeUnique<TestBrowserThread>(
115 BrowserThread::FILE_USER_BLOCKING, message_loop_.get())); 132 BrowserThread::FILE_USER_BLOCKING, base::MessageLoop::current());
116 process_launcher_thread_.reset(new TestBrowserThread( 133 process_launcher_thread_ = base::MakeUnique<TestBrowserThread>(
117 BrowserThread::PROCESS_LAUNCHER, message_loop_.get())); 134 BrowserThread::PROCESS_LAUNCHER, base::MessageLoop::current());
118 cache_thread_.reset( 135 cache_thread_ = base::MakeUnique<TestBrowserThread>(
119 new TestBrowserThread(BrowserThread::CACHE, message_loop_.get())); 136 BrowserThread::CACHE, base::MessageLoop::current());
120 137
121 if (options_ & REAL_IO_THREAD) { 138 if (options_ & REAL_IO_THREAD) {
122 io_thread_.reset(new TestBrowserThread(BrowserThread::IO)); 139 io_thread_ = base::MakeUnique<TestBrowserThread>(BrowserThread::IO);
123 io_thread_->StartIOThread(); 140 io_thread_->StartIOThread();
124 } else { 141 } else {
125 io_thread_.reset( 142 io_thread_ = base::MakeUnique<TestBrowserThread>(
126 new TestBrowserThread(BrowserThread::IO, message_loop_.get())); 143 BrowserThread::IO, base::MessageLoop::current());
127 } 144 }
128 145
129 threads_created_ = true; 146 threads_created_ = true;
130 } 147 }
131 148
132 } // namespace content 149 } // namespace content
OLDNEW
« no previous file with comments | « content/public/test/test_browser_thread_bundle.h ('k') | content/public/test/test_browser_thread_bundle_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698