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.h" |
| 6 |
| 7 #include "net/base/ssl_config_service.h" |
| 8 #include "net/http/http_pipelined_connection.h" |
| 9 #include "net/proxy/proxy_info.h" |
| 10 #include "testing/gmock/include/gmock/gmock.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" |
| 12 |
| 13 using testing::_; |
| 14 using testing::NiceMock; |
| 15 using testing::Ref; |
| 16 using testing::Return; |
| 17 using testing::ReturnNull; |
| 18 |
| 19 static const int kMaxCapacity = 3; |
| 20 |
| 21 namespace net { |
| 22 |
| 23 static HttpPipelinedStream* kDummyStream = |
| 24 reinterpret_cast<HttpPipelinedStream*>(42); |
| 25 |
| 26 class MockHostDelegate : public HttpPipelinedHost::Delegate { |
| 27 public: |
| 28 MOCK_METHOD1(OnHostIdle, void(HttpPipelinedHost* host)); |
| 29 MOCK_METHOD1(OnHostHasAdditionalCapacity, void(HttpPipelinedHost* host)); |
| 30 }; |
| 31 |
| 32 class MockPipelineFactory : public HttpPipelinedConnection::Factory { |
| 33 public: |
| 34 MOCK_METHOD6(CreateNewPipeline, HttpPipelinedConnection*( |
| 35 ClientSocketHandle* connection, |
| 36 HttpPipelinedConnection::Delegate* delegate, |
| 37 const SSLConfig& used_ssl_config, |
| 38 const ProxyInfo& used_proxy_info, |
| 39 const BoundNetLog& net_log, |
| 40 bool was_npn_negotiated)); |
| 41 }; |
| 42 |
| 43 class MockPipeline : public HttpPipelinedConnection { |
| 44 public: |
| 45 MockPipeline(int depth, bool usable, bool active) |
| 46 : depth_(depth), |
| 47 usable_(usable), |
| 48 active_(active) { |
| 49 } |
| 50 |
| 51 void SetState(int depth, bool usable, bool active) { |
| 52 depth_ = depth; |
| 53 usable_ = usable; |
| 54 active_ = active; |
| 55 } |
| 56 |
| 57 virtual int depth() const OVERRIDE { return depth_; } |
| 58 virtual bool usable() const OVERRIDE { return usable_; } |
| 59 virtual bool active() const OVERRIDE { return active_; } |
| 60 |
| 61 MOCK_METHOD0(CreateNewStream, HttpPipelinedStream*()); |
| 62 MOCK_METHOD1(OnStreamDeleted, void(int pipeline_id)); |
| 63 MOCK_CONST_METHOD0(used_ssl_config, const SSLConfig&()); |
| 64 MOCK_CONST_METHOD0(used_proxy_info, const ProxyInfo&()); |
| 65 MOCK_CONST_METHOD0(source, const NetLog::Source&()); |
| 66 MOCK_CONST_METHOD0(was_npn_negotiated, bool()); |
| 67 |
| 68 private: |
| 69 int depth_; |
| 70 bool usable_; |
| 71 bool active_; |
| 72 }; |
| 73 |
| 74 class HttpPipelinedHostTest : public testing::Test { |
| 75 public: |
| 76 HttpPipelinedHostTest() |
| 77 : origin_("host", 123), |
| 78 factory_(new MockPipelineFactory), // Owned by host_. |
| 79 host_(&delegate_, origin_, factory_) { |
| 80 } |
| 81 |
| 82 MockPipeline* AddTestPipeline(int depth, bool usable, bool active) { |
| 83 MockPipeline* pipeline = new MockPipeline(depth, usable, active); |
| 84 EXPECT_CALL(*factory_, CreateNewPipeline(connection_, &host_, |
| 85 Ref(ssl_config_), Ref(proxy_info_), |
| 86 Ref(net_log_), true)) |
| 87 .Times(1) |
| 88 .WillOnce(Return(pipeline)); |
| 89 EXPECT_CALL(*pipeline, CreateNewStream()) |
| 90 .Times(1) |
| 91 .WillOnce(Return(kDummyStream)); |
| 92 EXPECT_EQ(kDummyStream, host_.CreateStreamOnNewPipeline( |
| 93 connection_, ssl_config_, proxy_info_, net_log_, true)); |
| 94 return pipeline; |
| 95 } |
| 96 |
| 97 void ClearTestPipeline(MockPipeline* pipeline) { |
| 98 pipeline->SetState(0, true, true); |
| 99 host_.OnPipelineHasCapacity(pipeline); |
| 100 } |
| 101 |
| 102 NiceMock<MockHostDelegate> delegate_; |
| 103 HostPortPair origin_; |
| 104 MockPipelineFactory* factory_; |
| 105 HttpPipelinedHost host_; |
| 106 |
| 107 ClientSocketHandle* connection_; |
| 108 SSLConfig ssl_config_; |
| 109 ProxyInfo proxy_info_; |
| 110 BoundNetLog net_log_; |
| 111 }; |
| 112 |
| 113 TEST_F(HttpPipelinedHostTest, Delegate) { |
| 114 EXPECT_TRUE(origin_.Equals(host_.origin())); |
| 115 } |
| 116 |
| 117 TEST_F(HttpPipelinedHostTest, OnHostIdle) { |
| 118 MockPipeline* pipeline = AddTestPipeline(0, false, true); |
| 119 |
| 120 EXPECT_CALL(delegate_, OnHostHasAdditionalCapacity(&host_)) |
| 121 .Times(0); |
| 122 EXPECT_CALL(delegate_, OnHostIdle(&host_)) |
| 123 .Times(1); |
| 124 host_.OnPipelineHasCapacity(pipeline); |
| 125 } |
| 126 |
| 127 TEST_F(HttpPipelinedHostTest, OnHostHasAdditionalCapacity) { |
| 128 MockPipeline* pipeline = AddTestPipeline(1, true, true); |
| 129 |
| 130 EXPECT_CALL(delegate_, OnHostHasAdditionalCapacity(&host_)) |
| 131 .Times(2); |
| 132 EXPECT_CALL(delegate_, OnHostIdle(&host_)) |
| 133 .Times(0); |
| 134 |
| 135 host_.OnPipelineHasCapacity(pipeline); |
| 136 |
| 137 EXPECT_CALL(delegate_, OnHostIdle(&host_)) |
| 138 .Times(1); |
| 139 ClearTestPipeline(pipeline); |
| 140 } |
| 141 |
| 142 TEST_F(HttpPipelinedHostTest, IgnoresUnusablePipeline) { |
| 143 MockPipeline* pipeline = AddTestPipeline(1, false, true); |
| 144 |
| 145 EXPECT_FALSE(host_.IsExistingPipelineAvailable()); |
| 146 EXPECT_EQ(NULL, host_.CreateStreamOnExistingPipeline()); |
| 147 |
| 148 ClearTestPipeline(pipeline); |
| 149 } |
| 150 |
| 151 TEST_F(HttpPipelinedHostTest, IgnoresInactivePipeline) { |
| 152 MockPipeline* pipeline = AddTestPipeline(1, true, false); |
| 153 |
| 154 EXPECT_FALSE(host_.IsExistingPipelineAvailable()); |
| 155 EXPECT_EQ(NULL, host_.CreateStreamOnExistingPipeline()); |
| 156 |
| 157 ClearTestPipeline(pipeline); |
| 158 } |
| 159 |
| 160 TEST_F(HttpPipelinedHostTest, IgnoresFullPipeline) { |
| 161 MockPipeline* pipeline = AddTestPipeline(kMaxCapacity, true, true); |
| 162 |
| 163 EXPECT_FALSE(host_.IsExistingPipelineAvailable()); |
| 164 EXPECT_EQ(NULL, host_.CreateStreamOnExistingPipeline()); |
| 165 |
| 166 ClearTestPipeline(pipeline); |
| 167 } |
| 168 |
| 169 TEST_F(HttpPipelinedHostTest, PicksLeastLoadedPipeline) { |
| 170 MockPipeline* full_pipeline = AddTestPipeline(kMaxCapacity, true, true); |
| 171 MockPipeline* usable_pipeline = AddTestPipeline(kMaxCapacity - 1, true, true); |
| 172 MockPipeline* empty_pipeline = AddTestPipeline(0, true, true); |
| 173 |
| 174 EXPECT_TRUE(host_.IsExistingPipelineAvailable()); |
| 175 EXPECT_CALL(*empty_pipeline, CreateNewStream()) |
| 176 .Times(1) |
| 177 .WillOnce(ReturnNull()); |
| 178 EXPECT_EQ(NULL, host_.CreateStreamOnExistingPipeline()); |
| 179 |
| 180 ClearTestPipeline(full_pipeline); |
| 181 ClearTestPipeline(usable_pipeline); |
| 182 ClearTestPipeline(empty_pipeline); |
| 183 } |
| 184 |
| 185 TEST_F(HttpPipelinedHostTest, EmptyPipelineIsRemoved) { |
| 186 MockPipeline* empty_pipeline = AddTestPipeline(0, true, true); |
| 187 |
| 188 EXPECT_TRUE(host_.IsExistingPipelineAvailable()); |
| 189 EXPECT_CALL(*empty_pipeline, CreateNewStream()) |
| 190 .Times(1) |
| 191 .WillOnce(Return(kDummyStream)); |
| 192 EXPECT_EQ(kDummyStream, host_.CreateStreamOnExistingPipeline()); |
| 193 |
| 194 ClearTestPipeline(empty_pipeline); |
| 195 |
| 196 EXPECT_FALSE(host_.IsExistingPipelineAvailable()); |
| 197 EXPECT_EQ(NULL, host_.CreateStreamOnExistingPipeline()); |
| 198 } |
| 199 |
| 200 } // namespace net |
OLD | NEW |