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/bind.h" | 7 #include "base/bind.h" |
8 #include "base/callback.h" | 8 #include "base/callback.h" |
9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
10 #include "net/base/io_buffer.h" | 10 #include "net/base/io_buffer.h" |
(...skipping 468 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
479 EXPECT_TRUE(session->IsClosed()); | 479 EXPECT_TRUE(session->IsClosed()); |
480 EXPECT_EQ(0u, session->num_active_streams()); | 480 EXPECT_EQ(0u, session->num_active_streams()); |
481 EXPECT_EQ(0u, session->num_unclaimed_pushed_streams()); | 481 EXPECT_EQ(0u, session->num_unclaimed_pushed_streams()); |
482 EXPECT_FALSE(spdy_session_pool_->HasSession(pair_)); | 482 EXPECT_FALSE(spdy_session_pool_->HasSession(pair_)); |
483 | 483 |
484 // Delete the first session. | 484 // Delete the first session. |
485 session = NULL; | 485 session = NULL; |
486 } | 486 } |
487 | 487 |
488 TEST_F(SpdySessionSpdy3Test, CloseIdleSessions) { | 488 TEST_F(SpdySessionSpdy3Test, CloseIdleSessions) { |
| 489 MockConnect connect_data(SYNCHRONOUS, OK); |
| 490 MockRead reads[] = { |
| 491 MockRead(ASYNC, 0, 0) // EOF |
| 492 }; |
| 493 |
| 494 StaticSocketDataProvider data(reads, arraysize(reads), NULL, 0); |
| 495 data.set_connect_data(connect_data); |
| 496 session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 497 |
489 CreateNetworkSession(); | 498 CreateNetworkSession(); |
490 | 499 |
491 // Set up session 1 | 500 // Set up session 1 |
492 const std::string kTestHost1("http://www.a.com"); | 501 const std::string kTestHost1("http://www.a.com"); |
493 HostPortPair test_host_port_pair1(kTestHost1, 80); | 502 HostPortPair test_host_port_pair1(kTestHost1, 80); |
494 HostPortProxyPair pair1(test_host_port_pair1, ProxyServer::Direct()); | 503 HostPortProxyPair pair1(test_host_port_pair1, ProxyServer::Direct()); |
495 scoped_refptr<SpdySession> session1 = GetSession(pair1); | 504 scoped_refptr<SpdySession> session1 = GetSession(pair1); |
| 505 EXPECT_EQ( |
| 506 OK, |
| 507 InitializeSession( |
| 508 http_session_.get(), session1.get(), test_host_port_pair1)); |
496 GURL url1(kTestHost1); | 509 GURL url1(kTestHost1); |
497 scoped_refptr<SpdyStream> spdy_stream1 = | 510 scoped_refptr<SpdyStream> spdy_stream1 = |
498 CreateStreamSynchronously(session1, url1, MEDIUM, BoundNetLog()); | 511 CreateStreamSynchronously(session1, url1, MEDIUM, BoundNetLog()); |
499 ASSERT_TRUE(spdy_stream1.get() != NULL); | 512 ASSERT_TRUE(spdy_stream1.get() != NULL); |
500 | 513 |
501 // Set up session 2 | 514 // Set up session 2 |
| 515 session_deps_.socket_factory->AddSocketDataProvider(&data); |
502 const std::string kTestHost2("http://www.b.com"); | 516 const std::string kTestHost2("http://www.b.com"); |
503 HostPortPair test_host_port_pair2(kTestHost2, 80); | 517 HostPortPair test_host_port_pair2(kTestHost2, 80); |
504 HostPortProxyPair pair2(test_host_port_pair2, ProxyServer::Direct()); | 518 HostPortProxyPair pair2(test_host_port_pair2, ProxyServer::Direct()); |
505 scoped_refptr<SpdySession> session2 = GetSession(pair2); | 519 scoped_refptr<SpdySession> session2 = GetSession(pair2); |
| 520 EXPECT_EQ( |
| 521 OK, |
| 522 InitializeSession( |
| 523 http_session_.get(), session2.get(), test_host_port_pair2)); |
506 GURL url2(kTestHost2); | 524 GURL url2(kTestHost2); |
507 scoped_refptr<SpdyStream> spdy_stream2 = | 525 scoped_refptr<SpdyStream> spdy_stream2 = |
508 CreateStreamSynchronously(session2, url2, MEDIUM, BoundNetLog()); | 526 CreateStreamSynchronously(session2, url2, MEDIUM, BoundNetLog()); |
509 ASSERT_TRUE(spdy_stream2.get() != NULL); | 527 ASSERT_TRUE(spdy_stream2.get() != NULL); |
510 | 528 |
511 // Set up session 3 | 529 // Set up session 3 |
| 530 session_deps_.socket_factory->AddSocketDataProvider(&data); |
512 const std::string kTestHost3("http://www.c.com"); | 531 const std::string kTestHost3("http://www.c.com"); |
513 HostPortPair test_host_port_pair3(kTestHost3, 80); | 532 HostPortPair test_host_port_pair3(kTestHost3, 80); |
514 HostPortProxyPair pair3(test_host_port_pair3, ProxyServer::Direct()); | 533 HostPortProxyPair pair3(test_host_port_pair3, ProxyServer::Direct()); |
515 scoped_refptr<SpdySession> session3 = GetSession(pair3); | 534 scoped_refptr<SpdySession> session3 = GetSession(pair3); |
| 535 EXPECT_EQ( |
| 536 OK, |
| 537 InitializeSession( |
| 538 http_session_.get(), session3.get(), test_host_port_pair3)); |
516 GURL url3(kTestHost3); | 539 GURL url3(kTestHost3); |
517 scoped_refptr<SpdyStream> spdy_stream3 = | 540 scoped_refptr<SpdyStream> spdy_stream3 = |
518 CreateStreamSynchronously(session3, url3, MEDIUM, BoundNetLog()); | 541 CreateStreamSynchronously(session3, url3, MEDIUM, BoundNetLog()); |
519 ASSERT_TRUE(spdy_stream3.get() != NULL); | 542 ASSERT_TRUE(spdy_stream3.get() != NULL); |
520 | 543 |
521 // All sessions are active and not closed | 544 // All sessions are active and not closed |
522 EXPECT_TRUE(session1->is_active()); | 545 EXPECT_TRUE(session1->is_active()); |
523 EXPECT_FALSE(session1->IsClosed()); | 546 EXPECT_FALSE(session1->IsClosed()); |
524 EXPECT_TRUE(session2->is_active()); | 547 EXPECT_TRUE(session2->is_active()); |
525 EXPECT_FALSE(session2->IsClosed()); | 548 EXPECT_FALSE(session2->IsClosed()); |
(...skipping 250 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
776 | 799 |
777 namespace { | 800 namespace { |
778 | 801 |
779 // Specifies the style for closing the connection. | 802 // Specifies the style for closing the connection. |
780 enum SpdyPoolCloseSessionsType { | 803 enum SpdyPoolCloseSessionsType { |
781 SPDY_POOL_CLOSE_SESSIONS_MANUALLY, | 804 SPDY_POOL_CLOSE_SESSIONS_MANUALLY, |
782 SPDY_POOL_CLOSE_CURRENT_SESSIONS, | 805 SPDY_POOL_CLOSE_CURRENT_SESSIONS, |
783 SPDY_POOL_CLOSE_IDLE_SESSIONS, | 806 SPDY_POOL_CLOSE_IDLE_SESSIONS, |
784 }; | 807 }; |
785 | 808 |
| 809 // Initialize the SpdySession with socket. |
| 810 void IPPoolingInitializedSession( |
| 811 const std::string& group_name, |
| 812 const scoped_refptr<TransportSocketParams>& transport_params, |
| 813 HttpNetworkSession* http_session, |
| 814 SpdySession* session) { |
| 815 scoped_ptr<ClientSocketHandle> connection(new ClientSocketHandle); |
| 816 EXPECT_EQ(OK, connection->Init(group_name, |
| 817 transport_params, MEDIUM, CompletionCallback(), |
| 818 http_session->GetTransportSocketPool( |
| 819 HttpNetworkSession::NORMAL_SOCKET_POOL), |
| 820 BoundNetLog())); |
| 821 EXPECT_EQ(OK, session->InitializeWithSocket(connection.release(), false, OK)); |
| 822 } |
| 823 |
786 // This test has three variants, one for each style of closing the connection. | 824 // This test has three variants, one for each style of closing the connection. |
787 // If |clean_via_close_current_sessions| is SPDY_POOL_CLOSE_SESSIONS_MANUALLY, | 825 // If |clean_via_close_current_sessions| is SPDY_POOL_CLOSE_SESSIONS_MANUALLY, |
788 // the sessions are closed manually, calling SpdySessionPool::Remove() directly. | 826 // the sessions are closed manually, calling SpdySessionPool::Remove() directly. |
789 // If |clean_via_close_current_sessions| is SPDY_POOL_CLOSE_CURRENT_SESSIONS, | 827 // If |clean_via_close_current_sessions| is SPDY_POOL_CLOSE_CURRENT_SESSIONS, |
790 // sessions are closed with SpdySessionPool::CloseCurrentSessions(). | 828 // sessions are closed with SpdySessionPool::CloseCurrentSessions(). |
791 // If |clean_via_close_current_sessions| is SPDY_POOL_CLOSE_IDLE_SESSIONS, | 829 // If |clean_via_close_current_sessions| is SPDY_POOL_CLOSE_IDLE_SESSIONS, |
792 // sessions are closed with SpdySessionPool::CloseIdleSessions(). | 830 // sessions are closed with SpdySessionPool::CloseIdleSessions(). |
793 void IPPoolingTest(SpdyPoolCloseSessionsType close_sessions_type) { | 831 void IPPoolingTest(SpdyPoolCloseSessionsType close_sessions_type) { |
794 const int kTestPort = 80; | 832 const int kTestPort = 80; |
795 struct TestHosts { | 833 struct TestHosts { |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
848 | 886 |
849 // Setup the first session to the first host. | 887 // Setup the first session to the first host. |
850 SpdySessionPool* spdy_session_pool(http_session->spdy_session_pool()); | 888 SpdySessionPool* spdy_session_pool(http_session->spdy_session_pool()); |
851 EXPECT_FALSE(spdy_session_pool->HasSession(test_hosts[0].pair)); | 889 EXPECT_FALSE(spdy_session_pool->HasSession(test_hosts[0].pair)); |
852 scoped_refptr<SpdySession> session = | 890 scoped_refptr<SpdySession> session = |
853 spdy_session_pool->Get(test_hosts[0].pair, BoundNetLog()); | 891 spdy_session_pool->Get(test_hosts[0].pair, BoundNetLog()); |
854 EXPECT_TRUE(spdy_session_pool->HasSession(test_hosts[0].pair)); | 892 EXPECT_TRUE(spdy_session_pool->HasSession(test_hosts[0].pair)); |
855 | 893 |
856 HostPortPair test_host_port_pair(test_hosts[0].name, kTestPort); | 894 HostPortPair test_host_port_pair(test_hosts[0].name, kTestPort); |
857 | 895 |
| 896 // Initialize session for the first host. |
858 scoped_refptr<TransportSocketParams> transport_params( | 897 scoped_refptr<TransportSocketParams> transport_params( |
859 new TransportSocketParams(test_host_port_pair, | 898 new TransportSocketParams(test_host_port_pair, |
860 MEDIUM, | 899 MEDIUM, |
861 false, | 900 false, |
862 false, | 901 false, |
863 OnHostResolutionCallback())); | 902 OnHostResolutionCallback())); |
864 scoped_ptr<ClientSocketHandle> connection(new ClientSocketHandle); | 903 IPPoolingInitializedSession(test_host_port_pair.ToString(), |
865 EXPECT_EQ(OK, connection->Init(test_host_port_pair.ToString(), | 904 transport_params, http_session, session); |
866 transport_params, MEDIUM, CompletionCallback(), | |
867 http_session->GetTransportSocketPool( | |
868 HttpNetworkSession::NORMAL_SOCKET_POOL), | |
869 BoundNetLog())); | |
870 EXPECT_EQ(OK, session->InitializeWithSocket(connection.release(), false, OK)); | |
871 | 905 |
872 // TODO(rtenneti): MockClientSocket::GetPeerAddress return's 0 as the port | 906 // TODO(rtenneti): MockClientSocket::GetPeerAddress return's 0 as the port |
873 // number. Fix it to return port 80 and then use GetPeerAddress to AddAlias. | 907 // number. Fix it to return port 80 and then use GetPeerAddress to AddAlias. |
874 SpdySessionPoolPeer pool_peer(spdy_session_pool); | 908 SpdySessionPoolPeer pool_peer(spdy_session_pool); |
875 pool_peer.AddAlias(test_hosts[0].addresses.front(), test_hosts[0].pair); | 909 pool_peer.AddAlias(test_hosts[0].addresses.front(), test_hosts[0].pair); |
876 | 910 |
877 // Flush the SpdySession::OnReadComplete() task. | 911 // Flush the SpdySession::OnReadComplete() task. |
878 MessageLoop::current()->RunUntilIdle(); | 912 MessageLoop::current()->RunUntilIdle(); |
879 | 913 |
880 // The third host has no overlap with the first, so it can't pool IPs. | 914 // The third host has no overlap with the first, so it can't pool IPs. |
(...skipping 12 matching lines...) Expand all Loading... |
893 | 927 |
894 // Create a new session to host 2. | 928 // Create a new session to host 2. |
895 scoped_refptr<SpdySession> session2 = | 929 scoped_refptr<SpdySession> session2 = |
896 spdy_session_pool->Get(test_hosts[2].pair, BoundNetLog()); | 930 spdy_session_pool->Get(test_hosts[2].pair, BoundNetLog()); |
897 | 931 |
898 // Verify that we have sessions for everything. | 932 // Verify that we have sessions for everything. |
899 EXPECT_TRUE(spdy_session_pool->HasSession(test_hosts[0].pair)); | 933 EXPECT_TRUE(spdy_session_pool->HasSession(test_hosts[0].pair)); |
900 EXPECT_TRUE(spdy_session_pool->HasSession(test_hosts[1].pair)); | 934 EXPECT_TRUE(spdy_session_pool->HasSession(test_hosts[1].pair)); |
901 EXPECT_TRUE(spdy_session_pool->HasSession(test_hosts[2].pair)); | 935 EXPECT_TRUE(spdy_session_pool->HasSession(test_hosts[2].pair)); |
902 | 936 |
| 937 // Initialize session for host 2. |
| 938 session_deps.socket_factory->AddSocketDataProvider(&data); |
| 939 IPPoolingInitializedSession(test_hosts[2].pair.first.ToString(), |
| 940 transport_params, http_session, session2); |
| 941 |
903 // Grab the session to host 1 and verify that it is the same session | 942 // Grab the session to host 1 and verify that it is the same session |
904 // we got with host 0, and that is a different than host 2's session. | 943 // we got with host 0, and that is a different than host 2's session. |
905 scoped_refptr<SpdySession> session1 = | 944 scoped_refptr<SpdySession> session1 = |
906 spdy_session_pool->Get(test_hosts[1].pair, BoundNetLog()); | 945 spdy_session_pool->Get(test_hosts[1].pair, BoundNetLog()); |
907 EXPECT_EQ(session.get(), session1.get()); | 946 EXPECT_EQ(session.get(), session1.get()); |
908 EXPECT_NE(session2.get(), session1.get()); | 947 EXPECT_NE(session2.get(), session1.get()); |
909 | 948 |
| 949 // Initialize session for host 1. |
| 950 session_deps.socket_factory->AddSocketDataProvider(&data); |
| 951 IPPoolingInitializedSession(test_hosts[2].pair.first.ToString(), |
| 952 transport_params, http_session, session2); |
| 953 |
910 // Remove the aliases and observe that we still have a session for host1. | 954 // Remove the aliases and observe that we still have a session for host1. |
911 pool_peer.RemoveAliases(test_hosts[0].pair); | 955 pool_peer.RemoveAliases(test_hosts[0].pair); |
912 pool_peer.RemoveAliases(test_hosts[1].pair); | 956 pool_peer.RemoveAliases(test_hosts[1].pair); |
913 EXPECT_TRUE(spdy_session_pool->HasSession(test_hosts[1].pair)); | 957 EXPECT_TRUE(spdy_session_pool->HasSession(test_hosts[1].pair)); |
914 | 958 |
915 // Expire the host cache | 959 // Expire the host cache |
916 session_deps.host_resolver->GetHostCache()->clear(); | 960 session_deps.host_resolver->GetHostCache()->clear(); |
917 EXPECT_TRUE(spdy_session_pool->HasSession(test_hosts[1].pair)); | 961 EXPECT_TRUE(spdy_session_pool->HasSession(test_hosts[1].pair)); |
918 | 962 |
919 // Cleanup the sessions. | 963 // Cleanup the sessions. |
(...skipping 2432 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3352 EXPECT_EQ(0, delegate1.body_data_sent()); | 3396 EXPECT_EQ(0, delegate1.body_data_sent()); |
3353 | 3397 |
3354 EXPECT_TRUE(delegate2.send_headers_completed()); | 3398 EXPECT_TRUE(delegate2.send_headers_completed()); |
3355 EXPECT_EQ("200", delegate2.GetResponseHeaderValue(":status")); | 3399 EXPECT_EQ("200", delegate2.GetResponseHeaderValue(":status")); |
3356 EXPECT_EQ("HTTP/1.1", delegate2.GetResponseHeaderValue(":version")); | 3400 EXPECT_EQ("HTTP/1.1", delegate2.GetResponseHeaderValue(":version")); |
3357 EXPECT_EQ(std::string(), delegate2.TakeReceivedData()); | 3401 EXPECT_EQ(std::string(), delegate2.TakeReceivedData()); |
3358 EXPECT_EQ(0, delegate2.body_data_sent()); | 3402 EXPECT_EQ(0, delegate2.body_data_sent()); |
3359 } | 3403 } |
3360 | 3404 |
3361 } // namespace net | 3405 } // namespace net |
OLD | NEW |