Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 474 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 485 } | 485 } |
| 486 | 486 |
| 487 bool HasGroup(const std::string& group_name) const { | 487 bool HasGroup(const std::string& group_name) const { |
| 488 return base_.HasGroup(group_name); | 488 return base_.HasGroup(group_name); |
| 489 } | 489 } |
| 490 | 490 |
| 491 void CleanupTimedOutIdleSockets() { base_.CleanupIdleSockets(false); } | 491 void CleanupTimedOutIdleSockets() { base_.CleanupIdleSockets(false); } |
| 492 | 492 |
| 493 void EnableConnectBackupJobs() { base_.EnableConnectBackupJobs(); } | 493 void EnableConnectBackupJobs() { base_.EnableConnectBackupJobs(); } |
| 494 | 494 |
| 495 void UseCleanupTimer(bool enabled) { | |
| 496 base_.set_cleanup_timer_enabled(enabled); | |
| 497 } | |
| 498 | |
| 495 private: | 499 private: |
| 496 TestClientSocketPoolBase base_; | 500 TestClientSocketPoolBase base_; |
| 497 | 501 |
| 498 DISALLOW_COPY_AND_ASSIGN(TestClientSocketPool); | 502 DISALLOW_COPY_AND_ASSIGN(TestClientSocketPool); |
| 499 }; | 503 }; |
| 500 | 504 |
| 501 } // namespace | 505 } // namespace |
| 502 | 506 |
| 503 REGISTER_SOCKET_PARAMS_FOR_POOL(TestClientSocketPool, TestSocketParams); | 507 REGISTER_SOCKET_PARAMS_FOR_POOL(TestClientSocketPool, TestSocketParams); |
| 504 | 508 |
| (...skipping 1434 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1939 pool_.get(), | 1943 pool_.get(), |
| 1940 BoundNetLog())); | 1944 BoundNetLog())); |
| 1941 EXPECT_EQ(LOAD_STATE_CONNECTING, pool_->GetLoadState("a", &handle)); | 1945 EXPECT_EQ(LOAD_STATE_CONNECTING, pool_->GetLoadState("a", &handle)); |
| 1942 EXPECT_EQ(ERR_CONNECTION_FAILED, callback.WaitForResult()); | 1946 EXPECT_EQ(ERR_CONNECTION_FAILED, callback.WaitForResult()); |
| 1943 EXPECT_FALSE(handle.is_initialized()); | 1947 EXPECT_FALSE(handle.is_initialized()); |
| 1944 EXPECT_FALSE(handle.socket()); | 1948 EXPECT_FALSE(handle.socket()); |
| 1945 EXPECT_TRUE(handle.is_ssl_error()); | 1949 EXPECT_TRUE(handle.is_ssl_error()); |
| 1946 EXPECT_FALSE(handle.ssl_error_response_info().headers.get() == NULL); | 1950 EXPECT_FALSE(handle.ssl_error_response_info().headers.get() == NULL); |
| 1947 } | 1951 } |
| 1948 | 1952 |
| 1953 TEST_F(ClientSocketPoolBaseTest, DisableCleanupTimer) { | |
| 1954 CreatePoolWithIdleTimeouts( | |
| 1955 kDefaultMaxSockets, kDefaultMaxSocketsPerGroup, | |
| 1956 base::TimeDelta::FromMilliseconds(10), // Time out unused sockets | |
|
mmenke
2011/11/11 03:33:01
nit: Google style is two spaces between code and
selim
2011/11/11 19:30:58
done
| |
| 1957 base::TimeDelta::FromMilliseconds(10)); // Time out used sockets | |
| 1958 | |
| 1959 // disable cleanup timer | |
|
mmenke
2011/11/11 03:33:01
nit: "Disable cleanup timer."
selim
2011/11/11 19:30:58
done
| |
| 1960 pool_->UseCleanupTimer(false); | |
| 1961 | |
| 1962 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); | |
| 1963 | |
| 1964 // Startup two mock pending connect jobs, which will sit in the MessageLoop. | |
| 1965 | |
| 1966 ClientSocketHandle handle; | |
| 1967 TestOldCompletionCallback callback; | |
| 1968 int rv = handle.Init("a", | |
| 1969 params_, | |
| 1970 LOWEST, | |
| 1971 &callback, | |
| 1972 pool_.get(), | |
| 1973 BoundNetLog()); | |
| 1974 EXPECT_EQ(ERR_IO_PENDING, rv); | |
| 1975 EXPECT_EQ(LOAD_STATE_CONNECTING, pool_->GetLoadState("a", &handle)); | |
| 1976 | |
| 1977 ClientSocketHandle handle2; | |
| 1978 TestOldCompletionCallback callback2; | |
| 1979 rv = handle2.Init("a", | |
| 1980 params_, | |
| 1981 LOWEST, | |
| 1982 &callback2, | |
| 1983 pool_.get(), | |
| 1984 BoundNetLog()); | |
| 1985 EXPECT_EQ(ERR_IO_PENDING, rv); | |
| 1986 EXPECT_EQ(LOAD_STATE_CONNECTING, pool_->GetLoadState("a", &handle2)); | |
| 1987 | |
| 1988 // Cancel one of the requests. Wait for the other, which will get the first | |
| 1989 // job. Release the socket. Run the loop again to make sure the second | |
| 1990 // socket is sitting idle and the first one is released (since ReleaseSocket() | |
| 1991 // just posts a DoReleaseSocket() task). | |
| 1992 | |
| 1993 handle.Reset(); | |
| 1994 EXPECT_EQ(OK, callback2.WaitForResult()); | |
| 1995 // Use the socket. | |
| 1996 EXPECT_EQ(1, handle2.socket()->Write(NULL, 1, NULL)); | |
| 1997 handle2.Reset(); | |
| 1998 | |
| 1999 // We post all of our delayed tasks with a 2ms delay. I.e. they don't | |
| 2000 // actually become pending until 2ms after they have been created. In order | |
| 2001 // to flush all tasks, we need to wait so that we know there are no | |
| 2002 // soon-to-be-pending tasks waiting. | |
| 2003 base::PlatformThread::Sleep(20); | |
| 2004 MessageLoop::current()->RunAllPending(); | |
| 2005 | |
| 2006 ASSERT_EQ(2, pool_->IdleSocketCount()); | |
| 2007 | |
| 2008 // Request a new socket. This should cleanup the unused and timed out ones. | |
| 2009 CapturingBoundNetLog log(CapturingNetLog::kUnbounded); | |
| 2010 rv = handle.Init("a", | |
| 2011 params_, | |
| 2012 LOWEST, | |
| 2013 &callback, | |
| 2014 pool_.get(), | |
| 2015 log.bound()); | |
| 2016 EXPECT_EQ(OK, rv); | |
| 2017 EXPECT_TRUE(handle.is_reused()); | |
| 2018 | |
| 2019 // Make sure the idle socket is closed | |
| 2020 ASSERT_TRUE(pool_->HasGroup("a")); | |
| 2021 EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a")); | |
| 2022 EXPECT_EQ(1, pool_->NumActiveSocketsInGroup("a")); | |
| 2023 | |
| 2024 net::CapturingNetLog::EntryList entries; | |
| 2025 log.GetEntries(&entries); | |
| 2026 EXPECT_TRUE(LogContainsEntryWithType( | |
| 2027 entries, 1, NetLog::TYPE_SOCKET_POOL_REUSED_AN_EXISTING_SOCKET)); | |
| 2028 } | |
| 2029 | |
| 1949 TEST_F(ClientSocketPoolBaseTest, CleanupTimedOutIdleSockets) { | 2030 TEST_F(ClientSocketPoolBaseTest, CleanupTimedOutIdleSockets) { |
| 1950 CreatePoolWithIdleTimeouts( | 2031 CreatePoolWithIdleTimeouts( |
| 1951 kDefaultMaxSockets, kDefaultMaxSocketsPerGroup, | 2032 kDefaultMaxSockets, kDefaultMaxSocketsPerGroup, |
| 1952 base::TimeDelta(), // Time out unused sockets immediately. | 2033 base::TimeDelta(), // Time out unused sockets immediately. |
| 1953 base::TimeDelta::FromDays(1)); // Don't time out used sockets. | 2034 base::TimeDelta::FromDays(1)); // Don't time out used sockets. |
| 1954 | 2035 |
| 1955 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); | 2036 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 1956 | 2037 |
| 1957 // Startup two mock pending connect jobs, which will sit in the MessageLoop. | 2038 // Startup two mock pending connect jobs, which will sit in the MessageLoop. |
| 1958 | 2039 |
| (...skipping 1307 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 3266 // The hung connect job should still be there, but everything else should be | 3347 // The hung connect job should still be there, but everything else should be |
| 3267 // complete. | 3348 // complete. |
| 3268 EXPECT_EQ(1, pool_->NumConnectJobsInGroup("a")); | 3349 EXPECT_EQ(1, pool_->NumConnectJobsInGroup("a")); |
| 3269 EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a")); | 3350 EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a")); |
| 3270 EXPECT_EQ(1, pool_->NumActiveSocketsInGroup("a")); | 3351 EXPECT_EQ(1, pool_->NumActiveSocketsInGroup("a")); |
| 3271 } | 3352 } |
| 3272 | 3353 |
| 3273 } // namespace | 3354 } // namespace |
| 3274 | 3355 |
| 3275 } // namespace net | 3356 } // namespace net |
| OLD | NEW |