Chromium Code Reviews| Index: net/socket/client_socket_pool_base_unittest.cc |
| diff --git a/net/socket/client_socket_pool_base_unittest.cc b/net/socket/client_socket_pool_base_unittest.cc |
| index 985a4c59988a7fb4793d3ac2903346f98d639279..d064a5cef45d6076b9346978b9563ed1848f3756 100644 |
| --- a/net/socket/client_socket_pool_base_unittest.cc |
| +++ b/net/socket/client_socket_pool_base_unittest.cc |
| @@ -25,8 +25,12 @@ |
| #include "net/socket/socket_test_util.h" |
| #include "net/socket/ssl_host_info.h" |
| #include "net/socket/stream_socket.h" |
| +#include "testing/gmock/include/gmock/gmock.h" |
| #include "testing/gtest/include/gtest/gtest.h" |
| +using ::testing::Invoke; |
| +using ::testing::Return; |
| + |
| namespace net { |
| namespace { |
| @@ -405,6 +409,14 @@ class TestClientSocketPool : public ClientSocketPool { |
| virtual ~TestClientSocketPool() {} |
| + void AddLayeredPool(LayeredPool* pool) { |
| + base_.AddLayeredPool(pool); |
| + } |
| + |
| + void RemoveLayeredPool(LayeredPool* pool) { |
| + base_.RemoveLayeredPool(pool); |
| + } |
| + |
| virtual int RequestSocket( |
| const std::string& group_name, |
| const void* params, |
| @@ -421,7 +433,7 @@ class TestClientSocketPool : public ClientSocketPool { |
| virtual void RequestSockets(const std::string& group_name, |
| const void* params, |
| int num_sockets, |
| - const BoundNetLog& net_log) { |
| + const BoundNetLog& net_log) OVERRIDE { |
| const scoped_refptr<TestSocketParams>* casted_params = |
| static_cast<const scoped_refptr<TestSocketParams>*>(params); |
| @@ -430,47 +442,56 @@ class TestClientSocketPool : public ClientSocketPool { |
| virtual void CancelRequest( |
| const std::string& group_name, |
| - ClientSocketHandle* handle) { |
| + ClientSocketHandle* handle) OVERRIDE { |
| base_.CancelRequest(group_name, handle); |
| } |
| virtual void ReleaseSocket( |
| const std::string& group_name, |
| StreamSocket* socket, |
| - int id) { |
| + int id) OVERRIDE { |
| base_.ReleaseSocket(group_name, socket, id); |
| } |
| - virtual void Flush() { |
| + virtual void Flush() OVERRIDE { |
| base_.Flush(); |
| } |
| - virtual void CloseIdleSockets() { |
| + virtual bool IsStalled() const OVERRIDE { |
| + return base_.IsStalled(); |
| + } |
| + |
| + virtual void CloseIdleSockets() OVERRIDE { |
| base_.CloseIdleSockets(); |
| } |
| - virtual int IdleSocketCount() const { return base_.idle_socket_count(); } |
| + virtual int IdleSocketCount() const OVERRIDE { |
| + return base_.idle_socket_count(); |
| + } |
| - virtual int IdleSocketCountInGroup(const std::string& group_name) const { |
| + virtual int IdleSocketCountInGroup( |
| + const std::string& group_name) const OVERRIDE { |
| return base_.IdleSocketCountInGroup(group_name); |
| } |
| - virtual LoadState GetLoadState(const std::string& group_name, |
| - const ClientSocketHandle* handle) const { |
| + virtual LoadState GetLoadState( |
| + const std::string& group_name, |
| + const ClientSocketHandle* handle) const OVERRIDE { |
| return base_.GetLoadState(group_name, handle); |
| } |
| - virtual DictionaryValue* GetInfoAsValue(const std::string& name, |
| - const std::string& type, |
| - bool include_nested_pools) const { |
| + virtual DictionaryValue* GetInfoAsValue( |
| + const std::string& name, |
| + const std::string& type, |
| + bool include_nested_pools) const OVERRIDE { |
| return base_.GetInfoAsValue(name, type); |
| } |
| - virtual base::TimeDelta ConnectionTimeout() const { |
| + virtual base::TimeDelta ConnectionTimeout() const OVERRIDE { |
| return base_.ConnectionTimeout(); |
| } |
| - virtual ClientSocketPoolHistograms* histograms() const { |
| + virtual ClientSocketPoolHistograms* histograms() const OVERRIDE { |
| return base_.histograms(); |
| } |
| @@ -492,6 +513,10 @@ class TestClientSocketPool : public ClientSocketPool { |
| void EnableConnectBackupJobs() { base_.EnableConnectBackupJobs(); } |
| + bool CloseOneIdleConnectionInLayeredPool() { |
| + return base_.CloseOneIdleConnectionInLayeredPool(); |
| + } |
| + |
| private: |
| TestClientSocketPoolBase base_; |
| @@ -1152,6 +1177,7 @@ TEST_F(ClientSocketPoolBaseTest, WaitForStalledSocketAtSocketLimit) { |
| ClientSocketHandle stalled_handle; |
| TestOldCompletionCallback callback; |
| { |
| + EXPECT_FALSE(pool_->IsStalled()); |
| ClientSocketHandle handles[kDefaultMaxSockets]; |
| for (int i = 0; i < kDefaultMaxSockets; ++i) { |
| TestOldCompletionCallback callback; |
| @@ -1166,6 +1192,7 @@ TEST_F(ClientSocketPoolBaseTest, WaitForStalledSocketAtSocketLimit) { |
| EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count()); |
| EXPECT_EQ(0, pool_->IdleSocketCount()); |
| + EXPECT_FALSE(pool_->IsStalled()); |
| // Now we will hit the socket limit. |
| EXPECT_EQ(ERR_IO_PENDING, stalled_handle.Init("foo", |
| @@ -1174,6 +1201,7 @@ TEST_F(ClientSocketPoolBaseTest, WaitForStalledSocketAtSocketLimit) { |
| &callback, |
| pool_.get(), |
| BoundNetLog())); |
| + EXPECT_TRUE(pool_->IsStalled()); |
| // Dropping out of scope will close all handles and return them to idle. |
| } |
| @@ -2944,6 +2972,7 @@ TEST_F(ClientSocketPoolBaseTest, RequestSocketsHitMaxSocketLimit) { |
| ASSERT_TRUE(pool_->HasGroup("a")); |
| EXPECT_EQ(kDefaultMaxSockets - 1, pool_->NumConnectJobsInGroup("a")); |
| + EXPECT_FALSE(pool_->IsStalled()); |
| ASSERT_FALSE(pool_->HasGroup("b")); |
| @@ -2952,6 +2981,7 @@ TEST_F(ClientSocketPoolBaseTest, RequestSocketsHitMaxSocketLimit) { |
| ASSERT_TRUE(pool_->HasGroup("b")); |
| EXPECT_EQ(1, pool_->NumConnectJobsInGroup("b")); |
| + EXPECT_FALSE(pool_->IsStalled()); |
| } |
| TEST_F(ClientSocketPoolBaseTest, RequestSocketsCountIdleSockets) { |
| @@ -3270,6 +3300,83 @@ TEST_F(ClientSocketPoolBaseTest, PreconnectWithBackupJob) { |
| EXPECT_EQ(1, pool_->NumActiveSocketsInGroup("a")); |
| } |
| +class MockLayeredPool : public LayeredPool { |
| + public: |
| + MockLayeredPool(TestClientSocketPool* pool) |
| + : pool_(pool), |
| + params_(new TestSocketParams) { |
| + pool_->AddLayeredPool(this); |
| + } |
| + |
| + ~MockLayeredPool() { |
| + pool_->RemoveLayeredPool(this); |
| + } |
| + |
| + int RequestSocket(TestClientSocketPool* pool) { |
| + return handle_.Init("foo", params_, kDefaultPriority, &callback_, |
| + pool, BoundNetLog()); |
| + } |
| + |
| + bool ReleaseOneConnection() { |
| + if (!handle_.is_initialized()) { |
| + return false; |
| + } |
| + handle_.Reset(); |
| + return true; |
| + } |
| + |
| + MOCK_METHOD0(CloseOneIdleConnection, bool()); |
| + |
| + private: |
| + TestClientSocketPool* const pool_; |
| + scoped_refptr<TestSocketParams> params_; |
| + ClientSocketHandle handle_; |
| + TestOldCompletionCallback callback_; |
| +}; |
| + |
| +TEST_F(ClientSocketPoolBaseTest, FailToCloseIdleSocketsNotHeldByLayeredPool) { |
| + CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| + connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| + |
| + MockLayeredPool mock_layered_pool(pool_.get()); |
| + EXPECT_CALL(mock_layered_pool, CloseOneIdleConnection()) |
| + .WillOnce(Return(false)); |
| + EXPECT_EQ(OK, mock_layered_pool.RequestSocket(pool_.get())); |
| + EXPECT_FALSE(pool_->CloseOneIdleConnectionInLayeredPool()); |
| +} |
| + |
| +TEST_F(ClientSocketPoolBaseTest, ForciblyCloseIdleSocketsHeldByLayeredPool) { |
| + CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| + connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| + |
| + MockLayeredPool mock_layered_pool(pool_.get()); |
| + EXPECT_EQ(OK, mock_layered_pool.RequestSocket(pool_.get())); |
|
mmenke
2011/10/27 18:52:37
Don't think you need to request a socket twice in
willchan no longer on Chromium
2011/11/08 22:26:13
Done.
|
| + EXPECT_CALL(mock_layered_pool, CloseOneIdleConnection()) |
| + .WillOnce(Invoke(&mock_layered_pool, |
| + &MockLayeredPool::ReleaseOneConnection)); |
| + EXPECT_EQ(OK, mock_layered_pool.RequestSocket(pool_.get())); |
| + EXPECT_TRUE(pool_->CloseOneIdleConnectionInLayeredPool()); |
| +} |
| + |
| +TEST_F(ClientSocketPoolBaseTest, CloseIdleSocketsHeldByLayeredPoolWhenNeeded) { |
| + CreatePool(1, 1); |
| + connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| + |
| + MockLayeredPool mock_layered_pool(pool_.get()); |
| + EXPECT_EQ(OK, mock_layered_pool.RequestSocket(pool_.get())); |
| + EXPECT_CALL(mock_layered_pool, CloseOneIdleConnection()) |
| + .WillOnce(Invoke(&mock_layered_pool, |
| + &MockLayeredPool::ReleaseOneConnection)); |
| + ClientSocketHandle handle; |
| + TestOldCompletionCallback callback; |
| + EXPECT_EQ(OK, handle.Init("a", |
| + params_, |
| + kDefaultPriority, |
| + &callback, |
| + pool_.get(), |
| + BoundNetLog())); |
| +} |
| + |
| } // namespace |
| } // namespace net |