Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "content/browser/renderer_host/websocket_dispatcher_host.h" | 5 #include "content/browser/renderer_host/websocket_dispatcher_host.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/bind_helpers.h" | 11 #include "base/bind_helpers.h" |
| 12 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
| 13 #include "base/memory/weak_ptr.h" | 13 #include "base/memory/weak_ptr.h" |
| 14 #include "base/message_loop/message_loop.h" | |
| 14 #include "content/browser/renderer_host/websocket_host.h" | 15 #include "content/browser/renderer_host/websocket_host.h" |
| 15 #include "content/common/websocket.h" | 16 #include "content/common/websocket.h" |
| 16 #include "content/common/websocket_messages.h" | 17 #include "content/common/websocket_messages.h" |
| 17 #include "ipc/ipc_message.h" | 18 #include "ipc/ipc_message.h" |
| 19 #include "net/websockets/websocket_errors.h" | |
| 18 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
| 19 #include "url/gurl.h" | 21 #include "url/gurl.h" |
| 20 #include "url/origin.h" | 22 #include "url/origin.h" |
| 21 | 23 |
| 22 namespace content { | 24 namespace content { |
| 23 namespace { | 25 namespace { |
| 24 | 26 |
| 25 // This number is unlikely to occur by chance. | 27 // This number is unlikely to occur by chance. |
| 26 static const int kMagicRenderProcessId = 506116062; | 28 static const int kMagicRenderProcessId = 506116062; |
| 27 | 29 |
| 28 class WebSocketDispatcherHostTest; | 30 class WebSocketDispatcherHostTest; |
| 29 | 31 |
| 30 // A mock of WebsocketHost which records received messages. | 32 // A mock of WebsocketHost which records received messages. |
| 31 class MockWebSocketHost : public WebSocketHost { | 33 class MockWebSocketHost : public WebSocketHost { |
| 32 public: | 34 public: |
| 33 MockWebSocketHost(int routing_id, | 35 MockWebSocketHost(int routing_id, |
| 34 WebSocketDispatcherHost* dispatcher, | 36 WebSocketDispatcherHost* dispatcher, |
| 35 net::URLRequestContext* url_request_context, | 37 net::URLRequestContext* url_request_context, |
| 38 base::TimeDelta delay, | |
| 36 WebSocketDispatcherHostTest* owner); | 39 WebSocketDispatcherHostTest* owner); |
| 37 | 40 |
| 38 ~MockWebSocketHost() override {} | 41 ~MockWebSocketHost() override {} |
| 39 | 42 |
| 40 bool OnMessageReceived(const IPC::Message& message) override { | 43 bool OnMessageReceived(const IPC::Message& message) override { |
| 41 received_messages_.push_back(message); | 44 received_messages_.push_back(message); |
| 42 return true; | 45 switch (message.type()) { |
| 46 case WebSocketMsg_DropChannel::ID: | |
| 47 // Needed for PerRendererThrottlingFailedHandshakes, because without | |
| 48 // calling WebSocketHost::OnMessageReceived() (and thus | |
| 49 // WebSocketHost::OnDropChannel()), the connection stays pending and | |
| 50 // we cannot test per-renderer throttling with failed connections. | |
| 51 return WebSocketHost::OnMessageReceived(message); | |
| 52 default: | |
|
Adam Rice
2015/03/06 11:01:06
Blank line above default: please.
hiroshige
2015/03/11 06:21:25
Done.
| |
| 53 return true; | |
| 54 } | |
| 43 } | 55 } |
| 44 | 56 |
| 45 void GoAway() override; | 57 void GoAway() override; |
| 46 | 58 |
| 47 std::vector<IPC::Message> received_messages_; | 59 std::vector<IPC::Message> received_messages_; |
| 48 base::WeakPtr<WebSocketDispatcherHostTest> owner_; | 60 base::WeakPtr<WebSocketDispatcherHostTest> owner_; |
| 61 base::TimeDelta delay_; | |
| 49 }; | 62 }; |
| 50 | 63 |
| 51 class WebSocketDispatcherHostTest : public ::testing::Test { | 64 class WebSocketDispatcherHostTest : public ::testing::Test { |
| 52 public: | 65 public: |
| 53 WebSocketDispatcherHostTest() | 66 WebSocketDispatcherHostTest() |
| 54 : weak_ptr_factory_(this) { | 67 : weak_ptr_factory_(this) { |
| 55 dispatcher_host_ = new WebSocketDispatcherHost( | 68 dispatcher_host_ = new WebSocketDispatcherHost( |
| 56 kMagicRenderProcessId, | 69 kMagicRenderProcessId, |
| 57 base::Bind(&WebSocketDispatcherHostTest::OnGetRequestContext, | 70 base::Bind(&WebSocketDispatcherHostTest::OnGetRequestContext, |
| 58 base::Unretained(this)), | 71 base::Unretained(this)), |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 75 } | 88 } |
| 76 | 89 |
| 77 protected: | 90 protected: |
| 78 scoped_refptr<WebSocketDispatcherHost> dispatcher_host_; | 91 scoped_refptr<WebSocketDispatcherHost> dispatcher_host_; |
| 79 | 92 |
| 80 // Stores allocated MockWebSocketHost instances. Doesn't take ownership of | 93 // Stores allocated MockWebSocketHost instances. Doesn't take ownership of |
| 81 // them. | 94 // them. |
| 82 std::vector<MockWebSocketHost*> mock_hosts_; | 95 std::vector<MockWebSocketHost*> mock_hosts_; |
| 83 std::vector<int> gone_hosts_; | 96 std::vector<int> gone_hosts_; |
| 84 | 97 |
| 98 base::MessageLoop message_loop_; | |
| 99 | |
| 85 base::WeakPtrFactory<WebSocketDispatcherHostTest> weak_ptr_factory_; | 100 base::WeakPtrFactory<WebSocketDispatcherHostTest> weak_ptr_factory_; |
| 86 | 101 |
| 87 private: | 102 private: |
| 88 net::URLRequestContext* OnGetRequestContext() { | 103 net::URLRequestContext* OnGetRequestContext() { |
| 89 return NULL; | 104 return NULL; |
| 90 } | 105 } |
| 91 | 106 |
| 92 WebSocketHost* CreateWebSocketHost(int routing_id) { | 107 WebSocketHost* CreateWebSocketHost(int routing_id, base::TimeDelta delay) { |
| 93 MockWebSocketHost* host = | 108 MockWebSocketHost* host = new MockWebSocketHost( |
| 94 new MockWebSocketHost(routing_id, dispatcher_host_.get(), NULL, this); | 109 routing_id, dispatcher_host_.get(), NULL, delay, this); |
| 95 mock_hosts_.push_back(host); | 110 mock_hosts_.push_back(host); |
| 96 return host; | 111 return host; |
| 97 } | 112 } |
| 98 }; | 113 }; |
| 99 | 114 |
| 100 MockWebSocketHost::MockWebSocketHost( | 115 MockWebSocketHost::MockWebSocketHost( |
| 101 int routing_id, | 116 int routing_id, |
| 102 WebSocketDispatcherHost* dispatcher, | 117 WebSocketDispatcherHost* dispatcher, |
| 103 net::URLRequestContext* url_request_context, | 118 net::URLRequestContext* url_request_context, |
| 119 base::TimeDelta delay, | |
| 104 WebSocketDispatcherHostTest* owner) | 120 WebSocketDispatcherHostTest* owner) |
| 105 : WebSocketHost(routing_id, dispatcher, url_request_context), | 121 : WebSocketHost(routing_id, dispatcher, url_request_context, delay), |
| 106 owner_(owner->GetWeakPtr()) {} | 122 owner_(owner->GetWeakPtr()), |
| 123 delay_(delay) {} | |
| 107 | 124 |
| 108 void MockWebSocketHost::GoAway() { | 125 void MockWebSocketHost::GoAway() { |
| 109 if (owner_) | 126 if (owner_) |
| 110 owner_->GoAway(routing_id()); | 127 owner_->GoAway(routing_id()); |
| 111 } | 128 } |
| 112 | 129 |
| 113 TEST_F(WebSocketDispatcherHostTest, Construct) { | 130 TEST_F(WebSocketDispatcherHostTest, Construct) { |
| 114 // Do nothing. | 131 // Do nothing. |
| 115 } | 132 } |
| 116 | 133 |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 208 dispatcher_host_ = NULL; | 225 dispatcher_host_ = NULL; |
| 209 | 226 |
| 210 ASSERT_EQ(2u, gone_hosts_.size()); | 227 ASSERT_EQ(2u, gone_hosts_.size()); |
| 211 // The gone_hosts_ ordering is not predictable because it depends on the | 228 // The gone_hosts_ ordering is not predictable because it depends on the |
| 212 // hash_map ordering. | 229 // hash_map ordering. |
| 213 std::sort(gone_hosts_.begin(), gone_hosts_.end()); | 230 std::sort(gone_hosts_.begin(), gone_hosts_.end()); |
| 214 EXPECT_EQ(123, gone_hosts_[0]); | 231 EXPECT_EQ(123, gone_hosts_[0]); |
| 215 EXPECT_EQ(456, gone_hosts_[1]); | 232 EXPECT_EQ(456, gone_hosts_[1]); |
| 216 } | 233 } |
| 217 | 234 |
| 235 TEST_F(WebSocketDispatcherHostTest, PerRendererThrottling) { | |
|
Adam Rice
2015/03/06 11:01:06
Please split this into three separate unit tests f
hiroshige
2015/03/11 06:21:25
Done.
| |
| 236 GURL socket_url("ws://example.com/test"); | |
| 237 std::vector<std::string> requested_protocols; | |
| 238 requested_protocols.push_back("hello"); | |
|
Adam Rice
2015/03/06 11:01:06
This makes no difference to the outcome of the tes
hiroshige
2015/03/11 06:21:25
Done.
| |
| 239 url::Origin origin("http://example.com/test"); | |
|
Adam Rice
2015/03/06 11:01:06
An origin never contains a path. It should be just
hiroshige
2015/03/11 06:21:24
Done. Also fixed other tests in this file.
| |
| 240 int render_frame_id = -3; | |
| 241 | |
| 242 for (int i = 0; i < 256; ++i) { | |
|
Adam Rice
2015/03/06 11:01:06
Since you are going to need this in several tests,
hiroshige
2015/03/11 06:21:25
Done.
Omitted |url| parameter because there is no
| |
| 243 int routing_id = 123 + i; | |
| 244 WebSocketHostMsg_AddChannelRequest message( | |
| 245 routing_id, socket_url, requested_protocols, origin, render_frame_id); | |
| 246 ASSERT_TRUE(dispatcher_host_->OnMessageReceived(message)); | |
| 247 } | |
| 248 | |
| 249 // The first 255 connections are added to mock_hosts_, staying pending. | |
| 250 // The 256th connection is rejected. | |
| 251 ASSERT_EQ(255U, mock_hosts_.size()); | |
| 252 | |
| 253 // For the 1st--4th pending WebSocketHosts, delay is 0. | |
| 254 for (int i = 0; i < 4; ++i) { | |
| 255 MockWebSocketHost* host = mock_hosts_[i]; | |
| 256 EXPECT_EQ(host->delay_, base::TimeDelta()); | |
| 257 } | |
| 258 | |
| 259 // For the 8th--16th pending WebSocketHosts, delay is > 0. | |
| 260 for (int i = 7; i < 16; ++i) { | |
| 261 MockWebSocketHost* host = mock_hosts_[i]; | |
| 262 EXPECT_GT(host->delay_, base::TimeDelta()); | |
| 263 } | |
| 264 | |
| 265 // For the 17th-- pending WebSocketHosts, delay is >=1000 and <= 5000. | |
| 266 for (int i = 16; i < 255; ++i) { | |
|
Adam Rice
2015/03/06 11:01:06
You don't need to test every number. Tests should
hiroshige
2015/03/11 06:21:25
Done.
| |
| 267 MockWebSocketHost* host = mock_hosts_[i]; | |
| 268 EXPECT_GE(host->delay_, base::TimeDelta::FromMilliseconds(1000)); | |
| 269 EXPECT_LE(host->delay_, base::TimeDelta::FromMilliseconds(5000)); | |
| 270 } | |
| 271 } | |
| 272 | |
| 273 TEST_F(WebSocketDispatcherHostTest, PerRendererThrottlingFailedHandshakes) { | |
| 274 GURL socket_url("ws://example.com/test"); | |
| 275 std::vector<std::string> requested_protocols; | |
| 276 requested_protocols.push_back("hello"); | |
| 277 url::Origin origin("http://example.com/test"); | |
| 278 int render_frame_id = -4; | |
| 279 | |
| 280 // 256 AddChannelRequest are sent but are cancelled by DropChannel during | |
|
Adam Rice
2015/03/06 11:01:06
s/during/while/
hiroshige
2015/03/11 06:21:25
Done.
| |
| 281 // they are pending. They are counted as failure. | |
| 282 // Delay is increased by the increasing number of failures. | |
| 283 // However, the number of pending connections stays <= 1, so no connections | |
| 284 // are rejected. | |
| 285 for (int i = 0; i < 256; ++i) { | |
| 286 int routing_id = 123 + i; | |
| 287 WebSocketHostMsg_AddChannelRequest message( | |
| 288 routing_id, socket_url, requested_protocols, origin, render_frame_id); | |
| 289 ASSERT_TRUE(dispatcher_host_->OnMessageReceived(message)); | |
| 290 | |
| 291 ASSERT_EQ(static_cast<size_t>(i + 1), mock_hosts_.size()); | |
| 292 | |
| 293 MockWebSocketHost* host = mock_hosts_[i]; | |
| 294 if (i < 4) { | |
|
Adam Rice
2015/03/06 11:01:06
I like to avoid having if statements in tests beca
hiroshige
2015/03/11 06:21:25
Split into separate tests.
| |
| 295 // For the 1st--4th WebSocketHosts, delay is 0. | |
| 296 EXPECT_EQ(host->delay_, base::TimeDelta()); | |
| 297 } | |
| 298 else if (7 <= i && i < 16) { | |
| 299 // For the 8th--16th WebSocketHosts, delay is > 0. | |
| 300 EXPECT_GT(host->delay_, base::TimeDelta()); | |
| 301 } | |
| 302 else if (16 <= i) { | |
| 303 // For the 17th-- WebSocketHosts, delay is >=1000 and <= 5000. | |
| 304 EXPECT_GE(host->delay_, base::TimeDelta::FromMilliseconds(1000)); | |
| 305 EXPECT_LE(host->delay_, base::TimeDelta::FromMilliseconds(5000)); | |
| 306 } | |
| 307 | |
| 308 WebSocketMsg_DropChannel message2( | |
| 309 routing_id, false, net::kWebSocketErrorAbnormalClosure, ""); | |
| 310 ASSERT_TRUE(dispatcher_host_->OnMessageReceived(message2)); | |
| 311 // The connection is cancelled before WebSocketChannel is created, and | |
| 312 // thus counted as failure. | |
| 313 } | |
| 314 } | |
| 315 | |
| 218 } // namespace | 316 } // namespace |
| 219 } // namespace content | 317 } // namespace content |
| OLD | NEW |