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

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

Issue 2721933002: HttpCache::Transaction layer allowing parallel validation (Closed)
Patch Set: Simplified ActiveEntry's state transitions Created 3 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 (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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_cache.h" 5 #include "net/http/http_cache.h"
6 6
7 #include <stdint.h> 7 #include <stdint.h>
8 8
9 #include <algorithm> 9 #include <algorithm>
10 #include <memory> 10 #include <memory>
(...skipping 1358 matching lines...) Expand 10 before | Expand all | Expand 10 after
1369 // All requests are waiting for the active entry. 1369 // All requests are waiting for the active entry.
1370 for (int i = 0; i < kNumTransactions; ++i) { 1370 for (int i = 0; i < kNumTransactions; ++i) {
1371 Context* c = context_list[i]; 1371 Context* c = context_list[i];
1372 EXPECT_EQ(LOAD_STATE_WAITING_FOR_CACHE, c->trans->GetLoadState()); 1372 EXPECT_EQ(LOAD_STATE_WAITING_FOR_CACHE, c->trans->GetLoadState());
1373 } 1373 }
1374 1374
1375 // Allow all requests to move from the Create queue to the active entry. 1375 // Allow all requests to move from the Create queue to the active entry.
1376 base::RunLoop().RunUntilIdle(); 1376 base::RunLoop().RunUntilIdle();
1377 1377
1378 // The first request should be a writer at this point, and the subsequent 1378 // The first request should be a writer at this point, and the subsequent
1379 // requests should be pending. 1379 // requests should have passed the validation phase and waiting for the
1380 // response to be written to the cache before they can read.
1380 1381
1381 EXPECT_EQ(1, cache.network_layer()->transaction_count()); 1382 EXPECT_EQ(1, cache.network_layer()->transaction_count());
1382 EXPECT_EQ(0, cache.disk_cache()->open_count()); 1383 EXPECT_EQ(0, cache.disk_cache()->open_count());
1383 EXPECT_EQ(1, cache.disk_cache()->create_count()); 1384 EXPECT_EQ(1, cache.disk_cache()->create_count());
1384 1385
1385 // All requests depend on the writer, and the writer is between Start and 1386 // All requests depend on the writer, and the writer is between Start and
1386 // Read, i.e. idle. 1387 // Read, i.e. idle.
1387 for (int i = 0; i < kNumTransactions; ++i) { 1388 for (int i = 0; i < kNumTransactions; ++i) {
1388 Context* c = context_list[i]; 1389 Context* c = context_list[i];
1389 EXPECT_EQ(LOAD_STATE_IDLE, c->trans->GetLoadState()); 1390 EXPECT_EQ(LOAD_STATE_IDLE, c->trans->GetLoadState());
(...skipping 11 matching lines...) Expand all
1401 EXPECT_EQ(1, cache.network_layer()->transaction_count()); 1402 EXPECT_EQ(1, cache.network_layer()->transaction_count());
1402 EXPECT_EQ(0, cache.disk_cache()->open_count()); 1403 EXPECT_EQ(0, cache.disk_cache()->open_count());
1403 EXPECT_EQ(1, cache.disk_cache()->create_count()); 1404 EXPECT_EQ(1, cache.disk_cache()->create_count());
1404 1405
1405 for (int i = 0; i < kNumTransactions; ++i) { 1406 for (int i = 0; i < kNumTransactions; ++i) {
1406 Context* c = context_list[i]; 1407 Context* c = context_list[i];
1407 delete c; 1408 delete c;
1408 } 1409 }
1409 } 1410 }
1410 1411
1412 // Tests parallel validation similar to the above test, except here validation
1413 // request is sent out to the network and results in 304.
1414 TEST(HttpCache, SimpleGET_ParallelValidation) {
1415 MockHttpCache cache;
1416
1417 std::vector<MockHttpRequest> request;
1418 ScopedMockTransaction transaction(kTypicalGET_Transaction);
1419 transaction.response_headers =
1420 "Etag: \"foopy\"\n"
1421 "Cache-Control: max-age=0\n";
1422 MockHttpRequest request0(transaction);
1423 request.push_back(request0);
1424
1425 ScopedMockTransaction transaction1(kTypicalGET_Transaction);
1426 transaction1.status = "HTTP/1.1 304 Not Modified";
1427 transaction1.load_flags |= LOAD_VALIDATE_CACHE;
1428 transaction1.response_headers =
1429 "Etag: \"foopy\"\n"
1430 "Cache-Control: max-age=3600\n";
1431 MockHttpRequest request1(transaction1);
1432 request.push_back(request1);
1433
1434 std::vector<Context*> context_list;
1435 const int kNumTransactions = 2;
1436
1437 for (int i = 0; i < kNumTransactions; ++i) {
1438 context_list.push_back(new Context());
1439 Context* c = context_list[i];
1440
1441 c->result = cache.CreateTransaction(&c->trans);
1442 ASSERT_THAT(c->result, IsOk());
1443 EXPECT_EQ(LOAD_STATE_IDLE, c->trans->GetLoadState());
1444
1445 c->result = c->trans->Start(&request[i], c->callback.callback(),
1446 NetLogWithSource());
1447 }
1448
1449 // All requests are waiting for the active entry.
1450 for (int i = 0; i < kNumTransactions; ++i) {
1451 Context* c = context_list[i];
1452 EXPECT_EQ(LOAD_STATE_WAITING_FOR_CACHE, c->trans->GetLoadState());
1453 }
1454
1455 // Allow all requests to move from the Create queue to the active entry.
1456 base::RunLoop().RunUntilIdle();
1457
1458 // The first request should be a writer at this point, and the subsequent
1459 // requests should have passed the validation phase and waiting for the
1460 // response to be written to the cache before they can read.
1461
1462 EXPECT_EQ(2, cache.network_layer()->transaction_count());
1463 EXPECT_EQ(0, cache.disk_cache()->open_count());
1464 EXPECT_EQ(1, cache.disk_cache()->create_count());
1465
1466 // All requests depend on the writer, and the writer is between Start and
1467 // Read, i.e. idle.
1468 for (int i = 0; i < kNumTransactions; ++i) {
1469 Context* c = context_list[i];
1470 EXPECT_EQ(LOAD_STATE_IDLE, c->trans->GetLoadState());
1471 }
1472
1473 for (int i = 0; i < kNumTransactions; ++i) {
1474 Context* c = context_list[i];
1475 if (c->result == ERR_IO_PENDING)
1476 c->result = c->callback.WaitForResult();
1477 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction);
1478 }
1479
1480 // We should not have had to re-open the disk entry
1481
1482 EXPECT_EQ(2, cache.network_layer()->transaction_count());
1483 EXPECT_EQ(0, cache.disk_cache()->open_count());
1484 EXPECT_EQ(1, cache.disk_cache()->create_count());
1485
1486 for (int i = 0; i < kNumTransactions; ++i) {
1487 Context* c = context_list[i];
1488 delete c;
1489 }
1490 }
1491
1492 // Tests parallel validation similar to the above test, except here cache write
1493 // fails and the validated transactions should be restarted.
1494 TEST(HttpCache, SimpleGET_ParallelValidationFailWrite) {
1495 MockHttpCache cache;
1496
1497 std::vector<MockHttpRequest> request;
1498 ScopedMockTransaction transaction(kTypicalGET_Transaction);
1499 transaction.response_headers =
1500 "Etag: \"foopy\"\n"
1501 "Cache-Control: max-age=0\n";
1502 MockHttpRequest request0(transaction);
1503 request.push_back(request0);
1504
1505 ScopedMockTransaction transaction1(kTypicalGET_Transaction);
1506 transaction1.status = "HTTP/1.1 304 Not Modified";
1507 transaction1.load_flags |= LOAD_VALIDATE_CACHE;
1508 transaction1.response_headers =
1509 "Etag: \"foopy\"\n"
1510 "Cache-Control: max-age=3600\n";
1511 MockHttpRequest request1(transaction1);
1512 request.push_back(request1);
1513
1514 std::vector<Context*> context_list;
1515 const int kNumTransactions = 2;
1516
1517 for (int i = 0; i < kNumTransactions; ++i) {
1518 context_list.push_back(new Context());
1519 Context* c = context_list[i];
1520
1521 c->result = cache.CreateTransaction(&c->trans);
1522 ASSERT_THAT(c->result, IsOk());
1523 EXPECT_EQ(LOAD_STATE_IDLE, c->trans->GetLoadState());
1524
1525 c->result = c->trans->Start(&request[i], c->callback.callback(),
1526 NetLogWithSource());
1527 }
1528
1529 // All requests are waiting for the active entry.
1530 for (int i = 0; i < kNumTransactions; ++i) {
1531 Context* c = context_list[i];
1532 EXPECT_EQ(LOAD_STATE_WAITING_FOR_CACHE, c->trans->GetLoadState());
1533 }
1534
1535 // Allow all requests to move from the Create queue to the active entry.
1536 base::RunLoop().RunUntilIdle();
1537
1538 // The first request should be a writer at this point, and the subsequent
1539 // requests should have passed the validation phase and waiting for the
1540 // response to be written to the cache before they can read.
1541
1542 EXPECT_EQ(2, cache.network_layer()->transaction_count());
1543 EXPECT_EQ(0, cache.disk_cache()->open_count());
1544 EXPECT_EQ(1, cache.disk_cache()->create_count());
1545
1546 // All requests depend on the writer, and the writer is between Start and
1547 // Read, i.e. idle.
1548 for (int i = 0; i < kNumTransactions; ++i) {
1549 Context* c = context_list[i];
1550 EXPECT_EQ(LOAD_STATE_IDLE, c->trans->GetLoadState());
1551 }
1552
1553 // Fail the request.
1554 cache.disk_cache()->set_soft_failures(true);
1555 // We have to open the entry again to propagate the failure flag.
1556 disk_cache::Entry* en;
1557 cache.OpenBackendEntry(kTypicalGET_Transaction.url, &en);
1558 en->Close();
1559
1560 for (int i = 0; i < kNumTransactions; ++i) {
1561 Context* c = context_list[i];
1562 if (c->result == ERR_IO_PENDING)
1563 c->result = c->callback.WaitForResult();
1564 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction);
1565 }
1566
1567 // We should not have had to re-open the disk entry
1568
1569 // Since validated transaction was restarted, there is an additional network
1570 // transaction created.
1571 EXPECT_EQ(3, cache.network_layer()->transaction_count());
1572 EXPECT_EQ(1, cache.disk_cache()->open_count());
1573 EXPECT_EQ(2, cache.disk_cache()->create_count());
1574
1575 for (int i = 0; i < kNumTransactions; ++i) {
1576 Context* c = context_list[i];
1577 delete c;
1578 }
1579 }
1411 // This is a test for http://code.google.com/p/chromium/issues/detail?id=4769. 1580 // This is a test for http://code.google.com/p/chromium/issues/detail?id=4769.
1412 // If cancelling a request is racing with another request for the same resource 1581 // If cancelling a request is racing with another request for the same resource
1413 // finishing, we have to make sure that we remove both transactions from the 1582 // finishing, we have to make sure that we remove both transactions from the
1414 // entry. 1583 // entry.
1415 TEST(HttpCache, SimpleGET_RacingReaders) { 1584 TEST(HttpCache, SimpleGET_RacingReaders) {
1416 MockHttpCache cache; 1585 MockHttpCache cache;
1417 1586
1418 MockHttpRequest request(kSimpleGET_Transaction); 1587 MockHttpRequest request(kSimpleGET_Transaction);
1419 MockHttpRequest reader_request(kSimpleGET_Transaction); 1588 MockHttpRequest reader_request(kSimpleGET_Transaction);
1420 reader_request.load_flags = LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION; 1589 reader_request.load_flags = LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION;
(...skipping 24 matching lines...) Expand all
1445 1614
1446 EXPECT_EQ(1, cache.network_layer()->transaction_count()); 1615 EXPECT_EQ(1, cache.network_layer()->transaction_count());
1447 EXPECT_EQ(0, cache.disk_cache()->open_count()); 1616 EXPECT_EQ(0, cache.disk_cache()->open_count());
1448 EXPECT_EQ(1, cache.disk_cache()->create_count()); 1617 EXPECT_EQ(1, cache.disk_cache()->create_count());
1449 1618
1450 Context* c = context_list[0]; 1619 Context* c = context_list[0];
1451 ASSERT_THAT(c->result, IsError(ERR_IO_PENDING)); 1620 ASSERT_THAT(c->result, IsError(ERR_IO_PENDING));
1452 c->result = c->callback.WaitForResult(); 1621 c->result = c->callback.WaitForResult();
1453 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction); 1622 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction);
1454 1623
1455 // Now we have 2 active readers and two queued transactions. 1624 // Now all transactions should be waiting for read to be invoked. Two readers
1456 1625 // are because of the load flags and remaining two transactions were converted
1626 // to readers after skipping validation.
1457 EXPECT_EQ(LOAD_STATE_IDLE, context_list[2]->trans->GetLoadState()); 1627 EXPECT_EQ(LOAD_STATE_IDLE, context_list[2]->trans->GetLoadState());
1458 EXPECT_EQ(LOAD_STATE_WAITING_FOR_CACHE, 1628 EXPECT_EQ(LOAD_STATE_IDLE, context_list[3]->trans->GetLoadState());
1459 context_list[3]->trans->GetLoadState());
1460 1629
1461 c = context_list[1]; 1630 c = context_list[1];
1462 ASSERT_THAT(c->result, IsError(ERR_IO_PENDING)); 1631 ASSERT_THAT(c->result, IsError(ERR_IO_PENDING));
1463 c->result = c->callback.WaitForResult(); 1632 c->result = c->callback.WaitForResult();
1464 if (c->result == OK) 1633 if (c->result == OK)
1465 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction); 1634 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction);
1466 1635
1467 // At this point we have one reader, two pending transactions and a task on 1636 // At this point we have one reader, two pending transactions and a task on
1468 // the queue to move to the next transaction. Now we cancel the request that 1637 // the queue to move to the next transaction. Now we cancel the request that
1469 // is the current reader, and expect the queued task to be able to start the 1638 // is the current reader, and expect the queued task to be able to start the
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
1514 ASSERT_THAT(c->result, IsOk()); 1683 ASSERT_THAT(c->result, IsOk());
1515 1684
1516 MockHttpRequest* this_request = &request; 1685 MockHttpRequest* this_request = &request;
1517 if (i == 3) 1686 if (i == 3)
1518 this_request = &writer_request; 1687 this_request = &writer_request;
1519 1688
1520 c->result = c->trans->Start(this_request, c->callback.callback(), 1689 c->result = c->trans->Start(this_request, c->callback.callback(),
1521 NetLogWithSource()); 1690 NetLogWithSource());
1522 } 1691 }
1523 1692
1693 base::RunLoop().RunUntilIdle();
1694
1524 // The first request should be a writer at this point, and the two subsequent 1695 // The first request should be a writer at this point, and the two subsequent
1525 // requests should be pending. The last request doomed the first entry. 1696 // requests should be pending. The last request doomed the first entry.
1526 1697
1527 EXPECT_EQ(2, cache.network_layer()->transaction_count()); 1698 EXPECT_EQ(2, cache.network_layer()->transaction_count());
1528 1699
1529 // Cancel the first queued transaction. 1700 // Cancel the first queued transaction.
1530 context_list[1].reset(); 1701 context_list[1].reset();
1531 1702
1532 for (int i = 0; i < kNumTransactions; ++i) { 1703 for (int i = 0; i < kNumTransactions; ++i) {
1533 if (i == 1) 1704 if (i == 1)
(...skipping 27 matching lines...) Expand all
1561 ASSERT_THAT(c->result, IsOk()); 1732 ASSERT_THAT(c->result, IsOk());
1562 1733
1563 c->result = 1734 c->result =
1564 c->trans->Start(&request, c->callback.callback(), NetLogWithSource()); 1735 c->trans->Start(&request, c->callback.callback(), NetLogWithSource());
1565 } 1736 }
1566 1737
1567 // Allow all requests to move from the Create queue to the active entry. 1738 // Allow all requests to move from the Create queue to the active entry.
1568 base::RunLoop().RunUntilIdle(); 1739 base::RunLoop().RunUntilIdle();
1569 1740
1570 // The first request should be a writer at this point, and the subsequent 1741 // The first request should be a writer at this point, and the subsequent
1571 // requests should be pending. 1742 // requests should have completed validation. Since the validation does not
1743 // result in a match, a new entry would be created.
1572 1744
1573 EXPECT_EQ(1, cache.network_layer()->transaction_count()); 1745 EXPECT_EQ(3, cache.network_layer()->transaction_count());
1574 EXPECT_EQ(0, cache.disk_cache()->open_count()); 1746 EXPECT_EQ(0, cache.disk_cache()->open_count());
1575 EXPECT_EQ(1, cache.disk_cache()->create_count()); 1747 EXPECT_EQ(2, cache.disk_cache()->create_count());
1576 1748
1577 // Now, make sure that the second request asks for the entry not to be stored. 1749 // Now, make sure that the second request asks for the entry not to be stored.
1578 request_handler.set_no_store(true); 1750 request_handler.set_no_store(true);
1579 1751
1580 for (int i = 0; i < kNumTransactions; ++i) { 1752 for (int i = 0; i < kNumTransactions; ++i) {
1581 Context* c = context_list[i]; 1753 Context* c = context_list[i];
1582 if (c->result == ERR_IO_PENDING) 1754 if (c->result == ERR_IO_PENDING)
1583 c->result = c->callback.WaitForResult(); 1755 c->result = c->callback.WaitForResult();
1584 ReadAndVerifyTransaction(c->trans.get(), kFastNoStoreGET_Transaction); 1756 ReadAndVerifyTransaction(c->trans.get(), kFastNoStoreGET_Transaction);
1585 delete c; 1757 delete c;
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
1619 1791
1620 EXPECT_EQ(1, cache.network_layer()->transaction_count()); 1792 EXPECT_EQ(1, cache.network_layer()->transaction_count());
1621 EXPECT_EQ(0, cache.disk_cache()->open_count()); 1793 EXPECT_EQ(0, cache.disk_cache()->open_count());
1622 EXPECT_EQ(1, cache.disk_cache()->create_count()); 1794 EXPECT_EQ(1, cache.disk_cache()->create_count());
1623 1795
1624 for (int i = 0; i < kNumTransactions; ++i) { 1796 for (int i = 0; i < kNumTransactions; ++i) {
1625 Context* c = context_list[i]; 1797 Context* c = context_list[i];
1626 if (c->result == ERR_IO_PENDING) 1798 if (c->result == ERR_IO_PENDING)
1627 c->result = c->callback.WaitForResult(); 1799 c->result = c->callback.WaitForResult();
1628 // Destroy only the first transaction. 1800 // Destroy only the first transaction.
1801 // This should lead to all transactions to restart, even those that have
1802 // validated themselves and were waiting for the writer transaction to
1803 // complete writing to the cache.
1629 if (i == 0) { 1804 if (i == 0) {
1630 delete c; 1805 delete c;
1631 context_list[i] = NULL; 1806 context_list[i] = NULL;
1632 } 1807 }
1633 } 1808 }
1634 1809
1635 // Complete the rest of the transactions. 1810 // Complete the rest of the transactions.
1636 for (int i = 1; i < kNumTransactions; ++i) { 1811 for (int i = 1; i < kNumTransactions; ++i) {
1637 Context* c = context_list[i]; 1812 Context* c = context_list[i];
1638 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction); 1813 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction);
1639 } 1814 }
1640 1815
1641 // We should have had to re-open the disk entry. 1816 // We should have had to re-open the disk entry.
1642 1817
1643 EXPECT_EQ(2, cache.network_layer()->transaction_count()); 1818 EXPECT_EQ(2, cache.network_layer()->transaction_count());
1644 EXPECT_EQ(0, cache.disk_cache()->open_count()); 1819 EXPECT_EQ(0, cache.disk_cache()->open_count());
1645 EXPECT_EQ(2, cache.disk_cache()->create_count()); 1820 EXPECT_EQ(2, cache.disk_cache()->create_count());
1646 1821
1647 for (int i = 1; i < kNumTransactions; ++i) { 1822 for (int i = 1; i < kNumTransactions; ++i) {
1648 Context* c = context_list[i]; 1823 Context* c = context_list[i];
1649 delete c; 1824 delete c;
1650 } 1825 }
1651 } 1826 }
1652 1827
1828 // Tests that a transaction which is in validated queue can be destroyed without
1829 // any impact to other transactions.
1830 TEST(HttpCache, SimpleGET_ManyWriters_CancelValidated) {
1831 MockHttpCache cache;
1832
1833 MockHttpRequest request(kSimpleGET_Transaction);
1834
1835 std::vector<Context*> context_list;
1836 const int kNumTransactions = 2;
1837
1838 for (int i = 0; i < kNumTransactions; ++i) {
1839 context_list.push_back(new Context());
1840 Context* c = context_list[i];
1841
1842 c->result = cache.CreateTransaction(&c->trans);
1843 ASSERT_THAT(c->result, IsOk());
1844
1845 c->result =
1846 c->trans->Start(&request, c->callback.callback(), NetLogWithSource());
1847 }
1848
1849 // Allow all requests to move from the Create queue to the active entry.
1850 base::RunLoop().RunUntilIdle();
1851
1852 // The first request should be a writer at this point, and the subsequent
1853 // requests should be pending.
1854
1855 EXPECT_EQ(1, cache.network_layer()->transaction_count());
1856 EXPECT_EQ(0, cache.disk_cache()->open_count());
1857 EXPECT_EQ(1, cache.disk_cache()->create_count());
1858
1859 for (int i = 0; i < kNumTransactions; ++i) {
1860 Context* c = context_list[i];
1861 if (i == 0 && c->result == ERR_IO_PENDING)
1862 c->result = c->callback.WaitForResult();
1863 // Destroy one of the transactions waiting after the validated stage.
1864 if (i == 1) {
1865 delete c;
1866 context_list[i] = NULL;
1867 }
1868 }
1869
1870 // Complete the rest of the transactions.
1871 for (int i = 0; i < kNumTransactions; ++i) {
1872 if (i == 1)
1873 continue;
1874 Context* c = context_list[i];
1875 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction);
1876 }
1877
1878 // We should have had to re-open the disk entry.
1879
1880 EXPECT_EQ(1, cache.network_layer()->transaction_count());
1881 EXPECT_EQ(0, cache.disk_cache()->open_count());
1882 EXPECT_EQ(1, cache.disk_cache()->create_count());
1883
1884 for (int i = 0; i < kNumTransactions; ++i) {
1885 if (i == 1)
1886 continue;
1887 Context* c = context_list[i];
1888 delete c;
1889 }
1890 }
1891
1892 // Tests that a transaction which is currently validating can be destroyed
1893 // without any impact to other transactions.
1894 TEST(HttpCache, SimpleGET_ManyWriters_CancelValidating) {
1895 MockHttpCache cache;
1896
1897 MockHttpRequest request(kSimpleGET_Transaction);
1898
1899 std::vector<Context*> context_list;
1900 const int kNumTransactions = 2;
1901
1902 for (int i = 0; i < kNumTransactions; ++i) {
1903 context_list.push_back(new Context());
1904 Context* c = context_list[i];
1905
1906 c->result = cache.CreateTransaction(&c->trans);
1907 ASSERT_THAT(c->result, IsOk());
1908
1909 c->result =
1910 c->trans->Start(&request, c->callback.callback(), NetLogWithSource());
1911 }
1912
1913 // Delete the currently validating transaction.
1914 Context* c = context_list[1];
1915 EXPECT_EQ(ERR_IO_PENDING, c->result);
1916 delete c;
1917 context_list[1] = nullptr;
1918
1919 // Allow all requests to move from the Create queue to the active entry.
1920 base::RunLoop().RunUntilIdle();
1921
1922 // The first request should be a writer at this point, and the subsequent
1923 // requests should be pending.
1924
1925 EXPECT_EQ(1, cache.network_layer()->transaction_count());
1926 EXPECT_EQ(0, cache.disk_cache()->open_count());
1927 EXPECT_EQ(1, cache.disk_cache()->create_count());
1928
1929 for (int i = 0; i < kNumTransactions; ++i) {
1930 if (i == 1)
1931 continue;
1932 Context* c = context_list[i];
1933 if (c->result == ERR_IO_PENDING)
1934 c->result = c->callback.WaitForResult();
1935 }
1936
1937 // Complete the rest of the transactions.
1938 for (int i = 0; i < kNumTransactions; ++i) {
1939 if (i == 1)
1940 continue;
1941 Context* c = context_list[i];
1942 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction);
1943 }
1944
1945 // We should have had to re-open the disk entry.
1946
1947 EXPECT_EQ(1, cache.network_layer()->transaction_count());
1948 EXPECT_EQ(0, cache.disk_cache()->open_count());
1949 EXPECT_EQ(1, cache.disk_cache()->create_count());
1950
1951 for (int i = 0; i < kNumTransactions; ++i) {
1952 if (i == 1)
1953 continue;
1954 Context* c = context_list[i];
1955 delete c;
1956 }
1957 }
1958
1653 // Tests that we can cancel requests that are queued waiting to open the disk 1959 // Tests that we can cancel requests that are queued waiting to open the disk
1654 // cache entry. 1960 // cache entry.
1655 TEST(HttpCache, SimpleGET_ManyWriters_CancelCreate) { 1961 TEST(HttpCache, SimpleGET_ManyWriters_CancelCreate) {
1656 MockHttpCache cache; 1962 MockHttpCache cache;
1657 1963
1658 MockHttpRequest request(kSimpleGET_Transaction); 1964 MockHttpRequest request(kSimpleGET_Transaction);
1659 1965
1660 std::vector<Context*> context_list; 1966 std::vector<Context*> context_list;
1661 const int kNumTransactions = 5; 1967 const int kNumTransactions = 5;
1662 1968
(...skipping 871 matching lines...) Expand 10 before | Expand all | Expand 10 after
2534 } 2840 }
2535 2841
2536 // Helper that does 4 requests using HttpCache: 2842 // Helper that does 4 requests using HttpCache:
2537 // 2843 //
2538 // (1) loads |kUrl| -- expects |net_response_1| to be returned. 2844 // (1) loads |kUrl| -- expects |net_response_1| to be returned.
2539 // (2) loads |kUrl| from cache only -- expects |net_response_1| to be returned. 2845 // (2) loads |kUrl| from cache only -- expects |net_response_1| to be returned.
2540 // (3) loads |kUrl| using |extra_request_headers| -- expects |net_response_2| to 2846 // (3) loads |kUrl| using |extra_request_headers| -- expects |net_response_2| to
2541 // be returned. 2847 // be returned.
2542 // (4) loads |kUrl| from cache only -- expects |cached_response_2| to be 2848 // (4) loads |kUrl| from cache only -- expects |cached_response_2| to be
2543 // returned. 2849 // returned.
2850 // The entry will be created once and will be opened for the 3 subsequent
2851 // requests.
2544 static void ConditionalizedRequestUpdatesCacheHelper( 2852 static void ConditionalizedRequestUpdatesCacheHelper(
2545 const Response& net_response_1, 2853 const Response& net_response_1,
2546 const Response& net_response_2, 2854 const Response& net_response_2,
2547 const Response& cached_response_2, 2855 const Response& cached_response_2,
2548 const char* extra_request_headers) { 2856 const char* extra_request_headers) {
2549 MockHttpCache cache; 2857 MockHttpCache cache;
2550 2858
2551 // The URL we will be requesting. 2859 // The URL we will be requesting.
2552 const char kUrl[] = "http://foobar.com/main.css"; 2860 const char kUrl[] = "http://foobar.com/main.css";
2553 2861
(...skipping 3555 matching lines...) Expand 10 before | Expand all | Expand 10 after
6109 pending->trans->Start(&request, pending->callback.callback(), 6417 pending->trans->Start(&request, pending->callback.callback(),
6110 NetLogWithSource())); 6418 NetLogWithSource()));
6111 EXPECT_THAT(c->callback.GetResult(rv), IsOk()); 6419 EXPECT_THAT(c->callback.GetResult(rv), IsOk());
6112 6420
6113 // Make sure that the entry has some data stored. 6421 // Make sure that the entry has some data stored.
6114 scoped_refptr<IOBufferWithSize> buf(new IOBufferWithSize(5)); 6422 scoped_refptr<IOBufferWithSize> buf(new IOBufferWithSize(5));
6115 rv = c->trans->Read(buf.get(), buf->size(), c->callback.callback()); 6423 rv = c->trans->Read(buf.get(), buf->size(), c->callback.callback());
6116 EXPECT_EQ(5, c->callback.GetResult(rv)); 6424 EXPECT_EQ(5, c->callback.GetResult(rv));
6117 6425
6118 // Cancel the requests. 6426 // Cancel the requests.
6427 // Since |pending| is currently vaidating the already written headers
6428 // it will be restarted as well.
6119 delete c; 6429 delete c;
6120 delete pending; 6430 delete pending;
6121 6431
6122 EXPECT_EQ(1, cache.network_layer()->transaction_count()); 6432 EXPECT_EQ(1, cache.network_layer()->transaction_count());
6123 EXPECT_EQ(1, cache.disk_cache()->open_count()); 6433 EXPECT_EQ(1, cache.disk_cache()->open_count());
6124 EXPECT_EQ(2, cache.disk_cache()->create_count()); 6434 EXPECT_EQ(1, cache.disk_cache()->create_count());
6125 6435
6126 base::RunLoop().RunUntilIdle(); 6436 base::RunLoop().RunUntilIdle();
6127 RemoveMockTransaction(&transaction); 6437 RemoveMockTransaction(&transaction);
6128 } 6438 }
6129 6439
6130 // Tests that we delete truncated entries if the server changes its mind midway. 6440 // Tests that we delete truncated entries if the server changes its mind midway.
6131 TEST(HttpCache, GET_IncompleteResource2) { 6441 TEST(HttpCache, GET_IncompleteResource2) {
6132 MockHttpCache cache; 6442 MockHttpCache cache;
6133 AddMockTransaction(&kRangeGET_TransactionOK); 6443 AddMockTransaction(&kRangeGET_TransactionOK);
6134 6444
(...skipping 704 matching lines...) Expand 10 before | Expand all | Expand 10 after
6839 7149
6840 RunTransactionTestWithResponseInfo(cache.http_cache(), kSimpleGET_Transaction, 7150 RunTransactionTestWithResponseInfo(cache.http_cache(), kSimpleGET_Transaction,
6841 &response); 7151 &response);
6842 EXPECT_TRUE(response.metadata.get() == NULL); 7152 EXPECT_TRUE(response.metadata.get() == NULL);
6843 7153
6844 EXPECT_EQ(1, cache.network_layer()->transaction_count()); 7154 EXPECT_EQ(1, cache.network_layer()->transaction_count());
6845 EXPECT_EQ(2, cache.disk_cache()->open_count()); 7155 EXPECT_EQ(2, cache.disk_cache()->open_count());
6846 EXPECT_EQ(1, cache.disk_cache()->create_count()); 7156 EXPECT_EQ(1, cache.disk_cache()->create_count());
6847 } 7157 }
6848 7158
6849 // Tests that if a metadata writer transaction hits cache lock timeout, it will
6850 // error out.
6851 TEST(HttpCache, WriteMetadata_CacheLockTimeout) {
6852 MockHttpCache cache;
6853
6854 // Write to the cache
6855 HttpResponseInfo response;
6856 RunTransactionTestWithResponseInfo(cache.http_cache(), kSimpleGET_Transaction,
6857 &response);
6858 EXPECT_FALSE(response.metadata.get());
6859
6860 MockHttpRequest request(kSimpleGET_Transaction);
6861 Context c1;
6862 ASSERT_THAT(cache.CreateTransaction(&c1.trans), IsOk());
6863 ASSERT_EQ(ERR_IO_PENDING, c1.trans->Start(&request, c1.callback.callback(),
6864 NetLogWithSource()));
6865
6866 cache.SimulateCacheLockTimeout();
6867
6868 // Write meta data to the same entry.
6869 scoped_refptr<IOBufferWithSize> buf(new IOBufferWithSize(50));
6870 memset(buf->data(), 0, buf->size());
6871 base::strlcpy(buf->data(), "Hi there", buf->size());
6872 cache.http_cache()->WriteMetadata(GURL(kSimpleGET_Transaction.url),
6873 DEFAULT_PRIORITY, response.response_time,
6874 buf.get(), buf->size());
6875
6876 // Release the buffer before the operation takes place.
6877 buf = NULL;
6878
6879 // Makes sure we finish pending operations.
6880 base::RunLoop().RunUntilIdle();
6881
6882 RunTransactionTestWithResponseInfo(cache.http_cache(), kSimpleGET_Transaction,
6883 &response);
6884
6885 // The writer transaction should fail due to cache lock timeout.
6886 ASSERT_FALSE(response.metadata.get());
6887 }
6888
6889 // Tests that we ignore VARY checks when writing metadata since the request 7159 // Tests that we ignore VARY checks when writing metadata since the request
6890 // headers for the WriteMetadata transaction are made up. 7160 // headers for the WriteMetadata transaction are made up.
6891 TEST(HttpCache, WriteMetadata_IgnoreVary) { 7161 TEST(HttpCache, WriteMetadata_IgnoreVary) {
6892 MockHttpCache cache; 7162 MockHttpCache cache;
6893 7163
6894 // Write to the cache 7164 // Write to the cache
6895 HttpResponseInfo response; 7165 HttpResponseInfo response;
6896 ScopedMockTransaction transaction(kSimpleGET_Transaction); 7166 ScopedMockTransaction transaction(kSimpleGET_Transaction);
6897 transaction.request_headers = "accept-encoding: gzip\r\n"; 7167 transaction.request_headers = "accept-encoding: gzip\r\n";
6898 transaction.response_headers = 7168 transaction.response_headers =
(...skipping 1504 matching lines...) Expand 10 before | Expand all | Expand 10 after
8403 ASSERT_TRUE(attrs->GetDictionary( 8673 ASSERT_TRUE(attrs->GetDictionary(
8404 base::trace_event::MemoryAllocatorDump::kNameSize, &size_attrs)); 8674 base::trace_event::MemoryAllocatorDump::kNameSize, &size_attrs));
8405 std::string size; 8675 std::string size;
8406 ASSERT_TRUE(size_attrs->GetString("value", &size)); 8676 ASSERT_TRUE(size_attrs->GetString("value", &size));
8407 int actual_size = 0; 8677 int actual_size = 0;
8408 ASSERT_TRUE(base::HexStringToInt(size, &actual_size)); 8678 ASSERT_TRUE(base::HexStringToInt(size, &actual_size));
8409 ASSERT_LT(0, actual_size); 8679 ASSERT_LT(0, actual_size);
8410 } 8680 }
8411 8681
8412 } // namespace net 8682 } // namespace net
OLDNEW
« net/http/http_cache_transaction.cc ('K') | « net/http/http_cache_transaction.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698