| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "net/http/http_network_session.h" | |
| 6 | |
| 7 #include "base/ref_counted.h" | |
| 8 #include "net/base/mock_host_resolver.h" | |
| 9 #include "net/base/mock_network_change_notifier.h" | |
| 10 #include "net/base/ssl_config_service_defaults.h" | |
| 11 #include "net/base/test_completion_callback.h" | |
| 12 #include "net/http/http_auth_handler_factory.h" | |
| 13 #include "net/proxy/proxy_service.h" | |
| 14 #include "net/socket/client_socket_handle.h" | |
| 15 #include "net/socket/socket_test_util.h" | |
| 16 #include "net/socket/tcp_client_socket_pool.h" | |
| 17 #include "net/spdy/spdy_session.h" | |
| 18 #include "net/spdy/spdy_session_pool.h" | |
| 19 #include "testing/gtest/include/gtest/gtest.h" | |
| 20 | |
| 21 namespace net { | |
| 22 namespace { | |
| 23 | |
| 24 TEST(HttpNetworkSessionTest, FlushOnNetworkChange) { | |
| 25 MockNetworkChangeNotifier mock_notifier; | |
| 26 scoped_refptr<MockCachingHostResolver> mock_resolver( | |
| 27 new MockCachingHostResolver); | |
| 28 MockClientSocketFactory mock_factory; | |
| 29 scoped_ptr<HttpAuthHandlerFactory> auth_handler_factory( | |
| 30 HttpAuthHandlerFactory::CreateDefault()); | |
| 31 scoped_refptr<HttpNetworkSession> session( | |
| 32 new HttpNetworkSession(&mock_notifier, | |
| 33 mock_resolver, | |
| 34 ProxyService::CreateNull(), | |
| 35 &mock_factory, | |
| 36 new SSLConfigServiceDefaults, | |
| 37 auth_handler_factory.get())); | |
| 38 | |
| 39 scoped_refptr<TCPClientSocketPool> tcp_socket_pool( | |
| 40 session->tcp_socket_pool()); | |
| 41 | |
| 42 // Start up one socket to leave idle in the pool. | |
| 43 StaticSocketDataProvider data; | |
| 44 mock_factory.AddSocketDataProvider(&data); | |
| 45 TestCompletionCallback callback; | |
| 46 ClientSocketHandle handle; | |
| 47 TCPSocketParams dest("www.google.com", 80, LOW, GURL(), false); | |
| 48 int rv = handle.Init( | |
| 49 "1", dest, LOW, &callback, tcp_socket_pool, BoundNetLog()); | |
| 50 EXPECT_EQ(ERR_IO_PENDING, rv); | |
| 51 EXPECT_FALSE(handle.is_initialized()); | |
| 52 EXPECT_FALSE(handle.socket()); | |
| 53 EXPECT_EQ(OK, callback.WaitForResult()); | |
| 54 EXPECT_TRUE(handle.is_initialized()); | |
| 55 EXPECT_TRUE(handle.socket()); | |
| 56 | |
| 57 // Need to run all pending to release the socket back to the pool. | |
| 58 handle.Reset(); | |
| 59 MessageLoop::current()->RunAllPending(); | |
| 60 | |
| 61 // Now we should have 1 idle socket. | |
| 62 EXPECT_EQ(1, tcp_socket_pool->IdleSocketCount()); | |
| 63 | |
| 64 // Start up another socket to keep as active. Hold onto it until after the IP | |
| 65 // address change causes a session flush. | |
| 66 StaticSocketDataProvider data2; | |
| 67 mock_factory.AddSocketDataProvider(&data2); | |
| 68 rv = handle.Init("2", dest, LOW, &callback, tcp_socket_pool, BoundNetLog()); | |
| 69 EXPECT_EQ(ERR_IO_PENDING, rv); | |
| 70 EXPECT_FALSE(handle.is_initialized()); | |
| 71 EXPECT_FALSE(handle.socket()); | |
| 72 EXPECT_EQ(OK, callback.WaitForResult()); | |
| 73 EXPECT_TRUE(handle.is_initialized()); | |
| 74 EXPECT_TRUE(handle.socket()); | |
| 75 | |
| 76 HostPortPair host_port_pair("www.google.com", 80); | |
| 77 | |
| 78 scoped_refptr<SpdySessionPool> | |
| 79 spdy_session_pool(session->spdy_session_pool()); | |
| 80 scoped_refptr<SpdySession> spdy_session(session->spdy_session_pool()->Get( | |
| 81 host_port_pair, session, BoundNetLog())); | |
| 82 | |
| 83 EXPECT_TRUE(spdy_session_pool->HasSession(host_port_pair)); | |
| 84 | |
| 85 // After an IP address change, we should have 0 idle sockets in the old pool | |
| 86 // and we should have replaced all the pools. | |
| 87 mock_notifier.NotifyIPAddressChange(); | |
| 88 EXPECT_EQ(0, tcp_socket_pool->IdleSocketCount()); | |
| 89 EXPECT_NE(tcp_socket_pool, session->tcp_socket_pool()); | |
| 90 EXPECT_EQ(0, session->tcp_socket_pool()->IdleSocketCount()); | |
| 91 EXPECT_FALSE(session->spdy_session_pool()->HasSession(host_port_pair)); | |
| 92 EXPECT_NE(session->spdy_session_pool(), spdy_session_pool); | |
| 93 | |
| 94 // Release all the handles to old objects. Run all pending tasks afterwards | |
| 95 // to flush pending tasks such as releasing sockets. | |
| 96 tcp_socket_pool = NULL; | |
| 97 spdy_session_pool = NULL; | |
| 98 handle.Reset(); | |
| 99 MessageLoop::current()->RunAllPending(); | |
| 100 } | |
| 101 | |
| 102 } // namespace | |
| 103 } // namespace net | |
| OLD | NEW |