Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "net/http/http_pipelined_host_impl.h" | |
| 6 | |
| 7 #include "base/memory/scoped_ptr.h" | |
| 8 #include "net/base/ssl_config_service.h" | |
| 9 #include "net/http/http_pipelined_connection.h" | |
| 10 #include "net/proxy/proxy_info.h" | |
| 11 #include "testing/gmock/include/gmock/gmock.h" | |
| 12 #include "testing/gtest/include/gtest/gtest.h" | |
| 13 | |
| 14 using testing::_; | |
| 15 using testing::NiceMock; | |
| 16 using testing::Ref; | |
| 17 using testing::Return; | |
| 18 using testing::ReturnNull; | |
| 19 | |
| 20 static const int kMaxCapacity = 3; | |
|
mmenke
2011/11/28 23:05:28
Think it's best to just use HttpPipelinedHostImpl:
James Simonsen
2011/12/01 01:17:10
Done.
FYI, I expect max_pipeline_capacity() to be
mmenke
2011/12/01 02:17:31
Yea, figured that was the reason, but then you'll
| |
| 21 | |
| 22 namespace net { | |
| 23 | |
| 24 static ClientSocketHandle* kDummyConnection = | |
| 25 reinterpret_cast<ClientSocketHandle*>(84); | |
|
mmenke
2011/11/28 23:05:28
Can you use an anonymous namespace here, too? And
James Simonsen
2011/12/01 01:17:10
Done.
| |
| 26 static HttpPipelinedStream* kDummyStream = | |
| 27 reinterpret_cast<HttpPipelinedStream*>(42); | |
| 28 | |
| 29 class MockHostDelegate : public HttpPipelinedHost::Delegate { | |
| 30 public: | |
| 31 MOCK_METHOD1(OnHostIdle, void(HttpPipelinedHost* host)); | |
| 32 MOCK_METHOD1(OnHostHasAdditionalCapacity, void(HttpPipelinedHost* host)); | |
| 33 MOCK_METHOD2(OnHostDeterminedCapability, | |
| 34 void(HttpPipelinedHost* host, | |
| 35 HttpPipelinedHost::Capability capability)); | |
| 36 }; | |
| 37 | |
| 38 class MockPipelineFactory : public HttpPipelinedConnection::Factory { | |
| 39 public: | |
| 40 MOCK_METHOD6(CreateNewPipeline, HttpPipelinedConnection*( | |
| 41 ClientSocketHandle* connection, | |
| 42 HttpPipelinedConnection::Delegate* delegate, | |
| 43 const SSLConfig& used_ssl_config, | |
| 44 const ProxyInfo& used_proxy_info, | |
| 45 const BoundNetLog& net_log, | |
| 46 bool was_npn_negotiated)); | |
| 47 }; | |
| 48 | |
| 49 class MockPipeline : public HttpPipelinedConnection { | |
| 50 public: | |
| 51 MockPipeline(int depth, bool usable, bool active) | |
| 52 : depth_(depth), | |
| 53 usable_(usable), | |
| 54 active_(active) { | |
| 55 } | |
| 56 | |
| 57 void SetState(int depth, bool usable, bool active) { | |
| 58 depth_ = depth; | |
| 59 usable_ = usable; | |
| 60 active_ = active; | |
| 61 } | |
| 62 | |
| 63 virtual int depth() const OVERRIDE { return depth_; } | |
| 64 virtual bool usable() const OVERRIDE { return usable_; } | |
| 65 virtual bool active() const OVERRIDE { return active_; } | |
| 66 | |
| 67 MOCK_METHOD0(CreateNewStream, HttpPipelinedStream*()); | |
| 68 MOCK_METHOD1(OnStreamDeleted, void(int pipeline_id)); | |
| 69 MOCK_CONST_METHOD0(used_ssl_config, const SSLConfig&()); | |
| 70 MOCK_CONST_METHOD0(used_proxy_info, const ProxyInfo&()); | |
| 71 MOCK_CONST_METHOD0(source, const NetLog::Source&()); | |
| 72 MOCK_CONST_METHOD0(was_npn_negotiated, bool()); | |
| 73 | |
| 74 private: | |
| 75 int depth_; | |
| 76 bool usable_; | |
| 77 bool active_; | |
| 78 }; | |
| 79 | |
| 80 class HttpPipelinedHostImplTest : public testing::Test { | |
| 81 public: | |
| 82 HttpPipelinedHostImplTest() | |
| 83 : origin_("host", 123), | |
| 84 factory_(new MockPipelineFactory), // Owned by host_. | |
| 85 host_(new HttpPipelinedHostImpl(&delegate_, origin_, factory_, | |
| 86 HttpPipelinedHost::CAPABLE)) { | |
| 87 } | |
| 88 | |
| 89 void SetCapability(HttpPipelinedHost::Capability capability) { | |
| 90 factory_ = new MockPipelineFactory; | |
| 91 host_.reset(new HttpPipelinedHostImpl( | |
| 92 &delegate_, origin_, factory_, capability)); | |
| 93 } | |
| 94 | |
| 95 MockPipeline* AddTestPipeline(int depth, bool usable, bool active) { | |
| 96 MockPipeline* pipeline = new MockPipeline(depth, usable, active); | |
| 97 EXPECT_CALL(*factory_, CreateNewPipeline(kDummyConnection, host_.get(), | |
| 98 Ref(ssl_config_), Ref(proxy_info_), | |
| 99 Ref(net_log_), true)) | |
| 100 .Times(1) | |
| 101 .WillOnce(Return(pipeline)); | |
| 102 EXPECT_CALL(*pipeline, CreateNewStream()) | |
| 103 .Times(1) | |
| 104 .WillOnce(Return(kDummyStream)); | |
| 105 EXPECT_EQ(kDummyStream, host_->CreateStreamOnNewPipeline( | |
| 106 kDummyConnection, ssl_config_, proxy_info_, net_log_, true)); | |
| 107 return pipeline; | |
| 108 } | |
| 109 | |
| 110 void ClearTestPipeline(MockPipeline* pipeline) { | |
| 111 pipeline->SetState(0, true, true); | |
| 112 host_->OnPipelineHasCapacity(pipeline); | |
| 113 } | |
| 114 | |
| 115 NiceMock<MockHostDelegate> delegate_; | |
| 116 HostPortPair origin_; | |
| 117 MockPipelineFactory* factory_; | |
| 118 scoped_ptr<HttpPipelinedHostImpl> host_; | |
| 119 | |
| 120 SSLConfig ssl_config_; | |
| 121 ProxyInfo proxy_info_; | |
| 122 BoundNetLog net_log_; | |
| 123 }; | |
| 124 | |
| 125 TEST_F(HttpPipelinedHostImplTest, Delegate) { | |
| 126 EXPECT_TRUE(origin_.Equals(host_->origin())); | |
| 127 } | |
| 128 | |
| 129 TEST_F(HttpPipelinedHostImplTest, OnHostIdle) { | |
|
mmenke
2011/11/28 23:05:28
nit: Maybe OnHostIdleUnusablePipeline?
Or maybe
James Simonsen
2011/12/01 01:17:10
Done. Went with the latter.
| |
| 130 MockPipeline* pipeline = AddTestPipeline(0, false, true); | |
| 131 | |
| 132 EXPECT_CALL(delegate_, OnHostHasAdditionalCapacity(host_.get())) | |
| 133 .Times(0); | |
| 134 EXPECT_CALL(delegate_, OnHostIdle(host_.get())) | |
| 135 .Times(1); | |
| 136 host_->OnPipelineHasCapacity(pipeline); | |
|
mmenke
2011/11/28 23:05:28
Might want to make sure |pipeline| gets deleted.
James Simonsen
2011/12/01 01:17:10
I think we're already covered here. Valgrind will
| |
| 137 } | |
| 138 | |
| 139 TEST_F(HttpPipelinedHostImplTest, OnHostHasAdditionalCapacity) { | |
| 140 MockPipeline* pipeline = AddTestPipeline(1, true, true); | |
| 141 | |
| 142 EXPECT_CALL(delegate_, OnHostHasAdditionalCapacity(host_.get())) | |
| 143 .Times(2); | |
|
mmenke
2011/11/28 23:05:28
I think you should split this into two lines, one
James Simonsen
2011/12/01 01:17:10
Done.
| |
| 144 EXPECT_CALL(delegate_, OnHostIdle(host_.get())) | |
| 145 .Times(0); | |
| 146 | |
| 147 host_->OnPipelineHasCapacity(pipeline); | |
| 148 | |
| 149 EXPECT_CALL(delegate_, OnHostIdle(host_.get())) | |
| 150 .Times(1); | |
| 151 ClearTestPipeline(pipeline); | |
| 152 } | |
| 153 | |
| 154 TEST_F(HttpPipelinedHostImplTest, IgnoresUnusablePipeline) { | |
| 155 MockPipeline* pipeline = AddTestPipeline(1, false, true); | |
| 156 | |
| 157 EXPECT_FALSE(host_->IsExistingPipelineAvailable()); | |
| 158 EXPECT_EQ(NULL, host_->CreateStreamOnExistingPipeline()); | |
| 159 | |
| 160 ClearTestPipeline(pipeline); | |
| 161 } | |
| 162 | |
| 163 TEST_F(HttpPipelinedHostImplTest, IgnoresInactivePipeline) { | |
| 164 MockPipeline* pipeline = AddTestPipeline(1, true, false); | |
| 165 | |
| 166 EXPECT_FALSE(host_->IsExistingPipelineAvailable()); | |
| 167 EXPECT_EQ(NULL, host_->CreateStreamOnExistingPipeline()); | |
| 168 | |
| 169 ClearTestPipeline(pipeline); | |
| 170 } | |
| 171 | |
| 172 TEST_F(HttpPipelinedHostImplTest, IgnoresFullPipeline) { | |
| 173 MockPipeline* pipeline = AddTestPipeline(kMaxCapacity, true, true); | |
| 174 | |
| 175 EXPECT_FALSE(host_->IsExistingPipelineAvailable()); | |
| 176 EXPECT_EQ(NULL, host_->CreateStreamOnExistingPipeline()); | |
| 177 | |
| 178 ClearTestPipeline(pipeline); | |
| 179 } | |
| 180 | |
| 181 TEST_F(HttpPipelinedHostImplTest, PicksLeastLoadedPipeline) { | |
| 182 MockPipeline* full_pipeline = AddTestPipeline(kMaxCapacity, true, true); | |
| 183 MockPipeline* usable_pipeline = AddTestPipeline(kMaxCapacity - 1, true, true); | |
| 184 MockPipeline* empty_pipeline = AddTestPipeline(0, true, true); | |
| 185 | |
| 186 EXPECT_TRUE(host_->IsExistingPipelineAvailable()); | |
| 187 EXPECT_CALL(*empty_pipeline, CreateNewStream()) | |
| 188 .Times(1) | |
| 189 .WillOnce(ReturnNull()); | |
| 190 EXPECT_EQ(NULL, host_->CreateStreamOnExistingPipeline()); | |
| 191 | |
| 192 ClearTestPipeline(full_pipeline); | |
| 193 ClearTestPipeline(usable_pipeline); | |
| 194 ClearTestPipeline(empty_pipeline); | |
| 195 } | |
| 196 | |
| 197 TEST_F(HttpPipelinedHostImplTest, EmptyPipelineIsRemoved) { | |
| 198 MockPipeline* empty_pipeline = AddTestPipeline(0, true, true); | |
| 199 | |
| 200 EXPECT_TRUE(host_->IsExistingPipelineAvailable()); | |
| 201 EXPECT_CALL(*empty_pipeline, CreateNewStream()) | |
| 202 .Times(1) | |
| 203 .WillOnce(Return(kDummyStream)); | |
| 204 EXPECT_EQ(kDummyStream, host_->CreateStreamOnExistingPipeline()); | |
|
mmenke
2011/11/28 23:05:28
Is there really a need to create a dummy stream he
James Simonsen
2011/12/01 01:17:10
Hmm. I guess not. The whole test doesn't seem all
| |
| 205 | |
| 206 ClearTestPipeline(empty_pipeline); | |
| 207 | |
| 208 EXPECT_FALSE(host_->IsExistingPipelineAvailable()); | |
| 209 EXPECT_EQ(NULL, host_->CreateStreamOnExistingPipeline()); | |
| 210 } | |
| 211 | |
| 212 TEST_F(HttpPipelinedHostImplTest, OpensUpOnPipelineSuccess) { | |
| 213 SetCapability(HttpPipelinedHost::UNKNOWN); | |
| 214 MockPipeline* pipeline = AddTestPipeline(1, true, true); | |
| 215 | |
| 216 EXPECT_EQ(NULL, host_->CreateStreamOnExistingPipeline()); | |
| 217 EXPECT_CALL(delegate_, OnHostHasAdditionalCapacity(host_.get())) | |
| 218 .Times(1); | |
| 219 host_->OnPipelineFeedback(pipeline, HttpPipelinedConnection::OK); | |
| 220 | |
| 221 EXPECT_CALL(*pipeline, CreateNewStream()) | |
| 222 .Times(1) | |
| 223 .WillOnce(Return(kDummyStream)); | |
| 224 EXPECT_EQ(kDummyStream, host_->CreateStreamOnExistingPipeline()); | |
|
mmenke
2011/11/28 23:05:28
You don't seem to have a test here that this call
James Simonsen
2011/12/01 01:17:10
I'm probably misunderstanding you. The 4th line of
mmenke
2011/12/01 02:17:31
You aren't misunderstanding me. I just managed to
| |
| 225 | |
| 226 EXPECT_CALL(delegate_, OnHostHasAdditionalCapacity(host_.get())) | |
| 227 .Times(1); | |
| 228 ClearTestPipeline(pipeline); | |
| 229 } | |
| 230 | |
| 231 TEST_F(HttpPipelinedHostImplTest, ShutsDownOnOldVersion) { | |
| 232 SetCapability(HttpPipelinedHost::UNKNOWN); | |
| 233 MockPipeline* pipeline = AddTestPipeline(1, true, true); | |
| 234 | |
| 235 EXPECT_EQ(NULL, host_->CreateStreamOnExistingPipeline()); | |
| 236 EXPECT_CALL(delegate_, OnHostHasAdditionalCapacity(host_.get())) | |
| 237 .Times(0); | |
| 238 EXPECT_CALL(delegate_, | |
| 239 OnHostDeterminedCapability(host_.get(), | |
| 240 HttpPipelinedHost::INCAPABLE)) | |
| 241 .Times(1); | |
| 242 host_->OnPipelineFeedback(pipeline, | |
| 243 HttpPipelinedConnection::OLD_HTTP_VERSION); | |
| 244 | |
| 245 ClearTestPipeline(pipeline); | |
| 246 EXPECT_EQ(NULL, host_->CreateStreamOnExistingPipeline()); | |
|
mmenke
2011/11/28 23:05:28
This doesn't seem to be testing anything. ClearTe
James Simonsen
2011/12/01 01:17:10
Done.
| |
| 247 } | |
| 248 | |
| 249 TEST_F(HttpPipelinedHostImplTest, ConnectionCloseHasNoEffect) { | |
| 250 SetCapability(HttpPipelinedHost::UNKNOWN); | |
| 251 MockPipeline* pipeline = AddTestPipeline(1, true, true); | |
| 252 | |
| 253 EXPECT_CALL(delegate_, OnHostHasAdditionalCapacity(host_.get())) | |
| 254 .Times(0); | |
| 255 EXPECT_CALL(delegate_, OnHostDeterminedCapability(host_.get(), _)) | |
| 256 .Times(0); | |
| 257 host_->OnPipelineFeedback(pipeline, | |
| 258 HttpPipelinedConnection::MUST_CLOSE_CONNECTION); | |
| 259 EXPECT_EQ(NULL, host_->CreateStreamOnExistingPipeline()); | |
| 260 | |
| 261 EXPECT_CALL(delegate_, OnHostHasAdditionalCapacity(host_.get())) | |
| 262 .Times(1); | |
| 263 ClearTestPipeline(pipeline); | |
| 264 } | |
| 265 | |
| 266 TEST_F(HttpPipelinedHostImplTest, SuccessesLeadToCapable) { | |
| 267 SetCapability(HttpPipelinedHost::UNKNOWN); | |
| 268 MockPipeline* pipeline = AddTestPipeline(1, true, true); | |
| 269 | |
| 270 EXPECT_CALL(delegate_, OnHostHasAdditionalCapacity(host_.get())) | |
| 271 .Times(1); | |
| 272 EXPECT_CALL(delegate_, | |
| 273 OnHostDeterminedCapability(host_.get(), | |
| 274 HttpPipelinedHost::CAPABLE)) | |
| 275 .Times(1); | |
| 276 host_->OnPipelineFeedback(pipeline, HttpPipelinedConnection::OK); | |
| 277 | |
| 278 pipeline->SetState(3, true, true); | |
| 279 host_->OnPipelineFeedback(pipeline, HttpPipelinedConnection::OK); | |
| 280 host_->OnPipelineFeedback(pipeline, HttpPipelinedConnection::OK); | |
| 281 | |
| 282 EXPECT_CALL(delegate_, OnHostHasAdditionalCapacity(host_.get())) | |
| 283 .Times(1); | |
| 284 ClearTestPipeline(pipeline); | |
| 285 } | |
| 286 | |
| 287 } // namespace net | |
| OLD | NEW |