Commit 93605d24 authored by fdoray's avatar fdoray Committed by Commit bot

Change DONT_START_THREADS to DONT_CREATE_THREADS in TestBrowserThreadBundle.

In a real browser process, threads are started as BrowserThreadImpl
objects are created. TestBrowserThreadBundle should do the same.

This is a pre-requisite to add the option to create a real TaskScheduler
in TestBrowserThreadBundle, because TaskScheduler has to be created and
started at the same time.

BUG=659191

Review-Url: https://codereview.chromium.org/2628903002
Cr-Commit-Position: refs/heads/master@{#443536}
parent 0e1f1665
...@@ -107,7 +107,7 @@ class IOThreadTestWithIOThreadObject : public testing::Test { ...@@ -107,7 +107,7 @@ class IOThreadTestWithIOThreadObject : public testing::Test {
protected: protected:
IOThreadTestWithIOThreadObject() IOThreadTestWithIOThreadObject()
: thread_bundle_(content::TestBrowserThreadBundle::REAL_IO_THREAD | : thread_bundle_(content::TestBrowserThreadBundle::REAL_IO_THREAD |
content::TestBrowserThreadBundle::DONT_START_THREADS) { content::TestBrowserThreadBundle::DONT_CREATE_THREADS) {
#if BUILDFLAG(ENABLE_EXTENSIONS) #if BUILDFLAG(ENABLE_EXTENSIONS)
event_router_forwarder_ = new extensions::EventRouterForwarder; event_router_forwarder_ = new extensions::EventRouterForwarder;
#endif #endif
...@@ -139,7 +139,7 @@ class IOThreadTestWithIOThreadObject : public testing::Test { ...@@ -139,7 +139,7 @@ class IOThreadTestWithIOThreadObject : public testing::Test {
// Now that IOThread object is registered starting the threads will // Now that IOThread object is registered starting the threads will
// call the IOThread::Init(). This sets up the environment needed for // call the IOThread::Init(). This sets up the environment needed for
// these tests. // these tests.
thread_bundle_.Start(); thread_bundle_.CreateThreads();
} }
~IOThreadTestWithIOThreadObject() override { ~IOThreadTestWithIOThreadObject() override {
......
...@@ -17,11 +17,13 @@ TestBrowserThreadBundle::TestBrowserThreadBundle() ...@@ -17,11 +17,13 @@ TestBrowserThreadBundle::TestBrowserThreadBundle()
: TestBrowserThreadBundle(DEFAULT) {} : TestBrowserThreadBundle(DEFAULT) {}
TestBrowserThreadBundle::TestBrowserThreadBundle(int options) TestBrowserThreadBundle::TestBrowserThreadBundle(int options)
: options_(options), threads_started_(false) { : options_(options), threads_created_(false) {
Init(); Init();
} }
TestBrowserThreadBundle::~TestBrowserThreadBundle() { TestBrowserThreadBundle::~TestBrowserThreadBundle() {
DCHECK(threads_created_);
// To avoid memory leaks, we must ensure that any tasks posted to the blocking // To avoid memory leaks, we must ensure that any tasks posted to the blocking
// pool via PostTaskAndReply are able to reply back to the originating thread. // pool via PostTaskAndReply are able to reply back to the originating thread.
// Thus we must flush the blocking pool while the browser threads still exist. // Thus we must flush the blocking pool while the browser threads still exist.
...@@ -65,23 +67,34 @@ TestBrowserThreadBundle::~TestBrowserThreadBundle() { ...@@ -65,23 +67,34 @@ TestBrowserThreadBundle::~TestBrowserThreadBundle() {
void TestBrowserThreadBundle::Init() { void TestBrowserThreadBundle::Init() {
// Check for conflicting options can't have two IO threads. // Check for conflicting options can't have two IO threads.
CHECK(!(options_ & IO_MAINLOOP) || !(options_ & REAL_IO_THREAD)); CHECK(!(options_ & IO_MAINLOOP) || !(options_ & REAL_IO_THREAD));
// There must be a thread to start to use DONT_START_THREADS // There must be a thread to start to use DONT_CREATE_THREADS
CHECK((options_ & ~IO_MAINLOOP) != DONT_START_THREADS); CHECK((options_ & ~IO_MAINLOOP) != DONT_CREATE_THREADS);
// Create the UI thread. In production, this work is done in
// BrowserMainLoop::MainMessageLoopStart().
if (options_ & IO_MAINLOOP) { if (options_ & IO_MAINLOOP) {
message_loop_.reset(new base::MessageLoopForIO()); message_loop_.reset(new base::MessageLoopForIO());
} else { } else {
message_loop_.reset(new base::MessageLoopForUI()); message_loop_.reset(new base::MessageLoopForUI());
} }
task_scheduler_.reset(
new base::test::ScopedTaskScheduler(message_loop_.get()));
ui_thread_.reset( ui_thread_.reset(
new TestBrowserThread(BrowserThread::UI, message_loop_.get())); new TestBrowserThread(BrowserThread::UI, message_loop_.get()));
if (!(options_ & DONT_CREATE_THREADS))
CreateThreads();
}
// This method mimics the work done in BrowserMainLoop::CreateThreads().
void TestBrowserThreadBundle::CreateThreads() {
DCHECK(!threads_created_);
task_scheduler_.reset(
new base::test::ScopedTaskScheduler(message_loop_.get()));
if (options_ & REAL_DB_THREAD) { if (options_ & REAL_DB_THREAD) {
db_thread_.reset(new TestBrowserThread(BrowserThread::DB)); db_thread_.reset(new TestBrowserThread(BrowserThread::DB));
db_thread_->Start();
} else { } else {
db_thread_.reset( db_thread_.reset(
new TestBrowserThread(BrowserThread::DB, message_loop_.get())); new TestBrowserThread(BrowserThread::DB, message_loop_.get()));
...@@ -89,6 +102,7 @@ void TestBrowserThreadBundle::Init() { ...@@ -89,6 +102,7 @@ void TestBrowserThreadBundle::Init() {
if (options_ & REAL_FILE_THREAD) { if (options_ & REAL_FILE_THREAD) {
file_thread_.reset(new TestBrowserThread(BrowserThread::FILE)); file_thread_.reset(new TestBrowserThread(BrowserThread::FILE));
file_thread_->Start();
} else { } else {
file_thread_.reset( file_thread_.reset(
new TestBrowserThread(BrowserThread::FILE, message_loop_.get())); new TestBrowserThread(BrowserThread::FILE, message_loop_.get()));
...@@ -103,28 +117,13 @@ void TestBrowserThreadBundle::Init() { ...@@ -103,28 +117,13 @@ void TestBrowserThreadBundle::Init() {
if (options_ & REAL_IO_THREAD) { if (options_ & REAL_IO_THREAD) {
io_thread_.reset(new TestBrowserThread(BrowserThread::IO)); io_thread_.reset(new TestBrowserThread(BrowserThread::IO));
io_thread_->StartIOThread();
} else { } else {
io_thread_.reset( io_thread_.reset(
new TestBrowserThread(BrowserThread::IO, message_loop_.get())); new TestBrowserThread(BrowserThread::IO, message_loop_.get()));
} }
if (!(options_ & DONT_START_THREADS)) threads_created_ = true;
Start();
}
void TestBrowserThreadBundle::Start() {
DCHECK(!threads_started_);
if (options_ & REAL_DB_THREAD)
db_thread_->Start();
if (options_ & REAL_FILE_THREAD)
file_thread_->Start();
if (options_ & REAL_IO_THREAD)
io_thread_->StartIOThread();
threads_started_ = true;
} }
} // namespace content } // namespace content
...@@ -26,13 +26,11 @@ ...@@ -26,13 +26,11 @@
// REAL_IO_THREAD. // REAL_IO_THREAD.
// //
// For some tests it is important to emulate real browser startup. During real // For some tests it is important to emulate real browser startup. During real
// browser startup some initialization is done (e.g. creation of thread objects) // browser startup, the main MessageLoop is created before other threads.
// between creating the main thread message loop, which is bound to the existing // Passing DONT_CREATE_THREADS to constructor will delay creating other threads
// main thread, and starting the other threads. Passing DONT_START_THREADS to // until the test explicitly calls CreateThreads().
// constructor will delay staring these other threads until the test explicitly
// calls Start().
// //
// DONT_START_THREADS should only be used when the options specify at least // DONT_CREATE_THREADS should only be used when the options specify at least
// one real thread other than the main thread. // one real thread other than the main thread.
#ifndef CONTENT_PUBLIC_TEST_TEST_BROWSER_THREAD_BUNDLE_H_ #ifndef CONTENT_PUBLIC_TEST_TEST_BROWSER_THREAD_BUNDLE_H_
...@@ -64,15 +62,15 @@ class TestBrowserThreadBundle { ...@@ -64,15 +62,15 @@ class TestBrowserThreadBundle {
REAL_DB_THREAD = 0x02, REAL_DB_THREAD = 0x02,
REAL_FILE_THREAD = 0x08, REAL_FILE_THREAD = 0x08,
REAL_IO_THREAD = 0x10, REAL_IO_THREAD = 0x10,
DONT_START_THREADS = 0x20, DONT_CREATE_THREADS = 0x20,
}; };
TestBrowserThreadBundle(); TestBrowserThreadBundle();
explicit TestBrowserThreadBundle(int options); explicit TestBrowserThreadBundle(int options);
// Start the real threads; should only be called from other classes if the // Creates threads; should only be called from other classes if the
// DONT_START_THREADS option was used when the bundle was created. // DONT_CREATE_THREADS option was used when the bundle was created.
void Start(); void CreateThreads();
~TestBrowserThreadBundle(); ~TestBrowserThreadBundle();
...@@ -90,7 +88,7 @@ class TestBrowserThreadBundle { ...@@ -90,7 +88,7 @@ class TestBrowserThreadBundle {
std::unique_ptr<TestBrowserThread> io_thread_; std::unique_ptr<TestBrowserThread> io_thread_;
int options_; int options_;
bool threads_started_; bool threads_created_;
DISALLOW_COPY_AND_ASSIGN(TestBrowserThreadBundle); DISALLOW_COPY_AND_ASSIGN(TestBrowserThreadBundle);
}; };
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment