| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/websockets/websocket_throttle.h" | 5 #include "net/websockets/websocket_throttle.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
| 10 #include "googleurl/src/gurl.h" | 10 #include "googleurl/src/gurl.h" |
| 11 #include "net/base/address_list.h" | 11 #include "net/base/address_list.h" |
| 12 #include "net/base/sys_addrinfo.h" | 12 #include "net/base/sys_addrinfo.h" |
| 13 #include "net/base/test_completion_callback.h" | 13 #include "net/base/test_completion_callback.h" |
| 14 #include "net/socket_stream/socket_stream.h" | 14 #include "net/socket_stream/socket_stream.h" |
| 15 #include "net/spdy/spdy_session.h" |
| 15 #include "net/url_request/url_request_test_util.h" | 16 #include "net/url_request/url_request_test_util.h" |
| 16 #include "net/websockets/websocket_job.h" | 17 #include "net/websockets/websocket_job.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
| 18 #include "testing/platform_test.h" | 19 #include "testing/platform_test.h" |
| 19 | 20 |
| 20 class DummySocketStreamDelegate : public net::SocketStream::Delegate { | 21 class DummySocketStreamDelegate : public net::SocketStream::Delegate { |
| 21 public: | 22 public: |
| 22 DummySocketStreamDelegate() {} | 23 DummySocketStreamDelegate() {} |
| 23 virtual ~DummySocketStreamDelegate() {} | 24 virtual ~DummySocketStreamDelegate() {} |
| 24 virtual void OnConnected( | 25 virtual void OnConnected( |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 79 DCHECK_EQ(socket->next_state_, SocketStream::STATE_NONE); | 80 DCHECK_EQ(socket->next_state_, SocketStream::STATE_NONE); |
| 80 } | 81 } |
| 81 }; | 82 }; |
| 82 | 83 |
| 83 TEST_F(WebSocketThrottleTest, Throttle) { | 84 TEST_F(WebSocketThrottleTest, Throttle) { |
| 84 scoped_refptr<URLRequestContext> context(new TestURLRequestContext); | 85 scoped_refptr<URLRequestContext> context(new TestURLRequestContext); |
| 85 DummySocketStreamDelegate delegate; | 86 DummySocketStreamDelegate delegate; |
| 86 // TODO(toyoshim): We need to consider both spdy-enabled and spdy-disabled | 87 // TODO(toyoshim): We need to consider both spdy-enabled and spdy-disabled |
| 87 // configuration. | 88 // configuration. |
| 88 WebSocketJob::set_websocket_over_spdy_enabled(true); | 89 WebSocketJob::set_websocket_over_spdy_enabled(true); |
| 90 // Disable sending PING. |
| 91 SpdySession::set_enable_ping_based_connection_checking(false); |
| 89 | 92 |
| 90 // For host1: 1.2.3.4, 1.2.3.5, 1.2.3.6 | 93 // For host1: 1.2.3.4, 1.2.3.5, 1.2.3.6 |
| 91 struct addrinfo* addr = AddAddr(1, 2, 3, 4, NULL); | 94 struct addrinfo* addr = AddAddr(1, 2, 3, 4, NULL); |
| 92 addr = AddAddr(1, 2, 3, 5, addr); | 95 addr = AddAddr(1, 2, 3, 5, addr); |
| 93 addr = AddAddr(1, 2, 3, 6, addr); | 96 addr = AddAddr(1, 2, 3, 6, addr); |
| 94 scoped_refptr<WebSocketJob> w1(new WebSocketJob(&delegate)); | 97 scoped_refptr<WebSocketJob> w1(new WebSocketJob(&delegate)); |
| 95 scoped_refptr<SocketStream> s1( | 98 scoped_refptr<SocketStream> s1( |
| 96 new SocketStream(GURL("ws://host1/"), w1.get())); | 99 new SocketStream(GURL("ws://host1/"), w1.get())); |
| 97 s1->set_context(context.get()); | 100 s1->set_context(context.get()); |
| 98 w1->InitSocketStream(s1.get()); | 101 w1->InitSocketStream(s1.get()); |
| (...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 291 w4->OnClose(s4.get()); | 294 w4->OnClose(s4.get()); |
| 292 s4->DetachDelegate(); | 295 s4->DetachDelegate(); |
| 293 DVLOG(1) << "Done"; | 296 DVLOG(1) << "Done"; |
| 294 MessageLoopForIO::current()->RunAllPending(); | 297 MessageLoopForIO::current()->RunAllPending(); |
| 295 } | 298 } |
| 296 | 299 |
| 297 TEST_F(WebSocketThrottleTest, NoThrottleForDuplicateAddress) { | 300 TEST_F(WebSocketThrottleTest, NoThrottleForDuplicateAddress) { |
| 298 scoped_refptr<URLRequestContext> context(new TestURLRequestContext); | 301 scoped_refptr<URLRequestContext> context(new TestURLRequestContext); |
| 299 DummySocketStreamDelegate delegate; | 302 DummySocketStreamDelegate delegate; |
| 300 WebSocketJob::set_websocket_over_spdy_enabled(true); | 303 WebSocketJob::set_websocket_over_spdy_enabled(true); |
| 304 // Disable sending PING. |
| 305 SpdySession::set_enable_ping_based_connection_checking(false); |
| 301 | 306 |
| 302 // For localhost: 127.0.0.1, 127.0.0.1 | 307 // For localhost: 127.0.0.1, 127.0.0.1 |
| 303 struct addrinfo* addr = AddAddr(127, 0, 0, 1, NULL); | 308 struct addrinfo* addr = AddAddr(127, 0, 0, 1, NULL); |
| 304 addr = AddAddr(127, 0, 0, 1, addr); | 309 addr = AddAddr(127, 0, 0, 1, addr); |
| 305 scoped_refptr<WebSocketJob> w1(new WebSocketJob(&delegate)); | 310 scoped_refptr<WebSocketJob> w1(new WebSocketJob(&delegate)); |
| 306 scoped_refptr<SocketStream> s1( | 311 scoped_refptr<SocketStream> s1( |
| 307 new SocketStream(GURL("ws://localhost/"), w1.get())); | 312 new SocketStream(GURL("ws://localhost/"), w1.get())); |
| 308 s1->set_context(context.get()); | 313 s1->set_context(context.get()); |
| 309 w1->InitSocketStream(s1.get()); | 314 w1->InitSocketStream(s1.get()); |
| 310 WebSocketThrottleTest::MockSocketStreamConnect(s1, addr); | 315 WebSocketThrottleTest::MockSocketStreamConnect(s1, addr); |
| 311 DeleteAddrInfo(addr); | 316 DeleteAddrInfo(addr); |
| 312 | 317 |
| 313 DVLOG(1) << "socket1"; | 318 DVLOG(1) << "socket1"; |
| 314 TestOldCompletionCallback callback_s1; | 319 TestOldCompletionCallback callback_s1; |
| 315 // Trying to open connection to localhost will start without wait. | 320 // Trying to open connection to localhost will start without wait. |
| 316 EXPECT_EQ(OK, w1->OnStartOpenConnection(s1, &callback_s1)); | 321 EXPECT_EQ(OK, w1->OnStartOpenConnection(s1, &callback_s1)); |
| 317 | 322 |
| 318 DVLOG(1) << "socket1 close"; | 323 DVLOG(1) << "socket1 close"; |
| 319 w1->OnClose(s1.get()); | 324 w1->OnClose(s1.get()); |
| 320 s1->DetachDelegate(); | 325 s1->DetachDelegate(); |
| 321 DVLOG(1) << "Done"; | 326 DVLOG(1) << "Done"; |
| 322 MessageLoopForIO::current()->RunAllPending(); | 327 MessageLoopForIO::current()->RunAllPending(); |
| 323 } | 328 } |
| 324 | 329 |
| 325 } | 330 } |
| OLD | NEW |