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 27 matching lines...) Expand all Loading... |
38 public: | 38 public: |
39 bool ignore_limits() { return false; } | 39 bool ignore_limits() { return false; } |
40 private: | 40 private: |
41 friend class base::RefCounted<TestSocketParams>; | 41 friend class base::RefCounted<TestSocketParams>; |
42 ~TestSocketParams() {} | 42 ~TestSocketParams() {} |
43 }; | 43 }; |
44 typedef ClientSocketPoolBase<TestSocketParams> TestClientSocketPoolBase; | 44 typedef ClientSocketPoolBase<TestSocketParams> TestClientSocketPoolBase; |
45 | 45 |
46 class MockClientSocket : public StreamSocket { | 46 class MockClientSocket : public StreamSocket { |
47 public: | 47 public: |
48 MockClientSocket() : connected_(false), was_used_to_convey_data_(false) {} | 48 MockClientSocket() : connected_(false), was_used_to_convey_data_(false), |
| 49 num_bytes_read_(0) {} |
49 | 50 |
50 // Socket methods: | 51 // Socket methods: |
51 virtual int Read( | 52 virtual int Read( |
52 IOBuffer* /* buf */, int /* len */, CompletionCallback* /* callback */) { | 53 IOBuffer* /* buf */, int len, CompletionCallback* /* callback */) { |
53 return ERR_UNEXPECTED; | 54 num_bytes_read_ += len; |
| 55 return len; |
54 } | 56 } |
55 | 57 |
56 virtual int Write( | 58 virtual int Write( |
57 IOBuffer* /* buf */, int len, CompletionCallback* /* callback */) { | 59 IOBuffer* /* buf */, int len, CompletionCallback* /* callback */) { |
58 was_used_to_convey_data_ = true; | 60 was_used_to_convey_data_ = true; |
59 return len; | 61 return len; |
60 } | 62 } |
61 virtual bool SetReceiveBufferSize(int32 size) { return true; } | 63 virtual bool SetReceiveBufferSize(int32 size) { return true; } |
62 virtual bool SetSendBufferSize(int32 size) { return true; } | 64 virtual bool SetSendBufferSize(int32 size) { return true; } |
63 | 65 |
(...skipping 15 matching lines...) Expand all Loading... |
79 virtual int GetLocalAddress(IPEndPoint* /* address */) const { | 81 virtual int GetLocalAddress(IPEndPoint* /* address */) const { |
80 return ERR_UNEXPECTED; | 82 return ERR_UNEXPECTED; |
81 } | 83 } |
82 | 84 |
83 virtual const BoundNetLog& NetLog() const { | 85 virtual const BoundNetLog& NetLog() const { |
84 return net_log_; | 86 return net_log_; |
85 } | 87 } |
86 | 88 |
87 virtual void SetSubresourceSpeculation() {} | 89 virtual void SetSubresourceSpeculation() {} |
88 virtual void SetOmniboxSpeculation() {} | 90 virtual void SetOmniboxSpeculation() {} |
89 virtual bool WasEverUsed() const { return was_used_to_convey_data_; } | 91 virtual bool WasEverUsed() const { |
| 92 return was_used_to_convey_data_ || num_bytes_read_ > 0; |
| 93 } |
90 virtual bool UsingTCPFastOpen() const { return false; } | 94 virtual bool UsingTCPFastOpen() const { return false; } |
| 95 virtual int64 NumBytesRead() const { return num_bytes_read_; } |
| 96 virtual base::TimeDelta GetConnectTimeMicros() const { |
| 97 static const base::TimeDelta kDummyConnectTimeMicros = |
| 98 base::TimeDelta::FromMicroseconds(10); |
| 99 return kDummyConnectTimeMicros; // Dummy value. |
| 100 } |
91 | 101 |
92 private: | 102 private: |
93 bool connected_; | 103 bool connected_; |
94 BoundNetLog net_log_; | 104 BoundNetLog net_log_; |
95 bool was_used_to_convey_data_; | 105 bool was_used_to_convey_data_; |
| 106 int num_bytes_read_; |
96 | 107 |
97 DISALLOW_COPY_AND_ASSIGN(MockClientSocket); | 108 DISALLOW_COPY_AND_ASSIGN(MockClientSocket); |
98 }; | 109 }; |
99 | 110 |
100 class TestConnectJob; | 111 class TestConnectJob; |
101 | 112 |
102 class MockClientSocketFactory : public ClientSocketFactory { | 113 class MockClientSocketFactory : public ClientSocketFactory { |
103 public: | 114 public: |
104 MockClientSocketFactory() : allocation_count_(0) {} | 115 MockClientSocketFactory() : allocation_count_(0) {} |
105 | 116 |
(...skipping 491 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
597 | 608 |
598 bool connect_backup_jobs_enabled_; | 609 bool connect_backup_jobs_enabled_; |
599 MockClientSocketFactory client_socket_factory_; | 610 MockClientSocketFactory client_socket_factory_; |
600 TestConnectJobFactory* connect_job_factory_; | 611 TestConnectJobFactory* connect_job_factory_; |
601 scoped_refptr<TestSocketParams> params_; | 612 scoped_refptr<TestSocketParams> params_; |
602 ClientSocketPoolHistograms histograms_; | 613 ClientSocketPoolHistograms histograms_; |
603 scoped_ptr<TestClientSocketPool> pool_; | 614 scoped_ptr<TestClientSocketPool> pool_; |
604 ClientSocketPoolTest test_base_; | 615 ClientSocketPoolTest test_base_; |
605 }; | 616 }; |
606 | 617 |
| 618 TEST_F(ClientSocketPoolBaseTest, AssignIdleSocketToGroup_WarmestSocket) { |
| 619 CreatePool(4, 4); |
| 620 net::SetSocketReusePolicy(0); |
| 621 |
| 622 EXPECT_EQ(OK, StartRequest("a", kDefaultPriority)); |
| 623 EXPECT_EQ(OK, StartRequest("a", kDefaultPriority)); |
| 624 EXPECT_EQ(OK, StartRequest("a", kDefaultPriority)); |
| 625 EXPECT_EQ(OK, StartRequest("a", kDefaultPriority)); |
| 626 |
| 627 std::map<int, StreamSocket*> sockets_; |
| 628 for (size_t i = 0; i < test_base_.requests_size(); i++) { |
| 629 TestSocketRequest* req = test_base_.request(i); |
| 630 StreamSocket* s = req->handle()->socket(); |
| 631 MockClientSocket* sock = static_cast<MockClientSocket*>(s); |
| 632 CHECK(sock); |
| 633 sockets_[i] = sock; |
| 634 sock->Read(NULL, 1024 - i, NULL); |
| 635 } |
| 636 |
| 637 ReleaseAllConnections(ClientSocketPoolTest::KEEP_ALIVE); |
| 638 |
| 639 EXPECT_EQ(OK, StartRequest("a", kDefaultPriority)); |
| 640 TestSocketRequest* req = test_base_.request(test_base_.requests_size() - 1); |
| 641 |
| 642 // First socket is warmest. |
| 643 EXPECT_EQ(sockets_[0], req->handle()->socket()); |
| 644 |
| 645 // Test that NumBytes are as expected. |
| 646 EXPECT_EQ(1024, sockets_[0]->NumBytesRead()); |
| 647 EXPECT_EQ(1023, sockets_[1]->NumBytesRead()); |
| 648 EXPECT_EQ(1022, sockets_[2]->NumBytesRead()); |
| 649 EXPECT_EQ(1021, sockets_[3]->NumBytesRead()); |
| 650 |
| 651 ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
| 652 } |
| 653 |
| 654 TEST_F(ClientSocketPoolBaseTest, AssignIdleSocketToGroup_LastAccessedSocket) { |
| 655 CreatePool(4, 4); |
| 656 net::SetSocketReusePolicy(2); |
| 657 |
| 658 EXPECT_EQ(OK, StartRequest("a", kDefaultPriority)); |
| 659 EXPECT_EQ(OK, StartRequest("a", kDefaultPriority)); |
| 660 EXPECT_EQ(OK, StartRequest("a", kDefaultPriority)); |
| 661 EXPECT_EQ(OK, StartRequest("a", kDefaultPriority)); |
| 662 |
| 663 std::map<int, StreamSocket*> sockets_; |
| 664 for (size_t i = 0; i < test_base_.requests_size(); i++) { |
| 665 TestSocketRequest* req = test_base_.request(i); |
| 666 StreamSocket* s = req->handle()->socket(); |
| 667 MockClientSocket* sock = static_cast<MockClientSocket*>(s); |
| 668 CHECK(sock); |
| 669 sockets_[i] = sock; |
| 670 sock->Read(NULL, 1024 - i, NULL); |
| 671 } |
| 672 |
| 673 ReleaseAllConnections(ClientSocketPoolTest::KEEP_ALIVE); |
| 674 |
| 675 EXPECT_EQ(OK, StartRequest("a", kDefaultPriority)); |
| 676 TestSocketRequest* req = test_base_.request(test_base_.requests_size() - 1); |
| 677 |
| 678 // Last socket is most recently accessed. |
| 679 EXPECT_EQ(sockets_[3], req->handle()->socket()); |
| 680 ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
| 681 } |
| 682 |
607 // Even though a timeout is specified, it doesn't time out on a synchronous | 683 // Even though a timeout is specified, it doesn't time out on a synchronous |
608 // completion. | 684 // completion. |
609 TEST_F(ClientSocketPoolBaseTest, ConnectJob_NoTimeoutOnSynchronousCompletion) { | 685 TEST_F(ClientSocketPoolBaseTest, ConnectJob_NoTimeoutOnSynchronousCompletion) { |
610 TestConnectJobDelegate delegate; | 686 TestConnectJobDelegate delegate; |
611 ClientSocketHandle ignored; | 687 ClientSocketHandle ignored; |
612 TestClientSocketPoolBase::Request request( | 688 TestClientSocketPoolBase::Request request( |
613 &ignored, NULL, kDefaultPriority, | 689 &ignored, NULL, kDefaultPriority, |
614 internal::ClientSocketPoolBaseHelper::NORMAL, | 690 internal::ClientSocketPoolBaseHelper::NORMAL, |
615 false, params_, BoundNetLog()); | 691 false, params_, BoundNetLog()); |
616 scoped_ptr<TestConnectJob> job( | 692 scoped_ptr<TestConnectJob> job( |
(...skipping 2571 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3188 // The hung connect job should still be there, but everything else should be | 3264 // The hung connect job should still be there, but everything else should be |
3189 // complete. | 3265 // complete. |
3190 EXPECT_EQ(1, pool_->NumConnectJobsInGroup("a")); | 3266 EXPECT_EQ(1, pool_->NumConnectJobsInGroup("a")); |
3191 EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a")); | 3267 EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a")); |
3192 EXPECT_EQ(1, pool_->NumActiveSocketsInGroup("a")); | 3268 EXPECT_EQ(1, pool_->NumActiveSocketsInGroup("a")); |
3193 } | 3269 } |
3194 | 3270 |
3195 } // namespace | 3271 } // namespace |
3196 | 3272 |
3197 } // namespace net | 3273 } // namespace net |
OLD | NEW |