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