| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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/spdy/spdy_session_pool.h" | 5 #include "net/spdy/spdy_session_pool.h" |
| 6 | 6 |
| 7 #include <cstddef> | 7 #include <cstddef> |
| 8 #include <memory> | 8 #include <memory> |
| 9 #include <string> | 9 #include <string> |
| 10 #include <utility> | 10 #include <utility> |
| (...skipping 19 matching lines...) Expand all Loading... |
| 30 class SpdySessionPoolTest : public ::testing::Test, | 30 class SpdySessionPoolTest : public ::testing::Test, |
| 31 public ::testing::WithParamInterface<NextProto> { | 31 public ::testing::WithParamInterface<NextProto> { |
| 32 protected: | 32 protected: |
| 33 // Used by RunIPPoolingTest(). | 33 // Used by RunIPPoolingTest(). |
| 34 enum SpdyPoolCloseSessionsType { | 34 enum SpdyPoolCloseSessionsType { |
| 35 SPDY_POOL_CLOSE_SESSIONS_MANUALLY, | 35 SPDY_POOL_CLOSE_SESSIONS_MANUALLY, |
| 36 SPDY_POOL_CLOSE_CURRENT_SESSIONS, | 36 SPDY_POOL_CLOSE_CURRENT_SESSIONS, |
| 37 SPDY_POOL_CLOSE_IDLE_SESSIONS, | 37 SPDY_POOL_CLOSE_IDLE_SESSIONS, |
| 38 }; | 38 }; |
| 39 | 39 |
| 40 SpdySessionPoolTest() | 40 SpdySessionPoolTest() : spdy_session_pool_(NULL) {} |
| 41 : session_deps_(GetParam()), | |
| 42 spdy_session_pool_(NULL) {} | |
| 43 | 41 |
| 44 void CreateNetworkSession() { | 42 void CreateNetworkSession() { |
| 45 http_session_ = SpdySessionDependencies::SpdyCreateSession(&session_deps_); | 43 http_session_ = SpdySessionDependencies::SpdyCreateSession(&session_deps_); |
| 46 spdy_session_pool_ = http_session_->spdy_session_pool(); | 44 spdy_session_pool_ = http_session_->spdy_session_pool(); |
| 47 } | 45 } |
| 48 | 46 |
| 49 void RunIPPoolingTest(SpdyPoolCloseSessionsType close_sessions_type); | 47 void RunIPPoolingTest(SpdyPoolCloseSessionsType close_sessions_type); |
| 50 | 48 |
| 51 SpdySessionDependencies session_deps_; | 49 SpdySessionDependencies session_deps_; |
| 52 std::unique_ptr<HttpNetworkSession> http_session_; | 50 std::unique_ptr<HttpNetworkSession> http_session_; |
| 53 SpdySessionPool* spdy_session_pool_; | 51 SpdySessionPool* spdy_session_pool_; |
| 54 }; | 52 }; |
| 55 | 53 |
| 56 INSTANTIATE_TEST_CASE_P(NextProto, | |
| 57 SpdySessionPoolTest, | |
| 58 testing::Values(kProtoSPDY31, | |
| 59 kProtoHTTP2)); | |
| 60 | |
| 61 // A delegate that opens a new session when it is closed. | 54 // A delegate that opens a new session when it is closed. |
| 62 class SessionOpeningDelegate : public SpdyStream::Delegate { | 55 class SessionOpeningDelegate : public SpdyStream::Delegate { |
| 63 public: | 56 public: |
| 64 SessionOpeningDelegate(SpdySessionPool* spdy_session_pool, | 57 SessionOpeningDelegate(SpdySessionPool* spdy_session_pool, |
| 65 const SpdySessionKey& key) | 58 const SpdySessionKey& key) |
| 66 : spdy_session_pool_(spdy_session_pool), | 59 : spdy_session_pool_(spdy_session_pool), |
| 67 key_(key) {} | 60 key_(key) {} |
| 68 | 61 |
| 69 ~SessionOpeningDelegate() override {} | 62 ~SessionOpeningDelegate() override {} |
| 70 | 63 |
| (...skipping 14 matching lines...) Expand all Loading... |
| 85 ignore_result(CreateFakeSpdySession(spdy_session_pool_, key_)); | 78 ignore_result(CreateFakeSpdySession(spdy_session_pool_, key_)); |
| 86 } | 79 } |
| 87 | 80 |
| 88 private: | 81 private: |
| 89 SpdySessionPool* const spdy_session_pool_; | 82 SpdySessionPool* const spdy_session_pool_; |
| 90 const SpdySessionKey key_; | 83 const SpdySessionKey key_; |
| 91 }; | 84 }; |
| 92 | 85 |
| 93 // Set up a SpdyStream to create a new session when it is closed. | 86 // Set up a SpdyStream to create a new session when it is closed. |
| 94 // CloseCurrentSessions should not close the newly-created session. | 87 // CloseCurrentSessions should not close the newly-created session. |
| 95 TEST_P(SpdySessionPoolTest, CloseCurrentSessions) { | 88 TEST_F(SpdySessionPoolTest, CloseCurrentSessions) { |
| 96 const char kTestHost[] = "www.foo.com"; | 89 const char kTestHost[] = "www.foo.com"; |
| 97 const int kTestPort = 80; | 90 const int kTestPort = 80; |
| 98 | 91 |
| 99 session_deps_.host_resolver->set_synchronous_mode(true); | 92 session_deps_.host_resolver->set_synchronous_mode(true); |
| 100 | 93 |
| 101 HostPortPair test_host_port_pair(kTestHost, kTestPort); | 94 HostPortPair test_host_port_pair(kTestHost, kTestPort); |
| 102 SpdySessionKey test_key = | 95 SpdySessionKey test_key = |
| 103 SpdySessionKey( | 96 SpdySessionKey( |
| 104 test_host_port_pair, ProxyServer::Direct(), | 97 test_host_port_pair, ProxyServer::Direct(), |
| 105 PRIVACY_MODE_DISABLED); | 98 PRIVACY_MODE_DISABLED); |
| (...skipping 29 matching lines...) Expand all Loading... |
| 135 MEDIUM, BoundNetLog()); | 128 MEDIUM, BoundNetLog()); |
| 136 SessionOpeningDelegate delegate(spdy_session_pool_, test_key); | 129 SessionOpeningDelegate delegate(spdy_session_pool_, test_key); |
| 137 spdy_stream->SetDelegate(&delegate); | 130 spdy_stream->SetDelegate(&delegate); |
| 138 | 131 |
| 139 // Close the current session. | 132 // Close the current session. |
| 140 spdy_session_pool_->CloseCurrentSessions(ERR_ABORTED); | 133 spdy_session_pool_->CloseCurrentSessions(ERR_ABORTED); |
| 141 | 134 |
| 142 EXPECT_TRUE(HasSpdySession(spdy_session_pool_, test_key)); | 135 EXPECT_TRUE(HasSpdySession(spdy_session_pool_, test_key)); |
| 143 } | 136 } |
| 144 | 137 |
| 145 TEST_P(SpdySessionPoolTest, CloseCurrentIdleSessions) { | 138 TEST_F(SpdySessionPoolTest, CloseCurrentIdleSessions) { |
| 146 MockConnect connect_data(SYNCHRONOUS, OK); | 139 MockConnect connect_data(SYNCHRONOUS, OK); |
| 147 MockRead reads[] = { | 140 MockRead reads[] = { |
| 148 MockRead(SYNCHRONOUS, ERR_IO_PENDING) // Stall forever. | 141 MockRead(SYNCHRONOUS, ERR_IO_PENDING) // Stall forever. |
| 149 }; | 142 }; |
| 150 | 143 |
| 151 session_deps_.host_resolver->set_synchronous_mode(true); | 144 session_deps_.host_resolver->set_synchronous_mode(true); |
| 152 | 145 |
| 153 StaticSocketDataProvider data1(reads, arraysize(reads), nullptr, 0); | 146 StaticSocketDataProvider data1(reads, arraysize(reads), nullptr, 0); |
| 154 data1.set_connect_data(connect_data); | 147 data1.set_connect_data(connect_data); |
| 155 session_deps_.socket_factory->AddSocketDataProvider(&data1); | 148 session_deps_.socket_factory->AddSocketDataProvider(&data1); |
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 260 | 253 |
| 261 // This should close session 2 | 254 // This should close session 2 |
| 262 spdy_session_pool_->CloseCurrentIdleSessions(); | 255 spdy_session_pool_->CloseCurrentIdleSessions(); |
| 263 base::RunLoop().RunUntilIdle(); | 256 base::RunLoop().RunUntilIdle(); |
| 264 | 257 |
| 265 EXPECT_FALSE(session2); | 258 EXPECT_FALSE(session2); |
| 266 } | 259 } |
| 267 | 260 |
| 268 // Set up a SpdyStream to create a new session when it is closed. | 261 // Set up a SpdyStream to create a new session when it is closed. |
| 269 // CloseAllSessions should close the newly-created session. | 262 // CloseAllSessions should close the newly-created session. |
| 270 TEST_P(SpdySessionPoolTest, CloseAllSessions) { | 263 TEST_F(SpdySessionPoolTest, CloseAllSessions) { |
| 271 const char kTestHost[] = "www.foo.com"; | 264 const char kTestHost[] = "www.foo.com"; |
| 272 const int kTestPort = 80; | 265 const int kTestPort = 80; |
| 273 | 266 |
| 274 session_deps_.host_resolver->set_synchronous_mode(true); | 267 session_deps_.host_resolver->set_synchronous_mode(true); |
| 275 | 268 |
| 276 HostPortPair test_host_port_pair(kTestHost, kTestPort); | 269 HostPortPair test_host_port_pair(kTestHost, kTestPort); |
| 277 SpdySessionKey test_key = | 270 SpdySessionKey test_key = |
| 278 SpdySessionKey( | 271 SpdySessionKey( |
| 279 test_host_port_pair, ProxyServer::Direct(), | 272 test_host_port_pair, ProxyServer::Direct(), |
| 280 PRIVACY_MODE_DISABLED); | 273 PRIVACY_MODE_DISABLED); |
| (...skipping 215 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 496 EXPECT_FALSE(session2); | 489 EXPECT_FALSE(session2); |
| 497 break; | 490 break; |
| 498 } | 491 } |
| 499 | 492 |
| 500 // Verify that the map is all cleaned up. | 493 // Verify that the map is all cleaned up. |
| 501 EXPECT_FALSE(HasSpdySession(spdy_session_pool_, test_hosts[0].key)); | 494 EXPECT_FALSE(HasSpdySession(spdy_session_pool_, test_hosts[0].key)); |
| 502 EXPECT_FALSE(HasSpdySession(spdy_session_pool_, test_hosts[1].key)); | 495 EXPECT_FALSE(HasSpdySession(spdy_session_pool_, test_hosts[1].key)); |
| 503 EXPECT_FALSE(HasSpdySession(spdy_session_pool_, test_hosts[2].key)); | 496 EXPECT_FALSE(HasSpdySession(spdy_session_pool_, test_hosts[2].key)); |
| 504 } | 497 } |
| 505 | 498 |
| 506 TEST_P(SpdySessionPoolTest, IPPooling) { | 499 TEST_F(SpdySessionPoolTest, IPPooling) { |
| 507 RunIPPoolingTest(SPDY_POOL_CLOSE_SESSIONS_MANUALLY); | 500 RunIPPoolingTest(SPDY_POOL_CLOSE_SESSIONS_MANUALLY); |
| 508 } | 501 } |
| 509 | 502 |
| 510 TEST_P(SpdySessionPoolTest, IPPoolingCloseCurrentSessions) { | 503 TEST_F(SpdySessionPoolTest, IPPoolingCloseCurrentSessions) { |
| 511 RunIPPoolingTest(SPDY_POOL_CLOSE_CURRENT_SESSIONS); | 504 RunIPPoolingTest(SPDY_POOL_CLOSE_CURRENT_SESSIONS); |
| 512 } | 505 } |
| 513 | 506 |
| 514 TEST_P(SpdySessionPoolTest, IPPoolingCloseIdleSessions) { | 507 TEST_F(SpdySessionPoolTest, IPPoolingCloseIdleSessions) { |
| 515 RunIPPoolingTest(SPDY_POOL_CLOSE_IDLE_SESSIONS); | 508 RunIPPoolingTest(SPDY_POOL_CLOSE_IDLE_SESSIONS); |
| 516 } | 509 } |
| 517 | 510 |
| 518 // Construct a Pool with SpdySessions in various availability states. Simulate | 511 // Construct a Pool with SpdySessions in various availability states. Simulate |
| 519 // an IP address change. Ensure sessions gracefully shut down. Regression test | 512 // an IP address change. Ensure sessions gracefully shut down. Regression test |
| 520 // for crbug.com/379469. | 513 // for crbug.com/379469. |
| 521 TEST_P(SpdySessionPoolTest, IPAddressChanged) { | 514 TEST_F(SpdySessionPoolTest, IPAddressChanged) { |
| 522 MockConnect connect_data(SYNCHRONOUS, OK); | 515 MockConnect connect_data(SYNCHRONOUS, OK); |
| 523 session_deps_.host_resolver->set_synchronous_mode(true); | 516 session_deps_.host_resolver->set_synchronous_mode(true); |
| 524 | 517 |
| 525 // This isn't testing anything having to do with SPDY frames; we | 518 // This isn't testing anything having to do with SPDY frames; we |
| 526 // can ignore issues of how dependencies are set. We default to | 519 // can ignore issues of how dependencies are set. We default to |
| 527 // setting them (when doing the appropriate protocol) since that's | 520 // setting them (when doing the appropriate protocol) since that's |
| 528 // where we're eventually headed for all HTTP/2 connections. | 521 // where we're eventually headed for all HTTP/2 connections. |
| 529 session_deps_.enable_priority_dependencies = true; | 522 session_deps_.enable_priority_dependencies = true; |
| 530 SpdyTestUtil spdy_util(GetParam(), /*enable_priority_dependencies*/ true); | 523 SpdyTestUtil spdy_util(/*enable_priority_dependencies*/ true); |
| 531 | 524 |
| 532 MockRead reads[] = { | 525 MockRead reads[] = { |
| 533 MockRead(SYNCHRONOUS, ERR_IO_PENDING) // Stall forever. | 526 MockRead(SYNCHRONOUS, ERR_IO_PENDING) // Stall forever. |
| 534 }; | 527 }; |
| 535 std::unique_ptr<SpdySerializedFrame> req( | 528 std::unique_ptr<SpdySerializedFrame> req( |
| 536 spdy_util.ConstructSpdyGet("http://www.a.com", 1, MEDIUM)); | 529 spdy_util.ConstructSpdyGet("http://www.a.com", 1, MEDIUM)); |
| 537 MockWrite writes[] = {CreateMockWrite(*req, 1)}; | 530 MockWrite writes[] = {CreateMockWrite(*req, 1)}; |
| 538 | 531 |
| 539 StaticSocketDataProvider dataA(reads, arraysize(reads), writes, | 532 StaticSocketDataProvider dataA(reads, arraysize(reads), writes, |
| 540 arraysize(writes)); | 533 arraysize(writes)); |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 631 EXPECT_TRUE(sessionC->IsDraining()); | 624 EXPECT_TRUE(sessionC->IsDraining()); |
| 632 | 625 |
| 633 // Both streams were closed with an error. | 626 // Both streams were closed with an error. |
| 634 EXPECT_TRUE(delegateA.StreamIsClosed()); | 627 EXPECT_TRUE(delegateA.StreamIsClosed()); |
| 635 EXPECT_THAT(delegateA.WaitForClose(), IsError(ERR_NETWORK_CHANGED)); | 628 EXPECT_THAT(delegateA.WaitForClose(), IsError(ERR_NETWORK_CHANGED)); |
| 636 EXPECT_TRUE(delegateB.StreamIsClosed()); | 629 EXPECT_TRUE(delegateB.StreamIsClosed()); |
| 637 EXPECT_THAT(delegateB.WaitForClose(), IsError(ERR_NETWORK_CHANGED)); | 630 EXPECT_THAT(delegateB.WaitForClose(), IsError(ERR_NETWORK_CHANGED)); |
| 638 #endif // defined(OS_ANDROID) || defined(OS_WIN) || defined(OS_IOS) | 631 #endif // defined(OS_ANDROID) || defined(OS_WIN) || defined(OS_IOS) |
| 639 } | 632 } |
| 640 | 633 |
| 641 TEST_P(SpdySessionPoolTest, FindAvailableSession) { | 634 TEST_F(SpdySessionPoolTest, FindAvailableSession) { |
| 642 SpdySessionKey key(HostPortPair("https://www.example.org", 443), | 635 SpdySessionKey key(HostPortPair("https://www.example.org", 443), |
| 643 ProxyServer::Direct(), PRIVACY_MODE_DISABLED); | 636 ProxyServer::Direct(), PRIVACY_MODE_DISABLED); |
| 644 | 637 |
| 645 MockRead reads[] = {MockRead(SYNCHRONOUS, ERR_IO_PENDING)}; | 638 MockRead reads[] = {MockRead(SYNCHRONOUS, ERR_IO_PENDING)}; |
| 646 StaticSocketDataProvider data(reads, arraysize(reads), nullptr, 0); | 639 StaticSocketDataProvider data(reads, arraysize(reads), nullptr, 0); |
| 647 data.set_connect_data(MockConnect(SYNCHRONOUS, OK)); | 640 data.set_connect_data(MockConnect(SYNCHRONOUS, OK)); |
| 648 session_deps_.socket_factory->AddSocketDataProvider(&data); | 641 session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 649 | 642 |
| 650 SSLSocketDataProvider ssl(SYNCHRONOUS, OK); | 643 SSLSocketDataProvider ssl(SYNCHRONOUS, OK); |
| 651 session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); | 644 session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 669 // there is no pushed stream on any sessions owned by |spdy_session_pool_|. | 662 // there is no pushed stream on any sessions owned by |spdy_session_pool_|. |
| 670 base::WeakPtr<SpdySession> session2 = | 663 base::WeakPtr<SpdySession> session2 = |
| 671 spdy_session_pool_->FindAvailableSession( | 664 spdy_session_pool_->FindAvailableSession( |
| 672 key, GURL("http://news.example.org/foo.html"), BoundNetLog()); | 665 key, GURL("http://news.example.org/foo.html"), BoundNetLog()); |
| 673 EXPECT_EQ(session.get(), session2.get()); | 666 EXPECT_EQ(session.get(), session2.get()); |
| 674 | 667 |
| 675 spdy_session_pool_->CloseCurrentSessions(ERR_ABORTED); | 668 spdy_session_pool_->CloseCurrentSessions(ERR_ABORTED); |
| 676 } | 669 } |
| 677 | 670 |
| 678 } // namespace net | 671 } // namespace net |
| OLD | NEW |