Index: net/socket/websocket_transport_client_socket_pool_unittest.cc |
diff --git a/net/socket/websocket_transport_client_socket_pool_unittest.cc b/net/socket/websocket_transport_client_socket_pool_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..e72b4b6cf38b682544641a16e88a50afb3429d93 |
--- /dev/null |
+++ b/net/socket/websocket_transport_client_socket_pool_unittest.cc |
@@ -0,0 +1,1311 @@ |
+// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "net/socket/websocket_transport_client_socket_pool.h" |
+ |
+#include <queue> |
+ |
+#include "base/bind.h" |
+#include "base/bind_helpers.h" |
+#include "base/callback.h" |
+#include "base/compiler_specific.h" |
+#include "base/logging.h" |
+#include "base/macros.h" |
+#include "base/message_loop/message_loop.h" |
+#include "base/run_loop.h" |
+#include "base/threading/platform_thread.h" |
+#include "net/base/capturing_net_log.h" |
+#include "net/base/ip_endpoint.h" |
+#include "net/base/load_timing_info.h" |
+#include "net/base/load_timing_info_test_util.h" |
+#include "net/base/net_errors.h" |
+#include "net/base/net_util.h" |
+#include "net/base/test_completion_callback.h" |
+#include "net/dns/mock_host_resolver.h" |
+#include "net/socket/client_socket_factory.h" |
+#include "net/socket/client_socket_handle.h" |
+#include "net/socket/client_socket_pool_histograms.h" |
+#include "net/socket/socket_test_util.h" |
+#include "net/socket/ssl_client_socket.h" |
+#include "net/socket/stream_socket.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+ |
+namespace net { |
+ |
+using internal::ClientSocketPoolBaseHelper; |
+ |
+namespace { |
+ |
+const int kMaxSockets = 32; |
+const int kMaxSocketsPerGroup = 6; |
+const net::RequestPriority kDefaultPriority = LOW; |
+ |
+// Make sure |handle| sets load times correctly when it has been assigned a |
+// reused socket. |
+void TestLoadTimingInfoConnectedReused(const ClientSocketHandle& handle) { |
+ LoadTimingInfo load_timing_info; |
+ // Only pass true in as |is_reused|, as in general, HttpStream types should |
+ // have stricter concepts of reuse than socket pools. |
+ EXPECT_TRUE(handle.GetLoadTimingInfo(true, &load_timing_info)); |
+ |
+ EXPECT_TRUE(load_timing_info.socket_reused); |
+ EXPECT_NE(NetLog::Source::kInvalidId, load_timing_info.socket_log_id); |
+ |
+ ExpectConnectTimingHasNoTimes(load_timing_info.connect_timing); |
+ ExpectLoadTimingHasOnlyConnectionTimes(load_timing_info); |
+} |
+ |
+// Make sure |handle| sets load times correctly when it has been assigned a |
+// fresh socket. Also runs TestLoadTimingInfoConnectedReused, since the owner |
+// of a connection where |is_reused| is false may consider the connection |
+// reused. |
+void TestLoadTimingInfoConnectedNotReused(const ClientSocketHandle& handle) { |
+ EXPECT_FALSE(handle.is_reused()); |
+ |
+ LoadTimingInfo load_timing_info; |
+ EXPECT_TRUE(handle.GetLoadTimingInfo(false, &load_timing_info)); |
+ |
+ EXPECT_FALSE(load_timing_info.socket_reused); |
+ EXPECT_NE(NetLog::Source::kInvalidId, load_timing_info.socket_log_id); |
+ |
+ ExpectConnectTimingHasTimes(load_timing_info.connect_timing, |
+ CONNECT_TIMING_HAS_DNS_TIMES); |
+ ExpectLoadTimingHasOnlyConnectionTimes(load_timing_info); |
+ |
+ TestLoadTimingInfoConnectedReused(handle); |
+} |
+ |
+void SetIPv4Address(IPEndPoint* address) { |
+ IPAddressNumber number; |
+ CHECK(ParseIPLiteralToNumber("1.1.1.1", &number)); |
+ *address = IPEndPoint(number, 80); |
+} |
+ |
+void SetIPv6Address(IPEndPoint* address) { |
+ IPAddressNumber number; |
+ CHECK(ParseIPLiteralToNumber("1:abcd::3:4:ff", &number)); |
+ *address = IPEndPoint(number, 80); |
+} |
+ |
+class MockClientSocket : public StreamSocket { |
+ public: |
+ MockClientSocket(const AddressList& addrlist, net::NetLog* net_log) |
+ : connected_(false), |
+ addrlist_(addrlist), |
+ net_log_(BoundNetLog::Make(net_log, NetLog::SOURCE_SOCKET)) {} |
+ |
+ // StreamSocket implementation. |
+ virtual int Connect(const CompletionCallback& callback) OVERRIDE { |
+ connected_ = true; |
+ return OK; |
+ } |
+ virtual void Disconnect() OVERRIDE { connected_ = false; } |
+ virtual bool IsConnected() const OVERRIDE { return connected_; } |
+ virtual bool IsConnectedAndIdle() const OVERRIDE { return connected_; } |
+ virtual int GetPeerAddress(IPEndPoint* address) const OVERRIDE { |
+ *address = addrlist_.front(); |
+ return OK; |
+ } |
+ virtual int GetLocalAddress(IPEndPoint* address) const OVERRIDE { |
+ if (!connected_) |
+ return ERR_SOCKET_NOT_CONNECTED; |
+ if (addrlist_.front().GetFamily() == ADDRESS_FAMILY_IPV4) |
+ SetIPv4Address(address); |
+ else |
+ SetIPv6Address(address); |
+ return OK; |
+ } |
+ virtual const BoundNetLog& NetLog() const OVERRIDE { return net_log_; } |
+ |
+ virtual void SetSubresourceSpeculation() OVERRIDE {} |
+ virtual void SetOmniboxSpeculation() OVERRIDE {} |
+ virtual bool WasEverUsed() const OVERRIDE { return false; } |
+ virtual bool UsingTCPFastOpen() const OVERRIDE { return false; } |
+ virtual bool WasNpnNegotiated() const OVERRIDE { return false; } |
+ virtual NextProto GetNegotiatedProtocol() const OVERRIDE { |
+ return kProtoUnknown; |
+ } |
+ virtual bool GetSSLInfo(SSLInfo* ssl_info) OVERRIDE { return false; } |
+ |
+ // Socket implementation. |
+ virtual int Read(IOBuffer* buf, |
+ int buf_len, |
+ const CompletionCallback& callback) OVERRIDE { |
+ return ERR_FAILED; |
+ } |
+ virtual int Write(IOBuffer* buf, |
+ int buf_len, |
+ const CompletionCallback& callback) OVERRIDE { |
+ return ERR_FAILED; |
+ } |
+ virtual int SetReceiveBufferSize(int32 size) OVERRIDE { return OK; } |
+ virtual int SetSendBufferSize(int32 size) OVERRIDE { return OK; } |
+ |
+ private: |
+ bool connected_; |
+ const AddressList addrlist_; |
+ BoundNetLog net_log_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(MockClientSocket); |
+}; |
+ |
+class MockFailingClientSocket : public StreamSocket { |
+ public: |
+ MockFailingClientSocket(const AddressList& addrlist, net::NetLog* net_log) |
+ : addrlist_(addrlist), |
+ net_log_(BoundNetLog::Make(net_log, NetLog::SOURCE_SOCKET)) {} |
+ |
+ // StreamSocket implementation. |
+ virtual int Connect(const CompletionCallback& callback) OVERRIDE { |
+ return ERR_CONNECTION_FAILED; |
+ } |
+ |
+ virtual void Disconnect() OVERRIDE {} |
+ |
+ virtual bool IsConnected() const OVERRIDE { return false; } |
+ virtual bool IsConnectedAndIdle() const OVERRIDE { return false; } |
+ virtual int GetPeerAddress(IPEndPoint* address) const OVERRIDE { |
+ return ERR_UNEXPECTED; |
+ } |
+ virtual int GetLocalAddress(IPEndPoint* address) const OVERRIDE { |
+ return ERR_UNEXPECTED; |
+ } |
+ virtual const BoundNetLog& NetLog() const OVERRIDE { return net_log_; } |
+ |
+ virtual void SetSubresourceSpeculation() OVERRIDE {} |
+ virtual void SetOmniboxSpeculation() OVERRIDE {} |
+ virtual bool WasEverUsed() const OVERRIDE { return false; } |
+ virtual bool UsingTCPFastOpen() const OVERRIDE { return false; } |
+ virtual bool WasNpnNegotiated() const OVERRIDE { return false; } |
+ virtual NextProto GetNegotiatedProtocol() const OVERRIDE { |
+ return kProtoUnknown; |
+ } |
+ virtual bool GetSSLInfo(SSLInfo* ssl_info) OVERRIDE { return false; } |
+ |
+ // Socket implementation. |
+ virtual int Read(IOBuffer* buf, |
+ int buf_len, |
+ const CompletionCallback& callback) OVERRIDE { |
+ return ERR_FAILED; |
+ } |
+ |
+ virtual int Write(IOBuffer* buf, |
+ int buf_len, |
+ const CompletionCallback& callback) OVERRIDE { |
+ return ERR_FAILED; |
+ } |
+ virtual int SetReceiveBufferSize(int32 size) OVERRIDE { return OK; } |
+ virtual int SetSendBufferSize(int32 size) OVERRIDE { return OK; } |
+ |
+ private: |
+ const AddressList addrlist_; |
+ BoundNetLog net_log_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(MockFailingClientSocket); |
+}; |
+ |
+class MockTriggerableClientSocket : public StreamSocket { |
+ public: |
+ // |should_connect| indicates whether the socket should successfully complete |
+ // or fail. |
+ MockTriggerableClientSocket(const AddressList& addrlist, |
+ bool should_connect, |
+ net::NetLog* net_log) |
+ : should_connect_(should_connect), |
+ is_connected_(false), |
+ addrlist_(addrlist), |
+ net_log_(BoundNetLog::Make(net_log, NetLog::SOURCE_SOCKET)), |
+ weak_factory_(this) {} |
+ |
+ // Call this method to get a closure which will trigger the connect callback |
+ // when called. The closure can be called even after the socket is deleted; it |
+ // will safely do nothing. |
+ base::Closure GetConnectCallback() { |
+ return base::Bind(&MockTriggerableClientSocket::DoCallback, |
+ weak_factory_.GetWeakPtr()); |
+ } |
+ |
+ static scoped_ptr<StreamSocket> MakeMockPendingClientSocket( |
+ const AddressList& addrlist, |
+ bool should_connect, |
+ net::NetLog* net_log) { |
+ scoped_ptr<MockTriggerableClientSocket> socket( |
+ new MockTriggerableClientSocket(addrlist, should_connect, net_log)); |
+ base::MessageLoop::current()->PostTask(FROM_HERE, |
+ socket->GetConnectCallback()); |
+ return socket.PassAs<StreamSocket>(); |
+ } |
+ |
+ static scoped_ptr<StreamSocket> MakeMockDelayedClientSocket( |
+ const AddressList& addrlist, |
+ bool should_connect, |
+ const base::TimeDelta& delay, |
+ net::NetLog* net_log) { |
+ scoped_ptr<MockTriggerableClientSocket> socket( |
+ new MockTriggerableClientSocket(addrlist, should_connect, net_log)); |
+ base::MessageLoop::current()->PostDelayedTask( |
+ FROM_HERE, socket->GetConnectCallback(), delay); |
+ return socket.PassAs<StreamSocket>(); |
+ } |
+ |
+ static scoped_ptr<StreamSocket> MakeMockStalledClientSocket( |
+ const AddressList& addrlist, |
+ net::NetLog* net_log) { |
+ scoped_ptr<MockTriggerableClientSocket> socket( |
+ new MockTriggerableClientSocket(addrlist, true, net_log)); |
+ return socket.PassAs<StreamSocket>(); |
+ } |
+ |
+ // StreamSocket implementation. |
+ virtual int Connect(const CompletionCallback& callback) OVERRIDE { |
+ DCHECK(callback_.is_null()); |
+ callback_ = callback; |
+ return ERR_IO_PENDING; |
+ } |
+ |
+ virtual void Disconnect() OVERRIDE {} |
+ |
+ virtual bool IsConnected() const OVERRIDE { return is_connected_; } |
+ virtual bool IsConnectedAndIdle() const OVERRIDE { return is_connected_; } |
+ virtual int GetPeerAddress(IPEndPoint* address) const OVERRIDE { |
+ *address = addrlist_.front(); |
+ return OK; |
+ } |
+ virtual int GetLocalAddress(IPEndPoint* address) const OVERRIDE { |
+ if (!is_connected_) |
+ return ERR_SOCKET_NOT_CONNECTED; |
+ if (addrlist_.front().GetFamily() == ADDRESS_FAMILY_IPV4) |
+ SetIPv4Address(address); |
+ else |
+ SetIPv6Address(address); |
+ return OK; |
+ } |
+ virtual const BoundNetLog& NetLog() const OVERRIDE { return net_log_; } |
+ |
+ virtual void SetSubresourceSpeculation() OVERRIDE {} |
+ virtual void SetOmniboxSpeculation() OVERRIDE {} |
+ virtual bool WasEverUsed() const OVERRIDE { return false; } |
+ virtual bool UsingTCPFastOpen() const OVERRIDE { return false; } |
+ virtual bool WasNpnNegotiated() const OVERRIDE { return false; } |
+ virtual NextProto GetNegotiatedProtocol() const OVERRIDE { |
+ return kProtoUnknown; |
+ } |
+ virtual bool GetSSLInfo(SSLInfo* ssl_info) OVERRIDE { return false; } |
+ |
+ // Socket implementation. |
+ virtual int Read(IOBuffer* buf, |
+ int buf_len, |
+ const CompletionCallback& callback) OVERRIDE { |
+ return ERR_FAILED; |
+ } |
+ |
+ virtual int Write(IOBuffer* buf, |
+ int buf_len, |
+ const CompletionCallback& callback) OVERRIDE { |
+ return ERR_FAILED; |
+ } |
+ virtual int SetReceiveBufferSize(int32 size) OVERRIDE { return OK; } |
+ virtual int SetSendBufferSize(int32 size) OVERRIDE { return OK; } |
+ |
+ private: |
+ void DoCallback() { |
+ is_connected_ = should_connect_; |
+ callback_.Run(is_connected_ ? OK : ERR_CONNECTION_FAILED); |
+ } |
+ |
+ bool should_connect_; |
+ bool is_connected_; |
+ const AddressList addrlist_; |
+ BoundNetLog net_log_; |
+ CompletionCallback callback_; |
+ |
+ base::WeakPtrFactory<MockTriggerableClientSocket> weak_factory_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(MockTriggerableClientSocket); |
+}; |
+ |
+class MockClientSocketFactory : public ClientSocketFactory { |
+ public: |
+ enum ClientSocketType { |
+ MOCK_CLIENT_SOCKET, |
+ MOCK_FAILING_CLIENT_SOCKET, |
+ MOCK_PENDING_CLIENT_SOCKET, |
+ MOCK_PENDING_FAILING_CLIENT_SOCKET, |
+ // A delayed socket will pause before connecting through the message loop. |
+ MOCK_DELAYED_CLIENT_SOCKET, |
+ // A delayed socket that fails. |
+ MOCK_DELAYED_FAILING_CLIENT_SOCKET, |
+ // A stalled socket that never connects at all. |
+ MOCK_STALLED_CLIENT_SOCKET, |
+ // A socket that can be triggered to connect explicitly. |
+ MOCK_TRIGGERABLE_CLIENT_SOCKET, |
+ }; |
+ |
+ explicit MockClientSocketFactory(NetLog* net_log) |
+ : net_log_(net_log), |
+ allocation_count_(0), |
+ client_socket_type_(MOCK_CLIENT_SOCKET), |
+ client_socket_types_(NULL), |
+ client_socket_index_(0), |
+ client_socket_index_max_(0), |
+ delay_(base::TimeDelta::FromMilliseconds( |
+ ClientSocketPool::kMaxConnectRetryIntervalMs)) {} |
+ |
+ virtual scoped_ptr<DatagramClientSocket> CreateDatagramClientSocket( |
+ DatagramSocket::BindType bind_type, |
+ const RandIntCallback& rand_int_cb, |
+ NetLog* net_log, |
+ const NetLog::Source& source) OVERRIDE { |
+ NOTREACHED(); |
+ return scoped_ptr<DatagramClientSocket>(); |
+ } |
+ |
+ virtual scoped_ptr<StreamSocket> CreateTransportClientSocket( |
+ const AddressList& addresses, |
+ NetLog* /* net_log */, |
+ const NetLog::Source& /* source */) OVERRIDE { |
+ allocation_count_++; |
+ |
+ ClientSocketType type = client_socket_type_; |
+ if (client_socket_types_ && |
+ client_socket_index_ < client_socket_index_max_) { |
+ type = client_socket_types_[client_socket_index_++]; |
+ } |
+ |
+ switch (type) { |
+ case MOCK_CLIENT_SOCKET: |
+ return scoped_ptr<StreamSocket>( |
+ new MockClientSocket(addresses, net_log_)); |
+ case MOCK_FAILING_CLIENT_SOCKET: |
+ return scoped_ptr<StreamSocket>( |
+ new MockFailingClientSocket(addresses, net_log_)); |
+ case MOCK_PENDING_CLIENT_SOCKET: |
+ return MockTriggerableClientSocket::MakeMockPendingClientSocket( |
+ addresses, true, net_log_); |
+ case MOCK_PENDING_FAILING_CLIENT_SOCKET: |
+ return MockTriggerableClientSocket::MakeMockPendingClientSocket( |
+ addresses, false, net_log_); |
+ case MOCK_DELAYED_CLIENT_SOCKET: |
+ return MockTriggerableClientSocket::MakeMockDelayedClientSocket( |
+ addresses, true, delay_, net_log_); |
+ case MOCK_DELAYED_FAILING_CLIENT_SOCKET: |
+ return MockTriggerableClientSocket::MakeMockDelayedClientSocket( |
+ addresses, false, delay_, net_log_); |
+ case MOCK_STALLED_CLIENT_SOCKET: |
+ return MockTriggerableClientSocket::MakeMockStalledClientSocket( |
+ addresses, net_log_); |
+ case MOCK_TRIGGERABLE_CLIENT_SOCKET: { |
+ scoped_ptr<MockTriggerableClientSocket> rv( |
+ new MockTriggerableClientSocket(addresses, true, net_log_)); |
+ triggerable_sockets_.push(rv->GetConnectCallback()); |
+ // run_loop_quit_closure_ behaves like a condition variable. It will |
+ // wake up WaitForTriggerableSocketCreation() if it is sleeping. We |
+ // don't need to worry about atomicity because this code is |
+ // single-threaded. |
+ if (!run_loop_quit_closure_.is_null()) |
+ run_loop_quit_closure_.Run(); |
+ return rv.PassAs<StreamSocket>(); |
+ } |
+ default: |
+ NOTREACHED(); |
+ return scoped_ptr<StreamSocket>( |
+ new MockClientSocket(addresses, net_log_)); |
+ } |
+ } |
+ |
+ virtual scoped_ptr<SSLClientSocket> CreateSSLClientSocket( |
+ scoped_ptr<ClientSocketHandle> transport_socket, |
+ const HostPortPair& host_and_port, |
+ const SSLConfig& ssl_config, |
+ const SSLClientSocketContext& context) OVERRIDE { |
+ NOTIMPLEMENTED(); |
+ return scoped_ptr<SSLClientSocket>(); |
+ } |
+ |
+ virtual void ClearSSLSessionCache() OVERRIDE { NOTIMPLEMENTED(); } |
+ |
+ int allocation_count() const { return allocation_count_; } |
+ |
+ // Set the default ClientSocketType. |
+ void set_client_socket_type(ClientSocketType type) { |
+ client_socket_type_ = type; |
+ } |
+ |
+ // Set a list of ClientSocketTypes to be used. |
+ void set_client_socket_types(ClientSocketType* type_list, int num_types) { |
+ DCHECK_GT(num_types, 0); |
+ client_socket_types_ = type_list; |
+ client_socket_index_ = 0; |
+ client_socket_index_max_ = num_types; |
+ } |
+ |
+ void set_delay(base::TimeDelta delay) { delay_ = delay; } |
+ |
+ base::Closure WaitForTriggerableSocketCreation() { |
+ while (triggerable_sockets_.empty()) { |
+ base::RunLoop run_loop; |
+ run_loop_quit_closure_ = run_loop.QuitClosure(); |
+ run_loop.Run(); |
+ run_loop_quit_closure_.Reset(); |
+ } |
+ base::Closure trigger = triggerable_sockets_.front(); |
+ triggerable_sockets_.pop(); |
+ return trigger; |
+ } |
+ |
+ private: |
+ NetLog* net_log_; |
+ int allocation_count_; |
+ ClientSocketType client_socket_type_; |
+ ClientSocketType* client_socket_types_; |
+ int client_socket_index_; |
+ int client_socket_index_max_; |
+ base::TimeDelta delay_; |
+ std::queue<base::Closure> triggerable_sockets_; |
+ base::Closure run_loop_quit_closure_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(MockClientSocketFactory); |
+}; |
+ |
+class WebSocketTransportClientSocketPoolTest : public testing::Test { |
+ protected: |
+ WebSocketTransportClientSocketPoolTest() |
+ : params_(new TransportSocketParams(HostPortPair("www.google.com", 80), |
+ false, |
+ false, |
+ OnHostResolutionCallback())), |
+ histograms_(new ClientSocketPoolHistograms("TCPUnitTest")), |
+ host_resolver_(new MockHostResolver), |
+ client_socket_factory_(&net_log_), |
+ pool_(kMaxSockets, |
+ kMaxSocketsPerGroup, |
+ histograms_.get(), |
+ host_resolver_.get(), |
+ &client_socket_factory_, |
+ NULL) {} |
+ |
+ virtual ~WebSocketTransportClientSocketPoolTest() {} |
+ |
+ int StartRequest(const std::string& group_name, RequestPriority priority) { |
+ scoped_refptr<TransportSocketParams> params( |
+ new TransportSocketParams(HostPortPair("www.google.com", 80), |
+ false, |
+ false, |
+ OnHostResolutionCallback())); |
+ return test_base_.StartRequestUsingPool( |
+ &pool_, group_name, priority, params); |
+ } |
+ |
+ int GetOrderOfRequest(size_t index) { |
+ return test_base_.GetOrderOfRequest(index); |
+ } |
+ |
+ bool ReleaseOneConnection(ClientSocketPoolTest::KeepAlive keep_alive) { |
+ return test_base_.ReleaseOneConnection(keep_alive); |
+ } |
+ |
+ void ReleaseAllConnections(ClientSocketPoolTest::KeepAlive keep_alive) { |
+ test_base_.ReleaseAllConnections(keep_alive); |
+ } |
+ |
+ ScopedVector<TestSocketRequest>* requests() { return test_base_.requests(); } |
+ size_t completion_count() const { return test_base_.completion_count(); } |
+ |
+ CapturingNetLog net_log_; |
+ scoped_refptr<TransportSocketParams> params_; |
+ scoped_ptr<ClientSocketPoolHistograms> histograms_; |
+ scoped_ptr<MockHostResolver> host_resolver_; |
+ MockClientSocketFactory client_socket_factory_; |
+ WebSocketTransportClientSocketPool pool_; |
+ ClientSocketPoolTest test_base_; |
+ |
+ private: |
+ DISALLOW_COPY_AND_ASSIGN(WebSocketTransportClientSocketPoolTest); |
+}; |
+ |
+TEST_F(WebSocketTransportClientSocketPoolTest, Basic) { |
+ TestCompletionCallback callback; |
+ ClientSocketHandle handle; |
+ int rv = handle.Init( |
+ "a", params_, LOW, callback.callback(), &pool_, BoundNetLog()); |
+ EXPECT_EQ(ERR_IO_PENDING, rv); |
+ EXPECT_FALSE(handle.is_initialized()); |
+ EXPECT_FALSE(handle.socket()); |
+ |
+ EXPECT_EQ(OK, callback.WaitForResult()); |
+ EXPECT_TRUE(handle.is_initialized()); |
+ EXPECT_TRUE(handle.socket()); |
+ TestLoadTimingInfoConnectedNotReused(handle); |
+} |
+ |
+// Make sure that WebSocketTransportConnectJob passes on its priority to its |
+// HostResolver request on Init. |
+TEST_F(WebSocketTransportClientSocketPoolTest, SetResolvePriorityOnInit) { |
+ for (int i = MINIMUM_PRIORITY; i <= MAXIMUM_PRIORITY; ++i) { |
+ RequestPriority priority = static_cast<RequestPriority>(i); |
+ TestCompletionCallback callback; |
+ ClientSocketHandle handle; |
+ EXPECT_EQ(ERR_IO_PENDING, |
+ handle.Init("a", |
+ params_, |
+ priority, |
+ callback.callback(), |
+ &pool_, |
+ BoundNetLog())); |
+ EXPECT_EQ(priority, host_resolver_->last_request_priority()); |
+ } |
+} |
+ |
+TEST_F(WebSocketTransportClientSocketPoolTest, InitHostResolutionFailure) { |
+ host_resolver_->rules()->AddSimulatedFailure("unresolvable.host.name"); |
+ TestCompletionCallback callback; |
+ ClientSocketHandle handle; |
+ HostPortPair host_port_pair("unresolvable.host.name", 80); |
+ scoped_refptr<TransportSocketParams> dest(new TransportSocketParams( |
+ host_port_pair, false, false, OnHostResolutionCallback())); |
+ EXPECT_EQ(ERR_IO_PENDING, |
+ handle.Init("a", |
+ dest, |
+ kDefaultPriority, |
+ callback.callback(), |
+ &pool_, |
+ BoundNetLog())); |
+ EXPECT_EQ(ERR_NAME_NOT_RESOLVED, callback.WaitForResult()); |
+} |
+ |
+TEST_F(WebSocketTransportClientSocketPoolTest, InitConnectionFailure) { |
+ client_socket_factory_.set_client_socket_type( |
+ MockClientSocketFactory::MOCK_FAILING_CLIENT_SOCKET); |
+ TestCompletionCallback callback; |
+ ClientSocketHandle handle; |
+ EXPECT_EQ(ERR_IO_PENDING, |
+ handle.Init("a", |
+ params_, |
+ kDefaultPriority, |
+ callback.callback(), |
+ &pool_, |
+ BoundNetLog())); |
+ EXPECT_EQ(ERR_CONNECTION_FAILED, callback.WaitForResult()); |
+ |
+ // Make the host resolutions complete synchronously this time. |
+ host_resolver_->set_synchronous_mode(true); |
+ EXPECT_EQ(ERR_CONNECTION_FAILED, |
+ handle.Init("a", |
+ params_, |
+ kDefaultPriority, |
+ callback.callback(), |
+ &pool_, |
+ BoundNetLog())); |
+} |
+ |
+TEST_F(WebSocketTransportClientSocketPoolTest, PendingRequestsFinishFifo) { |
+ // First request finishes asynchronously. |
+ EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); |
+ EXPECT_EQ(OK, (*requests())[0]->WaitForResult()); |
+ |
+ // Make all subsequent host resolutions complete synchronously. |
+ host_resolver_->set_synchronous_mode(true); |
+ |
+ // Rest of them wait for the first socket to be released. |
+ EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); |
+ EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); |
+ EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); |
+ EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); |
+ EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); |
+ |
+ ReleaseAllConnections(ClientSocketPoolTest::KEEP_ALIVE); |
+ |
+ EXPECT_EQ(6, client_socket_factory_.allocation_count()); |
+ |
+ // One initial asynchronous request and then 5 pending requests. |
+ EXPECT_EQ(6U, completion_count()); |
+ |
+ // The requests finish in FIFO order. |
+ EXPECT_EQ(1, GetOrderOfRequest(1)); |
+ EXPECT_EQ(2, GetOrderOfRequest(2)); |
+ EXPECT_EQ(3, GetOrderOfRequest(3)); |
+ EXPECT_EQ(4, GetOrderOfRequest(4)); |
+ EXPECT_EQ(5, GetOrderOfRequest(5)); |
+ EXPECT_EQ(6, GetOrderOfRequest(6)); |
+ |
+ // Make sure we test order of all requests made. |
+ EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(7)); |
+} |
+ |
+TEST_F(WebSocketTransportClientSocketPoolTest, PendingRequests_NoKeepAlive) { |
+ // First request finishes asynchronously. |
+ EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); |
+ EXPECT_EQ(OK, (*requests())[0]->WaitForResult()); |
+ |
+ // Make all subsequent host resolutions complete synchronously. |
+ host_resolver_->set_synchronous_mode(true); |
+ |
+ // Rest of them wait foe the first socket to be released. |
+ EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); |
+ EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); |
+ EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); |
+ EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); |
+ EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); |
+ |
+ ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
+ |
+ // The pending requests should finish successfully. |
+ EXPECT_EQ(OK, (*requests())[1]->WaitForResult()); |
+ EXPECT_EQ(OK, (*requests())[2]->WaitForResult()); |
+ EXPECT_EQ(OK, (*requests())[3]->WaitForResult()); |
+ EXPECT_EQ(OK, (*requests())[4]->WaitForResult()); |
+ EXPECT_EQ(OK, (*requests())[5]->WaitForResult()); |
+ |
+ EXPECT_EQ(static_cast<int>(requests()->size()), |
+ client_socket_factory_.allocation_count()); |
+ |
+ // First asynchronous request, and then last 5 pending requests. |
+ EXPECT_EQ(6U, completion_count()); |
+} |
+ |
+// This test will start up a RequestSocket() and then immediately Cancel() it. |
+// The pending host resolution will eventually complete, and destroy the |
+// ClientSocketPool which will crash if the group was not cleared properly. |
+TEST_F(WebSocketTransportClientSocketPoolTest, CancelRequestClearGroup) { |
+ TestCompletionCallback callback; |
+ ClientSocketHandle handle; |
+ EXPECT_EQ(ERR_IO_PENDING, |
+ handle.Init("a", |
+ params_, |
+ kDefaultPriority, |
+ callback.callback(), |
+ &pool_, |
+ BoundNetLog())); |
+ handle.Reset(); |
+} |
+ |
+TEST_F(WebSocketTransportClientSocketPoolTest, TwoRequestsCancelOne) { |
+ ClientSocketHandle handle; |
+ TestCompletionCallback callback; |
+ ClientSocketHandle handle2; |
+ TestCompletionCallback callback2; |
+ |
+ EXPECT_EQ(ERR_IO_PENDING, |
+ handle.Init("a", |
+ params_, |
+ kDefaultPriority, |
+ callback.callback(), |
+ &pool_, |
+ BoundNetLog())); |
+ EXPECT_EQ(ERR_IO_PENDING, |
+ handle2.Init("a", |
+ params_, |
+ kDefaultPriority, |
+ callback2.callback(), |
+ &pool_, |
+ BoundNetLog())); |
+ |
+ handle.Reset(); |
+ |
+ EXPECT_EQ(OK, callback2.WaitForResult()); |
+ handle2.Reset(); |
+} |
+ |
+TEST_F(WebSocketTransportClientSocketPoolTest, ConnectCancelConnect) { |
+ client_socket_factory_.set_client_socket_type( |
+ MockClientSocketFactory::MOCK_PENDING_CLIENT_SOCKET); |
+ ClientSocketHandle handle; |
+ TestCompletionCallback callback; |
+ EXPECT_EQ(ERR_IO_PENDING, |
+ handle.Init("a", |
+ params_, |
+ kDefaultPriority, |
+ callback.callback(), |
+ &pool_, |
+ BoundNetLog())); |
+ |
+ handle.Reset(); |
+ |
+ TestCompletionCallback callback2; |
+ EXPECT_EQ(ERR_IO_PENDING, |
+ handle.Init("a", |
+ params_, |
+ kDefaultPriority, |
+ callback2.callback(), |
+ &pool_, |
+ BoundNetLog())); |
+ |
+ host_resolver_->set_synchronous_mode(true); |
+ // At this point, handle has two ConnectingSockets out for it. Due to the |
+ // setting the mock resolver into synchronous mode, the host resolution for |
+ // both will return in the same loop of the MessageLoop. The client socket |
+ // is a pending socket, so the Connect() will asynchronously complete on the |
+ // next loop of the MessageLoop. That means that the first |
+ // ConnectingSocket will enter OnIOComplete, and then the second one will. |
+ // If the first one is not cancelled, it will advance the load state, and |
+ // then the second one will crash. |
+ |
+ EXPECT_EQ(OK, callback2.WaitForResult()); |
+ EXPECT_FALSE(callback.have_result()); |
+ |
+ handle.Reset(); |
+} |
+ |
+TEST_F(WebSocketTransportClientSocketPoolTest, CancelRequest) { |
+ // First request finishes asynchronously. |
+ EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); |
+ EXPECT_EQ(OK, (*requests())[0]->WaitForResult()); |
+ |
+ // Make all subsequent host resolutions complete synchronously. |
+ host_resolver_->set_synchronous_mode(true); |
+ |
+ EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); |
+ EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); |
+ EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); |
+ EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); |
+ EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); |
+ |
+ // Cancel a request. |
+ const size_t index_to_cancel = 2; |
+ EXPECT_FALSE((*requests())[index_to_cancel]->handle()->is_initialized()); |
+ (*requests())[index_to_cancel]->handle()->Reset(); |
+ |
+ ReleaseAllConnections(ClientSocketPoolTest::KEEP_ALIVE); |
+ |
+ EXPECT_EQ(5, client_socket_factory_.allocation_count()); |
+ |
+ EXPECT_EQ(1, GetOrderOfRequest(1)); |
+ EXPECT_EQ(2, GetOrderOfRequest(2)); |
+ EXPECT_EQ(ClientSocketPoolTest::kRequestNotFound, |
+ GetOrderOfRequest(3)); // Canceled request. |
+ EXPECT_EQ(3, GetOrderOfRequest(4)); |
+ EXPECT_EQ(4, GetOrderOfRequest(5)); |
+ EXPECT_EQ(5, GetOrderOfRequest(6)); |
+ |
+ // Make sure we test order of all requests made. |
+ EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(7)); |
+} |
+ |
+class RequestSocketCallback : public TestCompletionCallbackBase { |
+ public: |
+ RequestSocketCallback(ClientSocketHandle* handle, |
+ WebSocketTransportClientSocketPool* pool) |
+ : handle_(handle), |
+ pool_(pool), |
+ within_callback_(false), |
+ callback_(base::Bind(&RequestSocketCallback::OnComplete, |
+ base::Unretained(this))) {} |
+ |
+ virtual ~RequestSocketCallback() {} |
+ |
+ const CompletionCallback& callback() const { return callback_; } |
+ |
+ private: |
+ void OnComplete(int result) { |
+ SetResult(result); |
+ ASSERT_EQ(OK, result); |
+ |
+ if (!within_callback_) { |
+ // Don't allow reuse of the socket. Disconnect it and then release it and |
+ // run through the MessageLoop once to get it completely released. |
+ handle_->socket()->Disconnect(); |
+ handle_->Reset(); |
+ { |
+ base::MessageLoop::ScopedNestableTaskAllower allow( |
+ base::MessageLoop::current()); |
+ base::MessageLoop::current()->RunUntilIdle(); |
+ } |
+ within_callback_ = true; |
+ scoped_refptr<TransportSocketParams> dest( |
+ new TransportSocketParams(HostPortPair("www.google.com", 80), |
+ false, |
+ false, |
+ OnHostResolutionCallback())); |
+ int rv = |
+ handle_->Init("a", dest, LOWEST, callback(), pool_, BoundNetLog()); |
+ EXPECT_EQ(OK, rv); |
+ } |
+ } |
+ |
+ ClientSocketHandle* const handle_; |
+ WebSocketTransportClientSocketPool* const pool_; |
+ bool within_callback_; |
+ CompletionCallback callback_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(RequestSocketCallback); |
+}; |
+ |
+TEST_F(WebSocketTransportClientSocketPoolTest, RequestTwice) { |
+ ClientSocketHandle handle; |
+ RequestSocketCallback callback(&handle, &pool_); |
+ scoped_refptr<TransportSocketParams> dest( |
+ new TransportSocketParams(HostPortPair("www.google.com", 80), |
+ false, |
+ false, |
+ OnHostResolutionCallback())); |
+ int rv = handle.Init( |
+ "a", dest, LOWEST, callback.callback(), &pool_, BoundNetLog()); |
+ ASSERT_EQ(ERR_IO_PENDING, rv); |
+ |
+ // The callback is going to request "www.google.com". We want it to complete |
+ // synchronously this time. |
+ host_resolver_->set_synchronous_mode(true); |
+ |
+ EXPECT_EQ(OK, callback.WaitForResult()); |
+ |
+ handle.Reset(); |
+} |
+ |
+// Make sure that pending requests get serviced after active requests get |
+// cancelled. |
+TEST_F(WebSocketTransportClientSocketPoolTest, |
+ CancelActiveRequestWithPendingRequests) { |
+ client_socket_factory_.set_client_socket_type( |
+ MockClientSocketFactory::MOCK_PENDING_CLIENT_SOCKET); |
+ |
+ // Queue up all the requests |
+ EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); |
+ EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); |
+ EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); |
+ EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); |
+ EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); |
+ EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); |
+ EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); |
+ EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); |
+ EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); |
+ |
+ // Now, kMaxSocketsPerGroup requests should be active. Let's cancel them. |
+ ASSERT_LE(kMaxSocketsPerGroup, static_cast<int>(requests()->size())); |
+ for (int i = 0; i < kMaxSocketsPerGroup; i++) |
+ (*requests())[i]->handle()->Reset(); |
+ |
+ // Let's wait for the rest to complete now. |
+ for (size_t i = kMaxSocketsPerGroup; i < requests()->size(); ++i) { |
+ EXPECT_EQ(OK, (*requests())[i]->WaitForResult()); |
+ (*requests())[i]->handle()->Reset(); |
+ } |
+ |
+ EXPECT_EQ(requests()->size() - kMaxSocketsPerGroup, completion_count()); |
+} |
+ |
+// Make sure that pending requests get serviced after active requests fail. |
+TEST_F(WebSocketTransportClientSocketPoolTest, |
+ FailingActiveRequestWithPendingRequests) { |
+ client_socket_factory_.set_client_socket_type( |
+ MockClientSocketFactory::MOCK_PENDING_FAILING_CLIENT_SOCKET); |
+ |
+ const int kNumRequests = 2 * kMaxSocketsPerGroup + 1; |
+ ASSERT_LE(kNumRequests, kMaxSockets); // Otherwise the test will hang. |
+ |
+ // Queue up all the requests |
+ for (int i = 0; i < kNumRequests; i++) |
+ EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); |
+ |
+ for (int i = 0; i < kNumRequests; i++) |
+ EXPECT_EQ(ERR_CONNECTION_FAILED, (*requests())[i]->WaitForResult()); |
+} |
+ |
+// The lock on the endpoint is released when a ClientSocketHandle is reset. |
+TEST_F(WebSocketTransportClientSocketPoolTest, LockReleasedOnHandleReset) { |
+ EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); |
+ EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); |
+ EXPECT_EQ(OK, (*requests())[0]->WaitForResult()); |
+ EXPECT_FALSE((*requests())[1]->handle()->is_initialized()); |
+ (*requests())[0]->handle()->Reset(); |
+ base::RunLoop().RunUntilIdle(); |
+ EXPECT_TRUE((*requests())[1]->handle()->is_initialized()); |
+} |
+ |
+// The lock on the endpoint is released when a ClientSocketHandle is deleted. |
+TEST_F(WebSocketTransportClientSocketPoolTest, LockReleasedOnHandleDelete) { |
+ TestCompletionCallback callback; |
+ scoped_ptr<ClientSocketHandle> handle(new ClientSocketHandle); |
+ int rv = handle->Init( |
+ "a", params_, LOW, callback.callback(), &pool_, BoundNetLog()); |
+ EXPECT_EQ(ERR_IO_PENDING, rv); |
+ |
+ EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); |
+ EXPECT_EQ(OK, callback.WaitForResult()); |
+ EXPECT_FALSE((*requests())[0]->handle()->is_initialized()); |
+ handle.reset(); |
+ base::RunLoop().RunUntilIdle(); |
+ EXPECT_TRUE((*requests())[0]->handle()->is_initialized()); |
+} |
+ |
+// A new connection is performed when the lock on the previous connection is |
+// explicity released. |
+TEST_F(WebSocketTransportClientSocketPoolTest, |
+ ConnectionProceedsOnExplicitRelease) { |
+ EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); |
+ EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); |
+ EXPECT_EQ(OK, (*requests())[0]->WaitForResult()); |
+ EXPECT_FALSE((*requests())[1]->handle()->is_initialized()); |
+ WebSocketTransportClientSocketPool::UnlockEndpoint( |
+ (*requests())[0]->handle()); |
+ base::RunLoop().RunUntilIdle(); |
+ EXPECT_TRUE((*requests())[1]->handle()->is_initialized()); |
+} |
+ |
+// A connection which is cancelled before completion does not block subsequent |
+// connections. |
+TEST_F(WebSocketTransportClientSocketPoolTest, |
+ CancelDuringConnectionReleasesLock) { |
+ MockClientSocketFactory::ClientSocketType case_types[] = { |
+ MockClientSocketFactory::MOCK_STALLED_CLIENT_SOCKET, |
+ MockClientSocketFactory::MOCK_PENDING_CLIENT_SOCKET}; |
+ |
+ client_socket_factory_.set_client_socket_types(case_types, |
+ arraysize(case_types)); |
+ |
+ EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); |
+ EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); |
+ base::RunLoop().RunUntilIdle(); |
+ pool_.CancelRequest("a", ((*requests())[0])->handle()); |
+ EXPECT_EQ(OK, (*requests())[1]->WaitForResult()); |
+} |
+ |
+// Test the case of the IPv6 address stalling, and falling back to the IPv4 |
+// socket which finishes first. |
+TEST_F(WebSocketTransportClientSocketPoolTest, |
+ IPv6FallbackSocketIPv4FinishesFirst) { |
+ WebSocketTransportClientSocketPool pool(kMaxSockets, |
+ kMaxSocketsPerGroup, |
+ histograms_.get(), |
+ host_resolver_.get(), |
+ &client_socket_factory_, |
+ NULL); |
+ |
+ MockClientSocketFactory::ClientSocketType case_types[] = { |
+ // This is the IPv6 socket. |
+ MockClientSocketFactory::MOCK_STALLED_CLIENT_SOCKET, |
+ // This is the IPv4 socket. |
+ MockClientSocketFactory::MOCK_PENDING_CLIENT_SOCKET}; |
+ |
+ client_socket_factory_.set_client_socket_types(case_types, 2); |
+ |
+ // Resolve an AddressList with a IPv6 address first and then a IPv4 address. |
+ host_resolver_->rules()->AddIPLiteralRule( |
+ "*", "2:abcd::3:4:ff,2.2.2.2", std::string()); |
+ |
+ TestCompletionCallback callback; |
+ ClientSocketHandle handle; |
+ int rv = |
+ handle.Init("a", params_, LOW, callback.callback(), &pool, BoundNetLog()); |
+ EXPECT_EQ(ERR_IO_PENDING, rv); |
+ EXPECT_FALSE(handle.is_initialized()); |
+ EXPECT_FALSE(handle.socket()); |
+ |
+ EXPECT_EQ(OK, callback.WaitForResult()); |
+ EXPECT_TRUE(handle.is_initialized()); |
+ EXPECT_TRUE(handle.socket()); |
+ IPEndPoint endpoint; |
+ handle.socket()->GetLocalAddress(&endpoint); |
+ EXPECT_EQ(kIPv4AddressSize, endpoint.address().size()); |
+ EXPECT_EQ(2, client_socket_factory_.allocation_count()); |
+} |
+ |
+// Test the case of the IPv6 address being slow, thus falling back to trying to |
+// connect to the IPv4 address, but having the connect to the IPv6 address |
+// finish first. |
+TEST_F(WebSocketTransportClientSocketPoolTest, |
+ IPv6FallbackSocketIPv6FinishesFirst) { |
+ WebSocketTransportClientSocketPool pool(kMaxSockets, |
+ kMaxSocketsPerGroup, |
+ histograms_.get(), |
+ host_resolver_.get(), |
+ &client_socket_factory_, |
+ NULL); |
+ |
+ MockClientSocketFactory::ClientSocketType case_types[] = { |
+ // This is the IPv6 socket. |
+ MockClientSocketFactory::MOCK_DELAYED_CLIENT_SOCKET, |
+ // This is the IPv4 socket. |
+ MockClientSocketFactory::MOCK_STALLED_CLIENT_SOCKET}; |
+ |
+ client_socket_factory_.set_client_socket_types(case_types, 2); |
+ client_socket_factory_.set_delay(base::TimeDelta::FromMilliseconds( |
+ TransportConnectJobCommon::kIPv6FallbackTimerInMs + 50)); |
+ |
+ // Resolve an AddressList with a IPv6 address first and then a IPv4 address. |
+ host_resolver_->rules()->AddIPLiteralRule( |
+ "*", "2:abcd::3:4:ff,2.2.2.2", std::string()); |
+ |
+ TestCompletionCallback callback; |
+ ClientSocketHandle handle; |
+ int rv = |
+ handle.Init("a", params_, LOW, callback.callback(), &pool, BoundNetLog()); |
+ EXPECT_EQ(ERR_IO_PENDING, rv); |
+ EXPECT_FALSE(handle.is_initialized()); |
+ EXPECT_FALSE(handle.socket()); |
+ |
+ EXPECT_EQ(OK, callback.WaitForResult()); |
+ EXPECT_TRUE(handle.is_initialized()); |
+ EXPECT_TRUE(handle.socket()); |
+ IPEndPoint endpoint; |
+ handle.socket()->GetLocalAddress(&endpoint); |
+ EXPECT_EQ(kIPv6AddressSize, endpoint.address().size()); |
+ EXPECT_EQ(2, client_socket_factory_.allocation_count()); |
+} |
+ |
+TEST_F(WebSocketTransportClientSocketPoolTest, |
+ IPv6NoIPv4AddressesToFallbackTo) { |
+ WebSocketTransportClientSocketPool pool(kMaxSockets, |
+ kMaxSocketsPerGroup, |
+ histograms_.get(), |
+ host_resolver_.get(), |
+ &client_socket_factory_, |
+ NULL); |
+ |
+ client_socket_factory_.set_client_socket_type( |
+ MockClientSocketFactory::MOCK_DELAYED_CLIENT_SOCKET); |
+ |
+ // Resolve an AddressList with only IPv6 addresses. |
+ host_resolver_->rules()->AddIPLiteralRule( |
+ "*", "2:abcd::3:4:ff,3:abcd::3:4:ff", std::string()); |
+ |
+ TestCompletionCallback callback; |
+ ClientSocketHandle handle; |
+ int rv = |
+ handle.Init("a", params_, LOW, callback.callback(), &pool, BoundNetLog()); |
+ EXPECT_EQ(ERR_IO_PENDING, rv); |
+ EXPECT_FALSE(handle.is_initialized()); |
+ EXPECT_FALSE(handle.socket()); |
+ |
+ EXPECT_EQ(OK, callback.WaitForResult()); |
+ EXPECT_TRUE(handle.is_initialized()); |
+ EXPECT_TRUE(handle.socket()); |
+ IPEndPoint endpoint; |
+ handle.socket()->GetLocalAddress(&endpoint); |
+ EXPECT_EQ(kIPv6AddressSize, endpoint.address().size()); |
+ EXPECT_EQ(1, client_socket_factory_.allocation_count()); |
+} |
+ |
+TEST_F(WebSocketTransportClientSocketPoolTest, IPv4HasNoFallback) { |
+ WebSocketTransportClientSocketPool pool(kMaxSockets, |
+ kMaxSocketsPerGroup, |
+ histograms_.get(), |
+ host_resolver_.get(), |
+ &client_socket_factory_, |
+ NULL); |
+ |
+ client_socket_factory_.set_client_socket_type( |
+ MockClientSocketFactory::MOCK_DELAYED_CLIENT_SOCKET); |
+ |
+ // Resolve an AddressList with only IPv4 addresses. |
+ host_resolver_->rules()->AddIPLiteralRule("*", "1.1.1.1", std::string()); |
+ |
+ TestCompletionCallback callback; |
+ ClientSocketHandle handle; |
+ int rv = |
+ handle.Init("a", params_, LOW, callback.callback(), &pool, BoundNetLog()); |
+ EXPECT_EQ(ERR_IO_PENDING, rv); |
+ EXPECT_FALSE(handle.is_initialized()); |
+ EXPECT_FALSE(handle.socket()); |
+ |
+ EXPECT_EQ(OK, callback.WaitForResult()); |
+ EXPECT_TRUE(handle.is_initialized()); |
+ EXPECT_TRUE(handle.socket()); |
+ IPEndPoint endpoint; |
+ handle.socket()->GetLocalAddress(&endpoint); |
+ EXPECT_EQ(kIPv4AddressSize, endpoint.address().size()); |
+ EXPECT_EQ(1, client_socket_factory_.allocation_count()); |
+} |
+ |
+// If all IPv6 addresses fail to connect synchronously, then IPv4 connections |
+// proceeed immediately. |
+TEST_F(WebSocketTransportClientSocketPoolTest, IPv6InstantFail) { |
+ WebSocketTransportClientSocketPool pool(kMaxSockets, |
+ kMaxSocketsPerGroup, |
+ histograms_.get(), |
+ host_resolver_.get(), |
+ &client_socket_factory_, |
+ NULL); |
+ |
+ MockClientSocketFactory::ClientSocketType case_types[] = { |
+ // First IPv6 socket. |
+ MockClientSocketFactory::MOCK_FAILING_CLIENT_SOCKET, |
+ // Second IPv6 socket. |
+ MockClientSocketFactory::MOCK_FAILING_CLIENT_SOCKET, |
+ // This is the IPv4 socket. |
+ MockClientSocketFactory::MOCK_CLIENT_SOCKET}; |
+ |
+ client_socket_factory_.set_client_socket_types(case_types, |
+ arraysize(case_types)); |
+ |
+ // Resolve an AddressList with two IPv6 addresses and then a IPv4 address. |
+ host_resolver_->rules()->AddIPLiteralRule( |
+ "*", "2:abcd::3:4:ff,2:abcd::3:5:ff,2.2.2.2", std::string()); |
+ host_resolver_->set_synchronous_mode(true); |
+ TestCompletionCallback callback; |
+ ClientSocketHandle handle; |
+ int rv = |
+ handle.Init("a", params_, LOW, callback.callback(), &pool, BoundNetLog()); |
+ EXPECT_EQ(OK, rv); |
+ ASSERT_TRUE(handle.socket()); |
+ |
+ IPEndPoint endpoint; |
+ handle.socket()->GetPeerAddress(&endpoint); |
+ EXPECT_EQ("2.2.2.2", endpoint.ToStringWithoutPort()); |
+} |
+ |
+// If all IPv6 addresses fail before the IPv4 fallback timeout, then the IPv4 |
+// connections proceed immediately. |
+TEST_F(WebSocketTransportClientSocketPoolTest, IPv6RapidFail) { |
+ WebSocketTransportClientSocketPool pool(kMaxSockets, |
+ kMaxSocketsPerGroup, |
+ histograms_.get(), |
+ host_resolver_.get(), |
+ &client_socket_factory_, |
+ NULL); |
+ |
+ MockClientSocketFactory::ClientSocketType case_types[] = { |
+ // First IPv6 socket. |
+ MockClientSocketFactory::MOCK_PENDING_FAILING_CLIENT_SOCKET, |
+ // Second IPv6 socket. |
+ MockClientSocketFactory::MOCK_PENDING_FAILING_CLIENT_SOCKET, |
+ // This is the IPv4 socket. |
+ MockClientSocketFactory::MOCK_CLIENT_SOCKET}; |
+ |
+ client_socket_factory_.set_client_socket_types(case_types, |
+ arraysize(case_types)); |
+ |
+ // Resolve an AddressList with two IPv6 addresses and then a IPv4 address. |
+ host_resolver_->rules()->AddIPLiteralRule( |
+ "*", "2:abcd::3:4:ff,2:abcd::3:5:ff,2.2.2.2", std::string()); |
+ |
+ TestCompletionCallback callback; |
+ ClientSocketHandle handle; |
+ int rv = |
+ handle.Init("a", params_, LOW, callback.callback(), &pool, BoundNetLog()); |
+ EXPECT_EQ(ERR_IO_PENDING, rv); |
+ EXPECT_FALSE(handle.socket()); |
+ |
+ base::Time start(base::Time::NowFromSystemTime()); |
+ EXPECT_EQ(OK, callback.WaitForResult()); |
+ EXPECT_LT(base::Time::NowFromSystemTime() - start, |
+ base::TimeDelta::FromMilliseconds( |
+ TransportConnectJobCommon::kIPv6FallbackTimerInMs)); |
+ ASSERT_TRUE(handle.socket()); |
+ |
+ IPEndPoint endpoint; |
+ handle.socket()->GetPeerAddress(&endpoint); |
+ EXPECT_EQ("2.2.2.2", endpoint.ToStringWithoutPort()); |
+} |
+ |
+// If two sockets connect successfully, the one which connected first wins (this |
+// can only happen if the sockets are different types, since sockets of the same |
+// type do not race). |
+TEST_F(WebSocketTransportClientSocketPoolTest, FirstSuccessWins) { |
+ WebSocketTransportClientSocketPool pool(kMaxSockets, |
+ kMaxSocketsPerGroup, |
+ histograms_.get(), |
+ host_resolver_.get(), |
+ &client_socket_factory_, |
+ NULL); |
+ |
+ client_socket_factory_.set_client_socket_type( |
+ MockClientSocketFactory::MOCK_TRIGGERABLE_CLIENT_SOCKET); |
+ |
+ // Resolve an AddressList with an IPv6 addresses and an IPv4 address. |
+ host_resolver_->rules()->AddIPLiteralRule( |
+ "*", "2:abcd::3:4:ff,2.2.2.2", std::string()); |
+ |
+ TestCompletionCallback callback; |
+ ClientSocketHandle handle; |
+ int rv = |
+ handle.Init("a", params_, LOW, callback.callback(), &pool, BoundNetLog()); |
+ EXPECT_EQ(ERR_IO_PENDING, rv); |
+ ASSERT_FALSE(handle.socket()); |
+ |
+ base::Closure ipv6_connect_trigger = |
+ client_socket_factory_.WaitForTriggerableSocketCreation(); |
+ base::Closure ipv4_connect_trigger = |
+ client_socket_factory_.WaitForTriggerableSocketCreation(); |
+ |
+ ipv4_connect_trigger.Run(); |
+ ipv6_connect_trigger.Run(); |
+ |
+ EXPECT_EQ(OK, callback.WaitForResult()); |
+ ASSERT_TRUE(handle.socket()); |
+ |
+ IPEndPoint endpoint; |
+ handle.socket()->GetPeerAddress(&endpoint); |
+ EXPECT_EQ("2.2.2.2", endpoint.ToStringWithoutPort()); |
+} |
+ |
+// We should not report failure until all connections have failed. |
+TEST_F(WebSocketTransportClientSocketPoolTest, LastFailureWins) { |
+ WebSocketTransportClientSocketPool pool(kMaxSockets, |
+ kMaxSocketsPerGroup, |
+ histograms_.get(), |
+ host_resolver_.get(), |
+ &client_socket_factory_, |
+ NULL); |
+ |
+ client_socket_factory_.set_client_socket_type( |
+ MockClientSocketFactory::MOCK_DELAYED_FAILING_CLIENT_SOCKET); |
+ base::TimeDelta delay = base::TimeDelta::FromMilliseconds( |
+ TransportConnectJobCommon::kIPv6FallbackTimerInMs / 3); |
+ client_socket_factory_.set_delay(delay); |
+ |
+ // Resolve an AddressList with 4 IPv6 addresses and 2 IPv4 address. |
+ host_resolver_->rules()->AddIPLiteralRule("*", |
+ "1:abcd::3:4:ff,2:abcd::3:4:ff," |
+ "3:abcd::3:4:ff,4:abcd::3:4:ff," |
+ "1.1.1.1,2.2.2.2", |
+ std::string()); |
+ |
+ // Expected order of events: |
+ // After 100ms: Connect to 1:abcd::3:4:ff times out |
+ // After 200ms: Connect to 2:abcd::3:4:ff times out |
+ // After 300ms: Connect to 3:abcd::3:4:ff times out, IPv4 fallback starts |
+ // After 400ms: Connect to 4:abcd::3:4:ff and 1.1.1.1 time out |
+ // After 500ms: Connect to 2.2.2.2 times out |
+ |
+ TestCompletionCallback callback; |
+ ClientSocketHandle handle; |
+ base::Time start(base::Time::NowFromSystemTime()); |
+ int rv = |
+ handle.Init("a", params_, LOW, callback.callback(), &pool, BoundNetLog()); |
+ EXPECT_EQ(ERR_IO_PENDING, rv); |
+ |
+ EXPECT_EQ(ERR_CONNECTION_FAILED, callback.WaitForResult()); |
+ |
+ EXPECT_GE(base::Time::NowFromSystemTime() - start, delay * 5); |
+} |
+ |
+// Global timeout for all connects applies. This test is disabled by default |
+// because it takes 4 minutes. Run with --gtest_also_run_disabled_tests if you |
+// want to run it. |
+TEST_F(WebSocketTransportClientSocketPoolTest, DISABLED_OverallTimeoutApplies) { |
+ WebSocketTransportClientSocketPool pool(kMaxSockets, |
+ kMaxSocketsPerGroup, |
+ histograms_.get(), |
+ host_resolver_.get(), |
+ &client_socket_factory_, |
+ NULL); |
+ const base::TimeDelta connect_job_timeout = pool.ConnectionTimeout(); |
+ |
+ client_socket_factory_.set_client_socket_type( |
+ MockClientSocketFactory::MOCK_DELAYED_FAILING_CLIENT_SOCKET); |
+ client_socket_factory_.set_delay(base::TimeDelta::FromSeconds(1) + |
+ connect_job_timeout / 6); |
+ |
+ // Resolve an AddressList with 6 IPv6 addresses and 6 IPv4 address. |
+ host_resolver_->rules()->AddIPLiteralRule("*", |
+ "1:abcd::3:4:ff,2:abcd::3:4:ff," |
+ "3:abcd::3:4:ff,4:abcd::3:4:ff," |
+ "5:abcd::3:4:ff,6:abcd::3:4:ff," |
+ "1.1.1.1,2.2.2.2,3.3.3.3," |
+ "4.4.4.4,5.5.5.5,6.6.6.6", |
+ std::string()); |
+ |
+ TestCompletionCallback callback; |
+ ClientSocketHandle handle; |
+ |
+ int rv = |
+ handle.Init("a", params_, LOW, callback.callback(), &pool, BoundNetLog()); |
+ EXPECT_EQ(ERR_IO_PENDING, rv); |
+ |
+ EXPECT_EQ(ERR_TIMED_OUT, callback.WaitForResult()); |
+} |
+ |
+} // namespace |
+ |
+} // namespace net |