diff options
author | phajdan.jr@chromium.org <phajdan.jr@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-07-23 20:38:56 +0000 |
---|---|---|
committer | phajdan.jr@chromium.org <phajdan.jr@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-07-23 20:38:56 +0000 |
commit | e70c6a8ce0d7a9e426c7247f5b771bed1b9eae4a (patch) | |
tree | fa50ca32a7fc32fd0adb7d1aebdb96a84e0477eb /net/url_request | |
parent | f324cce9f59858e24b59258271d186f84c3f3f1a (diff) | |
download | chromium_src-e70c6a8ce0d7a9e426c7247f5b771bed1b9eae4a.zip chromium_src-e70c6a8ce0d7a9e426c7247f5b771bed1b9eae4a.tar.gz chromium_src-e70c6a8ce0d7a9e426c7247f5b771bed1b9eae4a.tar.bz2 |
GTTF: test server cleanup:
- simplify the public interface
- remove unneeded methods
- make it easier to understand
TEST=none
BUG=49680
Review URL: http://codereview.chromium.org/2881028
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@53509 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net/url_request')
-rw-r--r-- | net/url_request/url_request_unittest.cc | 111 | ||||
-rw-r--r-- | net/url_request/url_request_unittest.h | 170 |
2 files changed, 88 insertions, 193 deletions
diff --git a/net/url_request/url_request_unittest.cc b/net/url_request/url_request_unittest.cc index bd4e56a..8c842fe 100644 --- a/net/url_request/url_request_unittest.cc +++ b/net/url_request/url_request_unittest.cc @@ -96,8 +96,7 @@ class URLRequestTest : public PlatformTest { class URLRequestTestHTTP : public URLRequestTest { protected: static void SetUpTestCase() { - server_ = HTTPTestServer::CreateForkingServer( - L"net/data/url_request_unittest/"); + server_ = HTTPTestServer::CreateServer(L"net/data/url_request_unittest/"); } static void TearDownTestCase() { @@ -262,13 +261,81 @@ TEST_F(URLRequestTestHTTP, HTTPSToHTTPRedirectNoRefererTest) { EXPECT_EQ(std::string(), req.referrer()); } +namespace { + +// Used by MakeGETRequest to implement sync load behavior. +class SyncTestDelegate : public TestDelegate { + public: + SyncTestDelegate() : event_(false, false), success_(false) { + } + virtual void OnResponseCompleted(URLRequest* request) { + MessageLoop::current()->DeleteSoon(FROM_HERE, request); + success_ = request->status().is_success(); + event_.Signal(); + } + bool Wait(int64 secs) { + return event_.TimedWait(TimeDelta::FromSeconds(secs)); + } + bool did_succeed() const { return success_; } + private: + base::WaitableEvent event_; + bool success_; + DISALLOW_COPY_AND_ASSIGN(SyncTestDelegate); +}; + +void StartGETRequest(const GURL& url, URLRequest::Delegate* delegate) { + URLRequest* request = new URLRequest(url, delegate); + request->set_context(new TestURLRequestContext()); + request->set_method("GET"); + request->Start(); + EXPECT_TRUE(request->is_pending()); +} + +bool MakeGETRequest(const GURL& url) { + // Spin up a background thread for this request so that we have access to + // an IO message loop, and in cases where this thread already has an IO + // message loop, we also want to avoid spinning a nested message loop. + SyncTestDelegate d; + { + base::Thread io_thread("MakeGETRequest"); + base::Thread::Options options; + options.message_loop_type = MessageLoop::TYPE_IO; + io_thread.StartWithOptions(options); + io_thread.message_loop()->PostTask(FROM_HERE, NewRunnableFunction( + &StartGETRequest, url, &d)); + + const int kWaitSeconds = 30; + if (!d.Wait(kWaitSeconds)) + return false; + } + return d.did_succeed(); +} + +} // namespace + +// Some tests use browser javascript to fetch a 'kill' url that causes +// the server to exit by itself (rather than letting TestServerLauncher's +// destructor kill it). We now unit test this mechanism. 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); + scoped_refptr<HTTPTestServer> server(HTTPTestServer::CreateServer(L"")); ASSERT_TRUE(NULL != server.get()); - server->SendQuit(); + // Append the time to avoid problems where the kill page + // is being cached rather than being executed on the server + std::string page_name = StringPrintf("kill?%u", + static_cast<int>(base::Time::Now().ToInternalValue())); + int retry_count = 5; + while (retry_count > 0) { + bool r = MakeGETRequest(server->TestServerPage(page_name)); + // BUG #1048625 causes the kill GET to fail. For now we just retry. + // Once the bug is fixed, we should remove the while loop and put back + // the following DCHECK. + // DCHECK(r); + if (r) + break; + retry_count--; + } + // Make sure we were successful in stopping the testserver. + EXPECT_LT(0, retry_count); EXPECT_TRUE(server->WaitToFinish(20000)); } @@ -1189,8 +1256,7 @@ TEST_F(URLRequestTestHTTP, BasicAuthWithCookies) { } TEST_F(URLRequestTest, DoNotSendCookies) { - scoped_refptr<HTTPTestServer> server = - HTTPTestServer::CreateServer(L"", NULL); + scoped_refptr<HTTPTestServer> server(HTTPTestServer::CreateServer(L"")); ASSERT_TRUE(NULL != server.get()); scoped_refptr<URLRequestContext> context = new TestURLRequestContext(); @@ -1238,8 +1304,7 @@ TEST_F(URLRequestTest, DoNotSendCookies) { } TEST_F(URLRequestTest, DoNotSaveCookies) { - scoped_refptr<HTTPTestServer> server = - HTTPTestServer::CreateServer(L"", NULL); + scoped_refptr<HTTPTestServer> server(HTTPTestServer::CreateServer(L"")); ASSERT_TRUE(NULL != server.get()); scoped_refptr<URLRequestContext> context = new TestURLRequestContext(); @@ -1294,8 +1359,7 @@ TEST_F(URLRequestTest, DoNotSaveCookies) { } TEST_F(URLRequestTest, DoNotSendCookies_ViaPolicy) { - scoped_refptr<HTTPTestServer> server = - HTTPTestServer::CreateServer(L"", NULL); + scoped_refptr<HTTPTestServer> server(HTTPTestServer::CreateServer(L"")); ASSERT_TRUE(NULL != server.get()); scoped_refptr<TestURLRequestContext> context = new TestURLRequestContext(); @@ -1348,8 +1412,7 @@ TEST_F(URLRequestTest, DoNotSendCookies_ViaPolicy) { } TEST_F(URLRequestTest, DoNotSaveCookies_ViaPolicy) { - scoped_refptr<HTTPTestServer> server = - HTTPTestServer::CreateServer(L"", NULL); + scoped_refptr<HTTPTestServer> server(HTTPTestServer::CreateServer(L"")); ASSERT_TRUE(NULL != server.get()); scoped_refptr<TestURLRequestContext> context = new TestURLRequestContext(); @@ -1405,8 +1468,7 @@ TEST_F(URLRequestTest, DoNotSaveCookies_ViaPolicy) { } TEST_F(URLRequestTest, DoNotSendCookies_ViaPolicy_Async) { - scoped_refptr<HTTPTestServer> server = - HTTPTestServer::CreateServer(L"", NULL); + scoped_refptr<HTTPTestServer> server(HTTPTestServer::CreateServer(L"")); ASSERT_TRUE(NULL != server.get()); scoped_refptr<TestURLRequestContext> context = new TestURLRequestContext(); @@ -1460,8 +1522,7 @@ TEST_F(URLRequestTest, DoNotSendCookies_ViaPolicy_Async) { } TEST_F(URLRequestTest, DoNotSaveCookies_ViaPolicy_Async) { - scoped_refptr<HTTPTestServer> server = - HTTPTestServer::CreateServer(L"", NULL); + scoped_refptr<HTTPTestServer> server(HTTPTestServer::CreateServer(L"")); ASSERT_TRUE(NULL != server.get()); scoped_refptr<TestURLRequestContext> context = new TestURLRequestContext(); @@ -1517,8 +1578,7 @@ TEST_F(URLRequestTest, DoNotSaveCookies_ViaPolicy_Async) { } TEST_F(URLRequestTest, CancelTest_During_CookiePolicy) { - scoped_refptr<HTTPTestServer> server = - HTTPTestServer::CreateServer(L"", NULL); + scoped_refptr<HTTPTestServer> server(HTTPTestServer::CreateServer(L"")); ASSERT_TRUE(NULL != server.get()); scoped_refptr<TestURLRequestContext> context = new TestURLRequestContext(); @@ -1548,8 +1608,7 @@ TEST_F(URLRequestTest, CancelTest_During_CookiePolicy) { } TEST_F(URLRequestTest, CancelTest_During_OnGetCookies) { - scoped_refptr<HTTPTestServer> server = - HTTPTestServer::CreateServer(L"", NULL); + scoped_refptr<HTTPTestServer> server(HTTPTestServer::CreateServer(L"")); ASSERT_TRUE(NULL != server.get()); scoped_refptr<TestURLRequestContext> context = new TestURLRequestContext(); @@ -1577,8 +1636,7 @@ TEST_F(URLRequestTest, CancelTest_During_OnGetCookies) { } TEST_F(URLRequestTest, CancelTest_During_OnSetCookie) { - scoped_refptr<HTTPTestServer> server = - HTTPTestServer::CreateServer(L"", NULL); + scoped_refptr<HTTPTestServer> server(HTTPTestServer::CreateServer(L"")); ASSERT_TRUE(NULL != server.get()); scoped_refptr<TestURLRequestContext> context = new TestURLRequestContext(); @@ -1611,8 +1669,7 @@ TEST_F(URLRequestTest, CancelTest_During_OnSetCookie) { } TEST_F(URLRequestTest, CookiePolicy_ForceSession) { - scoped_refptr<HTTPTestServer> server = - HTTPTestServer::CreateServer(L"", NULL); + scoped_refptr<HTTPTestServer> server(HTTPTestServer::CreateServer(L"")); ASSERT_TRUE(NULL != server.get()); scoped_refptr<TestURLRequestContext> context = new TestURLRequestContext(); diff --git a/net/url_request/url_request_unittest.h b/net/url_request/url_request_unittest.h index 8f090ef..969e034 100644 --- a/net/url_request/url_request_unittest.h +++ b/net/url_request/url_request_unittest.h @@ -26,7 +26,6 @@ #include "net/base/host_resolver.h" #include "net/base/io_buffer.h" #include "net/base/net_errors.h" -#include "net/base/net_test_constants.h" #include "net/base/ssl_config_service_defaults.h" #include "net/disk_cache/disk_cache.h" #include "net/ftp/ftp_network_layer.h" @@ -402,15 +401,8 @@ class TestDelegate : public URLRequest::Delegate { class BaseTestServer : public base::RefCounted<BaseTestServer> { protected: BaseTestServer() {} - BaseTestServer(int connection_attempts, int connection_timeout) - : 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) { return launcher_.WaitToFinish(milliseconds); } @@ -442,8 +434,6 @@ class BaseTestServer : public base::RefCounted<BaseTestServer> { "@" + host_name_ + ":" + port_str_ + "/" + path); } - virtual bool MakeGETRequest(const std::string& page_name) = 0; - FilePath GetDataDirectory() { return launcher_.GetDocumentRootPath(); } @@ -473,29 +463,6 @@ class BaseTestServer : public base::RefCounted<BaseTestServer> { return true; } - // Used by MakeGETRequest to implement sync load behavior. - class SyncTestDelegate : public TestDelegate { - public: - SyncTestDelegate() : event_(false, false), success_(false) { - } - virtual void OnResponseCompleted(URLRequest* request) { - MessageLoop::current()->DeleteSoon(FROM_HERE, request); - success_ = request->status().is_success(); - event_.Signal(); - } - bool Wait(int64 secs) { - TimeDelta td = TimeDelta::FromSeconds(secs); - if (event_.TimedWait(td)) - return true; - return false; - } - bool did_succeed() const { return success_; } - private: - base::WaitableEvent event_; - bool success_; - DISALLOW_COPY_AND_ASSIGN(SyncTestDelegate); - }; - net::TestServerLauncher launcher_; std::string scheme_; std::string host_name_; @@ -507,65 +474,20 @@ class BaseTestServer : public base::RefCounted<BaseTestServer> { // HTTP class HTTPTestServer : public BaseTestServer { protected: - explicit HTTPTestServer() : loop_(NULL) { - } - - explicit HTTPTestServer(int connection_attempts, int connection_timeout) - : BaseTestServer(connection_attempts, connection_timeout), loop_(NULL) { - } - - virtual ~HTTPTestServer() {} + HTTPTestServer() {} public: // Creates and returns a new HTTPTestServer. If |loop| is non-null, requests // are serviced on it, otherwise a new thread and message loop are created. static scoped_refptr<HTTPTestServer> CreateServer( - const std::wstring& document_root, - MessageLoop* loop) { - return CreateServerWithFileRootURL(document_root, std::wstring(), loop); - } - - static scoped_refptr<HTTPTestServer> CreateServer( - const std::wstring& document_root, - MessageLoop* loop, - int connection_attempts, - int connection_timeout) { - return CreateServerWithFileRootURL(document_root, std::wstring(), loop, - connection_attempts, - connection_timeout); - } - - static scoped_refptr<HTTPTestServer> CreateServerWithFileRootURL( - const std::wstring& document_root, - const std::wstring& file_root_url, - MessageLoop* loop) { - return CreateServerWithFileRootURL(document_root, file_root_url, loop, - net::kDefaultTestConnectionAttempts, - net::kDefaultTestConnectionTimeout); - } - - static scoped_refptr<HTTPTestServer> CreateForkingServer( const std::wstring& document_root) { - scoped_refptr<HTTPTestServer> test_server = - new HTTPTestServer(net::kDefaultTestConnectionAttempts, - net::kDefaultTestConnectionTimeout); - 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; + return CreateServerWithFileRootURL(document_root, std::wstring()); } static scoped_refptr<HTTPTestServer> CreateServerWithFileRootURL( const std::wstring& document_root, - const std::wstring& file_root_url, - MessageLoop* loop, - int connection_attempts, - int connection_timeout) { - scoped_refptr<HTTPTestServer> test_server = - new HTTPTestServer(connection_attempts, connection_timeout); - test_server->loop_ = loop; + const std::wstring& file_root_url) { + scoped_refptr<HTTPTestServer> test_server(new HTTPTestServer()); FilePath no_cert; FilePath docroot = FilePath::FromWStringHack(document_root); if (!StartTestServer(test_server.get(), docroot, no_cert, file_root_url)) @@ -582,75 +504,7 @@ class HTTPTestServer : public BaseTestServer { 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); - - // Spin up a background thread for this request so that we have access to - // an IO message loop, and in cases where this thread already has an IO - // message loop, we also want to avoid spinning a nested message loop. - SyncTestDelegate d; - { - MessageLoop* loop = loop_; - scoped_ptr<base::Thread> io_thread; - - if (!loop) { - io_thread.reset(new base::Thread("MakeGETRequest")); - base::Thread::Options options; - options.message_loop_type = MessageLoop::TYPE_IO; - io_thread->StartWithOptions(options); - loop = io_thread->message_loop(); - } - loop->PostTask(FROM_HERE, NewRunnableFunction( - &HTTPTestServer::StartGETRequest, url, &d)); - - // Build bot wait for only 300 seconds we should ensure wait do not take - // more than 300 seconds - if (!d.Wait(250)) - return false; - } - return d.did_succeed(); - } - - static void StartGETRequest(const GURL& url, URLRequest::Delegate* delegate) { - URLRequest* request = new URLRequest(url, delegate); - request->set_context(new TestURLRequestContext()); - request->set_method("GET"); - request->Start(); - EXPECT_TRUE(request->is_pending()); - } - - // Some tests use browser javascript to fetch a 'kill' url that causes - // the server to exit by itself (rather than letting TestServerLauncher's - // destructor kill it). - // This method does the same thing so we can unit test that mechanism. - // You can then use WaitToFinish() to sleep until the server terminates. - void SendQuit() { - // Append the time to avoid problems where the kill page - // is being cached rather than being executed on the server - std::string page_name = StringPrintf("kill?%u", - static_cast<int>(base::Time::Now().ToInternalValue())); - int retry_count = 5; - while (retry_count > 0) { - bool r = MakeGETRequest(page_name); - // BUG #1048625 causes the kill GET to fail. For now we just retry. - // Once the bug is fixed, we should remove the while loop and put back - // the following DCHECK. - // DCHECK(r); - if (r) - break; - retry_count--; - } - // Make sure we were successful in stopping the testserver. - DCHECK_GT(retry_count, 0); - } - virtual std::string scheme() { return "http"; } - - private: - // If non-null a background thread isn't created and instead this message loop - // is used. - MessageLoop* loop_; }; //----------------------------------------------------------------------------- @@ -750,22 +604,6 @@ class FTPTestServer : public BaseTestServer { return test_server; } - virtual bool MakeGETRequest(const std::string& page_name) { - const GURL& url = TestServerPage(page_name); - TestDelegate d; - URLRequest request(url, &d); - request.set_context(new TestURLRequestContext()); - request.set_method("GET"); - request.Start(); - EXPECT_TRUE(request.is_pending()); - - MessageLoop::current()->Run(); - if (request.is_pending()) - return false; - - return true; - } - private: ~FTPTestServer() {} }; |