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 1134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1145 net::NetLog::TYPE_SPDY_SESSION_CLOSE, | 1145 net::NetLog::TYPE_SPDY_SESSION_CLOSE, |
1146 net::NetLog::PHASE_NONE); | 1146 net::NetLog::PHASE_NONE); |
1147 | 1147 |
1148 CapturingNetLog::Entry entry = entries[pos]; | 1148 CapturingNetLog::Entry entry = entries[pos]; |
1149 NetLogSpdySessionCloseParameter* request_params = | 1149 NetLogSpdySessionCloseParameter* request_params = |
1150 static_cast<NetLogSpdySessionCloseParameter*>( | 1150 static_cast<NetLogSpdySessionCloseParameter*>( |
1151 entry.extra_parameters.get()); | 1151 entry.extra_parameters.get()); |
1152 EXPECT_EQ(ERR_CONNECTION_CLOSED, request_params->status()); | 1152 EXPECT_EQ(ERR_CONNECTION_CLOSED, request_params->status()); |
1153 } | 1153 } |
1154 | 1154 |
1155 TEST_F(SpdySessionSpdy3Test, CloseOneIdleConnection) { | |
1156 MockHostResolver host_resolver; | |
1157 CapturingBoundNetLog log(CapturingNetLog::kUnbounded); | |
1158 ClientSocketPoolHistograms tcp_histograms(""); | |
1159 MockClientSocketFactory socket_factory; | |
1160 MockConnect connect_data(SYNCHRONOUS, OK); | |
1161 MockRead reads[] = { | |
1162 MockRead(SYNCHRONOUS, ERR_IO_PENDING) // Stall forever. | |
1163 }; | |
1164 StaticSocketDataProvider data(reads, arraysize(reads), NULL, 0); | |
1165 data.set_connect_data(connect_data); | |
1166 socket_factory.AddSocketDataProvider(&data); | |
1167 socket_factory.AddSocketDataProvider(&data); | |
1168 socket_factory.AddSocketDataProvider(&data); | |
1169 socket_factory.AddSocketDataProvider(&data); | |
1170 socket_factory.AddSocketDataProvider(&data); | |
1171 socket_factory.AddSocketDataProvider(&data); | |
1172 TransportClientSocketPool pool( | |
1173 3, 2, | |
1174 &tcp_histograms, | |
1175 &host_resolver, | |
1176 &socket_factory, NULL); | |
1177 // Now if I check out 1 socket from 3 different groups, the next request | |
1178 // will leave us stalled. | |
1179 | |
1180 TestCompletionCallback callback1; | |
1181 HostPortPair host_port1("1.com", 80); | |
1182 scoped_refptr<TransportSocketParams> params1( | |
1183 new TransportSocketParams(host_port1, MEDIUM, false, false)); | |
1184 scoped_ptr<ClientSocketHandle> connection1(new ClientSocketHandle); | |
1185 EXPECT_EQ(ERR_IO_PENDING, | |
1186 connection1->Init(host_port1.ToString(), params1, MEDIUM, | |
1187 callback1.callback(), &pool, log.bound())); | |
1188 EXPECT_EQ(OK, callback1.WaitForResult()); | |
1189 EXPECT_FALSE(pool.IsStalled()); | |
1190 EXPECT_TRUE(connection1->is_initialized()); | |
1191 EXPECT_TRUE(connection1->socket()); | |
1192 | |
1193 TestCompletionCallback callback2; | |
1194 HostPortPair host_port2("2.com", 80); | |
1195 scoped_refptr<TransportSocketParams> params2( | |
1196 new TransportSocketParams(host_port2, MEDIUM, false, false)); | |
1197 scoped_ptr<ClientSocketHandle> connection2(new ClientSocketHandle); | |
1198 EXPECT_EQ(ERR_IO_PENDING, | |
1199 connection2->Init(host_port2.ToString(), params2, MEDIUM, | |
1200 callback2.callback(), &pool, log.bound())); | |
1201 EXPECT_EQ(OK, callback2.WaitForResult()); | |
1202 EXPECT_FALSE(pool.IsStalled()); | |
1203 | |
1204 TestCompletionCallback callback3; | |
1205 HostPortPair host_port3("3.com", 80); | |
1206 scoped_refptr<TransportSocketParams> params3( | |
1207 new TransportSocketParams(host_port3, MEDIUM, false, false)); | |
1208 scoped_ptr<ClientSocketHandle> connection3(new ClientSocketHandle); | |
1209 EXPECT_EQ(ERR_IO_PENDING, | |
1210 connection3->Init(host_port3.ToString(), params3, MEDIUM, | |
1211 callback3.callback(), &pool, log.bound())); | |
1212 EXPECT_EQ(OK, callback3.WaitForResult()); | |
1213 EXPECT_FALSE(pool.IsStalled()); | |
1214 | |
1215 TestCompletionCallback callback4; | |
1216 HostPortPair host_port4("4.com", 80); | |
1217 scoped_refptr<TransportSocketParams> params4( | |
1218 new TransportSocketParams(host_port4, MEDIUM, false, false)); | |
1219 scoped_ptr<ClientSocketHandle> connection4(new ClientSocketHandle); | |
1220 EXPECT_EQ(ERR_IO_PENDING, | |
1221 connection4->Init(host_port4.ToString(), params4, MEDIUM, | |
1222 callback4.callback(), &pool, log.bound())); | |
1223 EXPECT_TRUE(pool.IsStalled()); | |
1224 | |
1225 // Return 1 socket to the pool so that we are no longer stalled | |
1226 connection3->socket()->Disconnect(); | |
1227 connection3->Reset(); | |
1228 EXPECT_EQ(OK, callback4.WaitForResult()); | |
1229 EXPECT_FALSE(pool.IsStalled()); | |
1230 | |
1231 // Now, wrap one of the sockets in a SpdySession | |
1232 HttpServerPropertiesImpl props; | |
1233 SpdySessionPool spdy_session_pool(&host_resolver, NULL, &props); | |
1234 HostPortProxyPair pair1(host_port1, ProxyServer::Direct()); | |
1235 EXPECT_FALSE(spdy_session_pool.HasSession(pair1)); | |
1236 scoped_refptr<SpdySession> session1 = | |
1237 spdy_session_pool.Get(pair1, log.bound()); | |
1238 EXPECT_TRUE(spdy_session_pool.HasSession(pair1)); | |
1239 EXPECT_EQ(OK, | |
1240 session1->InitializeWithSocket(connection1.release(), false, OK)); | |
1241 session1 = NULL; | |
1242 EXPECT_TRUE(spdy_session_pool.HasSession(pair1)); | |
1243 | |
1244 // The SpdySession is now idle. When we request the next socket from the | |
1245 // transport pool, the session will be closed via CloseOneIdleConnection(). | |
1246 TestCompletionCallback callback5; | |
1247 HostPortPair host_port5("5.com", 80); | |
1248 scoped_refptr<TransportSocketParams> params5( | |
1249 new TransportSocketParams(host_port5, MEDIUM, false, false)); | |
1250 scoped_ptr<ClientSocketHandle> connection5(new ClientSocketHandle); | |
1251 EXPECT_EQ(ERR_IO_PENDING, | |
1252 connection5->Init(host_port5.ToString(), params5, MEDIUM, | |
1253 callback5.callback(), &pool, log.bound())); | |
1254 EXPECT_FALSE(pool.IsStalled()); | |
1255 EXPECT_EQ(OK, callback5.WaitForResult()); | |
1256 EXPECT_FALSE(spdy_session_pool.HasSession(pair1)); | |
1257 EXPECT_FALSE(pool.IsStalled()); | |
1258 | |
1259 // Now, wrap one of the sockets in a SpdySession | |
1260 HostPortProxyPair pair2(host_port2, ProxyServer::Direct()); | |
1261 EXPECT_FALSE(spdy_session_pool.HasSession(pair2)); | |
1262 scoped_refptr<SpdySession> session2 = | |
1263 spdy_session_pool.Get(pair2, log.bound()); | |
1264 EXPECT_TRUE(spdy_session_pool.HasSession(pair2)); | |
1265 EXPECT_EQ(OK, | |
1266 session2->InitializeWithSocket(connection2.release(), false, OK)); | |
1267 | |
1268 // Manually remove the socket from the pool. This does *not* return the | |
1269 // transport socket. It will be returned only when the SpdySession is | |
1270 // destructed. | |
1271 session2->RemoveFromPool(); | |
1272 EXPECT_FALSE(spdy_session_pool.HasSession(pair2)); | |
1273 | |
1274 // Although there are no active streams on the session, the pool does not | |
1275 // hold a reference. This means that CloseOneIdleConnection should not | |
1276 // return true, and this request should stall. | |
1277 TestCompletionCallback callback6; | |
1278 HostPortPair host_port6("6.com", 80); | |
1279 scoped_refptr<TransportSocketParams> params6( | |
1280 new TransportSocketParams(host_port5, MEDIUM, false, false)); | |
1281 scoped_ptr<ClientSocketHandle> connection6(new ClientSocketHandle); | |
1282 EXPECT_EQ(ERR_IO_PENDING, | |
1283 connection6->Init(host_port6.ToString(), params6, MEDIUM, | |
1284 callback6.callback(), &pool, log.bound())); | |
1285 EXPECT_TRUE(pool.IsStalled()); | |
1286 | |
1287 // But now if we drop our reference to the session, it will be destructed | |
1288 // and the transport socket will return to the pool, unblocking this | |
1289 // request. | |
1290 session2 = NULL; | |
1291 EXPECT_EQ(OK, callback6.WaitForResult()); | |
1292 EXPECT_FALSE(pool.IsStalled()); | |
1293 } | |
1294 | |
1295 } // namespace net | 1155 } // namespace net |
OLD | NEW |