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()); | |
willchan no longer on Chromium
2011/06/24 11:11:36
Please verify that the NumBytesRead() is as you ex
gagansingh
2011/06/24 12:17:56
Done.
| |
644 ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); | |
645 } | |
646 | |
647 TEST_F(ClientSocketPoolBaseTest, AssignIdleSocketToGroup_LastAccessedSocket) { | |
648 CreatePool(4, 4); | |
649 net::SetSocketReusePolicy(2); | |
650 | |
651 EXPECT_EQ(OK, StartRequest("a", kDefaultPriority)); | |
652 EXPECT_EQ(OK, StartRequest("a", kDefaultPriority)); | |
653 EXPECT_EQ(OK, StartRequest("a", kDefaultPriority)); | |
654 EXPECT_EQ(OK, StartRequest("a", kDefaultPriority)); | |
655 | |
656 std::map<int, StreamSocket*> sockets_; | |
657 for (size_t i = 0; i < test_base_.requests_size(); i++) { | |
658 TestSocketRequest* req = test_base_.request(i); | |
659 StreamSocket* s = req->handle()->socket(); | |
660 MockClientSocket* sock = static_cast<MockClientSocket*>(s); | |
661 CHECK(sock); | |
662 sockets_[i] = sock; | |
663 sock->Read(NULL, 1024 - i, NULL); | |
664 } | |
665 | |
666 ReleaseAllConnections(ClientSocketPoolTest::KEEP_ALIVE); | |
667 | |
668 EXPECT_EQ(OK, StartRequest("a", kDefaultPriority)); | |
669 TestSocketRequest* req = test_base_.request(test_base_.requests_size() - 1); | |
670 | |
671 // Last socket is most recently accessed. | |
672 EXPECT_EQ(sockets_[3], req->handle()->socket()); | |
673 ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); | |
674 } | |
675 | |
607 // Even though a timeout is specified, it doesn't time out on a synchronous | 676 // Even though a timeout is specified, it doesn't time out on a synchronous |
608 // completion. | 677 // completion. |
609 TEST_F(ClientSocketPoolBaseTest, ConnectJob_NoTimeoutOnSynchronousCompletion) { | 678 TEST_F(ClientSocketPoolBaseTest, ConnectJob_NoTimeoutOnSynchronousCompletion) { |
610 TestConnectJobDelegate delegate; | 679 TestConnectJobDelegate delegate; |
611 ClientSocketHandle ignored; | 680 ClientSocketHandle ignored; |
612 TestClientSocketPoolBase::Request request( | 681 TestClientSocketPoolBase::Request request( |
613 &ignored, NULL, kDefaultPriority, | 682 &ignored, NULL, kDefaultPriority, |
614 internal::ClientSocketPoolBaseHelper::NORMAL, | 683 internal::ClientSocketPoolBaseHelper::NORMAL, |
615 false, params_, BoundNetLog()); | 684 false, params_, BoundNetLog()); |
616 scoped_ptr<TestConnectJob> job( | 685 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 | 3257 // The hung connect job should still be there, but everything else should be |
3189 // complete. | 3258 // complete. |
3190 EXPECT_EQ(1, pool_->NumConnectJobsInGroup("a")); | 3259 EXPECT_EQ(1, pool_->NumConnectJobsInGroup("a")); |
3191 EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a")); | 3260 EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a")); |
3192 EXPECT_EQ(1, pool_->NumActiveSocketsInGroup("a")); | 3261 EXPECT_EQ(1, pool_->NumActiveSocketsInGroup("a")); |
3193 } | 3262 } |
3194 | 3263 |
3195 } // namespace | 3264 } // namespace |
3196 | 3265 |
3197 } // namespace net | 3266 } // namespace net |
OLD | NEW |