| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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.h" | 5 #include "net/spdy/spdy_session.h" |
| 6 | 6 |
| 7 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
| 8 #include "net/base/io_buffer.h" | 8 #include "net/base/io_buffer.h" |
| 9 #include "net/base/ip_endpoint.h" | 9 #include "net/base/ip_endpoint.h" |
| 10 #include "net/base/net_log_unittest.h" | 10 #include "net/base/net_log_unittest.h" |
| (...skipping 382 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 393 EXPECT_TRUE(session->IsClosed()); | 393 EXPECT_TRUE(session->IsClosed()); |
| 394 EXPECT_EQ(0u, session->num_active_streams()); | 394 EXPECT_EQ(0u, session->num_active_streams()); |
| 395 EXPECT_EQ(0u, session->num_unclaimed_pushed_streams()); | 395 EXPECT_EQ(0u, session->num_unclaimed_pushed_streams()); |
| 396 EXPECT_FALSE(spdy_session_pool_->HasSession(pair_)); | 396 EXPECT_FALSE(spdy_session_pool_->HasSession(pair_)); |
| 397 | 397 |
| 398 // Delete the first session. | 398 // Delete the first session. |
| 399 session = NULL; | 399 session = NULL; |
| 400 } | 400 } |
| 401 | 401 |
| 402 TEST_F(SpdySessionSpdy2Test, CloseIdleSessions) { | 402 TEST_F(SpdySessionSpdy2Test, CloseIdleSessions) { |
| 403 MockConnect connect_data(SYNCHRONOUS, OK); |
| 404 MockRead reads[] = { |
| 405 MockRead(ASYNC, 0, 0) // EOF |
| 406 }; |
| 407 |
| 408 StaticSocketDataProvider data(reads, arraysize(reads), NULL, 0); |
| 409 data.set_connect_data(connect_data); |
| 410 session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 411 |
| 403 CreateNetworkSession(); | 412 CreateNetworkSession(); |
| 404 | 413 |
| 405 // Set up session 1 | 414 // Set up session 1 |
| 406 const std::string kTestHost1("http://www.a.com"); | 415 const std::string kTestHost1("http://www.a.com"); |
| 407 HostPortPair test_host_port_pair1(kTestHost1, 80); | 416 HostPortPair test_host_port_pair1(kTestHost1, 80); |
| 408 HostPortProxyPair pair1(test_host_port_pair1, ProxyServer::Direct()); | 417 HostPortProxyPair pair1(test_host_port_pair1, ProxyServer::Direct()); |
| 409 scoped_refptr<SpdySession> session1 = GetSession(pair1); | 418 scoped_refptr<SpdySession> session1 = GetSession(pair1); |
| 419 EXPECT_EQ( |
| 420 OK, |
| 421 InitializeSession( |
| 422 http_session_.get(), session1.get(), test_host_port_pair1)); |
| 410 GURL url1(kTestHost1); | 423 GURL url1(kTestHost1); |
| 411 scoped_refptr<SpdyStream> spdy_stream1 = | 424 scoped_refptr<SpdyStream> spdy_stream1 = |
| 412 CreateStreamSynchronously(session1, url1, MEDIUM, BoundNetLog()); | 425 CreateStreamSynchronously(session1, url1, MEDIUM, BoundNetLog()); |
| 413 ASSERT_TRUE(spdy_stream1.get() != NULL); | 426 ASSERT_TRUE(spdy_stream1.get() != NULL); |
| 414 | 427 |
| 415 // Set up session 2 | 428 // Set up session 2 |
| 429 session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 416 const std::string kTestHost2("http://www.b.com"); | 430 const std::string kTestHost2("http://www.b.com"); |
| 417 HostPortPair test_host_port_pair2(kTestHost2, 80); | 431 HostPortPair test_host_port_pair2(kTestHost2, 80); |
| 418 HostPortProxyPair pair2(test_host_port_pair2, ProxyServer::Direct()); | 432 HostPortProxyPair pair2(test_host_port_pair2, ProxyServer::Direct()); |
| 419 scoped_refptr<SpdySession> session2 = GetSession(pair2); | 433 scoped_refptr<SpdySession> session2 = GetSession(pair2); |
| 434 EXPECT_EQ( |
| 435 OK, |
| 436 InitializeSession( |
| 437 http_session_.get(), session2.get(), test_host_port_pair2)); |
| 420 GURL url2(kTestHost2); | 438 GURL url2(kTestHost2); |
| 421 scoped_refptr<SpdyStream> spdy_stream2 = | 439 scoped_refptr<SpdyStream> spdy_stream2 = |
| 422 CreateStreamSynchronously(session2, url2, MEDIUM, BoundNetLog()); | 440 CreateStreamSynchronously(session2, url2, MEDIUM, BoundNetLog()); |
| 423 ASSERT_TRUE(spdy_stream2.get() != NULL); | 441 ASSERT_TRUE(spdy_stream2.get() != NULL); |
| 424 | 442 |
| 425 // Set up session 3 | 443 // Set up session 3 |
| 444 session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 426 const std::string kTestHost3("http://www.c.com"); | 445 const std::string kTestHost3("http://www.c.com"); |
| 427 HostPortPair test_host_port_pair3(kTestHost3, 80); | 446 HostPortPair test_host_port_pair3(kTestHost3, 80); |
| 428 HostPortProxyPair pair3(test_host_port_pair3, ProxyServer::Direct()); | 447 HostPortProxyPair pair3(test_host_port_pair3, ProxyServer::Direct()); |
| 429 scoped_refptr<SpdySession> session3 = GetSession(pair3); | 448 scoped_refptr<SpdySession> session3 = GetSession(pair3); |
| 449 EXPECT_EQ( |
| 450 OK, |
| 451 InitializeSession( |
| 452 http_session_.get(), session3.get(), test_host_port_pair3)); |
| 430 GURL url3(kTestHost3); | 453 GURL url3(kTestHost3); |
| 431 scoped_refptr<SpdyStream> spdy_stream3 = | 454 scoped_refptr<SpdyStream> spdy_stream3 = |
| 432 CreateStreamSynchronously(session3, url3, MEDIUM, BoundNetLog()); | 455 CreateStreamSynchronously(session3, url3, MEDIUM, BoundNetLog()); |
| 433 ASSERT_TRUE(spdy_stream3.get() != NULL); | 456 ASSERT_TRUE(spdy_stream3.get() != NULL); |
| 434 | 457 |
| 435 // All sessions are active and not closed | 458 // All sessions are active and not closed |
| 436 EXPECT_TRUE(session1->is_active()); | 459 EXPECT_TRUE(session1->is_active()); |
| 437 EXPECT_FALSE(session1->IsClosed()); | 460 EXPECT_FALSE(session1->IsClosed()); |
| 438 EXPECT_TRUE(session2->is_active()); | 461 EXPECT_TRUE(session2->is_active()); |
| 439 EXPECT_FALSE(session2->IsClosed()); | 462 EXPECT_FALSE(session2->IsClosed()); |
| (...skipping 245 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 685 | 708 |
| 686 namespace { | 709 namespace { |
| 687 | 710 |
| 688 // Specifies the style for closing the connection. | 711 // Specifies the style for closing the connection. |
| 689 enum SpdyPoolCloseSessionsType { | 712 enum SpdyPoolCloseSessionsType { |
| 690 SPDY_POOL_CLOSE_SESSIONS_MANUALLY, | 713 SPDY_POOL_CLOSE_SESSIONS_MANUALLY, |
| 691 SPDY_POOL_CLOSE_CURRENT_SESSIONS, | 714 SPDY_POOL_CLOSE_CURRENT_SESSIONS, |
| 692 SPDY_POOL_CLOSE_IDLE_SESSIONS, | 715 SPDY_POOL_CLOSE_IDLE_SESSIONS, |
| 693 }; | 716 }; |
| 694 | 717 |
| 718 // Initialize the SpdySession with socket. |
| 719 void IPPoolingInitializedSession( |
| 720 const std::string& group_name, |
| 721 const scoped_refptr<TransportSocketParams>& transport_params, |
| 722 HttpNetworkSession* http_session, |
| 723 SpdySession* session) { |
| 724 scoped_ptr<ClientSocketHandle> connection(new ClientSocketHandle); |
| 725 EXPECT_EQ(OK, connection->Init(group_name, |
| 726 transport_params, MEDIUM, CompletionCallback(), |
| 727 http_session->GetTransportSocketPool( |
| 728 HttpNetworkSession::NORMAL_SOCKET_POOL), |
| 729 BoundNetLog())); |
| 730 EXPECT_EQ(OK, session->InitializeWithSocket(connection.release(), false, OK)); |
| 731 } |
| 732 |
| 695 // This test has three variants, one for each style of closing the connection. | 733 // This test has three variants, one for each style of closing the connection. |
| 696 // If |clean_via_close_current_sessions| is SPDY_POOL_CLOSE_SESSIONS_MANUALLY, | 734 // If |clean_via_close_current_sessions| is SPDY_POOL_CLOSE_SESSIONS_MANUALLY, |
| 697 // the sessions are closed manually, calling SpdySessionPool::Remove() directly. | 735 // the sessions are closed manually, calling SpdySessionPool::Remove() directly. |
| 698 // If |clean_via_close_current_sessions| is SPDY_POOL_CLOSE_CURRENT_SESSIONS, | 736 // If |clean_via_close_current_sessions| is SPDY_POOL_CLOSE_CURRENT_SESSIONS, |
| 699 // sessions are closed with SpdySessionPool::CloseCurrentSessions(). | 737 // sessions are closed with SpdySessionPool::CloseCurrentSessions(). |
| 700 // If |clean_via_close_current_sessions| is SPDY_POOL_CLOSE_IDLE_SESSIONS, | 738 // If |clean_via_close_current_sessions| is SPDY_POOL_CLOSE_IDLE_SESSIONS, |
| 701 // sessions are closed with SpdySessionPool::CloseIdleSessions(). | 739 // sessions are closed with SpdySessionPool::CloseIdleSessions(). |
| 702 void IPPoolingTest(SpdyPoolCloseSessionsType close_sessions_type) { | 740 void IPPoolingTest(SpdyPoolCloseSessionsType close_sessions_type) { |
| 703 const int kTestPort = 80; | 741 const int kTestPort = 80; |
| 704 struct TestHosts { | 742 struct TestHosts { |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 757 | 795 |
| 758 // Setup the first session to the first host. | 796 // Setup the first session to the first host. |
| 759 SpdySessionPool* spdy_session_pool(http_session->spdy_session_pool()); | 797 SpdySessionPool* spdy_session_pool(http_session->spdy_session_pool()); |
| 760 EXPECT_FALSE(spdy_session_pool->HasSession(test_hosts[0].pair)); | 798 EXPECT_FALSE(spdy_session_pool->HasSession(test_hosts[0].pair)); |
| 761 scoped_refptr<SpdySession> session = | 799 scoped_refptr<SpdySession> session = |
| 762 spdy_session_pool->Get(test_hosts[0].pair, BoundNetLog()); | 800 spdy_session_pool->Get(test_hosts[0].pair, BoundNetLog()); |
| 763 EXPECT_TRUE(spdy_session_pool->HasSession(test_hosts[0].pair)); | 801 EXPECT_TRUE(spdy_session_pool->HasSession(test_hosts[0].pair)); |
| 764 | 802 |
| 765 HostPortPair test_host_port_pair(test_hosts[0].name, kTestPort); | 803 HostPortPair test_host_port_pair(test_hosts[0].name, kTestPort); |
| 766 | 804 |
| 805 // Initialize session for the first host. |
| 767 scoped_refptr<TransportSocketParams> transport_params( | 806 scoped_refptr<TransportSocketParams> transport_params( |
| 768 new TransportSocketParams(test_host_port_pair, | 807 new TransportSocketParams(test_host_port_pair, |
| 769 MEDIUM, | 808 MEDIUM, |
| 770 false, | 809 false, |
| 771 false, | 810 false, |
| 772 OnHostResolutionCallback())); | 811 OnHostResolutionCallback())); |
| 773 scoped_ptr<ClientSocketHandle> connection(new ClientSocketHandle); | 812 IPPoolingInitializedSession(test_host_port_pair.ToString(), |
| 774 EXPECT_EQ(OK, connection->Init(test_host_port_pair.ToString(), | 813 transport_params, http_session, session); |
| 775 transport_params, MEDIUM, CompletionCallback(), | |
| 776 http_session->GetTransportSocketPool( | |
| 777 HttpNetworkSession::NORMAL_SOCKET_POOL), | |
| 778 BoundNetLog())); | |
| 779 EXPECT_EQ(OK, session->InitializeWithSocket(connection.release(), false, OK)); | |
| 780 | 814 |
| 781 // TODO(rtenneti): MockClientSocket::GetPeerAddress return's 0 as the port | 815 // TODO(rtenneti): MockClientSocket::GetPeerAddress return's 0 as the port |
| 782 // number. Fix it to return port 80 and then use GetPeerAddress to AddAlias. | 816 // number. Fix it to return port 80 and then use GetPeerAddress to AddAlias. |
| 783 SpdySessionPoolPeer pool_peer(spdy_session_pool); | 817 SpdySessionPoolPeer pool_peer(spdy_session_pool); |
| 784 pool_peer.AddAlias(test_hosts[0].addresses.front(), test_hosts[0].pair); | 818 pool_peer.AddAlias(test_hosts[0].addresses.front(), test_hosts[0].pair); |
| 785 | 819 |
| 786 // Flush the SpdySession::OnReadComplete() task. | 820 // Flush the SpdySession::OnReadComplete() task. |
| 787 MessageLoop::current()->RunUntilIdle(); | 821 MessageLoop::current()->RunUntilIdle(); |
| 788 | 822 |
| 789 // The third host has no overlap with the first, so it can't pool IPs. | 823 // The third host has no overlap with the first, so it can't pool IPs. |
| (...skipping 12 matching lines...) Expand all Loading... |
| 802 | 836 |
| 803 // Create a new session to host 2. | 837 // Create a new session to host 2. |
| 804 scoped_refptr<SpdySession> session2 = | 838 scoped_refptr<SpdySession> session2 = |
| 805 spdy_session_pool->Get(test_hosts[2].pair, BoundNetLog()); | 839 spdy_session_pool->Get(test_hosts[2].pair, BoundNetLog()); |
| 806 | 840 |
| 807 // Verify that we have sessions for everything. | 841 // Verify that we have sessions for everything. |
| 808 EXPECT_TRUE(spdy_session_pool->HasSession(test_hosts[0].pair)); | 842 EXPECT_TRUE(spdy_session_pool->HasSession(test_hosts[0].pair)); |
| 809 EXPECT_TRUE(spdy_session_pool->HasSession(test_hosts[1].pair)); | 843 EXPECT_TRUE(spdy_session_pool->HasSession(test_hosts[1].pair)); |
| 810 EXPECT_TRUE(spdy_session_pool->HasSession(test_hosts[2].pair)); | 844 EXPECT_TRUE(spdy_session_pool->HasSession(test_hosts[2].pair)); |
| 811 | 845 |
| 846 // Initialize session for host 2. |
| 847 session_deps.socket_factory->AddSocketDataProvider(&data); |
| 848 IPPoolingInitializedSession(test_hosts[2].pair.first.ToString(), |
| 849 transport_params, http_session, session2); |
| 850 |
| 812 // Grab the session to host 1 and verify that it is the same session | 851 // Grab the session to host 1 and verify that it is the same session |
| 813 // we got with host 0, and that is a different than host 2's session. | 852 // we got with host 0, and that is a different than host 2's session. |
| 814 scoped_refptr<SpdySession> session1 = | 853 scoped_refptr<SpdySession> session1 = |
| 815 spdy_session_pool->Get(test_hosts[1].pair, BoundNetLog()); | 854 spdy_session_pool->Get(test_hosts[1].pair, BoundNetLog()); |
| 816 EXPECT_EQ(session.get(), session1.get()); | 855 EXPECT_EQ(session.get(), session1.get()); |
| 817 EXPECT_NE(session2.get(), session1.get()); | 856 EXPECT_NE(session2.get(), session1.get()); |
| 818 | 857 |
| 858 // Initialize session for host 1. |
| 859 session_deps.socket_factory->AddSocketDataProvider(&data); |
| 860 IPPoolingInitializedSession(test_hosts[2].pair.first.ToString(), |
| 861 transport_params, http_session, session2); |
| 862 |
| 819 // Remove the aliases and observe that we still have a session for host1. | 863 // Remove the aliases and observe that we still have a session for host1. |
| 820 pool_peer.RemoveAliases(test_hosts[0].pair); | 864 pool_peer.RemoveAliases(test_hosts[0].pair); |
| 821 pool_peer.RemoveAliases(test_hosts[1].pair); | 865 pool_peer.RemoveAliases(test_hosts[1].pair); |
| 822 EXPECT_TRUE(spdy_session_pool->HasSession(test_hosts[1].pair)); | 866 EXPECT_TRUE(spdy_session_pool->HasSession(test_hosts[1].pair)); |
| 823 | 867 |
| 824 // Expire the host cache | 868 // Expire the host cache |
| 825 session_deps.host_resolver->GetHostCache()->clear(); | 869 session_deps.host_resolver->GetHostCache()->clear(); |
| 826 EXPECT_TRUE(spdy_session_pool->HasSession(test_hosts[1].pair)); | 870 EXPECT_TRUE(spdy_session_pool->HasSession(test_hosts[1].pair)); |
| 827 | 871 |
| 828 // Cleanup the sessions. | 872 // Cleanup the sessions. |
| (...skipping 1161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1990 http_session_.get(), session.get(), test_host_port_pair_); | 2034 http_session_.get(), session.get(), test_host_port_pair_); |
| 1991 | 2035 |
| 1992 EXPECT_EQ(SpdySession::FLOW_CONTROL_NONE, session->flow_control_state()); | 2036 EXPECT_EQ(SpdySession::FLOW_CONTROL_NONE, session->flow_control_state()); |
| 1993 EXPECT_EQ(kSpdyVersion2, session->buffered_spdy_framer_->protocol_version()); | 2037 EXPECT_EQ(kSpdyVersion2, session->buffered_spdy_framer_->protocol_version()); |
| 1994 EXPECT_EQ(0, session->session_send_window_size_); | 2038 EXPECT_EQ(0, session->session_send_window_size_); |
| 1995 EXPECT_EQ(0, session->session_recv_window_size_); | 2039 EXPECT_EQ(0, session->session_recv_window_size_); |
| 1996 EXPECT_EQ(0, session->session_unacked_recv_window_bytes_); | 2040 EXPECT_EQ(0, session->session_unacked_recv_window_bytes_); |
| 1997 } | 2041 } |
| 1998 | 2042 |
| 1999 } // namespace net | 2043 } // namespace net |
| OLD | NEW |