| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/string_util.h" |
| 5 #include "base/waitable_event.h" | 6 #include "base/waitable_event.h" |
| 6 #include "googleurl/src/gurl.h" | 7 #include "googleurl/src/gurl.h" |
| 7 #include "net/base/net_log.h" | 8 #include "net/base/net_log.h" |
| 8 #include "net/base/net_log_unittest.h" | 9 #include "net/base/net_log_unittest.h" |
| 9 #include "net/base/net_errors.h" | 10 #include "net/base/net_errors.h" |
| 10 #include "net/base/test_completion_callback.h" | 11 #include "net/base/test_completion_callback.h" |
| 11 #include "net/proxy/proxy_info.h" | 12 #include "net/proxy/proxy_info.h" |
| 12 #include "net/proxy/single_threaded_proxy_resolver.h" | 13 #include "net/proxy/single_threaded_proxy_resolver.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
| 14 | 15 |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 48 | 49 |
| 49 // Return a success code which represents the request's order. | 50 // Return a success code which represents the request's order. |
| 50 return request_count_++; | 51 return request_count_++; |
| 51 } | 52 } |
| 52 | 53 |
| 53 virtual void CancelRequest(RequestHandle request) { | 54 virtual void CancelRequest(RequestHandle request) { |
| 54 NOTREACHED(); | 55 NOTREACHED(); |
| 55 } | 56 } |
| 56 | 57 |
| 57 virtual int SetPacScript(const GURL& pac_url, | 58 virtual int SetPacScript(const GURL& pac_url, |
| 58 const std::string& bytes, | 59 const string16& text, |
| 59 CompletionCallback* callback) { | 60 CompletionCallback* callback) { |
| 60 CheckIsOnWorkerThread(); | 61 CheckIsOnWorkerThread(); |
| 61 last_pac_bytes_ = bytes; | 62 last_pac_script_ = text; |
| 62 return OK; | 63 return OK; |
| 63 } | 64 } |
| 64 | 65 |
| 65 virtual void PurgeMemory() { | 66 virtual void PurgeMemory() { |
| 66 CheckIsOnWorkerThread(); | 67 CheckIsOnWorkerThread(); |
| 67 ++purge_count_; | 68 ++purge_count_; |
| 68 } | 69 } |
| 69 | 70 |
| 70 int purge_count() const { return purge_count_; } | 71 int purge_count() const { return purge_count_; } |
| 71 | 72 |
| 72 const std::string& last_pac_bytes() const { return last_pac_bytes_; } | 73 const string16& last_pac_script() const { return last_pac_script_; } |
| 73 | 74 |
| 74 void SetResolveLatency(int latency_ms) { | 75 void SetResolveLatency(int latency_ms) { |
| 75 resolve_latency_ms_ = latency_ms; | 76 resolve_latency_ms_ = latency_ms; |
| 76 } | 77 } |
| 77 | 78 |
| 78 private: | 79 private: |
| 79 void CheckIsOnWorkerThread() { | 80 void CheckIsOnWorkerThread() { |
| 80 // We should be running on the worker thread -- while we don't know the | 81 // We should be running on the worker thread -- while we don't know the |
| 81 // message loop of SingleThreadedProxyResolver's worker thread, we do | 82 // message loop of SingleThreadedProxyResolver's worker thread, we do |
| 82 // know that it is going to be distinct from the loop running the | 83 // know that it is going to be distinct from the loop running the |
| 83 // test, so at least make sure it isn't the main loop. | 84 // test, so at least make sure it isn't the main loop. |
| 84 EXPECT_NE(MessageLoop::current(), wrong_loop_); | 85 EXPECT_NE(MessageLoop::current(), wrong_loop_); |
| 85 } | 86 } |
| 86 | 87 |
| 87 MessageLoop* wrong_loop_; | 88 MessageLoop* wrong_loop_; |
| 88 int request_count_; | 89 int request_count_; |
| 89 int purge_count_; | 90 int purge_count_; |
| 90 std::string last_pac_bytes_; | 91 string16 last_pac_script_; |
| 91 int resolve_latency_ms_; | 92 int resolve_latency_ms_; |
| 92 }; | 93 }; |
| 93 | 94 |
| 94 | 95 |
| 95 // A mock synchronous ProxyResolver which can be set to block upon reaching | 96 // A mock synchronous ProxyResolver which can be set to block upon reaching |
| 96 // GetProxyForURL(). | 97 // GetProxyForURL(). |
| 97 // TODO(eroman): WaitUntilBlocked() *must* be called before calling Unblock(), | 98 // TODO(eroman): WaitUntilBlocked() *must* be called before calling Unblock(), |
| 98 // otherwise there will be a race on |should_block_| since it is | 99 // otherwise there will be a race on |should_block_| since it is |
| 99 // read without any synchronization. | 100 // read without any synchronization. |
| 100 class BlockableProxyResolver : public MockProxyResolver { | 101 class BlockableProxyResolver : public MockProxyResolver { |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 144 MockProxyResolver* mock = new MockProxyResolver; | 145 MockProxyResolver* mock = new MockProxyResolver; |
| 145 SingleThreadedProxyResolver resolver(mock); | 146 SingleThreadedProxyResolver resolver(mock); |
| 146 | 147 |
| 147 int rv; | 148 int rv; |
| 148 | 149 |
| 149 EXPECT_TRUE(resolver.expects_pac_bytes()); | 150 EXPECT_TRUE(resolver.expects_pac_bytes()); |
| 150 | 151 |
| 151 // Call SetPacScriptByData() -- verify that it reaches the synchronous | 152 // Call SetPacScriptByData() -- verify that it reaches the synchronous |
| 152 // resolver. | 153 // resolver. |
| 153 TestCompletionCallback set_script_callback; | 154 TestCompletionCallback set_script_callback; |
| 154 rv = resolver.SetPacScriptByData("pac script bytes", &set_script_callback); | 155 rv = resolver.SetPacScriptByData(ASCIIToUTF16("pac script bytes"), |
| 156 &set_script_callback); |
| 155 EXPECT_EQ(ERR_IO_PENDING, rv); | 157 EXPECT_EQ(ERR_IO_PENDING, rv); |
| 156 EXPECT_EQ(OK, set_script_callback.WaitForResult()); | 158 EXPECT_EQ(OK, set_script_callback.WaitForResult()); |
| 157 EXPECT_EQ("pac script bytes", mock->last_pac_bytes()); | 159 EXPECT_EQ(ASCIIToUTF16("pac script bytes"), mock->last_pac_script()); |
| 158 | 160 |
| 159 // Start request 0. | 161 // Start request 0. |
| 160 TestCompletionCallback callback0; | 162 TestCompletionCallback callback0; |
| 161 CapturingBoundNetLog log0(CapturingNetLog::kUnbounded); | 163 CapturingBoundNetLog log0(CapturingNetLog::kUnbounded); |
| 162 ProxyInfo results0; | 164 ProxyInfo results0; |
| 163 rv = resolver.GetProxyForURL( | 165 rv = resolver.GetProxyForURL( |
| 164 GURL("http://request0"), &results0, &callback0, NULL, log0.bound()); | 166 GURL("http://request0"), &results0, &callback0, NULL, log0.bound()); |
| 165 EXPECT_EQ(ERR_IO_PENDING, rv); | 167 EXPECT_EQ(ERR_IO_PENDING, rv); |
| 166 | 168 |
| 167 // Wait for request 0 to finish. | 169 // Wait for request 0 to finish. |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 209 EXPECT_EQ("PROXY request3:80", results3.ToPacString()); | 211 EXPECT_EQ("PROXY request3:80", results3.ToPacString()); |
| 210 | 212 |
| 211 // Ensure that PurgeMemory() reaches the wrapped resolver and happens on the | 213 // Ensure that PurgeMemory() reaches the wrapped resolver and happens on the |
| 212 // right thread. | 214 // right thread. |
| 213 EXPECT_EQ(0, mock->purge_count()); | 215 EXPECT_EQ(0, mock->purge_count()); |
| 214 resolver.PurgeMemory(); | 216 resolver.PurgeMemory(); |
| 215 // There is no way to get a callback directly when PurgeMemory() completes, so | 217 // There is no way to get a callback directly when PurgeMemory() completes, so |
| 216 // we queue up a dummy request after the PurgeMemory() call and wait until it | 218 // we queue up a dummy request after the PurgeMemory() call and wait until it |
| 217 // finishes to ensure PurgeMemory() has had a chance to run. | 219 // finishes to ensure PurgeMemory() has had a chance to run. |
| 218 TestCompletionCallback dummy_callback; | 220 TestCompletionCallback dummy_callback; |
| 219 rv = resolver.SetPacScriptByData("dummy", &dummy_callback); | 221 rv = resolver.SetPacScriptByData(ASCIIToUTF16("dummy"), &dummy_callback); |
| 220 EXPECT_EQ(OK, dummy_callback.WaitForResult()); | 222 EXPECT_EQ(OK, dummy_callback.WaitForResult()); |
| 221 EXPECT_EQ(1, mock->purge_count()); | 223 EXPECT_EQ(1, mock->purge_count()); |
| 222 } | 224 } |
| 223 | 225 |
| 224 // Tests that the NetLog is updated to include the time the request was waiting | 226 // Tests that the NetLog is updated to include the time the request was waiting |
| 225 // to be scheduled to a thread. | 227 // to be scheduled to a thread. |
| 226 TEST(SingleThreadedProxyResolverTest, UpdatesNetLogWithThreadWait) { | 228 TEST(SingleThreadedProxyResolverTest, UpdatesNetLogWithThreadWait) { |
| 227 BlockableProxyResolver* mock = new BlockableProxyResolver; | 229 BlockableProxyResolver* mock = new BlockableProxyResolver; |
| 228 SingleThreadedProxyResolver resolver(mock); | 230 SingleThreadedProxyResolver resolver(mock); |
| 229 | 231 |
| (...skipping 200 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 430 TestCompletionCallback callback0; | 432 TestCompletionCallback callback0; |
| 431 ProxyInfo results0; | 433 ProxyInfo results0; |
| 432 rv = resolver.GetProxyForURL( | 434 rv = resolver.GetProxyForURL( |
| 433 GURL("http://request0"), &results0, &callback0, &request0, BoundNetLog()); | 435 GURL("http://request0"), &results0, &callback0, &request0, BoundNetLog()); |
| 434 EXPECT_EQ(ERR_IO_PENDING, rv); | 436 EXPECT_EQ(ERR_IO_PENDING, rv); |
| 435 | 437 |
| 436 // Wait until requests 0 reaches the worker thread. | 438 // Wait until requests 0 reaches the worker thread. |
| 437 mock->WaitUntilBlocked(); | 439 mock->WaitUntilBlocked(); |
| 438 | 440 |
| 439 TestCompletionCallback set_pac_script_callback; | 441 TestCompletionCallback set_pac_script_callback; |
| 440 rv = resolver.SetPacScriptByData("data", &set_pac_script_callback); | 442 rv = resolver.SetPacScriptByData(ASCIIToUTF16("data"), |
| 443 &set_pac_script_callback); |
| 441 EXPECT_EQ(ERR_IO_PENDING, rv); | 444 EXPECT_EQ(ERR_IO_PENDING, rv); |
| 442 | 445 |
| 443 // Cancel the SetPacScriptByData request (it can't have finished yet, | 446 // Cancel the SetPacScriptByData request (it can't have finished yet, |
| 444 // since the single-thread is currently blocked). | 447 // since the single-thread is currently blocked). |
| 445 resolver.CancelSetPacScript(); | 448 resolver.CancelSetPacScript(); |
| 446 | 449 |
| 447 // Start 1 more request. | 450 // Start 1 more request. |
| 448 | 451 |
| 449 TestCompletionCallback callback1; | 452 TestCompletionCallback callback1; |
| 450 ProxyInfo results1; | 453 ProxyInfo results1; |
| (...skipping 13 matching lines...) Expand all Loading... |
| 464 rv = callback1.WaitForResult(); | 467 rv = callback1.WaitForResult(); |
| 465 EXPECT_EQ(1, rv); | 468 EXPECT_EQ(1, rv); |
| 466 EXPECT_EQ("PROXY request1:80", results1.ToPacString()); | 469 EXPECT_EQ("PROXY request1:80", results1.ToPacString()); |
| 467 | 470 |
| 468 // The SetPacScript callback should never have been completed. | 471 // The SetPacScript callback should never have been completed. |
| 469 EXPECT_FALSE(set_pac_script_callback.have_result()); | 472 EXPECT_FALSE(set_pac_script_callback.have_result()); |
| 470 } | 473 } |
| 471 | 474 |
| 472 } // namespace | 475 } // namespace |
| 473 } // namespace net | 476 } // namespace net |
| OLD | NEW |