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

Side by Side Diff: chrome/browser/renderer_host/resource_dispatcher_host_unittest.cc

Issue 18541: Add a constraint on how many requests can be outstanding for any given render (browser-side). (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 11 years, 10 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 | Annotate | Revision Log
« no previous file with comments | « chrome/browser/renderer_host/resource_dispatcher_host.cc ('k') | net/base/net_error_list.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2006-2008 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 <vector> 5 #include <vector>
6 6
7 #include "base/message_loop.h" 7 #include "base/message_loop.h"
8 #include "chrome/browser/renderer_host/renderer_security_policy.h" 8 #include "chrome/browser/renderer_host/renderer_security_policy.h"
9 #include "chrome/browser/renderer_host/resource_dispatcher_host.h" 9 #include "chrome/browser/renderer_host/resource_dispatcher_host.h"
10 #include "chrome/common/chrome_plugin_lib.h" 10 #include "chrome/common/chrome_plugin_lib.h"
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after
97 // testing::Test 97 // testing::Test
98 virtual void SetUp() { 98 virtual void SetUp() {
99 RendererSecurityPolicy::GetInstance()->Add(0); 99 RendererSecurityPolicy::GetInstance()->Add(0);
100 URLRequest::RegisterProtocolFactory("test", &URLRequestTestJob::Factory); 100 URLRequest::RegisterProtocolFactory("test", &URLRequestTestJob::Factory);
101 EnsureTestSchemeIsAllowed(); 101 EnsureTestSchemeIsAllowed();
102 } 102 }
103 virtual void TearDown() { 103 virtual void TearDown() {
104 URLRequest::RegisterProtocolFactory("test", NULL); 104 URLRequest::RegisterProtocolFactory("test", NULL);
105 RendererSecurityPolicy::GetInstance()->Remove(0); 105 RendererSecurityPolicy::GetInstance()->Remove(0);
106 106
107 // The plugin lib is automatically loaded during these test 107 // The plugin lib is automatically loaded during these test
108 // and we want a clean environment for other tests. 108 // and we want a clean environment for other tests.
109 ChromePluginLib::UnloadAllPlugins(); 109 ChromePluginLib::UnloadAllPlugins();
110 110
111 // Flush the message loop to make Purify happy. 111 // Flush the message loop to make Purify happy.
112 message_loop_.RunAllPending(); 112 message_loop_.RunAllPending();
113 } 113 }
114 114
115 void MakeTestRequest(int render_process_id, 115 void MakeTestRequest(int render_process_id,
116 int render_view_id, 116 int render_view_id,
117 int request_id, 117 int request_id,
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
187 187
188 // followed by a 0-byte read 188 // followed by a 0-byte read
189 //ASSERT_EQ(ViewMsg_Resource_DataReceived::ID, messages[2].type()); 189 //ASSERT_EQ(ViewMsg_Resource_DataReceived::ID, messages[2].type());
190 190
191 // the last message should be all data received 191 // the last message should be all data received
192 ASSERT_EQ(ViewMsg_Resource_RequestComplete::ID, messages[2].type()); 192 ASSERT_EQ(ViewMsg_Resource_RequestComplete::ID, messages[2].type());
193 } 193 }
194 194
195 // Tests whether many messages get dispatched properly. 195 // Tests whether many messages get dispatched properly.
196 TEST_F(ResourceDispatcherHostTest, TestMany) { 196 TEST_F(ResourceDispatcherHostTest, TestMany) {
197 EXPECT_EQ(0, host_.GetOutstandingRequestsMemoryCost(0));
198
197 MakeTestRequest(0, 0, 1, URLRequestTestJob::test_url_1()); 199 MakeTestRequest(0, 0, 1, URLRequestTestJob::test_url_1());
198 MakeTestRequest(0, 0, 2, URLRequestTestJob::test_url_2()); 200 MakeTestRequest(0, 0, 2, URLRequestTestJob::test_url_2());
199 MakeTestRequest(0, 0, 3, URLRequestTestJob::test_url_3()); 201 MakeTestRequest(0, 0, 3, URLRequestTestJob::test_url_3());
200 202
201 // flush all the pending requests 203 // flush all the pending requests
202 while (URLRequestTestJob::ProcessOnePendingMessage()); 204 while (URLRequestTestJob::ProcessOnePendingMessage());
203 205
206 EXPECT_EQ(0, host_.GetOutstandingRequestsMemoryCost(0));
207
204 // sorts out all the messages we saw by request 208 // sorts out all the messages we saw by request
205 ResourceIPCAccumulator::ClassifiedMessages msgs; 209 ResourceIPCAccumulator::ClassifiedMessages msgs;
206 accum_.GetClassifiedMessages(&msgs); 210 accum_.GetClassifiedMessages(&msgs);
207 211
208 // there are three requests, so we should have gotten them classified as such 212 // there are three requests, so we should have gotten them classified as such
209 ASSERT_EQ(3, msgs.size()); 213 ASSERT_EQ(3, msgs.size());
210 214
211 CheckSuccessfulRequest(msgs[0], URLRequestTestJob::test_data_1()); 215 CheckSuccessfulRequest(msgs[0], URLRequestTestJob::test_data_1());
212 CheckSuccessfulRequest(msgs[1], URLRequestTestJob::test_data_2()); 216 CheckSuccessfulRequest(msgs[1], URLRequestTestJob::test_data_2());
213 CheckSuccessfulRequest(msgs[2], URLRequestTestJob::test_data_3()); 217 CheckSuccessfulRequest(msgs[2], URLRequestTestJob::test_data_3());
214 } 218 }
215 219
216 // Tests whether messages get canceled properly. We issue three requests, 220 // Tests whether messages get canceled properly. We issue three requests,
217 // cancel one of them, and make sure that each sent the proper notifications. 221 // cancel one of them, and make sure that each sent the proper notifications.
218 TEST_F(ResourceDispatcherHostTest, Cancel) { 222 TEST_F(ResourceDispatcherHostTest, Cancel) {
219 ResourceDispatcherHost host(NULL); 223 ResourceDispatcherHost host(NULL);
220 224
225 EXPECT_EQ(0, host_.GetOutstandingRequestsMemoryCost(0));
226
221 MakeTestRequest(0, 0, 1, URLRequestTestJob::test_url_1()); 227 MakeTestRequest(0, 0, 1, URLRequestTestJob::test_url_1());
222 MakeTestRequest(0, 0, 2, URLRequestTestJob::test_url_2()); 228 MakeTestRequest(0, 0, 2, URLRequestTestJob::test_url_2());
223 MakeTestRequest(0, 0, 3, URLRequestTestJob::test_url_3()); 229 MakeTestRequest(0, 0, 3, URLRequestTestJob::test_url_3());
224 MakeCancelRequest(2); 230 MakeCancelRequest(2);
225 231
226 // flush all the pending requests 232 // flush all the pending requests
227 while (URLRequestTestJob::ProcessOnePendingMessage()); 233 while (URLRequestTestJob::ProcessOnePendingMessage());
234 MessageLoop::current()->RunAllPending();
235
236 EXPECT_EQ(0, host_.GetOutstandingRequestsMemoryCost(0));
228 237
229 ResourceIPCAccumulator::ClassifiedMessages msgs; 238 ResourceIPCAccumulator::ClassifiedMessages msgs;
230 accum_.GetClassifiedMessages(&msgs); 239 accum_.GetClassifiedMessages(&msgs);
231 240
232 // there are three requests, so we should have gotten them classified as such 241 // there are three requests, so we should have gotten them classified as such
233 ASSERT_EQ(3, msgs.size()); 242 ASSERT_EQ(3, msgs.size());
234 243
235 CheckSuccessfulRequest(msgs[0], URLRequestTestJob::test_data_1()); 244 CheckSuccessfulRequest(msgs[0], URLRequestTestJob::test_data_1());
236 CheckSuccessfulRequest(msgs[2], URLRequestTestJob::test_data_3()); 245 CheckSuccessfulRequest(msgs[2], URLRequestTestJob::test_data_3());
237 246
238 // Check that request 2 got canceled before it finished reading, which gives 247 // Check that request 2 got canceled.
239 // us 1 ReceivedResponse message. 248 ASSERT_EQ(2, msgs[1].size());
240 ASSERT_EQ(1, msgs[1].size());
241 ASSERT_EQ(ViewMsg_Resource_ReceivedResponse::ID, msgs[1][0].type()); 249 ASSERT_EQ(ViewMsg_Resource_ReceivedResponse::ID, msgs[1][0].type());
250 ASSERT_EQ(ViewMsg_Resource_RequestComplete::ID, msgs[1][1].type());
242 251
243 // TODO(mbelshe):
244 // Now that the async IO path is in place, the IO always completes on the
245 // initial call; so the cancel doesn't arrive until after we finished.
246 // This basically means the test doesn't work.
247 #if 0
248 int request_id; 252 int request_id;
249 URLRequestStatus status; 253 URLRequestStatus status;
250 254
251 // The message should be all data received with an error.
252 ASSERT_EQ(ViewMsg_Resource_RequestComplete::ID, msgs[1][2].type());
253
254 void* iter = NULL; 255 void* iter = NULL;
255 ASSERT_TRUE(IPC::ReadParam(&msgs[1][2], &iter, &request_id)); 256 ASSERT_TRUE(IPC::ReadParam(&msgs[1][1], &iter, &request_id));
256 ASSERT_TRUE(IPC::ReadParam(&msgs[1][2], &iter, &status)); 257 ASSERT_TRUE(IPC::ReadParam(&msgs[1][1], &iter, &status));
257 258
258 EXPECT_EQ(URLRequestStatus::CANCELED, status.status()); 259 EXPECT_EQ(URLRequestStatus::CANCELED, status.status());
259 #endif
260 } 260 }
261 261
262 // Tests CancelRequestsForProcess 262 // Tests CancelRequestsForProcess
263 TEST_F(ResourceDispatcherHostTest, TestProcessCancel) { 263 TEST_F(ResourceDispatcherHostTest, TestProcessCancel) {
264 // the host delegate acts as a second one so we can have some requests 264 // the host delegate acts as a second one so we can have some requests
265 // pending and some canceled 265 // pending and some canceled
266 class TestReceiver : public ResourceDispatcherHost::Receiver { 266 class TestReceiver : public ResourceDispatcherHost::Receiver {
267 public: 267 public:
268 TestReceiver() : has_canceled_(false), received_after_canceled_(0) { 268 TestReceiver() : has_canceled_(false), received_after_canceled_(0) {
269 } 269 }
270 virtual bool Send(IPC::Message* msg) { 270 virtual bool Send(IPC::Message* msg) {
271 // no messages should be received when the process has been canceled 271 // no messages should be received when the process has been canceled
272 if (has_canceled_) 272 if (has_canceled_)
273 received_after_canceled_ ++; 273 received_after_canceled_++;
274 delete msg; 274 delete msg;
275 return true; 275 return true;
276 } 276 }
277 bool has_canceled_; 277 bool has_canceled_;
278 int received_after_canceled_; 278 int received_after_canceled_;
279 }; 279 };
280 TestReceiver test_receiver; 280 TestReceiver test_receiver;
281 281
282 // request 1 goes to the test delegate 282 // request 1 goes to the test delegate
283 ViewHostMsg_Resource_Request request = 283 ViewHostMsg_Resource_Request request =
284 CreateResourceRequest("GET", URLRequestTestJob::test_url_1()); 284 CreateResourceRequest("GET", URLRequestTestJob::test_url_1());
285 285
286 EXPECT_EQ(0, host_.GetOutstandingRequestsMemoryCost(0));
287
286 host_.BeginRequest(&test_receiver, GetCurrentProcess(), 0, MSG_ROUTING_NONE, 288 host_.BeginRequest(&test_receiver, GetCurrentProcess(), 0, MSG_ROUTING_NONE,
287 1, request, NULL, NULL); 289 1, request, NULL, NULL);
288 KickOffRequest(); 290 KickOffRequest();
289 291
290 // request 2 goes to us 292 // request 2 goes to us
291 MakeTestRequest(0, 0, 2, URLRequestTestJob::test_url_2()); 293 MakeTestRequest(0, 0, 2, URLRequestTestJob::test_url_2());
292 294
293 // request 3 goes to the test delegate 295 // request 3 goes to the test delegate
294 request.url = URLRequestTestJob::test_url_3(); 296 request.url = URLRequestTestJob::test_url_3();
295 host_.BeginRequest(&test_receiver, GetCurrentProcess(), 0, MSG_ROUTING_NONE, 297 host_.BeginRequest(&test_receiver, GetCurrentProcess(), 0, MSG_ROUTING_NONE,
296 3, request, NULL, NULL); 298 3, request, NULL, NULL);
297 KickOffRequest(); 299 KickOffRequest();
298 300
299 // TODO: mbelshe 301 // TODO(mbelshe):
300 // Now that the async IO path is in place, the IO always completes on the 302 // Now that the async IO path is in place, the IO always completes on the
301 // initial call; so the requests have already completed. This basically 303 // initial call; so the requests have already completed. This basically
302 // breaks the whole test. 304 // breaks the whole test.
303 //EXPECT_EQ(3, host_.pending_requests()); 305 //EXPECT_EQ(3, host_.pending_requests());
304 306
305 // process each request for one level so one callback is called 307 // process each request for one level so one callback is called
306 for (int i = 0; i < 3; i++) 308 for (int i = 0; i < 3; i++)
307 EXPECT_TRUE(URLRequestTestJob::ProcessOnePendingMessage()); 309 EXPECT_TRUE(URLRequestTestJob::ProcessOnePendingMessage());
308 310
309 // cancel the requests to the test process 311 // cancel the requests to the test process
310 host_.CancelRequestsForProcess(0); 312 host_.CancelRequestsForProcess(0);
311 test_receiver.has_canceled_ = true; 313 test_receiver.has_canceled_ = true;
312 314
313 // flush all the pending requests 315 // flush all the pending requests
314 while (URLRequestTestJob::ProcessOnePendingMessage()); 316 while (URLRequestTestJob::ProcessOnePendingMessage());
315 317
316 EXPECT_EQ(0, host_.pending_requests()); 318 EXPECT_EQ(0, host_.pending_requests());
319 EXPECT_EQ(0, host_.GetOutstandingRequestsMemoryCost(0));
317 320
318 // the test delegate should not have gotten any messages after being canceled 321 // the test delegate should not have gotten any messages after being canceled
319 ASSERT_EQ(0, test_receiver.received_after_canceled_); 322 ASSERT_EQ(0, test_receiver.received_after_canceled_);
320 323
321 // we should have gotten exactly one result 324 // we should have gotten exactly one result
322 ResourceIPCAccumulator::ClassifiedMessages msgs; 325 ResourceIPCAccumulator::ClassifiedMessages msgs;
323 accum_.GetClassifiedMessages(&msgs); 326 accum_.GetClassifiedMessages(&msgs);
324 ASSERT_EQ(1, msgs.size()); 327 ASSERT_EQ(1, msgs.size());
325 CheckSuccessfulRequest(msgs[0], URLRequestTestJob::test_data_2()); 328 CheckSuccessfulRequest(msgs[0], URLRequestTestJob::test_data_2());
326 } 329 }
327 330
328 // Tests blocking and resuming requests. 331 // Tests blocking and resuming requests.
329 TEST_F(ResourceDispatcherHostTest, TestBlockingResumingRequests) { 332 TEST_F(ResourceDispatcherHostTest, TestBlockingResumingRequests) {
333 EXPECT_EQ(0, host_.GetOutstandingRequestsMemoryCost(0));
334
330 host_.BlockRequestsForRenderView(0, 1); 335 host_.BlockRequestsForRenderView(0, 1);
331 host_.BlockRequestsForRenderView(0, 2); 336 host_.BlockRequestsForRenderView(0, 2);
332 host_.BlockRequestsForRenderView(0, 3); 337 host_.BlockRequestsForRenderView(0, 3);
333 338
334 MakeTestRequest(0, 0, 1, URLRequestTestJob::test_url_1()); 339 MakeTestRequest(0, 0, 1, URLRequestTestJob::test_url_1());
335 MakeTestRequest(0, 1, 2, URLRequestTestJob::test_url_2()); 340 MakeTestRequest(0, 1, 2, URLRequestTestJob::test_url_2());
336 MakeTestRequest(0, 0, 3, URLRequestTestJob::test_url_3()); 341 MakeTestRequest(0, 0, 3, URLRequestTestJob::test_url_3());
337 MakeTestRequest(0, 1, 4, URLRequestTestJob::test_url_1()); 342 MakeTestRequest(0, 1, 4, URLRequestTestJob::test_url_1());
338 MakeTestRequest(0, 2, 5, URLRequestTestJob::test_url_2()); 343 MakeTestRequest(0, 2, 5, URLRequestTestJob::test_url_2());
339 MakeTestRequest(0, 3, 6, URLRequestTestJob::test_url_3()); 344 MakeTestRequest(0, 3, 6, URLRequestTestJob::test_url_3());
(...skipping 29 matching lines...) Expand all
369 accum_.GetClassifiedMessages(&msgs); 374 accum_.GetClassifiedMessages(&msgs);
370 ASSERT_EQ(1, msgs.size()); 375 ASSERT_EQ(1, msgs.size());
371 CheckSuccessfulRequest(msgs[0], URLRequestTestJob::test_data_1()); 376 CheckSuccessfulRequest(msgs[0], URLRequestTestJob::test_data_1());
372 377
373 // Now resumes requests for all RVH (2 and 3). 378 // Now resumes requests for all RVH (2 and 3).
374 host_.ResumeBlockedRequestsForRenderView(0, 2); 379 host_.ResumeBlockedRequestsForRenderView(0, 2);
375 host_.ResumeBlockedRequestsForRenderView(0, 3); 380 host_.ResumeBlockedRequestsForRenderView(0, 3);
376 KickOffRequest(); 381 KickOffRequest();
377 while (URLRequestTestJob::ProcessOnePendingMessage()); 382 while (URLRequestTestJob::ProcessOnePendingMessage());
378 383
384 EXPECT_EQ(0, host_.GetOutstandingRequestsMemoryCost(0));
385
379 msgs.clear(); 386 msgs.clear();
380 accum_.GetClassifiedMessages(&msgs); 387 accum_.GetClassifiedMessages(&msgs);
381 ASSERT_EQ(2, msgs.size()); 388 ASSERT_EQ(2, msgs.size());
382 CheckSuccessfulRequest(msgs[0], URLRequestTestJob::test_data_2()); 389 CheckSuccessfulRequest(msgs[0], URLRequestTestJob::test_data_2());
383 CheckSuccessfulRequest(msgs[1], URLRequestTestJob::test_data_3()); 390 CheckSuccessfulRequest(msgs[1], URLRequestTestJob::test_data_3());
384 } 391 }
385 392
386 // Tests blocking and canceling requests. 393 // Tests blocking and canceling requests.
387 TEST_F(ResourceDispatcherHostTest, TestBlockingCancelingRequests) { 394 TEST_F(ResourceDispatcherHostTest, TestBlockingCancelingRequests) {
395 EXPECT_EQ(0, host_.GetOutstandingRequestsMemoryCost(0));
396
388 host_.BlockRequestsForRenderView(0, 1); 397 host_.BlockRequestsForRenderView(0, 1);
389 398
390 MakeTestRequest(0, 0, 1, URLRequestTestJob::test_url_1()); 399 MakeTestRequest(0, 0, 1, URLRequestTestJob::test_url_1());
391 MakeTestRequest(0, 1, 2, URLRequestTestJob::test_url_2()); 400 MakeTestRequest(0, 1, 2, URLRequestTestJob::test_url_2());
392 MakeTestRequest(0, 0, 3, URLRequestTestJob::test_url_3()); 401 MakeTestRequest(0, 0, 3, URLRequestTestJob::test_url_3());
393 MakeTestRequest(0, 1, 4, URLRequestTestJob::test_url_1()); 402 MakeTestRequest(0, 1, 4, URLRequestTestJob::test_url_1());
394 403
395 // Flush all the pending requests. 404 // Flush all the pending requests.
396 while (URLRequestTestJob::ProcessOnePendingMessage()); 405 while (URLRequestTestJob::ProcessOnePendingMessage());
397 406
398 // Sort out all the messages we saw by request. 407 // Sort out all the messages we saw by request.
399 ResourceIPCAccumulator::ClassifiedMessages msgs; 408 ResourceIPCAccumulator::ClassifiedMessages msgs;
400 accum_.GetClassifiedMessages(&msgs); 409 accum_.GetClassifiedMessages(&msgs);
401 410
402 // The 2 requests for the RVH 0 should have been processed. 411 // The 2 requests for the RVH 0 should have been processed.
403 ASSERT_EQ(2, msgs.size()); 412 ASSERT_EQ(2, msgs.size());
404 413
405 CheckSuccessfulRequest(msgs[0], URLRequestTestJob::test_data_1()); 414 CheckSuccessfulRequest(msgs[0], URLRequestTestJob::test_data_1());
406 CheckSuccessfulRequest(msgs[1], URLRequestTestJob::test_data_3()); 415 CheckSuccessfulRequest(msgs[1], URLRequestTestJob::test_data_3());
407 416
408 // Cancel requests for RVH 1. 417 // Cancel requests for RVH 1.
409 host_.CancelBlockedRequestsForRenderView(0, 1); 418 host_.CancelBlockedRequestsForRenderView(0, 1);
410 KickOffRequest(); 419 KickOffRequest();
411 while (URLRequestTestJob::ProcessOnePendingMessage()); 420 while (URLRequestTestJob::ProcessOnePendingMessage());
421
422 EXPECT_EQ(0, host_.GetOutstandingRequestsMemoryCost(0));
423
412 msgs.clear(); 424 msgs.clear();
413 accum_.GetClassifiedMessages(&msgs); 425 accum_.GetClassifiedMessages(&msgs);
414 ASSERT_EQ(0, msgs.size()); 426 ASSERT_EQ(0, msgs.size());
415 } 427 }
416 428
417 // Tests that blocked requests are canceled if their associated process dies. 429 // Tests that blocked requests are canceled if their associated process dies.
418 TEST_F(ResourceDispatcherHostTest, TestBlockedRequestsProcessDies) { 430 TEST_F(ResourceDispatcherHostTest, TestBlockedRequestsProcessDies) {
431 EXPECT_EQ(0, host_.GetOutstandingRequestsMemoryCost(0));
432 EXPECT_EQ(0, host_.GetOutstandingRequestsMemoryCost(1));
433
419 host_.BlockRequestsForRenderView(1, 0); 434 host_.BlockRequestsForRenderView(1, 0);
420 435
421 MakeTestRequest(0, 0, 1, URLRequestTestJob::test_url_1()); 436 MakeTestRequest(0, 0, 1, URLRequestTestJob::test_url_1());
422 MakeTestRequest(1, 0, 2, URLRequestTestJob::test_url_2()); 437 MakeTestRequest(1, 0, 2, URLRequestTestJob::test_url_2());
423 MakeTestRequest(0, 0, 3, URLRequestTestJob::test_url_3()); 438 MakeTestRequest(0, 0, 3, URLRequestTestJob::test_url_3());
424 MakeTestRequest(1, 0, 4, URLRequestTestJob::test_url_1()); 439 MakeTestRequest(1, 0, 4, URLRequestTestJob::test_url_1());
425 440
426 // Simulate process death. 441 // Simulate process death.
427 host_.CancelRequestsForProcess(1); 442 host_.CancelRequestsForProcess(1);
428 443
429 // Flush all the pending requests. 444 // Flush all the pending requests.
430 while (URLRequestTestJob::ProcessOnePendingMessage()); 445 while (URLRequestTestJob::ProcessOnePendingMessage());
431 446
447 EXPECT_EQ(0, host_.GetOutstandingRequestsMemoryCost(0));
448 EXPECT_EQ(0, host_.GetOutstandingRequestsMemoryCost(1));
449
432 // Sort out all the messages we saw by request. 450 // Sort out all the messages we saw by request.
433 ResourceIPCAccumulator::ClassifiedMessages msgs; 451 ResourceIPCAccumulator::ClassifiedMessages msgs;
434 accum_.GetClassifiedMessages(&msgs); 452 accum_.GetClassifiedMessages(&msgs);
435 453
436 // The 2 requests for the RVH 0 should have been processed. 454 // The 2 requests for the RVH 0 should have been processed.
437 ASSERT_EQ(2, msgs.size()); 455 ASSERT_EQ(2, msgs.size());
438 456
439 CheckSuccessfulRequest(msgs[0], URLRequestTestJob::test_data_1()); 457 CheckSuccessfulRequest(msgs[0], URLRequestTestJob::test_data_1());
440 CheckSuccessfulRequest(msgs[1], URLRequestTestJob::test_data_3()); 458 CheckSuccessfulRequest(msgs[1], URLRequestTestJob::test_data_3());
441 459
(...skipping 12 matching lines...) Expand all
454 MakeTestRequest(0, 0, 1, URLRequestTestJob::test_url_1()); 472 MakeTestRequest(0, 0, 1, URLRequestTestJob::test_url_1());
455 MakeTestRequest(0, 1, 2, URLRequestTestJob::test_url_2()); 473 MakeTestRequest(0, 1, 2, URLRequestTestJob::test_url_2());
456 MakeTestRequest(0, 0, 3, URLRequestTestJob::test_url_3()); 474 MakeTestRequest(0, 0, 3, URLRequestTestJob::test_url_3());
457 MakeTestRequest(1, 1, 4, URLRequestTestJob::test_url_1()); 475 MakeTestRequest(1, 1, 4, URLRequestTestJob::test_url_1());
458 MakeTestRequest(0, 2, 5, URLRequestTestJob::test_url_2()); 476 MakeTestRequest(0, 2, 5, URLRequestTestJob::test_url_2());
459 MakeTestRequest(0, 2, 6, URLRequestTestJob::test_url_3()); 477 MakeTestRequest(0, 2, 6, URLRequestTestJob::test_url_3());
460 478
461 // Flush all the pending requests. 479 // Flush all the pending requests.
462 while (URLRequestTestJob::ProcessOnePendingMessage()); 480 while (URLRequestTestJob::ProcessOnePendingMessage());
463 } 481 }
482
483 // Test the private helper method "CalculateApproximateMemoryCost()".
484 TEST_F(ResourceDispatcherHostTest, CalculateApproximateMemoryCost) {
485 URLRequest req(GURL("http://www.google.com"), NULL);
486 EXPECT_EQ(4425, ResourceDispatcherHost::CalculateApproximateMemoryCost(&req));
487
488 // Add 9 bytes of referrer.
489 req.set_referrer("123456789");
490 EXPECT_EQ(4434, ResourceDispatcherHost::CalculateApproximateMemoryCost(&req));
491
492 // Add 33 bytes of upload content.
493 std::string upload_content;
494 upload_content.resize(33);
495 std::fill(upload_content.begin(), upload_content.end(), 'x');
496 req.AppendBytesToUpload(upload_content.data(), upload_content.size());
497
498 // Since the upload throttling is disabled, this has no effect on the cost.
499 EXPECT_EQ(4434, ResourceDispatcherHost::CalculateApproximateMemoryCost(&req));
500
501 // Add a file upload -- should have no effect.
502 req.AppendFileToUpload(L"does-not-exist.png");
503 EXPECT_EQ(4434, ResourceDispatcherHost::CalculateApproximateMemoryCost(&req));
504 }
505
506 // Test the private helper method "IncrementOutstandingRequestsMemoryCost()".
507 TEST_F(ResourceDispatcherHostTest, IncrementOutstandingRequestsMemoryCost) {
508 ResourceDispatcherHost host(NULL);
509
510 // Add some counts for render_process_host=7
511 EXPECT_EQ(0, host.GetOutstandingRequestsMemoryCost(7));
512 EXPECT_EQ(1, host.IncrementOutstandingRequestsMemoryCost(1, 7));
513 EXPECT_EQ(2, host.IncrementOutstandingRequestsMemoryCost(1, 7));
514 EXPECT_EQ(3, host.IncrementOutstandingRequestsMemoryCost(1, 7));
515
516 // Add some counts for render_process_host=3
517 EXPECT_EQ(0, host.GetOutstandingRequestsMemoryCost(3));
518 EXPECT_EQ(1, host.IncrementOutstandingRequestsMemoryCost(1, 3));
519 EXPECT_EQ(2, host.IncrementOutstandingRequestsMemoryCost(1, 3));
520
521 // Remove all the counts for render_process_host=7
522 EXPECT_EQ(3, host.GetOutstandingRequestsMemoryCost(7));
523 EXPECT_EQ(2, host.IncrementOutstandingRequestsMemoryCost(-1, 7));
524 EXPECT_EQ(1, host.IncrementOutstandingRequestsMemoryCost(-1, 7));
525 EXPECT_EQ(0, host.IncrementOutstandingRequestsMemoryCost(-1, 7));
526 EXPECT_EQ(0, host.GetOutstandingRequestsMemoryCost(7));
527
528 // Remove all the counts for render_process_host=3
529 EXPECT_EQ(2, host.GetOutstandingRequestsMemoryCost(3));
530 EXPECT_EQ(1, host.IncrementOutstandingRequestsMemoryCost(-1, 3));
531 EXPECT_EQ(0, host.IncrementOutstandingRequestsMemoryCost(-1, 3));
532 EXPECT_EQ(0, host.GetOutstandingRequestsMemoryCost(3));
533
534 // When an entry reaches 0, it should be deleted.
535 EXPECT_TRUE(host.outstanding_requests_memory_cost_map_.end() ==
536 host.outstanding_requests_memory_cost_map_.find(7));
537 EXPECT_TRUE(host.outstanding_requests_memory_cost_map_.end() ==
538 host.outstanding_requests_memory_cost_map_.find(3));
539 }
540
541 // Test that when too many requests are outstanding for a particular
542 // render_process_host_id, any subsequent request from it fails.
543 TEST_F(ResourceDispatcherHostTest, TooManyOutstandingRequests) {
544 EXPECT_EQ(0, host_.GetOutstandingRequestsMemoryCost(0));
545
546 // Expected cost of each request as measured by
547 // ResourceDispatcherHost::CalculateApproximateMemoryCost().
548 int kMemoryCostOfTest2Req =
549 ResourceDispatcherHost::kAvgBytesPerOutstandingRequest +
550 std::string("GET").size() +
551 URLRequestTestJob::test_url_2().spec().size();
552
553 // Tighten the bound on the ResourceDispatcherHost, to speed things up.
554 int kMaxCostPerProcess = 440000;
555 host_.set_max_outstanding_requests_cost_per_process(kMaxCostPerProcess);
556
557 // Determine how many instance of test_url_2() we can request before
558 // throttling kicks in.
559 int kMaxRequests = kMaxCostPerProcess / kMemoryCostOfTest2Req;
560
561 // Saturate the number of outstanding requests for process 0.
562 for (int i = 0; i < kMaxRequests; ++i)
563 MakeTestRequest(0, 0, i + 1, URLRequestTestJob::test_url_2());
564
565 // Issue two more requests for process 0 -- these should fail immediately.
566 MakeTestRequest(0, 0, kMaxRequests + 1, URLRequestTestJob::test_url_2());
567 MakeTestRequest(0, 0, kMaxRequests + 2, URLRequestTestJob::test_url_2());
568
569 // Issue two requests for process 1 -- these should succeed since
570 // it is just process 0 that is saturated.
571 MakeTestRequest(1, 0, kMaxRequests + 3, URLRequestTestJob::test_url_2());
572 MakeTestRequest(1, 0, kMaxRequests + 4, URLRequestTestJob::test_url_2());
573
574 // Flush all the pending requests.
575 while (URLRequestTestJob::ProcessOnePendingMessage());
576 MessageLoop::current()->RunAllPending();
577
578 EXPECT_EQ(0, host_.GetOutstandingRequestsMemoryCost(0));
579
580 // Sorts out all the messages we saw by request.
581 ResourceIPCAccumulator::ClassifiedMessages msgs;
582 accum_.GetClassifiedMessages(&msgs);
583
584 // We issued (kMaxRequests + 4) total requests.
585 ASSERT_EQ(kMaxRequests + 4, msgs.size());
586
587 // Check that the first kMaxRequests succeeded.
588 for (int i = 0; i < kMaxRequests; ++i)
589 CheckSuccessfulRequest(msgs[i], URLRequestTestJob::test_data_2());
590
591 // Check that the subsequent two requests (kMaxRequests + 1) and
592 // (kMaxRequests + 2) were failed, since the per-process bound was reached.
593 for (int i = 0; i < 2; ++i) {
594 // Should have sent a single RequestComplete message.
595 int index = kMaxRequests + i;
596 EXPECT_EQ(1, msgs[index].size());
597 EXPECT_EQ(ViewMsg_Resource_RequestComplete::ID, msgs[index][0].type());
598
599 // The RequestComplete message should have had status
600 // (CANCELLED, ERR_INSUFFICIENT_RESOURCES).
601 int request_id;
602 URLRequestStatus status;
603
604 void* iter = NULL;
605 EXPECT_TRUE(IPC::ReadParam(&msgs[index][0], &iter, &request_id));
606 EXPECT_TRUE(IPC::ReadParam(&msgs[index][0], &iter, &status));
607
608 EXPECT_EQ(index + 1, request_id);
609 EXPECT_EQ(URLRequestStatus::CANCELED, status.status());
610 EXPECT_EQ(net::ERR_INSUFFICIENT_RESOURCES, status.os_error());
611 }
612
613 // The final 2 requests should have succeeded.
614 CheckSuccessfulRequest(msgs[kMaxRequests + 2],
615 URLRequestTestJob::test_data_2());
616 CheckSuccessfulRequest(msgs[kMaxRequests + 3],
617 URLRequestTestJob::test_data_2());
618 }
619
OLDNEW
« no previous file with comments | « chrome/browser/renderer_host/resource_dispatcher_host.cc ('k') | net/base/net_error_list.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698