Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(580)

Side by Side Diff: content/browser/renderer_host/websocket_dispatcher_host_unittest.cc

Issue 972963002: Per-renderer WebSocket throttling (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Add a comment / blank lines. Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
53 default:
54 return true;
55 }
43 } 56 }
44 57
45 void GoAway() override; 58 void GoAway() override;
46 59
47 std::vector<IPC::Message> received_messages_; 60 std::vector<IPC::Message> received_messages_;
48 base::WeakPtr<WebSocketDispatcherHostTest> owner_; 61 base::WeakPtr<WebSocketDispatcherHostTest> owner_;
62 base::TimeDelta delay_;
63 };
64
65 class TestingWebSocketDispatcherHost : public WebSocketDispatcherHost {
66 public:
67 TestingWebSocketDispatcherHost(
68 int process_id,
69 const GetRequestContextCallback& get_context_callback,
70 const WebSocketHostFactory& websocket_host_factory)
71 : WebSocketDispatcherHost(process_id,
72 get_context_callback,
73 websocket_host_factory) {}
74
75 // This is needed because BrowserMessageFilter::Send() tries post the task to
76 // the IO thread, which doesn't exist in the context of these tests.
77 bool Send(IPC::Message* message) override {
78 delete message;
79 return true;
80 }
81
82 using WebSocketDispatcherHost::num_pending_connections;
83 using WebSocketDispatcherHost::num_failed_connections;
84 using WebSocketDispatcherHost::num_succeeded_connections;
85
86 private:
87 ~TestingWebSocketDispatcherHost() override {}
49 }; 88 };
50 89
51 class WebSocketDispatcherHostTest : public ::testing::Test { 90 class WebSocketDispatcherHostTest : public ::testing::Test {
52 public: 91 public:
53 WebSocketDispatcherHostTest() 92 WebSocketDispatcherHostTest()
54 : weak_ptr_factory_(this) { 93 : next_routing_id_(123),
55 dispatcher_host_ = new WebSocketDispatcherHost( 94 weak_ptr_factory_(this) {
95 dispatcher_host_ = new TestingWebSocketDispatcherHost(
56 kMagicRenderProcessId, 96 kMagicRenderProcessId,
57 base::Bind(&WebSocketDispatcherHostTest::OnGetRequestContext, 97 base::Bind(&WebSocketDispatcherHostTest::OnGetRequestContext,
58 base::Unretained(this)), 98 base::Unretained(this)),
59 base::Bind(&WebSocketDispatcherHostTest::CreateWebSocketHost, 99 base::Bind(&WebSocketDispatcherHostTest::CreateWebSocketHost,
60 base::Unretained(this))); 100 base::Unretained(this)));
61 } 101 }
62 102
63 ~WebSocketDispatcherHostTest() override { 103 ~WebSocketDispatcherHostTest() override {
64 // We need to invalidate the issued WeakPtrs at the beginning of the 104 // We need to invalidate the issued WeakPtrs at the beginning of the
65 // destructor in order not to access destructed member variables. 105 // destructor in order not to access destructed member variables.
66 weak_ptr_factory_.InvalidateWeakPtrs(); 106 weak_ptr_factory_.InvalidateWeakPtrs();
67 } 107 }
68 108
69 void GoAway(int routing_id) { 109 void GoAway(int routing_id) {
70 gone_hosts_.push_back(routing_id); 110 gone_hosts_.push_back(routing_id);
71 } 111 }
72 112
73 base::WeakPtr<WebSocketDispatcherHostTest> GetWeakPtr() { 113 base::WeakPtr<WebSocketDispatcherHostTest> GetWeakPtr() {
74 return weak_ptr_factory_.GetWeakPtr(); 114 return weak_ptr_factory_.GetWeakPtr();
75 } 115 }
76 116
77 protected: 117 protected:
78 scoped_refptr<WebSocketDispatcherHost> dispatcher_host_; 118 // Adds |n| connections. Returns true if succeeded.
119 bool AddMultipleChannels(int number_of_channels) {
120 GURL socket_url("ws://example.com/test");
121 std::vector<std::string> requested_protocols;
122 url::Origin origin("http://example.com");
123 int render_frame_id = -3;
124
125 for (int i = 0; i < number_of_channels; ++i) {
126 int routing_id = next_routing_id_++;
127 WebSocketHostMsg_AddChannelRequest message(
128 routing_id,
129 socket_url,
130 requested_protocols,
131 origin,
132 render_frame_id);
133 if (!dispatcher_host_->OnMessageReceived(message))
134 return false;
135 }
136
137 return true;
138 }
139
140 // Adds and cancels |n| connections. Returns true if succeeded.
141 bool AddAndCancelMultipleChannels(int number_of_channels) {
142 GURL socket_url("ws://example.com/test");
143 std::vector<std::string> requested_protocols;
144 url::Origin origin("http://example.com");
145 int render_frame_id = -3;
146
147 for (int i = 0; i < number_of_channels; ++i) {
148 int routing_id = next_routing_id_++;
149 WebSocketHostMsg_AddChannelRequest messageAddChannelRequest(
150 routing_id,
151 socket_url,
152 requested_protocols,
153 origin,
154 render_frame_id);
155 if (!dispatcher_host_->OnMessageReceived(messageAddChannelRequest))
156 return false;
157
158 WebSocketMsg_DropChannel messageDropChannel(
159 routing_id, false, net::kWebSocketErrorAbnormalClosure, "");
160 if (!dispatcher_host_->OnMessageReceived(messageDropChannel))
161 return false;
162 }
163
164 return true;
165 }
166
167 scoped_refptr<TestingWebSocketDispatcherHost> dispatcher_host_;
79 168
80 // Stores allocated MockWebSocketHost instances. Doesn't take ownership of 169 // Stores allocated MockWebSocketHost instances. Doesn't take ownership of
81 // them. 170 // them.
82 std::vector<MockWebSocketHost*> mock_hosts_; 171 std::vector<MockWebSocketHost*> mock_hosts_;
83 std::vector<int> gone_hosts_; 172 std::vector<int> gone_hosts_;
84 173
85 base::WeakPtrFactory<WebSocketDispatcherHostTest> weak_ptr_factory_;
86
87 private: 174 private:
88 net::URLRequestContext* OnGetRequestContext() { 175 net::URLRequestContext* OnGetRequestContext() {
89 return NULL; 176 return NULL;
90 } 177 }
91 178
92 WebSocketHost* CreateWebSocketHost(int routing_id) { 179 WebSocketHost* CreateWebSocketHost(int routing_id, base::TimeDelta delay) {
93 MockWebSocketHost* host = 180 MockWebSocketHost* host = new MockWebSocketHost(
94 new MockWebSocketHost(routing_id, dispatcher_host_.get(), NULL, this); 181 routing_id, dispatcher_host_.get(), NULL, delay, this);
95 mock_hosts_.push_back(host); 182 mock_hosts_.push_back(host);
96 return host; 183 return host;
97 } 184 }
185
186 base::MessageLoop message_loop_;
187
188 int next_routing_id_;
189
190 base::WeakPtrFactory<WebSocketDispatcherHostTest> weak_ptr_factory_;
98 }; 191 };
99 192
100 MockWebSocketHost::MockWebSocketHost( 193 MockWebSocketHost::MockWebSocketHost(
101 int routing_id, 194 int routing_id,
102 WebSocketDispatcherHost* dispatcher, 195 WebSocketDispatcherHost* dispatcher,
103 net::URLRequestContext* url_request_context, 196 net::URLRequestContext* url_request_context,
197 base::TimeDelta delay,
104 WebSocketDispatcherHostTest* owner) 198 WebSocketDispatcherHostTest* owner)
105 : WebSocketHost(routing_id, dispatcher, url_request_context), 199 : WebSocketHost(routing_id, dispatcher, url_request_context, delay),
106 owner_(owner->GetWeakPtr()) {} 200 owner_(owner->GetWeakPtr()),
201 delay_(delay) {}
107 202
108 void MockWebSocketHost::GoAway() { 203 void MockWebSocketHost::GoAway() {
109 if (owner_) 204 if (owner_)
110 owner_->GoAway(routing_id()); 205 owner_->GoAway(routing_id());
111 } 206 }
112 207
113 TEST_F(WebSocketDispatcherHostTest, Construct) { 208 TEST_F(WebSocketDispatcherHostTest, Construct) {
114 // Do nothing. 209 // Do nothing.
115 } 210 }
116 211
117 TEST_F(WebSocketDispatcherHostTest, UnrelatedMessage) { 212 TEST_F(WebSocketDispatcherHostTest, UnrelatedMessage) {
118 IPC::Message message; 213 IPC::Message message;
119 EXPECT_FALSE(dispatcher_host_->OnMessageReceived(message)); 214 EXPECT_FALSE(dispatcher_host_->OnMessageReceived(message));
120 } 215 }
121 216
122 TEST_F(WebSocketDispatcherHostTest, RenderProcessIdGetter) { 217 TEST_F(WebSocketDispatcherHostTest, RenderProcessIdGetter) {
123 EXPECT_EQ(kMagicRenderProcessId, dispatcher_host_->render_process_id()); 218 EXPECT_EQ(kMagicRenderProcessId, dispatcher_host_->render_process_id());
124 } 219 }
125 220
126 TEST_F(WebSocketDispatcherHostTest, AddChannelRequest) { 221 TEST_F(WebSocketDispatcherHostTest, AddChannelRequest) {
127 int routing_id = 123; 222 int routing_id = 123;
128 GURL socket_url("ws://example.com/test"); 223 GURL socket_url("ws://example.com/test");
129 std::vector<std::string> requested_protocols; 224 std::vector<std::string> requested_protocols;
130 requested_protocols.push_back("hello"); 225 requested_protocols.push_back("hello");
131 url::Origin origin("http://example.com/test"); 226 url::Origin origin("http://example.com");
132 int render_frame_id = -2; 227 int render_frame_id = -2;
133 WebSocketHostMsg_AddChannelRequest message( 228 WebSocketHostMsg_AddChannelRequest message(
134 routing_id, socket_url, requested_protocols, origin, render_frame_id); 229 routing_id, socket_url, requested_protocols, origin, render_frame_id);
135 230
136 ASSERT_TRUE(dispatcher_host_->OnMessageReceived(message)); 231 ASSERT_TRUE(dispatcher_host_->OnMessageReceived(message));
137 232
138 ASSERT_EQ(1U, mock_hosts_.size()); 233 ASSERT_EQ(1U, mock_hosts_.size());
139 MockWebSocketHost* host = mock_hosts_[0]; 234 MockWebSocketHost* host = mock_hosts_[0];
140 235
141 ASSERT_EQ(1U, host->received_messages_.size()); 236 ASSERT_EQ(1U, host->received_messages_.size());
(...skipping 13 matching lines...) Expand all
155 250
156 EXPECT_EQ(0U, mock_hosts_.size()); 251 EXPECT_EQ(0U, mock_hosts_.size());
157 } 252 }
158 253
159 TEST_F(WebSocketDispatcherHostTest, SendFrame) { 254 TEST_F(WebSocketDispatcherHostTest, SendFrame) {
160 int routing_id = 123; 255 int routing_id = 123;
161 256
162 GURL socket_url("ws://example.com/test"); 257 GURL socket_url("ws://example.com/test");
163 std::vector<std::string> requested_protocols; 258 std::vector<std::string> requested_protocols;
164 requested_protocols.push_back("hello"); 259 requested_protocols.push_back("hello");
165 url::Origin origin("http://example.com/test"); 260 url::Origin origin("http://example.com");
166 int render_frame_id = -2; 261 int render_frame_id = -2;
167 WebSocketHostMsg_AddChannelRequest add_channel_message( 262 WebSocketHostMsg_AddChannelRequest add_channel_message(
168 routing_id, socket_url, requested_protocols, origin, render_frame_id); 263 routing_id, socket_url, requested_protocols, origin, render_frame_id);
169 264
170 ASSERT_TRUE(dispatcher_host_->OnMessageReceived(add_channel_message)); 265 ASSERT_TRUE(dispatcher_host_->OnMessageReceived(add_channel_message));
171 266
172 std::vector<char> data; 267 std::vector<char> data;
173 WebSocketMsg_SendFrame send_frame_message( 268 WebSocketMsg_SendFrame send_frame_message(
174 routing_id, true, WEB_SOCKET_MESSAGE_TYPE_TEXT, data); 269 routing_id, true, WEB_SOCKET_MESSAGE_TYPE_TEXT, data);
175 270
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
208 dispatcher_host_ = NULL; 303 dispatcher_host_ = NULL;
209 304
210 ASSERT_EQ(2u, gone_hosts_.size()); 305 ASSERT_EQ(2u, gone_hosts_.size());
211 // The gone_hosts_ ordering is not predictable because it depends on the 306 // The gone_hosts_ ordering is not predictable because it depends on the
212 // hash_map ordering. 307 // hash_map ordering.
213 std::sort(gone_hosts_.begin(), gone_hosts_.end()); 308 std::sort(gone_hosts_.begin(), gone_hosts_.end());
214 EXPECT_EQ(123, gone_hosts_[0]); 309 EXPECT_EQ(123, gone_hosts_[0]);
215 EXPECT_EQ(456, gone_hosts_[1]); 310 EXPECT_EQ(456, gone_hosts_[1]);
216 } 311 }
217 312
313 TEST_F(WebSocketDispatcherHostTest, DelayFor4thPendingConnectionIsZero) {
314 ASSERT_TRUE(AddMultipleChannels(4));
315
316 EXPECT_EQ(4, dispatcher_host_->num_pending_connections());
317 EXPECT_EQ(0, dispatcher_host_->num_failed_connections());
318 EXPECT_EQ(0, dispatcher_host_->num_succeeded_connections());
319
320 ASSERT_EQ(4U, mock_hosts_.size());
321 EXPECT_EQ(base::TimeDelta(), mock_hosts_[3]->delay_);
322 }
323
324 TEST_F(WebSocketDispatcherHostTest, DelayFor8thPendingConnectionIsNonZero) {
325 ASSERT_TRUE(AddMultipleChannels(8));
326
327 EXPECT_EQ(8, dispatcher_host_->num_pending_connections());
328 EXPECT_EQ(0, dispatcher_host_->num_failed_connections());
329 EXPECT_EQ(0, dispatcher_host_->num_succeeded_connections());
330
331 ASSERT_EQ(8U, mock_hosts_.size());
332 EXPECT_LT(base::TimeDelta(), mock_hosts_[7]->delay_);
333 }
334
335 TEST_F(WebSocketDispatcherHostTest, DelayFor17thPendingConnection) {
336 ASSERT_TRUE(AddMultipleChannels(17));
337
338 EXPECT_EQ(17, dispatcher_host_->num_pending_connections());
339 EXPECT_EQ(0, dispatcher_host_->num_failed_connections());
340 EXPECT_EQ(0, dispatcher_host_->num_succeeded_connections());
341
342 ASSERT_EQ(17U, mock_hosts_.size());
343 EXPECT_LE(base::TimeDelta::FromMilliseconds(1000), mock_hosts_[16]->delay_);
344 EXPECT_GE(base::TimeDelta::FromMilliseconds(5000), mock_hosts_[16]->delay_);
345 }
346
347 // The 256th connection is rejected by per-renderer WebSocket throttling.
348 // This is not counted as a failure.
349 TEST_F(WebSocketDispatcherHostTest, Rejects256thPendingConnection) {
350 ASSERT_TRUE(AddMultipleChannels(256));
351
352 EXPECT_EQ(255, dispatcher_host_->num_pending_connections());
353 EXPECT_EQ(0, dispatcher_host_->num_failed_connections());
354 EXPECT_EQ(0, dispatcher_host_->num_succeeded_connections());
355
356 ASSERT_EQ(255U, mock_hosts_.size());
357 }
358
359 TEST_F(WebSocketDispatcherHostTest, DelayIsZeroAfter3FailedConnections) {
360 ASSERT_TRUE(AddAndCancelMultipleChannels(3));
361
362 EXPECT_EQ(0, dispatcher_host_->num_pending_connections());
363 EXPECT_EQ(3, dispatcher_host_->num_failed_connections());
364 EXPECT_EQ(0, dispatcher_host_->num_succeeded_connections());
365
366 ASSERT_TRUE(AddMultipleChannels(1));
367
368 ASSERT_EQ(4U, mock_hosts_.size());
369 EXPECT_EQ(base::TimeDelta(), mock_hosts_[3]->delay_);
370 }
371
372 TEST_F(WebSocketDispatcherHostTest, DelayIsNonZeroAfter7FailedConnections) {
373 ASSERT_TRUE(AddAndCancelMultipleChannels(7));
374
375 EXPECT_EQ(0, dispatcher_host_->num_pending_connections());
376 EXPECT_EQ(7, dispatcher_host_->num_failed_connections());
377 EXPECT_EQ(0, dispatcher_host_->num_succeeded_connections());
378
379 ASSERT_TRUE(AddMultipleChannels(1));
380
381 ASSERT_EQ(8U, mock_hosts_.size());
382 EXPECT_LT(base::TimeDelta(), mock_hosts_[7]->delay_);
383 }
384
385 TEST_F(WebSocketDispatcherHostTest, DelayAfter16FailedConnections) {
386 ASSERT_TRUE(AddAndCancelMultipleChannels(16));
387
388 EXPECT_EQ(0, dispatcher_host_->num_pending_connections());
389 EXPECT_EQ(16, dispatcher_host_->num_failed_connections());
390 EXPECT_EQ(0, dispatcher_host_->num_succeeded_connections());
391
392 ASSERT_TRUE(AddMultipleChannels(1));
393
394 ASSERT_EQ(17U, mock_hosts_.size());
395 EXPECT_LE(base::TimeDelta::FromMilliseconds(1000), mock_hosts_[16]->delay_);
396 EXPECT_GE(base::TimeDelta::FromMilliseconds(5000), mock_hosts_[16]->delay_);
397 }
398
399 TEST_F(WebSocketDispatcherHostTest, NotRejectedAfter255FailedConnections) {
400 ASSERT_TRUE(AddAndCancelMultipleChannels(255));
401
402 EXPECT_EQ(0, dispatcher_host_->num_pending_connections());
403 EXPECT_EQ(255, dispatcher_host_->num_failed_connections());
404 EXPECT_EQ(0, dispatcher_host_->num_succeeded_connections());
405
406 ASSERT_TRUE(AddMultipleChannels(1));
407
408 EXPECT_EQ(1, dispatcher_host_->num_pending_connections());
409 EXPECT_EQ(255, dispatcher_host_->num_failed_connections());
410 EXPECT_EQ(0, dispatcher_host_->num_succeeded_connections());
411 }
412
218 } // namespace 413 } // namespace
219 } // namespace content 414 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/renderer_host/websocket_dispatcher_host.cc ('k') | content/browser/renderer_host/websocket_host.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698