diff options
author | oshima@chromium.org <oshima@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-12-04 04:02:56 +0000 |
---|---|---|
committer | oshima@chromium.org <oshima@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-12-04 04:02:56 +0000 |
commit | 244932d545ce1bea5e1724d3a77c5064fa567eb2 (patch) | |
tree | aaed157a5e258ce1accda2c2b9618c64d8180010 /net/test | |
parent | 9da582be89b7c9ab891383c6d6c26e370c7f579c (diff) | |
download | chromium_src-244932d545ce1bea5e1724d3a77c5064fa567eb2.zip chromium_src-244932d545ce1bea5e1724d3a77c5064fa567eb2.tar.gz chromium_src-244932d545ce1bea5e1724d3a77c5064fa567eb2.tar.bz2 |
Do not spawn a thread in browser/interactive ui tests before spawning sandbox host process
* Introduced DBThreadManager::SetInstanceForTesting to specify the instance to be used when DBThreadManager::Initialize is called in the browser setup rocess.
* Temporarily stop the thread in EmbeddedTestServer to fork/exec sandbox host process properly.
BUG=322732
TBR=sky@chromium.org
Review URL: https://codereview.chromium.org/83633004
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@238554 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net/test')
-rw-r--r-- | net/test/embedded_test_server/embedded_test_server.cc | 39 | ||||
-rw-r--r-- | net/test/embedded_test_server/embedded_test_server.h | 43 |
2 files changed, 65 insertions, 17 deletions
diff --git a/net/test/embedded_test_server/embedded_test_server.cc b/net/test/embedded_test_server/embedded_test_server.cc index 3d246d1..aacecd2 100644 --- a/net/test/embedded_test_server/embedded_test_server.cc +++ b/net/test/embedded_test_server/embedded_test_server.cc @@ -99,6 +99,10 @@ HttpListenSocket::~HttpListenSocket() { DCHECK(thread_checker_.CalledOnValidThread()); } +void HttpListenSocket::DetachFromThread() { + thread_checker_.DetachFromThread(); +} + EmbeddedTestServer::EmbeddedTestServer() : port_(-1), weak_factory_(this) { @@ -114,21 +118,28 @@ EmbeddedTestServer::~EmbeddedTestServer() { } bool EmbeddedTestServer::InitializeAndWaitUntilReady() { - base::Thread::Options thread_options; - thread_options.message_loop_type = base::MessageLoop::TYPE_IO; - io_thread_.reset(new base::Thread("EmbeddedTestServer io thread")); - CHECK(io_thread_->StartWithOptions(thread_options)); - + StartThread(); DCHECK(thread_checker_.CalledOnValidThread()); - if (!PostTaskToIOThreadAndWait(base::Bind( &EmbeddedTestServer::InitializeOnIOThread, base::Unretained(this)))) { return false; } - return Started() && base_url_.is_valid(); } +void EmbeddedTestServer::StopThread() { + io_thread_->Stop(); + io_thread_.reset(); + thread_checker_.DetachFromThread(); + listen_socket_->DetachFromThread(); +} + +void EmbeddedTestServer::RestartThreadAndListen() { + StartThread(); + CHECK(PostTaskToIOThreadAndWait(base::Bind( + &EmbeddedTestServer::ListenOnIOThread, base::Unretained(this)))); +} + bool EmbeddedTestServer::ShutdownAndWaitUntilComplete() { DCHECK(thread_checker_.CalledOnValidThread()); @@ -136,6 +147,14 @@ bool EmbeddedTestServer::ShutdownAndWaitUntilComplete() { &EmbeddedTestServer::ShutdownOnIOThread, base::Unretained(this))); } +void EmbeddedTestServer::StartThread() { + DCHECK(!io_thread_.get()); + base::Thread::Options thread_options; + thread_options.message_loop_type = base::MessageLoop::TYPE_IO; + io_thread_.reset(new base::Thread("EmbeddedTestServer io thread")); + CHECK(io_thread_->StartWithOptions(thread_options)); +} + void EmbeddedTestServer::InitializeOnIOThread() { DCHECK(io_thread_->message_loop_proxy()->BelongsToCurrentThread()); DCHECK(!Started()); @@ -157,6 +176,12 @@ void EmbeddedTestServer::InitializeOnIOThread() { } } +void EmbeddedTestServer::ListenOnIOThread() { + DCHECK(io_thread_->message_loop_proxy()->BelongsToCurrentThread()); + DCHECK(Started()); + listen_socket_->Listen(); +} + void EmbeddedTestServer::ShutdownOnIOThread() { DCHECK(io_thread_->message_loop_proxy()->BelongsToCurrentThread()); diff --git a/net/test/embedded_test_server/embedded_test_server.h b/net/test/embedded_test_server/embedded_test_server.h index d5300b4..5600e0f 100644 --- a/net/test/embedded_test_server/embedded_test_server.h +++ b/net/test/embedded_test_server/embedded_test_server.h @@ -39,6 +39,10 @@ class HttpListenSocket : public TCPListenSocket { virtual void Listen(); private: + friend class EmbeddedTestServer; + + // Detaches the current from |thread_checker_|. + void DetachFromThread(); base::ThreadChecker thread_checker_; }; @@ -48,18 +52,10 @@ class HttpListenSocket : public TCPListenSocket { // it assumes that the request syntax is correct. It *does not* support // a Chunked Transfer Encoding. // -// The common use case is below: -// -// base::Thread io_thread_; -// scoped_ptr<EmbeddedTestServer> test_server_; +// The common use case for unit tests is below: // // void SetUp() { -// base::Thread::Options thread_options; -// thread_options.message_loop_type = base::MessageLoop::TYPE_IO; -// ASSERT_TRUE(io_thread_.StartWithOptions(thread_options)); -// -// test_server_.reset( -// new EmbeddedTestServer(io_thread_.message_loop_proxy())); +// test_server_.reset(new EmbeddedTestServer()); // ASSERT_TRUE(test_server_.InitializeAndWaitUntilReady()); // test_server_->RegisterRequestHandler( // base::Bind(&FooTest::HandleRequest, base::Unretained(this))); @@ -77,6 +73,24 @@ class HttpListenSocket : public TCPListenSocket { // return http_response.Pass(); // } // +// For a test that spawns another process such as browser_tests, you +// need to stop the server's thread so that there is no no other +// threads running while spawning the process. To do so, please follow +// the following example: +// +// void SetUp() { +// test_server_.reset(new EmbeddedTestServer()); +// // EmbeddedTestServer spawns a thread to initialize socket. +// // Stop the thread in preparation for fork and exec. +// test_server_->StopThread(); +// ... +// InProcessBrowserTest::SetUp(); +// } +// +// void SetUpOnMainThread() { +// test_server_->RestartThreadAndListen(); +// } +// class EmbeddedTestServer : public StreamListenSocket::Delegate { public: typedef base::Callback<scoped_ptr<HttpResponse>( @@ -122,10 +136,19 @@ class EmbeddedTestServer : public StreamListenSocket::Delegate { // on UI thread. void RegisterRequestHandler(const HandleRequestCallback& callback); + // Stops IO thread that handles http requests. + void StopThread(); + + // Restarts IO thread and listen on the socket. + void RestartThreadAndListen(); + private: + void StartThread(); + // Initializes and starts the server. If initialization succeeds, Starts() // will return true. void InitializeOnIOThread(); + void ListenOnIOThread(); // Shuts down the server. void ShutdownOnIOThread(); |