diff options
author | phajdan.jr@chromium.org <phajdan.jr@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-08-14 22:37:35 +0000 |
---|---|---|
committer | phajdan.jr@chromium.org <phajdan.jr@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-08-14 22:37:35 +0000 |
commit | b89290213d94169f8293f9bd7668550975281d45 (patch) | |
tree | 951d1218d6c660cd3cf94c9c67b8c9f193a536a8 /net | |
parent | 0c6da50c299be943b4c04a3953e0931734af7eaf (diff) | |
download | chromium_src-b89290213d94169f8293f9bd7668550975281d45.zip chromium_src-b89290213d94169f8293f9bd7668550975281d45.tar.gz chromium_src-b89290213d94169f8293f9bd7668550975281d45.tar.bz2 |
Speed up net_unittests a bit by re-using launched test server.
On POSIX it makes the server fork a separate process for each request for better
test isolation.
Starting with just few tests to limit impact of an eventual breakage. The results are promising.
TEST=none
BUG=none
Review URL: http://codereview.chromium.org/164522
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@23481 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net')
-rw-r--r-- | net/socket/ssl_test_util.cc | 6 | ||||
-rw-r--r-- | net/socket/ssl_test_util.h | 7 | ||||
-rw-r--r-- | net/tools/testserver/testserver.py | 25 | ||||
-rw-r--r-- | net/url_request/url_request_unittest.cc | 46 | ||||
-rw-r--r-- | net/url_request/url_request_unittest.h | 29 |
5 files changed, 89 insertions, 24 deletions
diff --git a/net/socket/ssl_test_util.cc b/net/socket/ssl_test_util.cc index f6dc15e..a2874f5 100644 --- a/net/socket/ssl_test_util.cc +++ b/net/socket/ssl_test_util.cc @@ -95,6 +95,7 @@ const int TestServerLauncher::kBadHTTPSPort = 9666; const wchar_t TestServerLauncher::kCertIssuerName[] = L"Test CA"; TestServerLauncher::TestServerLauncher() : process_handle_(NULL), + forking_(false), connection_attempts_(10), connection_timeout_(1000) #if defined(OS_LINUX) @@ -107,6 +108,7 @@ TestServerLauncher::TestServerLauncher() : process_handle_(NULL), TestServerLauncher::TestServerLauncher(int connection_attempts, int connection_timeout) : process_handle_(NULL), + forking_(false), connection_attempts_(connection_attempts), connection_timeout_(connection_timeout) #if defined(OS_LINUX) @@ -222,6 +224,8 @@ bool TestServerLauncher::Start(Protocol protocol, command_line.append(file_root_url); command_line.append(L"\""); } + // Deliberately do not pass the --forking flag. It breaks the tests + // on Windows. if (!base::LaunchApp(command_line, false, true, &process_handle_)) { LOG(ERROR) << "Failed to launch " << command_line; @@ -238,6 +242,8 @@ bool TestServerLauncher::Start(Protocol protocol, command_line.push_back("-f"); if (!cert_path.value().empty()) command_line.push_back("--https=" + WideToUTF8(cert_path.ToWStringHack())); + if (forking_) + command_line.push_back("--forking"); base::file_handle_mapping_vector no_mappings; LOG(INFO) << "Trying to launch " << command_line[0] << " ..."; diff --git a/net/socket/ssl_test_util.h b/net/socket/ssl_test_util.h index ad9bbab..fb060da 100644 --- a/net/socket/ssl_test_util.h +++ b/net/socket/ssl_test_util.h @@ -37,6 +37,10 @@ class TestServerLauncher { // Load the test root cert, if it hasn't been loaded yet. bool LoadTestRootCert(); + // Tells the server to enable/disable servicing each request + // in a separate process. Takes effect only if called before Start. + void set_forking(bool forking) { forking_ = forking; } + // Start src/net/tools/testserver/testserver.py and // ask it to serve the given protocol. // If protocol is HTTP, and cert_path is not empty, serves HTTPS. @@ -106,6 +110,9 @@ class TestServerLauncher { base::ProcessHandle process_handle_; + // True if the server should handle each request in a separate process. + bool forking_; + // Number of tries and timeout for each try used for WaitToStart. int connection_attempts_; int connection_timeout_; diff --git a/net/tools/testserver/testserver.py b/net/tools/testserver/testserver.py index 14ae182..aeee5e2 100644 --- a/net/tools/testserver/testserver.py +++ b/net/tools/testserver/testserver.py @@ -78,6 +78,16 @@ class HTTPSServer(tlslite.api.TLSSocketServerMixIn, StoppableHTTPServer): print "Handshake failure:", str(error) return False +class ForkingHTTPServer(SocketServer.ForkingMixIn, StoppableHTTPServer): + """This is a specialization of of StoppableHTTPServer which serves each + request in a separate process""" + pass + +class ForkingHTTPSServer(SocketServer.ForkingMixIn, HTTPSServer): + """This is a specialization of of HTTPSServer which serves each + request in a separate process""" + pass + class TestPageHandler(BaseHTTPServer.BaseHTTPRequestHandler): def __init__(self, request, client_address, socket_server): @@ -1047,10 +1057,18 @@ def main(options, args): if not os.path.isfile(options.cert): print 'specified cert file not found: ' + options.cert + ' exiting...' return - server = HTTPSServer(('127.0.0.1', port), TestPageHandler, options.cert) + if options.forking: + server_class = ForkingHTTPSServer + else: + server_class = HTTPSServer + server = server_class(('127.0.0.1', port), TestPageHandler, options.cert) print 'HTTPS server started on port %d...' % port else: - server = StoppableHTTPServer(('127.0.0.1', port), TestPageHandler) + if options.forking: + server_class = ForkingHTTPServer + else: + server_class = StoppableHTTPServer + server = server_class(('127.0.0.1', port), TestPageHandler) print 'HTTP server started on port %d...' % port server.data_dir = MakeDataDir() @@ -1102,6 +1120,9 @@ if __name__ == '__main__': const=SERVER_FTP, default=SERVER_HTTP, dest='server_type', help='FTP or HTTP server default HTTP') + option_parser.add_option('--forking', action='store_true', default=False, + dest='forking', + help='Serve each request in a separate process') option_parser.add_option('', '--port', default='8888', type='int', help='Port used by the server') option_parser.add_option('', '--data-dir', dest='data_dir', diff --git a/net/url_request/url_request_unittest.cc b/net/url_request/url_request_unittest.cc index 50b647c..64da9fe 100644 --- a/net/url_request/url_request_unittest.cc +++ b/net/url_request/url_request_unittest.cc @@ -116,14 +116,28 @@ scoped_refptr<net::UploadData> CreateSimpleUploadData(const char* data) { class URLRequestTest : public PlatformTest { }; -TEST_F(URLRequestTest, ProxyTunnelRedirectTest) { +class URLRequestTestHTTP : public URLRequestTest { + protected: + static void SetUpTestCase() { + server_ = HTTPTestServer::CreateForkingServer(L""); + } + + static void TearDownTestCase() { + server_ = NULL; + } + + static scoped_refptr<HTTPTestServer> server_; +}; + +// static +scoped_refptr<HTTPTestServer> URLRequestTestHTTP::server_; + +TEST_F(URLRequestTestHTTP, ProxyTunnelRedirectTest) { // In this unit test, we're using the HTTPTestServer as a proxy server and // issuing a CONNECT request with the magic host name "www.redirect.com". // The HTTPTestServer will return a 302 response, which we should not // follow. - scoped_refptr<HTTPTestServer> server = - HTTPTestServer::CreateServer(L"", NULL); - ASSERT_TRUE(NULL != server.get()); + ASSERT_TRUE(NULL != server_.get()); TestDelegate d; { URLRequest r(GURL("https://www.redirect.com/"), &d); @@ -144,13 +158,11 @@ TEST_F(URLRequestTest, ProxyTunnelRedirectTest) { } } -TEST_F(URLRequestTest, UnexpectedServerAuthTest) { +TEST_F(URLRequestTestHTTP, UnexpectedServerAuthTest) { // In this unit test, we're using the HTTPTestServer as a proxy server and // issuing a CONNECT request with the magic host name "www.server-auth.com". // The HTTPTestServer will return a 401 response, which we should balk at. - scoped_refptr<HTTPTestServer> server = - HTTPTestServer::CreateServer(L"", NULL); - ASSERT_TRUE(NULL != server.get()); + ASSERT_TRUE(NULL != server_.get()); TestDelegate d; { URLRequest r(GURL("https://www.server-auth.com/"), &d); @@ -168,13 +180,11 @@ TEST_F(URLRequestTest, UnexpectedServerAuthTest) { } } -TEST_F(URLRequestTest, GetTest_NoCache) { - scoped_refptr<HTTPTestServer> server = - HTTPTestServer::CreateServer(L"", NULL); - ASSERT_TRUE(NULL != server.get()); +TEST_F(URLRequestTestHTTP, GetTest_NoCache) { + ASSERT_TRUE(NULL != server_.get()); TestDelegate d; { - TestURLRequest r(server->TestServerPage(""), &d); + TestURLRequest r(server_->TestServerPage(""), &d); r.Start(); EXPECT_TRUE(r.is_pending()); @@ -190,13 +200,11 @@ TEST_F(URLRequestTest, GetTest_NoCache) { #endif } -TEST_F(URLRequestTest, GetTest) { - scoped_refptr<HTTPTestServer> server = - HTTPTestServer::CreateServer(L"", NULL); - ASSERT_TRUE(NULL != server.get()); +TEST_F(URLRequestTestHTTP, GetTest) { + ASSERT_TRUE(NULL != server_.get()); TestDelegate d; { - TestURLRequest r(server->TestServerPage(""), &d); + TestURLRequest r(server_->TestServerPage(""), &d); r.Start(); EXPECT_TRUE(r.is_pending()); @@ -213,6 +221,8 @@ TEST_F(URLRequestTest, GetTest) { } TEST_F(URLRequestTest, QuitTest) { + // Don't use shared server here because we order it to quit. + // It would impact other tests. scoped_refptr<HTTPTestServer> server = HTTPTestServer::CreateServer(L"", NULL); ASSERT_TRUE(NULL != server.get()); diff --git a/net/url_request/url_request_unittest.h b/net/url_request/url_request_unittest.h index 03d678d..24714ce 100644 --- a/net/url_request/url_request_unittest.h +++ b/net/url_request/url_request_unittest.h @@ -242,6 +242,9 @@ class BaseTestServer : public base::RefCounted<BaseTestServer> { : launcher_(connection_attempts, connection_timeout) { } public: + void set_forking(bool forking) { + launcher_.set_forking(forking); + } // Used with e.g. HTTPTestServer::SendQuit() bool WaitToFinish(int milliseconds) { @@ -380,6 +383,18 @@ class HTTPTestServer : public BaseTestServer { loop, 10, 1000); } + static scoped_refptr<HTTPTestServer> CreateForkingServer( + const std::wstring& document_root) { + scoped_refptr<HTTPTestServer> test_server = + new HTTPTestServer(10, 1000); + test_server->set_forking(true); + FilePath no_cert; + FilePath docroot = FilePath::FromWStringHack(document_root); + if (!StartTestServer(test_server.get(), docroot, no_cert, std::wstring())) + return NULL; + return test_server; + } + static scoped_refptr<HTTPTestServer> CreateServerWithFileRootURL( const std::wstring& document_root, const std::wstring& file_root_url, @@ -391,14 +406,20 @@ class HTTPTestServer : public BaseTestServer { test_server->loop_ = loop; FilePath no_cert; FilePath docroot = FilePath::FromWStringHack(document_root); - if (!test_server->Start(net::TestServerLauncher::ProtoHTTP, - kDefaultHostName, kHTTPDefaultPort, - docroot, no_cert, file_root_url)) { + if (!StartTestServer(test_server.get(), docroot, no_cert, file_root_url)) return NULL; - } return test_server; } + static bool StartTestServer(HTTPTestServer* server, + const FilePath& document_root, + const FilePath& cert_path, + const std::wstring& file_root_url) { + return server->Start(net::TestServerLauncher::ProtoHTTP, kDefaultHostName, + kHTTPDefaultPort, document_root, cert_path, + file_root_url, "", ""); + } + // A subclass may wish to send the request in a different manner virtual bool MakeGETRequest(const std::string& page_name) { const GURL& url = TestServerPage(page_name); |