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

Side by Side Diff: net/http/http_network_transaction_unittest.cc

Issue 122453002: Allows deferral of a URLRequest just before talking to the network, at (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: The second set of mmenke's comments. Created 6 years, 11 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
« no previous file with comments | « net/http/http_network_transaction.cc ('k') | net/http/http_transaction.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 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 "net/http/http_network_transaction.h" 5 #include "net/http/http_network_transaction.h"
6 6
7 #include <math.h> // ceil 7 #include <math.h> // ceil
8 #include <stdarg.h> 8 #include <stdarg.h>
9 #include <string> 9 #include <string>
10 #include <vector> 10 #include <vector>
(...skipping 386 matching lines...) Expand 10 before | Expand all | Expand 10 after
397 397
398 INSTANTIATE_TEST_CASE_P( 398 INSTANTIATE_TEST_CASE_P(
399 NextProto, 399 NextProto,
400 HttpNetworkTransactionTest, 400 HttpNetworkTransactionTest,
401 testing::Values(kProtoDeprecatedSPDY2, 401 testing::Values(kProtoDeprecatedSPDY2,
402 kProtoSPDY3, kProtoSPDY31, kProtoSPDY4a2, 402 kProtoSPDY3, kProtoSPDY31, kProtoSPDY4a2,
403 kProtoHTTP2Draft04)); 403 kProtoHTTP2Draft04));
404 404
405 namespace { 405 namespace {
406 406
407 class BeforeNetworkStartHandler {
408 public:
409 explicit BeforeNetworkStartHandler(bool defer)
410 : defer_on_before_network_start_(defer),
411 observed_before_network_start_(false) {}
412
413 void OnBeforeNetworkStart(bool* defer) {
414 *defer = defer_on_before_network_start_;
415 observed_before_network_start_ = true;
416 }
417
418 bool observed_before_network_start() const {
419 return observed_before_network_start_;
420 }
421
422 private:
423 const bool defer_on_before_network_start_;
424 bool observed_before_network_start_;
425
426 DISALLOW_COPY_AND_ASSIGN(BeforeNetworkStartHandler);
427 };
428
407 // Fill |str| with a long header list that consumes >= |size| bytes. 429 // Fill |str| with a long header list that consumes >= |size| bytes.
408 void FillLargeHeadersString(std::string* str, int size) { 430 void FillLargeHeadersString(std::string* str, int size) {
409 const char* row = 431 const char* row =
410 "SomeHeaderName: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\r\n"; 432 "SomeHeaderName: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\r\n";
411 const int sizeof_row = strlen(row); 433 const int sizeof_row = strlen(row);
412 const int num_rows = static_cast<int>( 434 const int num_rows = static_cast<int>(
413 ceil(static_cast<float>(size) / sizeof_row)); 435 ceil(static_cast<float>(size) / sizeof_row));
414 const int sizeof_data = num_rows * sizeof_row; 436 const int sizeof_data = num_rows * sizeof_row;
415 DCHECK(sizeof_data >= size); 437 DCHECK(sizeof_data >= size);
416 str->reserve(sizeof_data); 438 str->reserve(sizeof_data);
(...skipping 919 matching lines...) Expand 10 before | Expand all | Expand 10 after
1336 MockRead(SYNCHRONOUS, OK), // EOF 1358 MockRead(SYNCHRONOUS, OK), // EOF
1337 MockRead("HTTP/1.0 200 OK\r\n\r\n"), // Should not be used 1359 MockRead("HTTP/1.0 200 OK\r\n\r\n"), // Should not be used
1338 MockRead("hello world"), 1360 MockRead("hello world"),
1339 MockRead(SYNCHRONOUS, OK), 1361 MockRead(SYNCHRONOUS, OK),
1340 }; 1362 };
1341 SimpleGetHelperResult out = SimpleGetHelper(data_reads, 1363 SimpleGetHelperResult out = SimpleGetHelper(data_reads,
1342 arraysize(data_reads)); 1364 arraysize(data_reads));
1343 EXPECT_EQ(ERR_EMPTY_RESPONSE, out.rv); 1365 EXPECT_EQ(ERR_EMPTY_RESPONSE, out.rv);
1344 } 1366 }
1345 1367
1368 // Test that network access can be deferred and resumed.
1369 TEST_P(HttpNetworkTransactionTest, ThrottleBeforeNetworkStart) {
1370 HttpRequestInfo request;
1371 request.method = "GET";
1372 request.url = GURL("http://www.google.com/");
1373 request.load_flags = 0;
1374
1375 scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps_));
1376 scoped_ptr<HttpTransaction> trans(
1377 new HttpNetworkTransaction(DEFAULT_PRIORITY, session.get()));
1378
1379 // Defer on OnBeforeNetworkStart.
1380 BeforeNetworkStartHandler net_start_handler(true); // defer
1381 trans->SetBeforeNetworkStartCallback(
1382 base::Bind(&BeforeNetworkStartHandler::OnBeforeNetworkStart,
1383 base::Unretained(&net_start_handler)));
1384
1385 MockRead data_reads[] = {
1386 MockRead("HTTP/1.0 200 OK\r\n"),
1387 MockRead("Connection: keep-alive\r\n"),
1388 MockRead("Content-Length: 100\r\n\r\n"),
mmenke 2014/01/07 20:31:38 Could you just change this to content-length: 5.
jkarlin 2014/01/08 12:15:10 Done.
1389 MockRead("hello"),
1390 MockRead(SYNCHRONOUS, 0),
1391 };
1392 StaticSocketDataProvider data(data_reads, arraysize(data_reads), NULL, 0);
1393 session_deps_.socket_factory->AddSocketDataProvider(&data);
1394
1395 TestCompletionCallback callback;
1396
1397 int rv = trans->Start(&request, callback.callback(), BoundNetLog());
1398 EXPECT_EQ(ERR_IO_PENDING, rv);
1399 base::MessageLoop::current()->RunUntilIdle();
1400
1401 // Should have deferred for network start.
1402 EXPECT_TRUE(net_start_handler.observed_before_network_start());
1403 EXPECT_EQ(LOAD_STATE_WAITING_FOR_DELEGATE, trans->GetLoadState());
1404 EXPECT_TRUE(trans->GetResponseInfo() == NULL);
1405
1406 trans->ResumeNetworkStart();
1407 rv = callback.WaitForResult();
1408 EXPECT_EQ(OK, rv);
1409 EXPECT_TRUE(trans->GetResponseInfo() != NULL);
1410
1411 scoped_refptr<IOBufferWithSize> io_buf(new IOBufferWithSize(100));
1412 rv = trans->Read(io_buf.get(), io_buf->size(), callback.callback());
1413 if (rv == ERR_IO_PENDING)
1414 rv = callback.WaitForResult();
1415 EXPECT_EQ(5, rv);
1416 trans.reset();
1417 }
1418
1419 // Test that network use can be deferred and canceled.
1420 TEST_P(HttpNetworkTransactionTest, ThrottleAndCancelBeforeNetworkStart) {
1421 HttpRequestInfo request;
1422 request.method = "GET";
1423 request.url = GURL("http://www.google.com/");
1424 request.load_flags = 0;
1425
1426 scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps_));
1427 scoped_ptr<HttpTransaction> trans(
1428 new HttpNetworkTransaction(DEFAULT_PRIORITY, session.get()));
1429
1430 // Defer on OnBeforeNetworkStart.
1431 BeforeNetworkStartHandler net_start_handler(true); // defer
1432 trans->SetBeforeNetworkStartCallback(
1433 base::Bind(&BeforeNetworkStartHandler::OnBeforeNetworkStart,
1434 base::Unretained(&net_start_handler)));
1435
1436 TestCompletionCallback callback;
1437
1438 int rv = trans->Start(&request, callback.callback(), BoundNetLog());
1439 EXPECT_EQ(ERR_IO_PENDING, rv);
1440 base::MessageLoop::current()->RunUntilIdle();
1441
1442 // Should have deferred for network start.
1443 EXPECT_TRUE(net_start_handler.observed_before_network_start());
1444 EXPECT_EQ(LOAD_STATE_WAITING_FOR_DELEGATE, trans->GetLoadState());
1445 EXPECT_TRUE(trans->GetResponseInfo() == NULL);
1446 }
1447
1346 // Next 2 cases (KeepAliveEarlyClose and KeepAliveEarlyClose2) are regression 1448 // Next 2 cases (KeepAliveEarlyClose and KeepAliveEarlyClose2) are regression
1347 // tests. There was a bug causing HttpNetworkTransaction to hang in the 1449 // tests. There was a bug causing HttpNetworkTransaction to hang in the
1348 // destructor in such situations. 1450 // destructor in such situations.
1349 // See http://crbug.com/154712 and http://crbug.com/156609. 1451 // See http://crbug.com/154712 and http://crbug.com/156609.
1350 TEST_P(HttpNetworkTransactionTest, KeepAliveEarlyClose) { 1452 TEST_P(HttpNetworkTransactionTest, KeepAliveEarlyClose) {
1351 HttpRequestInfo request; 1453 HttpRequestInfo request;
1352 request.method = "GET"; 1454 request.method = "GET";
1353 request.url = GURL("http://www.google.com/"); 1455 request.url = GURL("http://www.google.com/");
1354 request.load_flags = 0; 1456 request.load_flags = 0;
1355 1457
(...skipping 10977 matching lines...) Expand 10 before | Expand all | Expand 10 after
12333 // established, to let the HTTP request start. 12435 // established, to let the HTTP request start.
12334 ASSERT_EQ(OK, http_callback.WaitForResult()); 12436 ASSERT_EQ(OK, http_callback.WaitForResult());
12335 std::string response_data; 12437 std::string response_data;
12336 ASSERT_EQ(OK, ReadTransaction(http_trans.get(), &response_data)); 12438 ASSERT_EQ(OK, ReadTransaction(http_trans.get(), &response_data));
12337 EXPECT_EQ("falafel", response_data); 12439 EXPECT_EQ("falafel", response_data);
12338 12440
12339 EXPECT_EQ(1, GetIdleSocketCountInTransportSocketPool(session)); 12441 EXPECT_EQ(1, GetIdleSocketCountInTransportSocketPool(session));
12340 } 12442 }
12341 12443
12342 } // namespace net 12444 } // namespace net
OLDNEW
« no previous file with comments | « net/http/http_network_transaction.cc ('k') | net/http/http_transaction.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698