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 {
protected:
IOThreadTestWithIOThreadObject()
: thread_bundle_(content::TestBrowserThreadBundle::REAL_IO_THREAD |
content::TestBrowserThreadBundle::DONT_START_THREADS) {
content::TestBrowserThreadBundle::DONT_CREATE_THREADS) {
#if BUILDFLAG(ENABLE_EXTENSIONS)
event_router_forwarder_ = new extensions::EventRouterForwarder;
#endif
......@@ -139,7 +139,7 @@ class IOThreadTestWithIOThreadObject : public testing::Test {
// Now that IOThread object is registered starting the threads will
// call the IOThread::Init(). This sets up the environment needed for
// these tests.
thread_bundle_.Start();
thread_bundle_.CreateThreads();
}
~IOThreadTestWithIOThreadObject() override {
......
......@@ -17,11 +17,13 @@ TestBrowserThreadBundle::TestBrowserThreadBundle()
: TestBrowserThreadBundle(DEFAULT) {}
TestBrowserThreadBundle::TestBrowserThreadBundle(int options)
: options_(options), threads_started_(false) {
: options_(options), threads_created_(false) {
Init();
}
TestBrowserThreadBundle::~TestBrowserThreadBundle() {
DCHECK(threads_created_);
// 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.
// Thus we must flush the blocking pool while the browser threads still exist.
......@@ -65,23 +67,34 @@ TestBrowserThreadBundle::~TestBrowserThreadBundle() {
void TestBrowserThreadBundle::Init() {
// Check for conflicting options can't have two IO threads.
CHECK(!(options_ & IO_MAINLOOP) || !(options_ & REAL_IO_THREAD));
// There must be a thread to start to use DONT_START_THREADS
CHECK((options_ & ~IO_MAINLOOP) != DONT_START_THREADS);
// There must be a thread to start to use DONT_CREATE_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) {
message_loop_.reset(new base::MessageLoopForIO());
} else {
message_loop_.reset(new base::MessageLoopForUI());
}
task_scheduler_.reset(
new base::test::ScopedTaskScheduler(message_loop_.get()));
ui_thread_.reset(
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) {
db_thread_.reset(new TestBrowserThread(BrowserThread::DB));
db_thread_->Start();
} else {
db_thread_.reset(
new TestBrowserThread(BrowserThread::DB, message_loop_.get()));
......@@ -89,6 +102,7 @@ void TestBrowserThreadBundle::Init() {
if (options_ & REAL_FILE_THREAD) {
file_thread_.reset(new TestBrowserThread(BrowserThread::FILE));
file_thread_->Start();
} else {
file_thread_.reset(
new TestBrowserThread(BrowserThread::FILE, message_loop_.get()));
......@@ -103,28 +117,13 @@ void TestBrowserThreadBundle::Init() {
if (options_ & REAL_IO_THREAD) {
io_thread_.reset(new TestBrowserThread(BrowserThread::IO));
io_thread_->StartIOThread();
} else {
io_thread_.reset(
new TestBrowserThread(BrowserThread::IO, message_loop_.get()));
}
if (!(options_ & DONT_START_THREADS))
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;
threads_created_ = true;
}
} // namespace content
......@@ -26,13 +26,11 @@
// REAL_IO_THREAD.
//
// 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)
// between creating the main thread message loop, which is bound to the existing
// main thread, and starting the other threads. Passing DONT_START_THREADS to
// constructor will delay staring these other threads until the test explicitly
// calls Start().
// browser startup, the main MessageLoop is created before other threads.
// Passing DONT_CREATE_THREADS to constructor will delay creating other threads
// until the test explicitly calls CreateThreads().
//
// 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.
#ifndef CONTENT_PUBLIC_TEST_TEST_BROWSER_THREAD_BUNDLE_H_
......@@ -64,15 +62,15 @@ class TestBrowserThreadBundle {
REAL_DB_THREAD = 0x02,
REAL_FILE_THREAD = 0x08,
REAL_IO_THREAD = 0x10,
DONT_START_THREADS = 0x20,
DONT_CREATE_THREADS = 0x20,
};
TestBrowserThreadBundle();
explicit TestBrowserThreadBundle(int options);
// Start the real threads; should only be called from other classes if the
// DONT_START_THREADS option was used when the bundle was created.
void Start();
// Creates threads; should only be called from other classes if the
// DONT_CREATE_THREADS option was used when the bundle was created.
void CreateThreads();
~TestBrowserThreadBundle();
......@@ -90,7 +88,7 @@ class TestBrowserThreadBundle {
std::unique_ptr<TestBrowserThread> io_thread_;
int options_;
bool threads_started_;
bool threads_created_;
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