Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1158)

Side by Side Diff: net/socket/client_socket_pool_base_unittest.cc

Issue 8526006: Close idle sockets next time we are about to send data. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: use global variable. Created 9 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "net/socket/client_socket_pool_base.h" 5 #include "net/socket/client_socket_pool_base.h"
6 6
7 #include "base/callback.h" 7 #include "base/callback.h"
8 #include "base/compiler_specific.h" 8 #include "base/compiler_specific.h"
9 #include "base/memory/ref_counted.h" 9 #include "base/memory/ref_counted.h"
10 #include "base/memory/scoped_vector.h" 10 #include "base/memory/scoped_vector.h"
(...skipping 536 matching lines...) Expand 10 before | Expand all | Expand 10 after
547 }; 547 };
548 548
549 class ClientSocketPoolBaseTest : public testing::Test { 549 class ClientSocketPoolBaseTest : public testing::Test {
550 protected: 550 protected:
551 ClientSocketPoolBaseTest() 551 ClientSocketPoolBaseTest()
552 : params_(new TestSocketParams()), 552 : params_(new TestSocketParams()),
553 histograms_("ClientSocketPoolTest") { 553 histograms_("ClientSocketPoolTest") {
554 connect_backup_jobs_enabled_ = 554 connect_backup_jobs_enabled_ =
555 internal::ClientSocketPoolBaseHelper::connect_backup_jobs_enabled(); 555 internal::ClientSocketPoolBaseHelper::connect_backup_jobs_enabled();
556 internal::ClientSocketPoolBaseHelper::set_connect_backup_jobs_enabled(true); 556 internal::ClientSocketPoolBaseHelper::set_connect_backup_jobs_enabled(true);
557 cleanup_timer_enabled_ =
558 internal::ClientSocketPoolBaseHelper::cleanup_timer_enabled();
557 } 559 }
558 560
559 virtual ~ClientSocketPoolBaseTest() { 561 virtual ~ClientSocketPoolBaseTest() {
560 internal::ClientSocketPoolBaseHelper::set_connect_backup_jobs_enabled( 562 internal::ClientSocketPoolBaseHelper::set_connect_backup_jobs_enabled(
561 connect_backup_jobs_enabled_); 563 connect_backup_jobs_enabled_);
564 internal::ClientSocketPoolBaseHelper::set_cleanup_timer_enabled(
565 cleanup_timer_enabled_);
562 } 566 }
563 567
564 void CreatePool(int max_sockets, int max_sockets_per_group) { 568 void CreatePool(int max_sockets, int max_sockets_per_group) {
565 CreatePoolWithIdleTimeouts( 569 CreatePoolWithIdleTimeouts(
566 max_sockets, 570 max_sockets,
567 max_sockets_per_group, 571 max_sockets_per_group,
568 ClientSocketPool::unused_idle_socket_timeout(), 572 ClientSocketPool::unused_idle_socket_timeout(),
569 ClientSocketPool::used_idle_socket_timeout()); 573 ClientSocketPool::used_idle_socket_timeout());
570 } 574 }
571 575
(...skipping 29 matching lines...) Expand all
601 void ReleaseAllConnections(ClientSocketPoolTest::KeepAlive keep_alive) { 605 void ReleaseAllConnections(ClientSocketPoolTest::KeepAlive keep_alive) {
602 test_base_.ReleaseAllConnections(keep_alive); 606 test_base_.ReleaseAllConnections(keep_alive);
603 } 607 }
604 608
605 TestSocketRequest* request(int i) { return test_base_.request(i); } 609 TestSocketRequest* request(int i) { return test_base_.request(i); }
606 size_t requests_size() const { return test_base_.requests_size(); } 610 size_t requests_size() const { return test_base_.requests_size(); }
607 ScopedVector<TestSocketRequest>* requests() { return test_base_.requests(); } 611 ScopedVector<TestSocketRequest>* requests() { return test_base_.requests(); }
608 size_t completion_count() const { return test_base_.completion_count(); } 612 size_t completion_count() const { return test_base_.completion_count(); }
609 613
610 bool connect_backup_jobs_enabled_; 614 bool connect_backup_jobs_enabled_;
615 bool cleanup_timer_enabled_;
611 MockClientSocketFactory client_socket_factory_; 616 MockClientSocketFactory client_socket_factory_;
612 TestConnectJobFactory* connect_job_factory_; 617 TestConnectJobFactory* connect_job_factory_;
613 scoped_refptr<TestSocketParams> params_; 618 scoped_refptr<TestSocketParams> params_;
614 ClientSocketPoolHistograms histograms_; 619 ClientSocketPoolHistograms histograms_;
615 scoped_ptr<TestClientSocketPool> pool_; 620 scoped_ptr<TestClientSocketPool> pool_;
616 ClientSocketPoolTest test_base_; 621 ClientSocketPoolTest test_base_;
617 }; 622 };
618 623
619 TEST_F(ClientSocketPoolBaseTest, AssignIdleSocketToGroup_WarmestSocket) { 624 TEST_F(ClientSocketPoolBaseTest, AssignIdleSocketToGroup_WarmestSocket) {
620 CreatePool(4, 4); 625 CreatePool(4, 4);
(...skipping 1318 matching lines...) Expand 10 before | Expand all | Expand 10 after
1939 pool_.get(), 1944 pool_.get(),
1940 BoundNetLog())); 1945 BoundNetLog()));
1941 EXPECT_EQ(LOAD_STATE_CONNECTING, pool_->GetLoadState("a", &handle)); 1946 EXPECT_EQ(LOAD_STATE_CONNECTING, pool_->GetLoadState("a", &handle));
1942 EXPECT_EQ(ERR_CONNECTION_FAILED, callback.WaitForResult()); 1947 EXPECT_EQ(ERR_CONNECTION_FAILED, callback.WaitForResult());
1943 EXPECT_FALSE(handle.is_initialized()); 1948 EXPECT_FALSE(handle.is_initialized());
1944 EXPECT_FALSE(handle.socket()); 1949 EXPECT_FALSE(handle.socket());
1945 EXPECT_TRUE(handle.is_ssl_error()); 1950 EXPECT_TRUE(handle.is_ssl_error());
1946 EXPECT_FALSE(handle.ssl_error_response_info().headers.get() == NULL); 1951 EXPECT_FALSE(handle.ssl_error_response_info().headers.get() == NULL);
1947 } 1952 }
1948 1953
1954 TEST_F(ClientSocketPoolBaseTest, DisableCleanupTimer) {
1955 // Disable cleanup timer.
1956 internal::ClientSocketPoolBaseHelper::set_cleanup_timer_enabled(false);
1957
1958 CreatePoolWithIdleTimeouts(
1959 kDefaultMaxSockets, kDefaultMaxSocketsPerGroup,
1960 base::TimeDelta::FromMilliseconds(10), // Time out unused sockets
1961 base::TimeDelta::FromMilliseconds(10)); // Time out used sockets
1962
1963 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob);
1964
1965 // Startup two mock pending connect jobs, which will sit in the MessageLoop.
1966
1967 ClientSocketHandle handle;
1968 TestOldCompletionCallback callback;
1969 int rv = handle.Init("a",
1970 params_,
1971 LOWEST,
1972 &callback,
1973 pool_.get(),
1974 BoundNetLog());
1975 EXPECT_EQ(ERR_IO_PENDING, rv);
1976 EXPECT_EQ(LOAD_STATE_CONNECTING, pool_->GetLoadState("a", &handle));
1977
1978 ClientSocketHandle handle2;
1979 TestOldCompletionCallback callback2;
1980 rv = handle2.Init("a",
1981 params_,
1982 LOWEST,
1983 &callback2,
1984 pool_.get(),
1985 BoundNetLog());
1986 EXPECT_EQ(ERR_IO_PENDING, rv);
1987 EXPECT_EQ(LOAD_STATE_CONNECTING, pool_->GetLoadState("a", &handle2));
1988
1989 // Cancel one of the requests. Wait for the other, which will get the first
1990 // job. Release the socket. Run the loop again to make sure the second
1991 // socket is sitting idle and the first one is released (since ReleaseSocket()
1992 // just posts a DoReleaseSocket() task).
1993
1994 handle.Reset();
1995 EXPECT_EQ(OK, callback2.WaitForResult());
1996 // Use the socket.
1997 EXPECT_EQ(1, handle2.socket()->Write(NULL, 1, NULL));
1998 handle2.Reset();
1999
2000 // We post all of our delayed tasks with a 2ms delay. I.e. they don't
willchan no longer on Chromium 2011/11/16 00:51:40 Is this comment true?
selim 2011/11/16 01:01:08 I took this part of the test exactly from the one
willchan no longer on Chromium 2011/11/16 01:04:29 Looks bogus to me. Please remove it. Explain what
2001 // actually become pending until 2ms after they have been created. In order
2002 // to flush all tasks, we need to wait so that we know there are no
2003 // soon-to-be-pending tasks waiting.
2004 base::PlatformThread::Sleep(20);
2005 MessageLoop::current()->RunAllPending();
2006
2007 ASSERT_EQ(2, pool_->IdleSocketCount());
2008
2009 // Request a new socket. This should cleanup the unused and timed out ones.
2010 // A new socket will be created rather than reusing the idle one.
2011 CapturingBoundNetLog log(CapturingNetLog::kUnbounded);
2012 rv = handle.Init("a",
2013 params_,
2014 LOWEST,
2015 &callback,
2016 pool_.get(),
2017 log.bound());
2018 EXPECT_EQ(ERR_IO_PENDING, rv);
2019 EXPECT_EQ(OK, callback.WaitForResult());
2020 EXPECT_FALSE(handle.is_reused());
2021
2022 // Make sure the idle socket is closed
2023 ASSERT_TRUE(pool_->HasGroup("a"));
2024 EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a"));
2025 EXPECT_EQ(1, pool_->NumActiveSocketsInGroup("a"));
2026
2027 net::CapturingNetLog::EntryList entries;
2028 log.GetEntries(&entries);
2029 EXPECT_FALSE(LogContainsEntryWithType(
2030 entries, 1, NetLog::TYPE_SOCKET_POOL_REUSED_AN_EXISTING_SOCKET));
2031 }
2032
1949 TEST_F(ClientSocketPoolBaseTest, CleanupTimedOutIdleSockets) { 2033 TEST_F(ClientSocketPoolBaseTest, CleanupTimedOutIdleSockets) {
1950 CreatePoolWithIdleTimeouts( 2034 CreatePoolWithIdleTimeouts(
1951 kDefaultMaxSockets, kDefaultMaxSocketsPerGroup, 2035 kDefaultMaxSockets, kDefaultMaxSocketsPerGroup,
1952 base::TimeDelta(), // Time out unused sockets immediately. 2036 base::TimeDelta(), // Time out unused sockets immediately.
1953 base::TimeDelta::FromDays(1)); // Don't time out used sockets. 2037 base::TimeDelta::FromDays(1)); // Don't time out used sockets.
1954 2038
1955 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); 2039 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob);
1956 2040
1957 // Startup two mock pending connect jobs, which will sit in the MessageLoop. 2041 // Startup two mock pending connect jobs, which will sit in the MessageLoop.
1958 2042
(...skipping 1307 matching lines...) Expand 10 before | Expand all | Expand 10 after
3266 // The hung connect job should still be there, but everything else should be 3350 // The hung connect job should still be there, but everything else should be
3267 // complete. 3351 // complete.
3268 EXPECT_EQ(1, pool_->NumConnectJobsInGroup("a")); 3352 EXPECT_EQ(1, pool_->NumConnectJobsInGroup("a"));
3269 EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a")); 3353 EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a"));
3270 EXPECT_EQ(1, pool_->NumActiveSocketsInGroup("a")); 3354 EXPECT_EQ(1, pool_->NumActiveSocketsInGroup("a"));
3271 } 3355 }
3272 3356
3273 } // namespace 3357 } // namespace
3274 3358
3275 } // namespace net 3359 } // namespace net
OLDNEW
« net/socket/client_socket_pool_base.cc ('K') | « net/socket/client_socket_pool_base.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698