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