OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "net/socket/client_socket_pool_base.h" | 5 #include "net/socket/client_socket_pool_base.h" |
6 | 6 |
7 #include "base/callback.h" | 7 #include "base/callback.h" |
8 #include "base/compiler_specific.h" | 8 #include "base/compiler_specific.h" |
9 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
10 #include "base/memory/scoped_vector.h" | 10 #include "base/memory/scoped_vector.h" |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
56 }; | 56 }; |
57 typedef ClientSocketPoolBase<TestSocketParams> TestClientSocketPoolBase; | 57 typedef ClientSocketPoolBase<TestSocketParams> TestClientSocketPoolBase; |
58 | 58 |
59 class MockClientSocket : public StreamSocket { | 59 class MockClientSocket : public StreamSocket { |
60 public: | 60 public: |
61 MockClientSocket() : connected_(false), was_used_to_convey_data_(false), | 61 MockClientSocket() : connected_(false), was_used_to_convey_data_(false), |
62 num_bytes_read_(0) {} | 62 num_bytes_read_(0) {} |
63 | 63 |
64 // Socket implementation. | 64 // Socket implementation. |
65 virtual int Read( | 65 virtual int Read( |
66 IOBuffer* /* buf */, int len, OldCompletionCallback* /* callback */) { | 66 IOBuffer* /* buf */, int len, |
67 num_bytes_read_ += len; | 67 const CompletionCallback& /* callback */) OVERRIDE { |
68 return len; | |
69 } | |
70 virtual int Read( | |
71 IOBuffer* /* buf */, int len, const CompletionCallback& /* callback */) { | |
72 num_bytes_read_ += len; | 68 num_bytes_read_ += len; |
73 return len; | 69 return len; |
74 } | 70 } |
75 | 71 |
76 virtual int Write( | 72 virtual int Write( |
77 IOBuffer* /* buf */, int len, OldCompletionCallback* /* callback */) { | 73 IOBuffer* /* buf */, int len, |
| 74 const CompletionCallback& /* callback */) OVERRIDE { |
78 was_used_to_convey_data_ = true; | 75 was_used_to_convey_data_ = true; |
79 return len; | 76 return len; |
80 } | 77 } |
81 virtual bool SetReceiveBufferSize(int32 size) { return true; } | 78 virtual bool SetReceiveBufferSize(int32 size) { return true; } |
82 virtual bool SetSendBufferSize(int32 size) { return true; } | 79 virtual bool SetSendBufferSize(int32 size) { return true; } |
83 | 80 |
84 // StreamSocket implementation. | 81 // StreamSocket implementation. |
85 virtual int Connect(OldCompletionCallback* callback) { | 82 virtual int Connect(const CompletionCallback& callback) OVERRIDE { |
86 connected_ = true; | 83 connected_ = true; |
87 return OK; | 84 return OK; |
88 } | 85 } |
89 virtual int Connect(const net::CompletionCallback& callback) { | |
90 connected_ = true; | |
91 return OK; | |
92 } | |
93 | 86 |
94 virtual void Disconnect() { connected_ = false; } | 87 virtual void Disconnect() { connected_ = false; } |
95 virtual bool IsConnected() const { return connected_; } | 88 virtual bool IsConnected() const { return connected_; } |
96 virtual bool IsConnectedAndIdle() const { return connected_; } | 89 virtual bool IsConnectedAndIdle() const { return connected_; } |
97 | 90 |
98 virtual int GetPeerAddress(AddressList* /* address */) const { | 91 virtual int GetPeerAddress(AddressList* /* address */) const { |
99 return ERR_UNEXPECTED; | 92 return ERR_UNEXPECTED; |
100 } | 93 } |
101 | 94 |
102 virtual int GetLocalAddress(IPEndPoint* /* address */) const { | 95 virtual int GetLocalAddress(IPEndPoint* /* address */) const { |
(...skipping 218 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
321 set_socket(NULL); | 314 set_socket(NULL); |
322 return ERR_FAILED; | 315 return ERR_FAILED; |
323 } | 316 } |
324 } | 317 } |
325 | 318 |
326 void set_load_state(LoadState load_state) { load_state_ = load_state; } | 319 void set_load_state(LoadState load_state) { load_state_ = load_state; } |
327 | 320 |
328 int DoConnect(bool succeed, bool was_async, bool recoverable) { | 321 int DoConnect(bool succeed, bool was_async, bool recoverable) { |
329 int result = OK; | 322 int result = OK; |
330 if (succeed) { | 323 if (succeed) { |
331 socket()->Connect(NULL); | 324 socket()->Connect(CompletionCallback()); |
332 } else if (recoverable) { | 325 } else if (recoverable) { |
333 result = ERR_PROXY_AUTH_REQUESTED; | 326 result = ERR_PROXY_AUTH_REQUESTED; |
334 } else { | 327 } else { |
335 result = ERR_CONNECTION_FAILED; | 328 result = ERR_CONNECTION_FAILED; |
336 set_socket(NULL); | 329 set_socket(NULL); |
337 } | 330 } |
338 | 331 |
339 if (was_async) | 332 if (was_async) |
340 NotifyDelegateOfCompletion(result); | 333 NotifyDelegateOfCompletion(result); |
341 return result; | 334 return result; |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
377 | 370 |
378 virtual ~TestConnectJobFactory() {} | 371 virtual ~TestConnectJobFactory() {} |
379 | 372 |
380 void set_job_type(TestConnectJob::JobType job_type) { job_type_ = job_type; } | 373 void set_job_type(TestConnectJob::JobType job_type) { job_type_ = job_type; } |
381 | 374 |
382 void set_timeout_duration(base::TimeDelta timeout_duration) { | 375 void set_timeout_duration(base::TimeDelta timeout_duration) { |
383 timeout_duration_ = timeout_duration; | 376 timeout_duration_ = timeout_duration; |
384 } | 377 } |
385 | 378 |
386 // ConnectJobFactory implementation. | 379 // ConnectJobFactory implementation. |
| 380 |
387 virtual ConnectJob* NewConnectJob( | 381 virtual ConnectJob* NewConnectJob( |
388 const std::string& group_name, | 382 const std::string& group_name, |
389 const TestClientSocketPoolBase::Request& request, | 383 const TestClientSocketPoolBase::Request& request, |
390 ConnectJob::Delegate* delegate) const { | 384 ConnectJob::Delegate* delegate) const { |
391 return new TestConnectJob(job_type_, | 385 return new TestConnectJob(job_type_, |
392 group_name, | 386 group_name, |
393 request, | 387 request, |
394 timeout_duration_, | 388 timeout_duration_, |
395 delegate, | 389 delegate, |
396 client_socket_factory_, | 390 client_socket_factory_, |
(...skipping 273 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
670 EXPECT_EQ(OK, StartRequest("a", kDefaultPriority)); | 664 EXPECT_EQ(OK, StartRequest("a", kDefaultPriority)); |
671 EXPECT_EQ(OK, StartRequest("a", kDefaultPriority)); | 665 EXPECT_EQ(OK, StartRequest("a", kDefaultPriority)); |
672 | 666 |
673 std::map<int, StreamSocket*> sockets_; | 667 std::map<int, StreamSocket*> sockets_; |
674 for (size_t i = 0; i < test_base_.requests_size(); i++) { | 668 for (size_t i = 0; i < test_base_.requests_size(); i++) { |
675 TestSocketRequest* req = test_base_.request(i); | 669 TestSocketRequest* req = test_base_.request(i); |
676 StreamSocket* s = req->handle()->socket(); | 670 StreamSocket* s = req->handle()->socket(); |
677 MockClientSocket* sock = static_cast<MockClientSocket*>(s); | 671 MockClientSocket* sock = static_cast<MockClientSocket*>(s); |
678 CHECK(sock); | 672 CHECK(sock); |
679 sockets_[i] = sock; | 673 sockets_[i] = sock; |
680 sock->Read(NULL, 1024 - i, NULL); | 674 sock->Read(NULL, 1024 - i, CompletionCallback()); |
681 } | 675 } |
682 | 676 |
683 ReleaseAllConnections(ClientSocketPoolTest::KEEP_ALIVE); | 677 ReleaseAllConnections(ClientSocketPoolTest::KEEP_ALIVE); |
684 | 678 |
685 EXPECT_EQ(OK, StartRequest("a", kDefaultPriority)); | 679 EXPECT_EQ(OK, StartRequest("a", kDefaultPriority)); |
686 TestSocketRequest* req = test_base_.request(test_base_.requests_size() - 1); | 680 TestSocketRequest* req = test_base_.request(test_base_.requests_size() - 1); |
687 | 681 |
688 // First socket is warmest. | 682 // First socket is warmest. |
689 EXPECT_EQ(sockets_[0], req->handle()->socket()); | 683 EXPECT_EQ(sockets_[0], req->handle()->socket()); |
690 | 684 |
(...skipping 15 matching lines...) Expand all Loading... |
706 EXPECT_EQ(OK, StartRequest("a", kDefaultPriority)); | 700 EXPECT_EQ(OK, StartRequest("a", kDefaultPriority)); |
707 EXPECT_EQ(OK, StartRequest("a", kDefaultPriority)); | 701 EXPECT_EQ(OK, StartRequest("a", kDefaultPriority)); |
708 | 702 |
709 std::map<int, StreamSocket*> sockets_; | 703 std::map<int, StreamSocket*> sockets_; |
710 for (size_t i = 0; i < test_base_.requests_size(); i++) { | 704 for (size_t i = 0; i < test_base_.requests_size(); i++) { |
711 TestSocketRequest* req = test_base_.request(i); | 705 TestSocketRequest* req = test_base_.request(i); |
712 StreamSocket* s = req->handle()->socket(); | 706 StreamSocket* s = req->handle()->socket(); |
713 MockClientSocket* sock = static_cast<MockClientSocket*>(s); | 707 MockClientSocket* sock = static_cast<MockClientSocket*>(s); |
714 CHECK(sock); | 708 CHECK(sock); |
715 sockets_[i] = sock; | 709 sockets_[i] = sock; |
716 sock->Read(NULL, 1024 - i, NULL); | 710 sock->Read(NULL, 1024 - i, CompletionCallback()); |
717 } | 711 } |
718 | 712 |
719 ReleaseAllConnections(ClientSocketPoolTest::KEEP_ALIVE); | 713 ReleaseAllConnections(ClientSocketPoolTest::KEEP_ALIVE); |
720 | 714 |
721 EXPECT_EQ(OK, StartRequest("a", kDefaultPriority)); | 715 EXPECT_EQ(OK, StartRequest("a", kDefaultPriority)); |
722 TestSocketRequest* req = test_base_.request(test_base_.requests_size() - 1); | 716 TestSocketRequest* req = test_base_.request(test_base_.requests_size() - 1); |
723 | 717 |
724 // Last socket is most recently accessed. | 718 // Last socket is most recently accessed. |
725 EXPECT_EQ(sockets_[3], req->handle()->socket()); | 719 EXPECT_EQ(sockets_[3], req->handle()->socket()); |
726 ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); | 720 ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
(...skipping 1303 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2030 EXPECT_EQ(LOAD_STATE_CONNECTING, pool_->GetLoadState("a", &handle2)); | 2024 EXPECT_EQ(LOAD_STATE_CONNECTING, pool_->GetLoadState("a", &handle2)); |
2031 | 2025 |
2032 // Cancel one of the requests. Wait for the other, which will get the first | 2026 // Cancel one of the requests. Wait for the other, which will get the first |
2033 // job. Release the socket. Run the loop again to make sure the second | 2027 // job. Release the socket. Run the loop again to make sure the second |
2034 // socket is sitting idle and the first one is released (since ReleaseSocket() | 2028 // socket is sitting idle and the first one is released (since ReleaseSocket() |
2035 // just posts a DoReleaseSocket() task). | 2029 // just posts a DoReleaseSocket() task). |
2036 | 2030 |
2037 handle.Reset(); | 2031 handle.Reset(); |
2038 EXPECT_EQ(OK, callback2.WaitForResult()); | 2032 EXPECT_EQ(OK, callback2.WaitForResult()); |
2039 // Use the socket. | 2033 // Use the socket. |
2040 EXPECT_EQ(1, handle2.socket()->Write(NULL, 1, NULL)); | 2034 EXPECT_EQ(1, handle2.socket()->Write(NULL, 1, CompletionCallback())); |
2041 handle2.Reset(); | 2035 handle2.Reset(); |
2042 | 2036 |
2043 // The idle socket timeout value was set to 10 milliseconds. Wait 100 | 2037 // The idle socket timeout value was set to 10 milliseconds. Wait 100 |
2044 // milliseconds so the sockets timeout. | 2038 // milliseconds so the sockets timeout. |
2045 base::PlatformThread::Sleep(100); | 2039 base::PlatformThread::Sleep(100); |
2046 MessageLoop::current()->RunAllPending(); | 2040 MessageLoop::current()->RunAllPending(); |
2047 | 2041 |
2048 ASSERT_EQ(2, pool_->IdleSocketCount()); | 2042 ASSERT_EQ(2, pool_->IdleSocketCount()); |
2049 | 2043 |
2050 // Request a new socket. This should cleanup the unused and timed out ones. | 2044 // Request a new socket. This should cleanup the unused and timed out ones. |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2104 EXPECT_EQ(LOAD_STATE_CONNECTING, pool_->GetLoadState("a", &handle2)); | 2098 EXPECT_EQ(LOAD_STATE_CONNECTING, pool_->GetLoadState("a", &handle2)); |
2105 | 2099 |
2106 // Cancel one of the requests. Wait for the other, which will get the first | 2100 // Cancel one of the requests. Wait for the other, which will get the first |
2107 // job. Release the socket. Run the loop again to make sure the second | 2101 // job. Release the socket. Run the loop again to make sure the second |
2108 // socket is sitting idle and the first one is released (since ReleaseSocket() | 2102 // socket is sitting idle and the first one is released (since ReleaseSocket() |
2109 // just posts a DoReleaseSocket() task). | 2103 // just posts a DoReleaseSocket() task). |
2110 | 2104 |
2111 handle.Reset(); | 2105 handle.Reset(); |
2112 EXPECT_EQ(OK, callback2.WaitForResult()); | 2106 EXPECT_EQ(OK, callback2.WaitForResult()); |
2113 // Use the socket. | 2107 // Use the socket. |
2114 EXPECT_EQ(1, handle2.socket()->Write(NULL, 1, NULL)); | 2108 EXPECT_EQ(1, handle2.socket()->Write(NULL, 1, CompletionCallback())); |
2115 handle2.Reset(); | 2109 handle2.Reset(); |
2116 | 2110 |
2117 // We post all of our delayed tasks with a 2ms delay. I.e. they don't | 2111 // We post all of our delayed tasks with a 2ms delay. I.e. they don't |
2118 // actually become pending until 2ms after they have been created. In order | 2112 // actually become pending until 2ms after they have been created. In order |
2119 // to flush all tasks, we need to wait so that we know there are no | 2113 // to flush all tasks, we need to wait so that we know there are no |
2120 // soon-to-be-pending tasks waiting. | 2114 // soon-to-be-pending tasks waiting. |
2121 base::PlatformThread::Sleep(10); | 2115 base::PlatformThread::Sleep(10); |
2122 MessageLoop::current()->RunAllPending(); | 2116 MessageLoop::current()->RunAllPending(); |
2123 | 2117 |
2124 ASSERT_EQ(2, pool_->IdleSocketCount()); | 2118 ASSERT_EQ(2, pool_->IdleSocketCount()); |
(...skipping 740 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2865 kDefaultPriority, | 2859 kDefaultPriority, |
2866 &callback3, | 2860 &callback3, |
2867 pool_.get(), | 2861 pool_.get(), |
2868 BoundNetLog())); | 2862 BoundNetLog())); |
2869 | 2863 |
2870 EXPECT_EQ(OK, callback1.WaitForResult()); | 2864 EXPECT_EQ(OK, callback1.WaitForResult()); |
2871 EXPECT_EQ(OK, callback2.WaitForResult()); | 2865 EXPECT_EQ(OK, callback2.WaitForResult()); |
2872 EXPECT_EQ(OK, callback3.WaitForResult()); | 2866 EXPECT_EQ(OK, callback3.WaitForResult()); |
2873 | 2867 |
2874 // Use the socket. | 2868 // Use the socket. |
2875 EXPECT_EQ(1, handle1.socket()->Write(NULL, 1, NULL)); | 2869 EXPECT_EQ(1, handle1.socket()->Write(NULL, 1, CompletionCallback())); |
2876 EXPECT_EQ(1, handle3.socket()->Write(NULL, 1, NULL)); | 2870 EXPECT_EQ(1, handle3.socket()->Write(NULL, 1, CompletionCallback())); |
2877 | 2871 |
2878 handle1.Reset(); | 2872 handle1.Reset(); |
2879 handle2.Reset(); | 2873 handle2.Reset(); |
2880 handle3.Reset(); | 2874 handle3.Reset(); |
2881 | 2875 |
2882 EXPECT_EQ(OK, handle1.Init("a", | 2876 EXPECT_EQ(OK, handle1.Init("a", |
2883 params_, | 2877 params_, |
2884 kDefaultPriority, | 2878 kDefaultPriority, |
2885 &callback1, | 2879 &callback1, |
2886 pool_.get(), | 2880 pool_.get(), |
(...skipping 617 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3504 params_, | 3498 params_, |
3505 kDefaultPriority, | 3499 kDefaultPriority, |
3506 &callback, | 3500 &callback, |
3507 pool_.get(), | 3501 pool_.get(), |
3508 BoundNetLog())); | 3502 BoundNetLog())); |
3509 } | 3503 } |
3510 | 3504 |
3511 } // namespace | 3505 } // namespace |
3512 | 3506 |
3513 } // namespace net | 3507 } // namespace net |
OLD | NEW |