| 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/http/http_pipelined_host_pool.h" | 5 #include "net/http/http_pipelined_host_pool.h" |
| 6 | 6 |
| 7 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
| 8 #include "net/base/ssl_config_service.h" | 8 #include "net/base/ssl_config_service.h" |
| 9 #include "net/http/http_pipelined_host.h" | 9 #include "net/http/http_pipelined_host.h" |
| 10 #include "net/http/http_pipelined_host_capability.h" | |
| 11 #include "net/http/http_server_properties_impl.h" | |
| 12 #include "net/proxy/proxy_info.h" | 10 #include "net/proxy/proxy_info.h" |
| 13 #include "testing/gmock/include/gmock/gmock.h" | 11 #include "testing/gmock/include/gmock/gmock.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
| 15 | 13 |
| 16 using testing::_; | 14 using testing::_; |
| 17 using testing::Ref; | 15 using testing::Ref; |
| 18 using testing::Return; | 16 using testing::Return; |
| 19 using testing::ReturnNull; | 17 using testing::ReturnNull; |
| 20 | 18 |
| 21 namespace net { | 19 namespace net { |
| 22 | 20 |
| 23 namespace { | 21 namespace { |
| 24 | 22 |
| 25 ClientSocketHandle* kDummyConnection = | 23 ClientSocketHandle* kDummyConnection = |
| 26 reinterpret_cast<ClientSocketHandle*>(188); | 24 reinterpret_cast<ClientSocketHandle*>(188); |
| 27 HttpPipelinedStream* kDummyStream = | 25 HttpPipelinedStream* kDummyStream = |
| 28 reinterpret_cast<HttpPipelinedStream*>(99); | 26 reinterpret_cast<HttpPipelinedStream*>(99); |
| 29 | 27 |
| 30 class MockPoolDelegate : public HttpPipelinedHostPool::Delegate { | 28 class MockPoolDelegate : public HttpPipelinedHostPool::Delegate { |
| 31 public: | 29 public: |
| 32 MOCK_METHOD1(OnHttpPipelinedHostHasAdditionalCapacity, | 30 MOCK_METHOD1(OnHttpPipelinedHostHasAdditionalCapacity, |
| 33 void(const HostPortPair& origin)); | 31 void(const HostPortPair& origin)); |
| 34 }; | 32 }; |
| 35 | 33 |
| 36 class MockHostFactory : public HttpPipelinedHost::Factory { | 34 class MockHostFactory : public HttpPipelinedHost::Factory { |
| 37 public: | 35 public: |
| 38 MOCK_METHOD4(CreateNewHost, HttpPipelinedHost*( | 36 MOCK_METHOD4(CreateNewHost, HttpPipelinedHost*( |
| 39 HttpPipelinedHost::Delegate* delegate, const HostPortPair& origin, | 37 HttpPipelinedHost::Delegate* delegate, const HostPortPair& origin, |
| 40 HttpPipelinedConnection::Factory* factory, | 38 HttpPipelinedConnection::Factory* factory, |
| 41 HttpPipelinedHostCapability capability)); | 39 HttpPipelinedHost::Capability capability)); |
| 42 }; | 40 }; |
| 43 | 41 |
| 44 class MockHost : public HttpPipelinedHost { | 42 class MockHost : public HttpPipelinedHost { |
| 45 public: | 43 public: |
| 46 MockHost(const HostPortPair& origin) | 44 MockHost(const HostPortPair& origin) |
| 47 : origin_(origin) { | 45 : origin_(origin) { |
| 48 } | 46 } |
| 49 | 47 |
| 50 MOCK_METHOD5(CreateStreamOnNewPipeline, HttpPipelinedStream*( | 48 MOCK_METHOD5(CreateStreamOnNewPipeline, HttpPipelinedStream*( |
| 51 ClientSocketHandle* connection, | 49 ClientSocketHandle* connection, |
| 52 const SSLConfig& used_ssl_config, | 50 const SSLConfig& used_ssl_config, |
| 53 const ProxyInfo& used_proxy_info, | 51 const ProxyInfo& used_proxy_info, |
| 54 const BoundNetLog& net_log, | 52 const BoundNetLog& net_log, |
| 55 bool was_npn_negotiated)); | 53 bool was_npn_negotiated)); |
| 56 MOCK_METHOD0(CreateStreamOnExistingPipeline, HttpPipelinedStream*()); | 54 MOCK_METHOD0(CreateStreamOnExistingPipeline, HttpPipelinedStream*()); |
| 57 MOCK_CONST_METHOD0(IsExistingPipelineAvailable, bool()); | 55 MOCK_CONST_METHOD0(IsExistingPipelineAvailable, bool()); |
| 58 | 56 |
| 59 virtual const HostPortPair& origin() const OVERRIDE { return origin_; } | 57 virtual const HostPortPair& origin() const OVERRIDE { return origin_; } |
| 60 | 58 |
| 61 private: | 59 private: |
| 62 HostPortPair origin_; | 60 HostPortPair origin_; |
| 63 }; | 61 }; |
| 64 | 62 |
| 65 class HttpPipelinedHostPoolTest : public testing::Test { | 63 class HttpPipelinedHostPoolTest : public testing::Test { |
| 66 public: | 64 public: |
| 67 HttpPipelinedHostPoolTest() | 65 HttpPipelinedHostPoolTest() |
| 68 : origin_("host", 123), | 66 : origin_("host", 123), |
| 69 factory_(new MockHostFactory), // Owned by pool_. | 67 factory_(new MockHostFactory), // Owned by pool_. |
| 70 host_(new MockHost(origin_)), // Owned by pool_. | 68 host_(new MockHost(origin_)), // Owned by pool_. |
| 71 http_server_properties_(new HttpServerPropertiesImpl), | 69 pool_(new HttpPipelinedHostPool(&delegate_, factory_)), |
| 72 pool_(new HttpPipelinedHostPool(&delegate_, factory_, | |
| 73 http_server_properties_.get())), | |
| 74 was_npn_negotiated_(false) { | 70 was_npn_negotiated_(false) { |
| 75 } | 71 } |
| 76 | 72 |
| 77 void CreateDummyStream() { | 73 void CreateDummyStream() { |
| 78 EXPECT_CALL(*host_, CreateStreamOnNewPipeline(kDummyConnection, | 74 EXPECT_CALL(*host_, CreateStreamOnNewPipeline(kDummyConnection, |
| 79 Ref(ssl_config_), | 75 Ref(ssl_config_), |
| 80 Ref(proxy_info_), | 76 Ref(proxy_info_), |
| 81 Ref(net_log_), | 77 Ref(net_log_), |
| 82 was_npn_negotiated_)) | 78 was_npn_negotiated_)) |
| 83 .Times(1) | 79 .Times(1) |
| 84 .WillOnce(Return(kDummyStream)); | 80 .WillOnce(Return(kDummyStream)); |
| 85 EXPECT_EQ(kDummyStream, | 81 EXPECT_EQ(kDummyStream, |
| 86 pool_->CreateStreamOnNewPipeline(origin_, kDummyConnection, | 82 pool_->CreateStreamOnNewPipeline(origin_, kDummyConnection, |
| 87 ssl_config_, proxy_info_, | 83 ssl_config_, proxy_info_, |
| 88 net_log_, was_npn_negotiated_)); | 84 net_log_, was_npn_negotiated_)); |
| 89 } | 85 } |
| 90 | 86 |
| 91 HostPortPair origin_; | 87 HostPortPair origin_; |
| 92 MockPoolDelegate delegate_; | 88 MockPoolDelegate delegate_; |
| 93 MockHostFactory* factory_; | 89 MockHostFactory* factory_; |
| 94 MockHost* host_; | 90 MockHost* host_; |
| 95 scoped_ptr<HttpServerPropertiesImpl> http_server_properties_; | |
| 96 scoped_ptr<HttpPipelinedHostPool> pool_; | 91 scoped_ptr<HttpPipelinedHostPool> pool_; |
| 97 | 92 |
| 98 const SSLConfig ssl_config_; | 93 const SSLConfig ssl_config_; |
| 99 const ProxyInfo proxy_info_; | 94 const ProxyInfo proxy_info_; |
| 100 const BoundNetLog net_log_; | 95 const BoundNetLog net_log_; |
| 101 bool was_npn_negotiated_; | 96 bool was_npn_negotiated_; |
| 102 }; | 97 }; |
| 103 | 98 |
| 104 TEST_F(HttpPipelinedHostPoolTest, DefaultUnknown) { | 99 TEST_F(HttpPipelinedHostPoolTest, DefaultUnknown) { |
| 105 EXPECT_TRUE(pool_->IsHostEligibleForPipelining(origin_)); | 100 EXPECT_TRUE(pool_->IsHostEligibleForPipelining(origin_)); |
| 106 EXPECT_CALL(*factory_, CreateNewHost(pool_.get(), Ref(origin_), _, | 101 EXPECT_CALL(*factory_, CreateNewHost(pool_.get(), Ref(origin_), _, |
| 107 PIPELINE_UNKNOWN)) | 102 HttpPipelinedHost::UNKNOWN)) |
| 108 .Times(1) | 103 .Times(1) |
| 109 .WillOnce(Return(host_)); | 104 .WillOnce(Return(host_)); |
| 110 | 105 |
| 111 CreateDummyStream(); | 106 CreateDummyStream(); |
| 112 pool_->OnHostIdle(host_); | 107 pool_->OnHostIdle(host_); |
| 113 } | 108 } |
| 114 | 109 |
| 115 TEST_F(HttpPipelinedHostPoolTest, RemembersIncapable) { | 110 TEST_F(HttpPipelinedHostPoolTest, RemembersIncapable) { |
| 116 EXPECT_CALL(*factory_, CreateNewHost(pool_.get(), Ref(origin_), _, | 111 EXPECT_CALL(*factory_, CreateNewHost(pool_.get(), Ref(origin_), _, |
| 117 PIPELINE_UNKNOWN)) | 112 HttpPipelinedHost::UNKNOWN)) |
| 118 .Times(1) | 113 .Times(1) |
| 119 .WillOnce(Return(host_)); | 114 .WillOnce(Return(host_)); |
| 120 | 115 |
| 121 CreateDummyStream(); | 116 CreateDummyStream(); |
| 122 pool_->OnHostDeterminedCapability(host_, PIPELINE_INCAPABLE); | 117 pool_->OnHostDeterminedCapability(host_, HttpPipelinedHost::INCAPABLE); |
| 123 pool_->OnHostIdle(host_); | 118 pool_->OnHostIdle(host_); |
| 124 EXPECT_FALSE(pool_->IsHostEligibleForPipelining(origin_)); | 119 EXPECT_FALSE(pool_->IsHostEligibleForPipelining(origin_)); |
| 125 EXPECT_CALL(*factory_, CreateNewHost(pool_.get(), Ref(origin_), _, | 120 EXPECT_CALL(*factory_, CreateNewHost(pool_.get(), Ref(origin_), _, |
| 126 PIPELINE_INCAPABLE)) | 121 HttpPipelinedHost::INCAPABLE)) |
| 127 .Times(0); | 122 .Times(0); |
| 128 EXPECT_EQ(NULL, | 123 EXPECT_EQ(NULL, |
| 129 pool_->CreateStreamOnNewPipeline(origin_, kDummyConnection, | 124 pool_->CreateStreamOnNewPipeline(origin_, kDummyConnection, |
| 130 ssl_config_, proxy_info_, net_log_, | 125 ssl_config_, proxy_info_, net_log_, |
| 131 was_npn_negotiated_)); | 126 was_npn_negotiated_)); |
| 132 } | 127 } |
| 133 | 128 |
| 134 TEST_F(HttpPipelinedHostPoolTest, RemembersCapable) { | 129 TEST_F(HttpPipelinedHostPoolTest, RemembersCapable) { |
| 135 EXPECT_CALL(*factory_, CreateNewHost(pool_.get(), Ref(origin_), _, | 130 EXPECT_CALL(*factory_, CreateNewHost(pool_.get(), Ref(origin_), _, |
| 136 PIPELINE_UNKNOWN)) | 131 HttpPipelinedHost::UNKNOWN)) |
| 137 .Times(1) | 132 .Times(1) |
| 138 .WillOnce(Return(host_)); | 133 .WillOnce(Return(host_)); |
| 139 | 134 |
| 140 CreateDummyStream(); | 135 CreateDummyStream(); |
| 141 pool_->OnHostDeterminedCapability(host_, PIPELINE_CAPABLE); | 136 pool_->OnHostDeterminedCapability(host_, HttpPipelinedHost::CAPABLE); |
| 142 pool_->OnHostIdle(host_); | 137 pool_->OnHostIdle(host_); |
| 143 EXPECT_TRUE(pool_->IsHostEligibleForPipelining(origin_)); | 138 EXPECT_TRUE(pool_->IsHostEligibleForPipelining(origin_)); |
| 144 | 139 |
| 145 host_ = new MockHost(origin_); | 140 host_ = new MockHost(origin_); |
| 146 EXPECT_CALL(*factory_, CreateNewHost(pool_.get(), Ref(origin_), _, | 141 EXPECT_CALL(*factory_, CreateNewHost(pool_.get(), Ref(origin_), _, |
| 147 PIPELINE_CAPABLE)) | 142 HttpPipelinedHost::CAPABLE)) |
| 148 .Times(1) | 143 .Times(1) |
| 149 .WillOnce(Return(host_)); | 144 .WillOnce(Return(host_)); |
| 150 CreateDummyStream(); | 145 CreateDummyStream(); |
| 151 pool_->OnHostIdle(host_); | 146 pool_->OnHostIdle(host_); |
| 152 } | 147 } |
| 153 | 148 |
| 154 TEST_F(HttpPipelinedHostPoolTest, IncapableIsSticky) { | 149 TEST_F(HttpPipelinedHostPoolTest, IncapableIsSticky) { |
| 155 EXPECT_CALL(*factory_, CreateNewHost(pool_.get(), Ref(origin_), _, | 150 EXPECT_CALL(*factory_, CreateNewHost(pool_.get(), Ref(origin_), _, |
| 156 PIPELINE_UNKNOWN)) | 151 HttpPipelinedHost::UNKNOWN)) |
| 157 .Times(1) | 152 .Times(1) |
| 158 .WillOnce(Return(host_)); | 153 .WillOnce(Return(host_)); |
| 159 | 154 |
| 160 CreateDummyStream(); | 155 CreateDummyStream(); |
| 161 pool_->OnHostDeterminedCapability(host_, PIPELINE_CAPABLE); | 156 pool_->OnHostDeterminedCapability(host_, HttpPipelinedHost::CAPABLE); |
| 162 pool_->OnHostDeterminedCapability(host_, PIPELINE_INCAPABLE); | 157 pool_->OnHostDeterminedCapability(host_, HttpPipelinedHost::INCAPABLE); |
| 163 pool_->OnHostDeterminedCapability(host_, PIPELINE_CAPABLE); | 158 pool_->OnHostDeterminedCapability(host_, HttpPipelinedHost::CAPABLE); |
| 164 pool_->OnHostIdle(host_); | 159 pool_->OnHostIdle(host_); |
| 165 EXPECT_FALSE(pool_->IsHostEligibleForPipelining(origin_)); | 160 EXPECT_FALSE(pool_->IsHostEligibleForPipelining(origin_)); |
| 166 } | 161 } |
| 167 | 162 |
| 168 TEST_F(HttpPipelinedHostPoolTest, RemainsUnknownWithoutFeedback) { | 163 TEST_F(HttpPipelinedHostPoolTest, RemainsUnknownWithoutFeedback) { |
| 169 EXPECT_CALL(*factory_, CreateNewHost(pool_.get(), Ref(origin_), _, | 164 EXPECT_CALL(*factory_, CreateNewHost(pool_.get(), Ref(origin_), _, |
| 170 PIPELINE_UNKNOWN)) | 165 HttpPipelinedHost::UNKNOWN)) |
| 171 .Times(1) | 166 .Times(1) |
| 172 .WillOnce(Return(host_)); | 167 .WillOnce(Return(host_)); |
| 173 | 168 |
| 174 CreateDummyStream(); | 169 CreateDummyStream(); |
| 175 pool_->OnHostIdle(host_); | 170 pool_->OnHostIdle(host_); |
| 176 EXPECT_TRUE(pool_->IsHostEligibleForPipelining(origin_)); | 171 EXPECT_TRUE(pool_->IsHostEligibleForPipelining(origin_)); |
| 177 | 172 |
| 178 host_ = new MockHost(origin_); | 173 host_ = new MockHost(origin_); |
| 179 EXPECT_CALL(*factory_, CreateNewHost(pool_.get(), Ref(origin_), _, | 174 EXPECT_CALL(*factory_, CreateNewHost(pool_.get(), Ref(origin_), _, |
| 180 PIPELINE_UNKNOWN)) | 175 HttpPipelinedHost::UNKNOWN)) |
| 181 .Times(1) | 176 .Times(1) |
| 182 .WillOnce(Return(host_)); | 177 .WillOnce(Return(host_)); |
| 183 | 178 |
| 184 CreateDummyStream(); | 179 CreateDummyStream(); |
| 185 pool_->OnHostIdle(host_); | 180 pool_->OnHostIdle(host_); |
| 186 } | 181 } |
| 187 | 182 |
| 188 TEST_F(HttpPipelinedHostPoolTest, PopulatesServerProperties) { | |
| 189 EXPECT_EQ(PIPELINE_UNKNOWN, | |
| 190 http_server_properties_->GetPipelineCapability(host_->origin())); | |
| 191 pool_->OnHostDeterminedCapability(host_, PIPELINE_CAPABLE); | |
| 192 EXPECT_EQ(PIPELINE_CAPABLE, | |
| 193 http_server_properties_->GetPipelineCapability(host_->origin())); | |
| 194 } | |
| 195 | |
| 196 } // anonymous namespace | 183 } // anonymous namespace |
| 197 | 184 |
| 198 } // namespace net | 185 } // namespace net |
| OLD | NEW |