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 "net/base/host_cache.h" | 7 #include "net/base/host_cache.h" |
8 #include "net/base/ip_endpoint.h" | 8 #include "net/base/ip_endpoint.h" |
9 #include "net/base/net_log_unittest.h" | 9 #include "net/base/net_log_unittest.h" |
10 #include "net/spdy/spdy_io_buffer.h" | 10 #include "net/spdy/spdy_io_buffer.h" |
(...skipping 1046 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1057 net::NetLog::TYPE_SPDY_SESSION_CLOSE, | 1057 net::NetLog::TYPE_SPDY_SESSION_CLOSE, |
1058 net::NetLog::PHASE_NONE); | 1058 net::NetLog::PHASE_NONE); |
1059 | 1059 |
1060 CapturingNetLog::Entry entry = entries[pos]; | 1060 CapturingNetLog::Entry entry = entries[pos]; |
1061 NetLogSpdySessionCloseParameter* request_params = | 1061 NetLogSpdySessionCloseParameter* request_params = |
1062 static_cast<NetLogSpdySessionCloseParameter*>( | 1062 static_cast<NetLogSpdySessionCloseParameter*>( |
1063 entry.extra_parameters.get()); | 1063 entry.extra_parameters.get()); |
1064 EXPECT_EQ(ERR_CONNECTION_CLOSED, request_params->status()); | 1064 EXPECT_EQ(ERR_CONNECTION_CLOSED, request_params->status()); |
1065 } | 1065 } |
1066 | 1066 |
| 1067 TEST_F(SpdySessionSpdy2Test, CloseOneIdleConnection) { |
| 1068 MockHostResolver host_resolver; |
| 1069 CapturingBoundNetLog log(CapturingNetLog::kUnbounded); |
| 1070 ClientSocketPoolHistograms tcp_histograms(""); |
| 1071 MockClientSocketFactory socket_factory; |
| 1072 MockConnect connect_data(SYNCHRONOUS, OK); |
| 1073 MockRead reads[] = { |
| 1074 MockRead(SYNCHRONOUS, ERR_IO_PENDING) // Stall forever. |
| 1075 }; |
| 1076 StaticSocketDataProvider data(reads, arraysize(reads), NULL, 0); |
| 1077 data.set_connect_data(connect_data); |
| 1078 socket_factory.AddSocketDataProvider(&data); |
| 1079 socket_factory.AddSocketDataProvider(&data); |
| 1080 socket_factory.AddSocketDataProvider(&data); |
| 1081 socket_factory.AddSocketDataProvider(&data); |
| 1082 socket_factory.AddSocketDataProvider(&data); |
| 1083 socket_factory.AddSocketDataProvider(&data); |
| 1084 TransportClientSocketPool pool( |
| 1085 3, 2, |
| 1086 &tcp_histograms, |
| 1087 &host_resolver, |
| 1088 &socket_factory, NULL); |
| 1089 // Now if I check out 1 socket from 3 different groups, the next request |
| 1090 // will leave us stalled. |
| 1091 |
| 1092 TestCompletionCallback callback1; |
| 1093 HostPortPair host_port1("1.com", 80); |
| 1094 scoped_refptr<TransportSocketParams> params1( |
| 1095 new TransportSocketParams(host_port1, MEDIUM, false, false)); |
| 1096 scoped_ptr<ClientSocketHandle> connection1(new ClientSocketHandle); |
| 1097 EXPECT_EQ(ERR_IO_PENDING, |
| 1098 connection1->Init(host_port1.ToString(), params1, MEDIUM, |
| 1099 callback1.callback(), &pool, log.bound())); |
| 1100 EXPECT_EQ(OK, callback1.WaitForResult()); |
| 1101 EXPECT_FALSE(pool.IsStalled()); |
| 1102 EXPECT_TRUE(connection1->is_initialized()); |
| 1103 EXPECT_TRUE(connection1->socket()); |
| 1104 |
| 1105 TestCompletionCallback callback2; |
| 1106 HostPortPair host_port2("2.com", 80); |
| 1107 scoped_refptr<TransportSocketParams> params2( |
| 1108 new TransportSocketParams(host_port2, MEDIUM, false, false)); |
| 1109 scoped_ptr<ClientSocketHandle> connection2(new ClientSocketHandle); |
| 1110 EXPECT_EQ(ERR_IO_PENDING, |
| 1111 connection2->Init(host_port2.ToString(), params2, MEDIUM, |
| 1112 callback2.callback(), &pool, log.bound())); |
| 1113 EXPECT_EQ(OK, callback2.WaitForResult()); |
| 1114 EXPECT_FALSE(pool.IsStalled()); |
| 1115 |
| 1116 TestCompletionCallback callback3; |
| 1117 HostPortPair host_port3("3.com", 80); |
| 1118 scoped_refptr<TransportSocketParams> params3( |
| 1119 new TransportSocketParams(host_port3, MEDIUM, false, false)); |
| 1120 scoped_ptr<ClientSocketHandle> connection3(new ClientSocketHandle); |
| 1121 EXPECT_EQ(ERR_IO_PENDING, |
| 1122 connection3->Init(host_port3.ToString(), params3, MEDIUM, |
| 1123 callback3.callback(), &pool, log.bound())); |
| 1124 EXPECT_EQ(OK, callback3.WaitForResult()); |
| 1125 EXPECT_FALSE(pool.IsStalled()); |
| 1126 |
| 1127 TestCompletionCallback callback4; |
| 1128 HostPortPair host_port4("4.com", 80); |
| 1129 scoped_refptr<TransportSocketParams> params4( |
| 1130 new TransportSocketParams(host_port4, MEDIUM, false, false)); |
| 1131 scoped_ptr<ClientSocketHandle> connection4(new ClientSocketHandle); |
| 1132 EXPECT_EQ(ERR_IO_PENDING, |
| 1133 connection4->Init(host_port4.ToString(), params4, MEDIUM, |
| 1134 callback4.callback(), &pool, log.bound())); |
| 1135 EXPECT_TRUE(pool.IsStalled()); |
| 1136 |
| 1137 // Return 1 socket to the pool so that we are no longer stalled |
| 1138 connection3->socket()->Disconnect(); |
| 1139 connection3->Reset(); |
| 1140 EXPECT_EQ(OK, callback4.WaitForResult()); |
| 1141 EXPECT_FALSE(pool.IsStalled()); |
| 1142 |
| 1143 // Now, wrap one of the sockets in a SpdySession |
| 1144 HttpServerPropertiesImpl props; |
| 1145 SpdySessionPool spdy_session_pool(&host_resolver, NULL, &props); |
| 1146 HostPortProxyPair pair1(host_port1, ProxyServer::Direct()); |
| 1147 EXPECT_FALSE(spdy_session_pool.HasSession(pair1)); |
| 1148 scoped_refptr<SpdySession> session1 = |
| 1149 spdy_session_pool.Get(pair1, log.bound()); |
| 1150 EXPECT_TRUE(spdy_session_pool.HasSession(pair1)); |
| 1151 EXPECT_EQ(OK, |
| 1152 session1->InitializeWithSocket(connection1.release(), false, OK)); |
| 1153 session1 = NULL; |
| 1154 EXPECT_TRUE(spdy_session_pool.HasSession(pair1)); |
| 1155 |
| 1156 // The SpdySession is now idle. When we request the next socket from the |
| 1157 // transport pool, the session will be closed via CloseOneIdleConnection(). |
| 1158 TestCompletionCallback callback5; |
| 1159 HostPortPair host_port5("5.com", 80); |
| 1160 scoped_refptr<TransportSocketParams> params5( |
| 1161 new TransportSocketParams(host_port5, MEDIUM, false, false)); |
| 1162 scoped_ptr<ClientSocketHandle> connection5(new ClientSocketHandle); |
| 1163 EXPECT_EQ(ERR_IO_PENDING, |
| 1164 connection5->Init(host_port5.ToString(), params5, MEDIUM, |
| 1165 callback5.callback(), &pool, log.bound())); |
| 1166 EXPECT_FALSE(pool.IsStalled()); |
| 1167 EXPECT_EQ(OK, callback5.WaitForResult()); |
| 1168 EXPECT_FALSE(spdy_session_pool.HasSession(pair1)); |
| 1169 EXPECT_FALSE(pool.IsStalled()); |
| 1170 |
| 1171 // Now, wrap one of the sockets in a SpdySession |
| 1172 HostPortProxyPair pair2(host_port2, ProxyServer::Direct()); |
| 1173 EXPECT_FALSE(spdy_session_pool.HasSession(pair2)); |
| 1174 scoped_refptr<SpdySession> session2 = |
| 1175 spdy_session_pool.Get(pair2, log.bound()); |
| 1176 EXPECT_TRUE(spdy_session_pool.HasSession(pair2)); |
| 1177 EXPECT_EQ(OK, |
| 1178 session2->InitializeWithSocket(connection2.release(), false, OK)); |
| 1179 |
| 1180 // Manually remove the socket from the pool. This does *not* return the |
| 1181 // transport socket. It will be returned only when the SpdySession is |
| 1182 // destructed. |
| 1183 session2->RemoveFromPool(); |
| 1184 EXPECT_FALSE(spdy_session_pool.HasSession(pair2)); |
| 1185 |
| 1186 // Although there are no active streams on the session, the pool does not |
| 1187 // hold a reference. This means that CloseOneIdleConnection should not |
| 1188 // return true, and this request should stall. |
| 1189 TestCompletionCallback callback6; |
| 1190 HostPortPair host_port6("6.com", 80); |
| 1191 scoped_refptr<TransportSocketParams> params6( |
| 1192 new TransportSocketParams(host_port5, MEDIUM, false, false)); |
| 1193 scoped_ptr<ClientSocketHandle> connection6(new ClientSocketHandle); |
| 1194 EXPECT_EQ(ERR_IO_PENDING, |
| 1195 connection6->Init(host_port6.ToString(), params6, MEDIUM, |
| 1196 callback6.callback(), &pool, log.bound())); |
| 1197 EXPECT_TRUE(pool.IsStalled()); |
| 1198 |
| 1199 // But now if we drop our reference to the session, it will be destructed |
| 1200 // and the transport socket will return to the pool, unblocking this |
| 1201 // request. |
| 1202 session2 = NULL; |
| 1203 EXPECT_EQ(OK, callback6.WaitForResult()); |
| 1204 EXPECT_FALSE(pool.IsStalled()); |
| 1205 } |
| 1206 |
1067 } // namespace net | 1207 } // namespace net |
OLD | NEW |