Chromium Code Reviews| Index: net/http/http_pipelined_host_unittest.cc |
| diff --git a/net/http/http_pipelined_host_unittest.cc b/net/http/http_pipelined_host_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..faa04bbe6c9477962149a1968b40f40f5e833ee4 |
| --- /dev/null |
| +++ b/net/http/http_pipelined_host_unittest.cc |
| @@ -0,0 +1,178 @@ |
| +// Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "net/http/http_pipelined_host.h" |
| + |
| +#include "net/base/ssl_config_service.h" |
| +#include "net/http/http_pipelined_connection.h" |
| +#include "net/proxy/proxy_info.h" |
| +#include "testing/gmock/include/gmock/gmock.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| +#if 0 |
| +#include "base/memory/ref_counted.h" |
| +#include "base/memory/scoped_vector.h" |
| +#include "net/base/io_buffer.h" |
| +#include "net/base/net_errors.h" |
| +#include "net/base/request_priority.h" |
| +#include "net/http/http_pipelined_stream.h" |
| +#include "net/socket/client_socket_handle.h" |
| +#include "net/socket/client_socket_pool_histograms.h" |
| +#include "net/socket/socket_test_util.h" |
| +#endif |
| + |
| +using testing::_; |
| +using testing::NiceMock; |
| +using testing::Ref; |
| +using testing::Return; |
| + |
| +static const int kMaxCapacity = 3; |
| + |
| +namespace net { |
| + |
| +class MockHostOwner : public HttpPipelinedHost::Owner { |
| + public: |
| + MOCK_METHOD1(OnHostIdle, void(HttpPipelinedHost* host)); |
| + MOCK_METHOD1(OnHostHasAdditionalCapacity, void(HttpPipelinedHost* host)); |
| +}; |
| + |
| +class MockPipelineFactory : public HttpPipelinedConnection::Factory { |
| + public: |
| + MOCK_METHOD6(CreateNewPipeline, HttpPipelinedConnection*( |
| + ClientSocketHandle* connection, |
| + HttpPipelinedConnection::Owner* owner, |
| + const SSLConfig& used_ssl_config, |
| + const ProxyInfo& used_proxy_info, |
| + const BoundNetLog& net_log, |
| + bool was_npn_negotiated)); |
| +}; |
| + |
| +class MockPipeline : public HttpPipelinedConnection { |
| + public: |
| + MockPipeline(int depth, bool usable, bool active) |
| + : depth_(depth), |
| + usable_(usable), |
| + active_(active) { |
| + } |
| + |
| + void SetState(int depth, bool usable, bool active) { |
| + depth_ = depth; |
| + usable_ = usable; |
| + active_ = active; |
| + } |
| + |
| + int depth() const OVERRIDE { return depth_; } |
| + bool usable() const OVERRIDE { return usable_; } |
| + bool active() const OVERRIDE { return active_; } |
|
mmenke
2011/08/23 19:05:25
nit: Use virtual as well as OVERRIDE.
James Simonsen
2011/08/26 22:19:07
Done.
|
| + |
| + MOCK_METHOD0(CreateNewStream, HttpStream*()); |
| + MOCK_METHOD1(OnStreamDeleted, void(int pipeline_id)); |
| + MOCK_CONST_METHOD0(used_ssl_config, const SSLConfig&()); |
| + MOCK_CONST_METHOD0(used_proxy_info, const ProxyInfo&()); |
| + MOCK_CONST_METHOD0(source, const NetLog::Source&()); |
| + MOCK_CONST_METHOD0(was_npn_negotiated, bool()); |
| + |
| + private: |
| + int depth_; |
| + bool usable_; |
| + bool active_; |
| +}; |
| + |
| +class HttpPipelinedHostTest : public testing::Test { |
| + public: |
| + HttpPipelinedHostTest() |
| + : origin_("host", 123), |
| + host_(&owner_, origin_, &factory_) { |
| + } |
| + |
| + MockPipeline* AddTestPipeline(int depth, bool usable, bool active) { |
| + MockPipeline* pipeline = new MockPipeline(depth, usable, active); |
| + EXPECT_CALL(factory_, CreateNewPipeline(connection_, &host_, |
| + Ref(ssl_config_), Ref(proxy_info_), |
| + Ref(net_log_), true)) |
| + .Times(1) |
| + .WillOnce(Return(pipeline)); |
| + EXPECT_EQ(pipeline, host_.CreateNewPipeline(connection_, ssl_config_, |
| + proxy_info_, net_log_, true)); |
| + return pipeline; |
| + } |
| + |
| + void ClearTestPipeline(MockPipeline* pipeline) { |
| + pipeline->SetState(0, true, true); |
| + host_.OnPipelineHasCapacity(pipeline); |
| + } |
| + |
| + NiceMock<MockHostOwner> owner_; |
| + HostPortPair origin_; |
| + MockPipelineFactory factory_; |
| + HttpPipelinedHost host_; |
| + |
| + ClientSocketHandle* connection_; |
| + SSLConfig ssl_config_; |
| + ProxyInfo proxy_info_; |
| + BoundNetLog net_log_; |
| +}; |
| + |
| +TEST_F(HttpPipelinedHostTest, Owner) { |
| + EXPECT_TRUE(origin_.Equals(host_.origin())); |
| +} |
| + |
| +TEST_F(HttpPipelinedHostTest, OnHostIdle) { |
| + MockPipeline* pipeline = AddTestPipeline(0, false, true); |
| + |
| + EXPECT_CALL(owner_, OnHostHasAdditionalCapacity(&host_)) |
| + .Times(0); |
| + EXPECT_CALL(owner_, OnHostIdle(&host_)) |
| + .Times(1); |
| + host_.OnPipelineHasCapacity(pipeline); |
| +} |
| + |
| +TEST_F(HttpPipelinedHostTest, OnHostHasAdditionalCapacity) { |
| + MockPipeline* pipeline = AddTestPipeline(1, true, true); |
| + |
| + EXPECT_CALL(owner_, OnHostHasAdditionalCapacity(&host_)) |
| + .Times(2); |
|
mmenke
2011/08/23 19:05:25
Maybe add:
EXPECT_CALL(owner_, OnHostIdle(&host
James Simonsen
2011/08/26 22:19:07
Done.
|
| + host_.OnPipelineHasCapacity(pipeline); |
| + |
| + EXPECT_CALL(owner_, OnHostIdle(&host_)) |
| + .Times(1); |
| + ClearTestPipeline(pipeline); |
| +} |
| + |
| +TEST_F(HttpPipelinedHostTest, IgnoresUnusablePipeline) { |
| + MockPipeline* pipeline = AddTestPipeline(1, false, true); |
| + |
| + EXPECT_EQ(NULL, host_.FindAvailablePipeline()); |
| + |
| + ClearTestPipeline(pipeline); |
| +} |
| + |
| +TEST_F(HttpPipelinedHostTest, IgnoresInactivePipeline) { |
| + MockPipeline* pipeline = AddTestPipeline(1, true, false); |
| + |
| + EXPECT_EQ(NULL, host_.FindAvailablePipeline()); |
| + |
| + ClearTestPipeline(pipeline); |
| +} |
| + |
| +TEST_F(HttpPipelinedHostTest, IgnoresFullPipeline) { |
| + MockPipeline* pipeline = AddTestPipeline(kMaxCapacity, true, true); |
| + |
| + EXPECT_EQ(NULL, host_.FindAvailablePipeline()); |
| + |
| + ClearTestPipeline(pipeline); |
| +} |
| + |
| +TEST_F(HttpPipelinedHostTest, PicksLeastLoadedPipeline) { |
| + MockPipeline* full_pipeline = AddTestPipeline(kMaxCapacity, true, true); |
| + MockPipeline* usable_pipeline = AddTestPipeline(kMaxCapacity - 1, true, true); |
| + MockPipeline* empty_pipeline = AddTestPipeline(0, true, true); |
| + |
| + EXPECT_EQ(empty_pipeline, host_.FindAvailablePipeline()); |
| + |
| + ClearTestPipeline(full_pipeline); |
| + ClearTestPipeline(usable_pipeline); |
| + ClearTestPipeline(empty_pipeline); |
| +} |
| + |
|
mmenke
2011/08/23 19:05:25
Maybe add something along the lines of:
TEST_F(Ht
James Simonsen
2011/08/26 22:19:07
Done.
|
| +} // namespace net |