OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "base/rand_util.h" |
8 #include "net/base/ssl_config_service.h" | 9 #include "net/base/ssl_config_service.h" |
9 #include "net/http/http_pipelined_host.h" | 10 #include "net/http/http_pipelined_host.h" |
10 #include "net/http/http_pipelined_host_capability.h" | 11 #include "net/http/http_pipelined_host_capability.h" |
11 #include "net/http/http_server_properties_impl.h" | 12 #include "net/http/http_server_properties_impl.h" |
12 #include "net/proxy/proxy_info.h" | 13 #include "net/proxy/proxy_info.h" |
13 #include "testing/gmock/include/gmock/gmock.h" | 14 #include "testing/gmock/include/gmock/gmock.h" |
14 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
15 | 16 |
16 using testing::_; | 17 using testing::_; |
17 using testing::Ref; | 18 using testing::Ref; |
18 using testing::Return; | 19 using testing::Return; |
19 using testing::ReturnNull; | 20 using testing::ReturnNull; |
20 | 21 |
21 namespace net { | 22 namespace net { |
22 | 23 |
23 namespace { | 24 namespace { |
24 | 25 |
25 ClientSocketHandle* kDummyConnection = | 26 ClientSocketHandle* kDummyConnection = |
26 reinterpret_cast<ClientSocketHandle*>(188); | 27 reinterpret_cast<ClientSocketHandle*>(188); |
27 HttpPipelinedStream* kDummyStream = | 28 HttpPipelinedStream* kDummyStream = |
28 reinterpret_cast<HttpPipelinedStream*>(99); | 29 reinterpret_cast<HttpPipelinedStream*>(99); |
29 | 30 |
30 class MockPoolDelegate : public HttpPipelinedHostPool::Delegate { | 31 class MockPoolDelegate : public HttpPipelinedHostPool::Delegate { |
31 public: | 32 public: |
32 MOCK_METHOD1(OnHttpPipelinedHostHasAdditionalCapacity, | 33 MOCK_METHOD1(OnHttpPipelinedHostHasAdditionalCapacity, |
33 void(const HostPortPair& origin)); | 34 void(HttpPipelinedHost* host)); |
34 }; | 35 }; |
35 | 36 |
36 class MockHostFactory : public HttpPipelinedHost::Factory { | 37 class MockHostFactory : public HttpPipelinedHost::Factory { |
37 public: | 38 public: |
38 MOCK_METHOD4(CreateNewHost, HttpPipelinedHost*( | 39 MOCK_METHOD5(CreateNewHost, HttpPipelinedHost*( |
39 HttpPipelinedHost::Delegate* delegate, const HostPortPair& origin, | 40 HttpPipelinedHost::Delegate* delegate, |
| 41 const HttpPipelinedHost::Key& key, |
40 HttpPipelinedConnection::Factory* factory, | 42 HttpPipelinedConnection::Factory* factory, |
41 HttpPipelinedHostCapability capability)); | 43 HttpPipelinedHostCapability capability, |
| 44 bool force_pipelining)); |
42 }; | 45 }; |
43 | 46 |
44 class MockHost : public HttpPipelinedHost { | 47 class MockHost : public HttpPipelinedHost { |
45 public: | 48 public: |
46 MockHost(const HostPortPair& origin) | 49 MockHost(const Key& key) |
47 : origin_(origin) { | 50 : key_(key) { |
48 } | 51 } |
49 | 52 |
50 MOCK_METHOD6(CreateStreamOnNewPipeline, HttpPipelinedStream*( | 53 MOCK_METHOD6(CreateStreamOnNewPipeline, HttpPipelinedStream*( |
51 ClientSocketHandle* connection, | 54 ClientSocketHandle* connection, |
52 const SSLConfig& used_ssl_config, | 55 const SSLConfig& used_ssl_config, |
53 const ProxyInfo& used_proxy_info, | 56 const ProxyInfo& used_proxy_info, |
54 const BoundNetLog& net_log, | 57 const BoundNetLog& net_log, |
55 bool was_npn_negotiated, | 58 bool was_npn_negotiated, |
56 SSLClientSocket::NextProto protocol_negotiated)); | 59 SSLClientSocket::NextProto protocol_negotiated)); |
57 MOCK_METHOD0(CreateStreamOnExistingPipeline, HttpPipelinedStream*()); | 60 MOCK_METHOD0(CreateStreamOnExistingPipeline, HttpPipelinedStream*()); |
58 MOCK_CONST_METHOD0(IsExistingPipelineAvailable, bool()); | 61 MOCK_CONST_METHOD0(IsExistingPipelineAvailable, bool()); |
59 MOCK_CONST_METHOD0(PipelineInfoToValue, base::Value*()); | 62 MOCK_CONST_METHOD0(PipelineInfoToValue, base::Value*()); |
60 | 63 |
61 virtual const HostPortPair& origin() const OVERRIDE { return origin_; } | 64 virtual const Key& GetKey() const OVERRIDE { return key_; } |
62 | 65 |
63 private: | 66 private: |
64 HostPortPair origin_; | 67 Key key_; |
65 }; | 68 }; |
66 | 69 |
67 class HttpPipelinedHostPoolTest : public testing::Test { | 70 class HttpPipelinedHostPoolTest : public testing::Test { |
68 public: | 71 public: |
69 HttpPipelinedHostPoolTest() | 72 HttpPipelinedHostPoolTest() |
70 : origin_("host", 123), | 73 : key_(HostPortPair("host", 123)), |
71 factory_(new MockHostFactory), // Owned by pool_. | 74 factory_(new MockHostFactory), // Owned by pool_. |
72 host_(new MockHost(origin_)), // Owned by pool_. | 75 host_(new MockHost(key_)), // Owned by pool_. |
73 http_server_properties_(new HttpServerPropertiesImpl), | 76 http_server_properties_(new HttpServerPropertiesImpl), |
74 pool_(new HttpPipelinedHostPool(&delegate_, factory_, | 77 pool_(new HttpPipelinedHostPool(&delegate_, factory_, |
75 http_server_properties_.get())), | 78 http_server_properties_.get(), false)), |
76 was_npn_negotiated_(false), | 79 was_npn_negotiated_(false), |
77 protocol_negotiated_(SSLClientSocket::kProtoUnknown) { | 80 protocol_negotiated_(SSLClientSocket::kProtoUnknown) { |
78 } | 81 } |
79 | 82 |
80 void CreateDummyStream() { | 83 void CreateDummyStream(const HttpPipelinedHost::Key& key, |
81 EXPECT_CALL(*host_, CreateStreamOnNewPipeline(kDummyConnection, | 84 ClientSocketHandle* connection, |
82 Ref(ssl_config_), | 85 HttpPipelinedStream* stream, |
83 Ref(proxy_info_), | 86 MockHost* host) { |
84 Ref(net_log_), | 87 EXPECT_CALL(*host, CreateStreamOnNewPipeline(connection, |
85 was_npn_negotiated_, | 88 Ref(ssl_config_), |
86 protocol_negotiated_)) | 89 Ref(proxy_info_), |
| 90 Ref(net_log_), |
| 91 was_npn_negotiated_, |
| 92 protocol_negotiated_)) |
87 .Times(1) | 93 .Times(1) |
88 .WillOnce(Return(kDummyStream)); | 94 .WillOnce(Return(stream)); |
89 EXPECT_EQ(kDummyStream, | 95 EXPECT_EQ(stream, |
90 pool_->CreateStreamOnNewPipeline(origin_, kDummyConnection, | 96 pool_->CreateStreamOnNewPipeline(key, connection, |
91 ssl_config_, proxy_info_, | 97 ssl_config_, proxy_info_, |
92 net_log_, was_npn_negotiated_, | 98 net_log_, was_npn_negotiated_, |
93 protocol_negotiated_)); | 99 protocol_negotiated_)); |
94 } | 100 } |
95 | 101 |
96 HostPortPair origin_; | 102 MockHost* CreateDummyHost(const HttpPipelinedHost::Key& key) { |
| 103 MockHost* mock_host = new MockHost(key); |
| 104 EXPECT_CALL(*factory_, CreateNewHost(pool_.get(), Ref(key), _, |
| 105 PIPELINE_UNKNOWN, false)) |
| 106 .Times(1) |
| 107 .WillOnce(Return(mock_host)); |
| 108 ClientSocketHandle* dummy_connection = |
| 109 reinterpret_cast<ClientSocketHandle*>(base::RandUint64()); |
| 110 HttpPipelinedStream* dummy_stream = |
| 111 reinterpret_cast<HttpPipelinedStream*>(base::RandUint64()); |
| 112 CreateDummyStream(key, dummy_connection, dummy_stream, mock_host); |
| 113 return mock_host; |
| 114 } |
| 115 |
| 116 HttpPipelinedHost::Key key_; |
97 MockPoolDelegate delegate_; | 117 MockPoolDelegate delegate_; |
98 MockHostFactory* factory_; | 118 MockHostFactory* factory_; |
99 MockHost* host_; | 119 MockHost* host_; |
100 scoped_ptr<HttpServerPropertiesImpl> http_server_properties_; | 120 scoped_ptr<HttpServerPropertiesImpl> http_server_properties_; |
101 scoped_ptr<HttpPipelinedHostPool> pool_; | 121 scoped_ptr<HttpPipelinedHostPool> pool_; |
102 | 122 |
103 const SSLConfig ssl_config_; | 123 const SSLConfig ssl_config_; |
104 const ProxyInfo proxy_info_; | 124 const ProxyInfo proxy_info_; |
105 const BoundNetLog net_log_; | 125 const BoundNetLog net_log_; |
106 bool was_npn_negotiated_; | 126 bool was_npn_negotiated_; |
107 SSLClientSocket::NextProto protocol_negotiated_; | 127 SSLClientSocket::NextProto protocol_negotiated_; |
108 }; | 128 }; |
109 | 129 |
110 TEST_F(HttpPipelinedHostPoolTest, DefaultUnknown) { | 130 TEST_F(HttpPipelinedHostPoolTest, DefaultUnknown) { |
111 EXPECT_TRUE(pool_->IsHostEligibleForPipelining(origin_)); | 131 EXPECT_TRUE(pool_->IsKeyEligibleForPipelining(key_)); |
112 EXPECT_CALL(*factory_, CreateNewHost(pool_.get(), Ref(origin_), _, | 132 EXPECT_CALL(*factory_, CreateNewHost(pool_.get(), Ref(key_), _, |
113 PIPELINE_UNKNOWN)) | 133 PIPELINE_UNKNOWN, false)) |
114 .Times(1) | 134 .Times(1) |
115 .WillOnce(Return(host_)); | 135 .WillOnce(Return(host_)); |
116 | 136 |
117 CreateDummyStream(); | 137 CreateDummyStream(key_, kDummyConnection, kDummyStream, host_); |
118 pool_->OnHostIdle(host_); | 138 pool_->OnHostIdle(host_); |
119 } | 139 } |
120 | 140 |
121 TEST_F(HttpPipelinedHostPoolTest, RemembersIncapable) { | 141 TEST_F(HttpPipelinedHostPoolTest, RemembersIncapable) { |
122 EXPECT_CALL(*factory_, CreateNewHost(pool_.get(), Ref(origin_), _, | 142 EXPECT_CALL(*factory_, CreateNewHost(pool_.get(), Ref(key_), _, |
123 PIPELINE_UNKNOWN)) | 143 PIPELINE_UNKNOWN, false)) |
124 .Times(1) | 144 .Times(1) |
125 .WillOnce(Return(host_)); | 145 .WillOnce(Return(host_)); |
126 | 146 |
127 CreateDummyStream(); | 147 CreateDummyStream(key_, kDummyConnection, kDummyStream, host_); |
128 pool_->OnHostDeterminedCapability(host_, PIPELINE_INCAPABLE); | 148 pool_->OnHostDeterminedCapability(host_, PIPELINE_INCAPABLE); |
129 pool_->OnHostIdle(host_); | 149 pool_->OnHostIdle(host_); |
130 EXPECT_FALSE(pool_->IsHostEligibleForPipelining(origin_)); | 150 EXPECT_FALSE(pool_->IsKeyEligibleForPipelining(key_)); |
131 EXPECT_CALL(*factory_, CreateNewHost(pool_.get(), Ref(origin_), _, | 151 EXPECT_CALL(*factory_, CreateNewHost(pool_.get(), Ref(key_), _, |
132 PIPELINE_INCAPABLE)) | 152 PIPELINE_INCAPABLE, false)) |
133 .Times(0); | 153 .Times(0); |
134 EXPECT_EQ(NULL, | 154 EXPECT_EQ(NULL, |
135 pool_->CreateStreamOnNewPipeline(origin_, kDummyConnection, | 155 pool_->CreateStreamOnNewPipeline(key_, kDummyConnection, |
136 ssl_config_, proxy_info_, net_log_, | 156 ssl_config_, proxy_info_, net_log_, |
137 was_npn_negotiated_, | 157 was_npn_negotiated_, |
138 protocol_negotiated_)); | 158 protocol_negotiated_)); |
139 } | 159 } |
140 | 160 |
141 TEST_F(HttpPipelinedHostPoolTest, RemembersCapable) { | 161 TEST_F(HttpPipelinedHostPoolTest, RemembersCapable) { |
142 EXPECT_CALL(*factory_, CreateNewHost(pool_.get(), Ref(origin_), _, | 162 EXPECT_CALL(*factory_, CreateNewHost(pool_.get(), Ref(key_), _, |
143 PIPELINE_UNKNOWN)) | 163 PIPELINE_UNKNOWN, false)) |
144 .Times(1) | 164 .Times(1) |
145 .WillOnce(Return(host_)); | 165 .WillOnce(Return(host_)); |
146 | 166 |
147 CreateDummyStream(); | 167 CreateDummyStream(key_, kDummyConnection, kDummyStream, host_); |
148 pool_->OnHostDeterminedCapability(host_, PIPELINE_CAPABLE); | 168 pool_->OnHostDeterminedCapability(host_, PIPELINE_CAPABLE); |
149 pool_->OnHostIdle(host_); | 169 pool_->OnHostIdle(host_); |
150 EXPECT_TRUE(pool_->IsHostEligibleForPipelining(origin_)); | 170 EXPECT_TRUE(pool_->IsKeyEligibleForPipelining(key_)); |
151 | 171 |
152 host_ = new MockHost(origin_); | 172 host_ = new MockHost(key_); |
153 EXPECT_CALL(*factory_, CreateNewHost(pool_.get(), Ref(origin_), _, | 173 EXPECT_CALL(*factory_, CreateNewHost(pool_.get(), Ref(key_), _, |
154 PIPELINE_CAPABLE)) | 174 PIPELINE_CAPABLE, false)) |
155 .Times(1) | 175 .Times(1) |
156 .WillOnce(Return(host_)); | 176 .WillOnce(Return(host_)); |
157 CreateDummyStream(); | 177 CreateDummyStream(key_, kDummyConnection, kDummyStream, host_); |
158 pool_->OnHostIdle(host_); | 178 pool_->OnHostIdle(host_); |
159 } | 179 } |
160 | 180 |
161 TEST_F(HttpPipelinedHostPoolTest, IncapableIsSticky) { | 181 TEST_F(HttpPipelinedHostPoolTest, IncapableIsSticky) { |
162 EXPECT_CALL(*factory_, CreateNewHost(pool_.get(), Ref(origin_), _, | 182 EXPECT_CALL(*factory_, CreateNewHost(pool_.get(), Ref(key_), _, |
163 PIPELINE_UNKNOWN)) | 183 PIPELINE_UNKNOWN, false)) |
164 .Times(1) | 184 .Times(1) |
165 .WillOnce(Return(host_)); | 185 .WillOnce(Return(host_)); |
166 | 186 |
167 CreateDummyStream(); | 187 CreateDummyStream(key_, kDummyConnection, kDummyStream, host_); |
168 pool_->OnHostDeterminedCapability(host_, PIPELINE_CAPABLE); | 188 pool_->OnHostDeterminedCapability(host_, PIPELINE_CAPABLE); |
169 pool_->OnHostDeterminedCapability(host_, PIPELINE_INCAPABLE); | 189 pool_->OnHostDeterminedCapability(host_, PIPELINE_INCAPABLE); |
170 pool_->OnHostDeterminedCapability(host_, PIPELINE_CAPABLE); | 190 pool_->OnHostDeterminedCapability(host_, PIPELINE_CAPABLE); |
171 pool_->OnHostIdle(host_); | 191 pool_->OnHostIdle(host_); |
172 EXPECT_FALSE(pool_->IsHostEligibleForPipelining(origin_)); | 192 EXPECT_FALSE(pool_->IsKeyEligibleForPipelining(key_)); |
173 } | 193 } |
174 | 194 |
175 TEST_F(HttpPipelinedHostPoolTest, RemainsUnknownWithoutFeedback) { | 195 TEST_F(HttpPipelinedHostPoolTest, RemainsUnknownWithoutFeedback) { |
176 EXPECT_CALL(*factory_, CreateNewHost(pool_.get(), Ref(origin_), _, | 196 EXPECT_CALL(*factory_, CreateNewHost(pool_.get(), Ref(key_), _, |
177 PIPELINE_UNKNOWN)) | 197 PIPELINE_UNKNOWN, false)) |
178 .Times(1) | 198 .Times(1) |
179 .WillOnce(Return(host_)); | 199 .WillOnce(Return(host_)); |
180 | 200 |
181 CreateDummyStream(); | 201 CreateDummyStream(key_, kDummyConnection, kDummyStream, host_); |
182 pool_->OnHostIdle(host_); | 202 pool_->OnHostIdle(host_); |
183 EXPECT_TRUE(pool_->IsHostEligibleForPipelining(origin_)); | 203 EXPECT_TRUE(pool_->IsKeyEligibleForPipelining(key_)); |
184 | 204 |
185 host_ = new MockHost(origin_); | 205 host_ = new MockHost(key_); |
186 EXPECT_CALL(*factory_, CreateNewHost(pool_.get(), Ref(origin_), _, | 206 EXPECT_CALL(*factory_, CreateNewHost(pool_.get(), Ref(key_), _, |
187 PIPELINE_UNKNOWN)) | 207 PIPELINE_UNKNOWN, false)) |
188 .Times(1) | 208 .Times(1) |
189 .WillOnce(Return(host_)); | 209 .WillOnce(Return(host_)); |
190 | 210 |
191 CreateDummyStream(); | 211 CreateDummyStream(key_, kDummyConnection, kDummyStream, host_); |
192 pool_->OnHostIdle(host_); | 212 pool_->OnHostIdle(host_); |
193 } | 213 } |
194 | 214 |
195 TEST_F(HttpPipelinedHostPoolTest, PopulatesServerProperties) { | 215 TEST_F(HttpPipelinedHostPoolTest, PopulatesServerProperties) { |
196 EXPECT_EQ(PIPELINE_UNKNOWN, | 216 EXPECT_EQ(PIPELINE_UNKNOWN, |
197 http_server_properties_->GetPipelineCapability(host_->origin())); | 217 http_server_properties_->GetPipelineCapability( |
| 218 host_->GetKey().origin())); |
198 pool_->OnHostDeterminedCapability(host_, PIPELINE_CAPABLE); | 219 pool_->OnHostDeterminedCapability(host_, PIPELINE_CAPABLE); |
199 EXPECT_EQ(PIPELINE_CAPABLE, | 220 EXPECT_EQ(PIPELINE_CAPABLE, |
200 http_server_properties_->GetPipelineCapability(host_->origin())); | 221 http_server_properties_->GetPipelineCapability( |
| 222 host_->GetKey().origin())); |
201 delete host_; // Must manually delete, because it's never added to |pool_|. | 223 delete host_; // Must manually delete, because it's never added to |pool_|. |
202 } | 224 } |
203 | 225 |
| 226 TEST_F(HttpPipelinedHostPoolTest, MultipleKeys) { |
| 227 HttpPipelinedHost::Key key1(HostPortPair("host", 123)); |
| 228 HttpPipelinedHost::Key key2(HostPortPair("host", 456)); |
| 229 HttpPipelinedHost::Key key3(HostPortPair("other", 456)); |
| 230 HttpPipelinedHost::Key key4(HostPortPair("other", 789)); |
| 231 MockHost* host1 = CreateDummyHost(key1); |
| 232 MockHost* host2 = CreateDummyHost(key2); |
| 233 MockHost* host3 = CreateDummyHost(key3); |
| 234 |
| 235 EXPECT_CALL(*host1, IsExistingPipelineAvailable()) |
| 236 .Times(1) |
| 237 .WillOnce(Return(true)); |
| 238 EXPECT_EQ(true, pool_->IsExistingPipelineAvailableForKey(key1)); |
| 239 |
| 240 EXPECT_CALL(*host2, IsExistingPipelineAvailable()) |
| 241 .Times(1) |
| 242 .WillOnce(Return(false)); |
| 243 EXPECT_EQ(false, pool_->IsExistingPipelineAvailableForKey(key2)); |
| 244 |
| 245 EXPECT_CALL(*host3, IsExistingPipelineAvailable()) |
| 246 .Times(1) |
| 247 .WillOnce(Return(true)); |
| 248 EXPECT_EQ(true, pool_->IsExistingPipelineAvailableForKey(key3)); |
| 249 |
| 250 EXPECT_EQ(false, pool_->IsExistingPipelineAvailableForKey(key4)); |
| 251 |
| 252 pool_->OnHostIdle(host1); |
| 253 pool_->OnHostIdle(host2); |
| 254 pool_->OnHostIdle(host3); |
| 255 |
| 256 delete host_; // Must manually delete, because it's never added to |pool_|. |
| 257 } |
| 258 |
204 } // anonymous namespace | 259 } // anonymous namespace |
205 | 260 |
206 } // namespace net | 261 } // namespace net |
OLD | NEW |