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

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

Issue 2721933002: HttpCache::Transaction layer allowing parallel validation (Closed)
Patch Set: Rebased till refs/heads/master@{#462134} Created 3 years, 8 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 1357 matching lines...) Expand 10 before | Expand all | Expand 10 after
1368 // All requests are waiting for the active entry. 1368 // All requests are waiting for the active entry.
1369 for (int i = 0; i < kNumTransactions; ++i) { 1369 for (int i = 0; i < kNumTransactions; ++i) {
1370 Context* c = context_list[i]; 1370 Context* c = context_list[i];
1371 EXPECT_EQ(LOAD_STATE_WAITING_FOR_CACHE, c->trans->GetLoadState()); 1371 EXPECT_EQ(LOAD_STATE_WAITING_FOR_CACHE, c->trans->GetLoadState());
1372 } 1372 }
1373 1373
1374 // Allow all requests to move from the Create queue to the active entry. 1374 // Allow all requests to move from the Create queue to the active entry.
1375 base::RunLoop().RunUntilIdle(); 1375 base::RunLoop().RunUntilIdle();
1376 1376
1377 // The first request should be a writer at this point, and the subsequent 1377 // The first request should be a writer at this point, and the subsequent
1378 // requests should be pending. 1378 // requests should have passed the validation phase and waiting for the
1379 // response to be written to the cache before they can read.
1380 EXPECT_TRUE(cache.IsWriterPresent(kSimpleGET_Transaction.url));
1381 EXPECT_EQ(4, cache.GetCountDoneHeadersQueue(kSimpleGET_Transaction.url));
1379 1382
1380 EXPECT_EQ(1, cache.network_layer()->transaction_count()); 1383 EXPECT_EQ(1, cache.network_layer()->transaction_count());
1381 EXPECT_EQ(0, cache.disk_cache()->open_count()); 1384 EXPECT_EQ(0, cache.disk_cache()->open_count());
1382 EXPECT_EQ(1, cache.disk_cache()->create_count()); 1385 EXPECT_EQ(1, cache.disk_cache()->create_count());
1383 1386
1384 // All requests depend on the writer, and the writer is between Start and 1387 // All requests depend on the writer, and the writer is between Start and
1385 // Read, i.e. idle. 1388 // Read, i.e. idle.
1386 for (int i = 0; i < kNumTransactions; ++i) { 1389 for (int i = 0; i < kNumTransactions; ++i) {
1387 Context* c = context_list[i]; 1390 Context* c = context_list[i];
1388 EXPECT_EQ(LOAD_STATE_IDLE, c->trans->GetLoadState()); 1391 EXPECT_EQ(LOAD_STATE_IDLE, c->trans->GetLoadState());
1389 } 1392 }
1390 1393
1391 for (int i = 0; i < kNumTransactions; ++i) { 1394 for (int i = 0; i < kNumTransactions; ++i) {
1392 Context* c = context_list[i]; 1395 Context* c = context_list[i];
1393 if (c->result == ERR_IO_PENDING) 1396 if (c->result == ERR_IO_PENDING)
1394 c->result = c->callback.WaitForResult(); 1397 c->result = c->callback.WaitForResult();
1398
1399 if (i == 1) {
1400 EXPECT_FALSE(cache.IsWriterPresent(kSimpleGET_Transaction.url));
1401 EXPECT_EQ(4, cache.GetCountReaders(kSimpleGET_Transaction.url));
1402 }
1403
1395 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction); 1404 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction);
1396 } 1405 }
1397 1406
1398 // We should not have had to re-open the disk entry 1407 // We should not have had to re-open the disk entry
1399 1408
1400 EXPECT_EQ(1, cache.network_layer()->transaction_count()); 1409 EXPECT_EQ(1, cache.network_layer()->transaction_count());
1401 EXPECT_EQ(0, cache.disk_cache()->open_count()); 1410 EXPECT_EQ(0, cache.disk_cache()->open_count());
1402 EXPECT_EQ(1, cache.disk_cache()->create_count()); 1411 EXPECT_EQ(1, cache.disk_cache()->create_count());
1403 1412
1404 for (int i = 0; i < kNumTransactions; ++i) { 1413 for (int i = 0; i < kNumTransactions; ++i) {
1405 Context* c = context_list[i]; 1414 Context* c = context_list[i];
1406 delete c; 1415 delete c;
1407 } 1416 }
1408 } 1417 }
1409 1418
1419 // Parallel validation results in 200.
1420 TEST(HttpCache, SimpleGET_ParallelValidationNoMatch) {
1421 MockHttpCache cache;
1422
1423 MockHttpRequest request(kSimpleGET_Transaction);
1424 request.load_flags |= LOAD_VALIDATE_CACHE;
1425
1426 std::vector<Context*> context_list;
1427 const int kNumTransactions = 5;
1428
1429 for (int i = 0; i < kNumTransactions; ++i) {
1430 context_list.push_back(new Context());
1431 Context* c = context_list[i];
1432
1433 c->result = cache.CreateTransaction(&c->trans);
1434 ASSERT_THAT(c->result, IsOk());
1435 EXPECT_EQ(LOAD_STATE_IDLE, c->trans->GetLoadState());
1436
1437 c->result =
1438 c->trans->Start(&request, c->callback.callback(), NetLogWithSource());
1439 }
1440
1441 // All requests are waiting for the active entry.
1442 for (int i = 0; i < kNumTransactions; ++i) {
1443 Context* c = context_list[i];
1444 EXPECT_EQ(LOAD_STATE_WAITING_FOR_CACHE, c->trans->GetLoadState());
1445 }
1446
1447 // Allow all requests to move from the Create queue to the active entry.
1448 base::RunLoop().RunUntilIdle();
1449
1450 // The first request should be a writer at this point, and the subsequent
1451 // requests should have passed the validation phase and created their own
1452 // entries since none of them matched the headers of the earlier one.
1453 EXPECT_TRUE(cache.IsWriterPresent(kSimpleGET_Transaction.url));
1454
1455 EXPECT_EQ(5, cache.network_layer()->transaction_count());
1456 EXPECT_EQ(0, cache.disk_cache()->open_count());
1457 EXPECT_EQ(3, cache.disk_cache()->create_count());
1458
1459 // All requests depend on the writer, and the writer is between Start and
1460 // Read, i.e. idle.
1461 for (int i = 0; i < kNumTransactions; ++i) {
1462 Context* c = context_list[i];
1463 EXPECT_EQ(LOAD_STATE_IDLE, c->trans->GetLoadState());
1464 }
1465
1466 for (int i = 0; i < kNumTransactions; ++i) {
1467 Context* c = context_list[i];
1468 if (c->result == ERR_IO_PENDING)
1469 c->result = c->callback.WaitForResult();
1470 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction);
1471 }
1472
1473 EXPECT_EQ(5, cache.network_layer()->transaction_count());
1474 EXPECT_EQ(0, cache.disk_cache()->open_count());
1475 EXPECT_EQ(3, cache.disk_cache()->create_count());
1476
1477 for (int i = 0; i < kNumTransactions; ++i) {
1478 Context* c = context_list[i];
1479 delete c;
1480 }
1481 }
1482
1483 // Tests that a GET followed by a DELETE results in DELETE immediately starting
1484 // the headers phase and the entry is doomed.
1485 TEST(HttpCache, SimpleGET_ParallelValidationDelete) {
1486 MockHttpCache cache;
1487
1488 MockHttpRequest request(kSimpleGET_Transaction);
1489 request.load_flags |= LOAD_VALIDATE_CACHE;
1490
1491 MockHttpRequest delete_request(kSimpleGET_Transaction);
1492 delete_request.method = "DELETE";
1493
1494 std::vector<Context*> context_list;
1495 const int kNumTransactions = 2;
1496
1497 for (int i = 0; i < kNumTransactions; ++i) {
1498 context_list.push_back(new Context());
1499 Context* c = context_list[i];
1500
1501 MockHttpRequest* this_request = &request;
1502 if (i == 1)
1503 this_request = &delete_request;
1504
1505 c->result = cache.CreateTransaction(&c->trans);
1506 ASSERT_THAT(c->result, IsOk());
1507 EXPECT_EQ(LOAD_STATE_IDLE, c->trans->GetLoadState());
1508
1509 c->result = c->trans->Start(this_request, c->callback.callback(),
1510 NetLogWithSource());
1511 }
1512
1513 // All requests are waiting for the active entry.
1514 for (int i = 0; i < kNumTransactions; ++i) {
1515 Context* c = context_list[i];
1516 EXPECT_EQ(LOAD_STATE_WAITING_FOR_CACHE, c->trans->GetLoadState());
1517 }
1518
1519 // Allow all requests to move from the Create queue to the active entry.
1520 base::RunLoop().RunUntilIdle();
1521
1522 // The first request should be a writer at this point, and the subsequent
1523 // request should have passed the validation phase and doomed the existing
1524 // entry.
1525 EXPECT_TRUE(
1526 cache.disk_cache()->IsDiskEntryDoomed(kSimpleGET_Transaction.url));
1527
1528 EXPECT_EQ(2, cache.network_layer()->transaction_count());
1529 EXPECT_EQ(0, cache.disk_cache()->open_count());
1530 EXPECT_EQ(1, cache.disk_cache()->create_count());
1531
1532 // All requests depend on the writer, and the writer is between Start and
1533 // Read, i.e. idle.
1534 for (int i = 0; i < kNumTransactions; ++i) {
1535 Context* c = context_list[i];
1536 EXPECT_EQ(LOAD_STATE_IDLE, c->trans->GetLoadState());
1537 }
1538
1539 for (int i = 0; i < kNumTransactions; ++i) {
1540 Context* c = context_list[i];
1541 if (c->result == ERR_IO_PENDING)
1542 c->result = c->callback.WaitForResult();
1543 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction);
1544 }
1545
1546 EXPECT_EQ(2, cache.network_layer()->transaction_count());
1547 EXPECT_EQ(0, cache.disk_cache()->open_count());
1548 EXPECT_EQ(1, cache.disk_cache()->create_count());
1549
1550 for (int i = 0; i < kNumTransactions; ++i) {
1551 Context* c = context_list[i];
1552 delete c;
1553 }
1554 }
1555
1556 // Tests that a transaction which is in validated queue can be destroyed without
1557 // any impact to other transactions.
1558 TEST(HttpCache, SimpleGET_ParallelValidationCancelValidated) {
1559 MockHttpCache cache;
1560
1561 MockHttpRequest request(kSimpleGET_Transaction);
1562
1563 std::vector<Context*> context_list;
1564 const int kNumTransactions = 2;
1565
1566 for (int i = 0; i < kNumTransactions; ++i) {
1567 context_list.push_back(new Context());
1568 Context* c = context_list[i];
1569
1570 c->result = cache.CreateTransaction(&c->trans);
1571 ASSERT_THAT(c->result, IsOk());
1572
1573 c->result =
1574 c->trans->Start(&request, c->callback.callback(), NetLogWithSource());
1575 }
1576
1577 // Allow all requests to move from the Create queue to the active entry.
1578 base::RunLoop().RunUntilIdle();
1579
1580 // The first request should be a writer at this point, and the subsequent
1581 // requests should have completed validation.
1582
1583 EXPECT_EQ(1, cache.network_layer()->transaction_count());
1584 EXPECT_EQ(0, cache.disk_cache()->open_count());
1585 EXPECT_EQ(1, cache.disk_cache()->create_count());
1586
1587 EXPECT_TRUE(cache.IsWriterPresent(kSimpleGET_Transaction.url));
1588 EXPECT_EQ(1, cache.GetCountDoneHeadersQueue(kSimpleGET_Transaction.url));
1589
1590 Context* c = context_list[1];
1591 delete c;
1592 context_list[1] = nullptr;
1593
1594 EXPECT_EQ(0, cache.GetCountDoneHeadersQueue(kSimpleGET_Transaction.url));
1595
1596 // Complete the rest of the transactions.
1597 for (int i = 0; i < kNumTransactions; ++i) {
1598 if (i == 1)
1599 continue;
1600 Context* c = context_list[i];
1601 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction);
1602 }
1603
1604 EXPECT_EQ(1, cache.network_layer()->transaction_count());
1605 EXPECT_EQ(0, cache.disk_cache()->open_count());
1606 EXPECT_EQ(1, cache.disk_cache()->create_count());
1607
1608 for (int i = 0; i < kNumTransactions; ++i) {
1609 if (i == 1)
1610 continue;
1611 Context* c = context_list[i];
1612 delete c;
1613 }
1614 }
1615
1616 // Tests that a transaction which is in readers can be destroyed without
1617 // any impact to other transactions.
1618 TEST(HttpCache, SimpleGET_ParallelValidationCancelReader) {
1619 MockHttpCache cache;
1620
1621 MockHttpRequest request(kSimpleGET_Transaction);
1622
1623 std::vector<Context*> context_list;
1624 int kNumTransactions = 3;
1625
1626 for (int i = 0; i < kNumTransactions; ++i) {
1627 context_list.push_back(new Context());
1628 Context* c = context_list[i];
1629
1630 c->result = cache.CreateTransaction(&c->trans);
1631 ASSERT_THAT(c->result, IsOk());
1632
1633 c->result =
1634 c->trans->Start(&request, c->callback.callback(), NetLogWithSource());
1635 }
1636
1637 // Allow all requests to move from the Create queue to the active entry.
1638 base::RunLoop().RunUntilIdle();
1639
1640 // The first request should be a writer at this point, and the subsequent
1641 // requests should have completed validation.
1642
1643 EXPECT_EQ(1, cache.network_layer()->transaction_count());
1644 EXPECT_EQ(0, cache.disk_cache()->open_count());
1645 EXPECT_EQ(1, cache.disk_cache()->create_count());
1646
1647 EXPECT_TRUE(cache.IsWriterPresent(kSimpleGET_Transaction.url));
1648 EXPECT_EQ(2, cache.GetCountDoneHeadersQueue(kSimpleGET_Transaction.url));
1649
1650 // Complete the response body.
1651 Context* c = context_list[0];
1652 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction);
1653
1654 // Rest of the transactions should move to readers.
1655 EXPECT_FALSE(cache.IsWriterPresent(kSimpleGET_Transaction.url));
1656 EXPECT_EQ(2, cache.GetCountReaders(kSimpleGET_Transaction.url));
1657 EXPECT_EQ(0, cache.GetCountDoneHeadersQueue(kSimpleGET_Transaction.url));
1658
1659 // Add 2 new transactions.
1660 kNumTransactions = 5;
1661
1662 for (int i = 3; i < kNumTransactions; ++i) {
1663 context_list.push_back(new Context());
1664 Context* c = context_list[i];
1665
1666 c->result = cache.CreateTransaction(&c->trans);
1667 ASSERT_THAT(c->result, IsOk());
1668
1669 c->result =
1670 c->trans->Start(&request, c->callback.callback(), NetLogWithSource());
1671 }
1672
1673 EXPECT_EQ(2, cache.GetCountAddToEntryQueue(kSimpleGET_Transaction.url));
1674
1675 // Delete a reader.
1676 c = context_list[1];
1677 delete c;
1678 context_list[1] = nullptr;
1679
1680 EXPECT_EQ(1, cache.GetCountReaders(kSimpleGET_Transaction.url));
1681
1682 // Allow all requests to move from the Create queue to the active entry.
1683 base::RunLoop().RunUntilIdle();
1684
1685 EXPECT_EQ(3, cache.GetCountReaders(kSimpleGET_Transaction.url));
1686
1687 // Complete the rest of the transactions.
1688 for (int i = 2; i < kNumTransactions; ++i) {
1689 Context* c = context_list[i];
1690 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction);
1691 }
1692
1693 EXPECT_EQ(1, cache.network_layer()->transaction_count());
1694 EXPECT_EQ(0, cache.disk_cache()->open_count());
1695 EXPECT_EQ(1, cache.disk_cache()->create_count());
1696
1697 for (int i = 0; i < kNumTransactions; ++i) {
1698 if (i == 1)
1699 continue;
1700 Context* c = context_list[i];
1701 delete c;
1702 }
1703 }
1704
1705 // Tests that a transaction is in validated queue and writer is destroyed
1706 // leading to restarting the validated transaction.
1707 TEST(HttpCache, SimpleGET_ParallelValidationCancelWriter) {
1708 MockHttpCache cache;
1709
1710 MockHttpRequest request(kSimpleGET_Transaction);
1711
1712 std::vector<Context*> context_list;
1713 const int kNumTransactions = 2;
1714
1715 for (int i = 0; i < kNumTransactions; ++i) {
1716 context_list.push_back(new Context());
1717 Context* c = context_list[i];
1718
1719 c->result = cache.CreateTransaction(&c->trans);
1720 ASSERT_THAT(c->result, IsOk());
1721
1722 c->result =
1723 c->trans->Start(&request, c->callback.callback(), NetLogWithSource());
1724 }
1725
1726 // Allow all requests to move from the Create queue to the active entry.
1727 base::RunLoop().RunUntilIdle();
1728
1729 // The first request should be a writer at this point, and the subsequent
1730 // requests should have completed validation.
1731
1732 EXPECT_EQ(1, cache.network_layer()->transaction_count());
1733 EXPECT_EQ(0, cache.disk_cache()->open_count());
1734 EXPECT_EQ(1, cache.disk_cache()->create_count());
1735
1736 EXPECT_TRUE(cache.IsWriterPresent(kSimpleGET_Transaction.url));
1737 EXPECT_EQ(1, cache.GetCountDoneHeadersQueue(kSimpleGET_Transaction.url));
1738
1739 Context* c = context_list[0];
1740 delete c;
1741 context_list[0] = nullptr;
1742
1743 base::RunLoop().RunUntilIdle();
1744
1745 // Complete the rest of the transactions.
1746 for (int i = 0; i < kNumTransactions; ++i) {
1747 if (i == 0)
1748 continue;
1749 Context* c = context_list[i];
1750 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction);
1751 }
1752
1753 EXPECT_EQ(2, cache.network_layer()->transaction_count());
1754 EXPECT_EQ(0, cache.disk_cache()->open_count());
1755 EXPECT_EQ(2, cache.disk_cache()->create_count());
1756
1757 for (int i = 0; i < kNumTransactions; ++i) {
1758 if (i == 1)
1759 continue;
1760 Context* c = context_list[i];
1761 delete c;
1762 }
1763 }
1764
1765 // Similar to the above test, except here cache write fails and the
1766 // validated transactions should be restarted.
1767 TEST(HttpCache, SimpleGET_ParallelValidationFailWrite) {
1768 MockHttpCache cache;
1769
1770 MockHttpRequest request(kSimpleGET_Transaction);
1771
1772 std::vector<Context*> context_list;
1773 const int kNumTransactions = 5;
1774
1775 for (int i = 0; i < kNumTransactions; ++i) {
1776 context_list.push_back(new Context());
1777 Context* c = context_list[i];
1778
1779 c->result = cache.CreateTransaction(&c->trans);
1780 ASSERT_THAT(c->result, IsOk());
1781 EXPECT_EQ(LOAD_STATE_IDLE, c->trans->GetLoadState());
1782
1783 c->result =
1784 c->trans->Start(&request, c->callback.callback(), NetLogWithSource());
1785 }
1786
1787 // All requests are waiting for the active entry.
1788 for (int i = 0; i < kNumTransactions; ++i) {
1789 Context* c = context_list[i];
1790 EXPECT_EQ(LOAD_STATE_WAITING_FOR_CACHE, c->trans->GetLoadState());
1791 }
1792
1793 // Allow all requests to move from the Create queue to the active entry.
1794 base::RunLoop().RunUntilIdle();
1795
1796 // The first request should be a writer at this point, and the subsequent
1797 // requests should have passed the validation phase and waiting for the
1798 // response to be written to the cache before they can read.
1799 EXPECT_TRUE(cache.IsWriterPresent(kSimpleGET_Transaction.url));
1800 EXPECT_EQ(4, cache.GetCountDoneHeadersQueue(kSimpleGET_Transaction.url));
1801
1802 // All requests depend on the writer, and the writer is between Start and
1803 // Read, i.e. idle.
1804 for (int i = 0; i < kNumTransactions; ++i) {
1805 Context* c = context_list[i];
1806 EXPECT_EQ(LOAD_STATE_IDLE, c->trans->GetLoadState());
1807 }
1808
1809 // The first request should be a writer at this point, and the subsequent
1810 // requests should have passed the validation phase and waiting for the
1811 // response to be written to the cache before they can read.
1812
1813 EXPECT_EQ(1, cache.network_layer()->transaction_count());
1814 EXPECT_EQ(0, cache.disk_cache()->open_count());
1815 EXPECT_EQ(1, cache.disk_cache()->create_count());
1816
1817 // Fail the request.
1818 cache.disk_cache()->set_soft_failures(true);
1819 // We have to open the entry again to propagate the failure flag.
1820 disk_cache::Entry* en;
1821 cache.OpenBackendEntry(kSimpleGET_Transaction.url, &en);
1822 en->Close();
1823
1824 for (int i = 0; i < kNumTransactions; ++i) {
1825 Context* c = context_list[i];
1826 if (c->result == ERR_IO_PENDING)
1827 c->result = c->callback.WaitForResult();
1828 if (i == 1) {
1829 // The earlier entry must be destroyed and its disk entry doomed.
1830 EXPECT_TRUE(
1831 cache.disk_cache()->IsDiskEntryDoomed(kSimpleGET_Transaction.url));
1832 }
1833 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction);
1834 }
1835
1836 // Since validated transactions were restarted and new entry read/write
1837 // operations would also fail, all requests would have gone to the network.
1838 EXPECT_EQ(5, cache.network_layer()->transaction_count());
1839 EXPECT_EQ(1, cache.disk_cache()->open_count());
1840 EXPECT_EQ(5, cache.disk_cache()->create_count());
1841
1842 for (int i = 0; i < kNumTransactions; ++i) {
1843 Context* c = context_list[i];
1844 delete c;
1845 }
1846 }
1847
1410 // This is a test for http://code.google.com/p/chromium/issues/detail?id=4769. 1848 // This is a test for http://code.google.com/p/chromium/issues/detail?id=4769.
1411 // If cancelling a request is racing with another request for the same resource 1849 // If cancelling a request is racing with another request for the same resource
1412 // finishing, we have to make sure that we remove both transactions from the 1850 // finishing, we have to make sure that we remove both transactions from the
1413 // entry. 1851 // entry.
1414 TEST(HttpCache, SimpleGET_RacingReaders) { 1852 TEST(HttpCache, SimpleGET_RacingReaders) {
1415 MockHttpCache cache; 1853 MockHttpCache cache;
1416 1854
1417 MockHttpRequest request(kSimpleGET_Transaction); 1855 MockHttpRequest request(kSimpleGET_Transaction);
1418 MockHttpRequest reader_request(kSimpleGET_Transaction); 1856 MockHttpRequest reader_request(kSimpleGET_Transaction);
1419 reader_request.load_flags = LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION; 1857 reader_request.load_flags = LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION;
(...skipping 24 matching lines...) Expand all
1444 1882
1445 EXPECT_EQ(1, cache.network_layer()->transaction_count()); 1883 EXPECT_EQ(1, cache.network_layer()->transaction_count());
1446 EXPECT_EQ(0, cache.disk_cache()->open_count()); 1884 EXPECT_EQ(0, cache.disk_cache()->open_count());
1447 EXPECT_EQ(1, cache.disk_cache()->create_count()); 1885 EXPECT_EQ(1, cache.disk_cache()->create_count());
1448 1886
1449 Context* c = context_list[0]; 1887 Context* c = context_list[0];
1450 ASSERT_THAT(c->result, IsError(ERR_IO_PENDING)); 1888 ASSERT_THAT(c->result, IsError(ERR_IO_PENDING));
1451 c->result = c->callback.WaitForResult(); 1889 c->result = c->callback.WaitForResult();
1452 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction); 1890 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction);
1453 1891
1454 // Now we have 2 active readers and two queued transactions. 1892 // Now all transactions should be waiting for read to be invoked. Two readers
1455 1893 // are because of the load flags and remaining two transactions were converted
1894 // to readers after skipping validation. Note that the remaining two went on
1895 // to process the headers in parallel with readers presnt on the entry.
1456 EXPECT_EQ(LOAD_STATE_IDLE, context_list[2]->trans->GetLoadState()); 1896 EXPECT_EQ(LOAD_STATE_IDLE, context_list[2]->trans->GetLoadState());
1457 EXPECT_EQ(LOAD_STATE_WAITING_FOR_CACHE, 1897 EXPECT_EQ(LOAD_STATE_IDLE, context_list[3]->trans->GetLoadState());
1458 context_list[3]->trans->GetLoadState());
1459 1898
1460 c = context_list[1]; 1899 c = context_list[1];
1461 ASSERT_THAT(c->result, IsError(ERR_IO_PENDING)); 1900 ASSERT_THAT(c->result, IsError(ERR_IO_PENDING));
1462 c->result = c->callback.WaitForResult(); 1901 c->result = c->callback.WaitForResult();
1463 if (c->result == OK) 1902 if (c->result == OK)
1464 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction); 1903 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction);
1465 1904
1466 // At this point we have one reader, two pending transactions and a task on 1905 // At this point we have one reader, two pending transactions and a task on
1467 // the queue to move to the next transaction. Now we cancel the request that 1906 // the queue to move to the next transaction. Now we cancel the request that
1468 // is the current reader, and expect the queued task to be able to start the 1907 // 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
1513 ASSERT_THAT(c->result, IsOk()); 1952 ASSERT_THAT(c->result, IsOk());
1514 1953
1515 MockHttpRequest* this_request = &request; 1954 MockHttpRequest* this_request = &request;
1516 if (i == 3) 1955 if (i == 3)
1517 this_request = &writer_request; 1956 this_request = &writer_request;
1518 1957
1519 c->result = c->trans->Start(this_request, c->callback.callback(), 1958 c->result = c->trans->Start(this_request, c->callback.callback(),
1520 NetLogWithSource()); 1959 NetLogWithSource());
1521 } 1960 }
1522 1961
1962 base::RunLoop().RunUntilIdle();
1963
1523 // The first request should be a writer at this point, and the two subsequent 1964 // The first request should be a writer at this point, and the two subsequent
1524 // requests should be pending. The last request doomed the first entry. 1965 // requests should be pending. The last request doomed the first entry.
1525 1966
1526 EXPECT_EQ(2, cache.network_layer()->transaction_count()); 1967 EXPECT_EQ(2, cache.network_layer()->transaction_count());
1527 1968
1528 // Cancel the first queued transaction. 1969 // Cancel the second transaction. Note that this and the 3rd transactions
1970 // would have completed their headers phase and would be waiting in the
1971 // done_headers_queue when the 2nd transaction is cancelled.
1529 context_list[1].reset(); 1972 context_list[1].reset();
1530 1973
1531 for (int i = 0; i < kNumTransactions; ++i) { 1974 for (int i = 0; i < kNumTransactions; ++i) {
1532 if (i == 1) 1975 if (i == 1)
1533 continue; 1976 continue;
1534 Context* c = context_list[i].get(); 1977 Context* c = context_list[i].get();
1535 ASSERT_THAT(c->result, IsError(ERR_IO_PENDING)); 1978 ASSERT_THAT(c->result, IsError(ERR_IO_PENDING));
1536 c->result = c->callback.WaitForResult(); 1979 c->result = c->callback.WaitForResult();
1537 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction); 1980 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction);
1538 } 1981 }
(...skipping 21 matching lines...) Expand all
1560 ASSERT_THAT(c->result, IsOk()); 2003 ASSERT_THAT(c->result, IsOk());
1561 2004
1562 c->result = 2005 c->result =
1563 c->trans->Start(&request, c->callback.callback(), NetLogWithSource()); 2006 c->trans->Start(&request, c->callback.callback(), NetLogWithSource());
1564 } 2007 }
1565 2008
1566 // Allow all requests to move from the Create queue to the active entry. 2009 // Allow all requests to move from the Create queue to the active entry.
1567 base::RunLoop().RunUntilIdle(); 2010 base::RunLoop().RunUntilIdle();
1568 2011
1569 // The first request should be a writer at this point, and the subsequent 2012 // The first request should be a writer at this point, and the subsequent
1570 // requests should be pending. 2013 // requests should have completed validation. Since the validation does not
2014 // result in a match, a new entry would be created.
1571 2015
1572 EXPECT_EQ(1, cache.network_layer()->transaction_count()); 2016 EXPECT_EQ(3, cache.network_layer()->transaction_count());
1573 EXPECT_EQ(0, cache.disk_cache()->open_count()); 2017 EXPECT_EQ(0, cache.disk_cache()->open_count());
1574 EXPECT_EQ(1, cache.disk_cache()->create_count()); 2018 EXPECT_EQ(2, cache.disk_cache()->create_count());
1575 2019
1576 // Now, make sure that the second request asks for the entry not to be stored. 2020 // Now, make sure that the second request asks for the entry not to be stored.
1577 request_handler.set_no_store(true); 2021 request_handler.set_no_store(true);
1578 2022
1579 for (int i = 0; i < kNumTransactions; ++i) { 2023 for (int i = 0; i < kNumTransactions; ++i) {
1580 Context* c = context_list[i]; 2024 Context* c = context_list[i];
1581 if (c->result == ERR_IO_PENDING) 2025 if (c->result == ERR_IO_PENDING)
1582 c->result = c->callback.WaitForResult(); 2026 c->result = c->callback.WaitForResult();
1583 ReadAndVerifyTransaction(c->trans.get(), kFastNoStoreGET_Transaction); 2027 ReadAndVerifyTransaction(c->trans.get(), kFastNoStoreGET_Transaction);
1584 delete c; 2028 delete c;
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
1618 2062
1619 EXPECT_EQ(1, cache.network_layer()->transaction_count()); 2063 EXPECT_EQ(1, cache.network_layer()->transaction_count());
1620 EXPECT_EQ(0, cache.disk_cache()->open_count()); 2064 EXPECT_EQ(0, cache.disk_cache()->open_count());
1621 EXPECT_EQ(1, cache.disk_cache()->create_count()); 2065 EXPECT_EQ(1, cache.disk_cache()->create_count());
1622 2066
1623 for (int i = 0; i < kNumTransactions; ++i) { 2067 for (int i = 0; i < kNumTransactions; ++i) {
1624 Context* c = context_list[i]; 2068 Context* c = context_list[i];
1625 if (c->result == ERR_IO_PENDING) 2069 if (c->result == ERR_IO_PENDING)
1626 c->result = c->callback.WaitForResult(); 2070 c->result = c->callback.WaitForResult();
1627 // Destroy only the first transaction. 2071 // Destroy only the first transaction.
2072 // This should lead to all transactions to restart, even those that have
2073 // validated themselves and were waiting for the writer transaction to
2074 // complete writing to the cache.
1628 if (i == 0) { 2075 if (i == 0) {
1629 delete c; 2076 delete c;
1630 context_list[i] = NULL; 2077 context_list[i] = NULL;
1631 } 2078 }
1632 } 2079 }
1633 2080
1634 // Complete the rest of the transactions. 2081 // Complete the rest of the transactions.
1635 for (int i = 1; i < kNumTransactions; ++i) { 2082 for (int i = 1; i < kNumTransactions; ++i) {
1636 Context* c = context_list[i]; 2083 Context* c = context_list[i];
1637 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction); 2084 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction);
(...skipping 895 matching lines...) Expand 10 before | Expand all | Expand 10 after
2533 } 2980 }
2534 2981
2535 // Helper that does 4 requests using HttpCache: 2982 // Helper that does 4 requests using HttpCache:
2536 // 2983 //
2537 // (1) loads |kUrl| -- expects |net_response_1| to be returned. 2984 // (1) loads |kUrl| -- expects |net_response_1| to be returned.
2538 // (2) loads |kUrl| from cache only -- expects |net_response_1| to be returned. 2985 // (2) loads |kUrl| from cache only -- expects |net_response_1| to be returned.
2539 // (3) loads |kUrl| using |extra_request_headers| -- expects |net_response_2| to 2986 // (3) loads |kUrl| using |extra_request_headers| -- expects |net_response_2| to
2540 // be returned. 2987 // be returned.
2541 // (4) loads |kUrl| from cache only -- expects |cached_response_2| to be 2988 // (4) loads |kUrl| from cache only -- expects |cached_response_2| to be
2542 // returned. 2989 // returned.
2990 // The entry will be created once and will be opened for the 3 subsequent
2991 // requests.
2543 static void ConditionalizedRequestUpdatesCacheHelper( 2992 static void ConditionalizedRequestUpdatesCacheHelper(
2544 const Response& net_response_1, 2993 const Response& net_response_1,
2545 const Response& net_response_2, 2994 const Response& net_response_2,
2546 const Response& cached_response_2, 2995 const Response& cached_response_2,
2547 const char* extra_request_headers) { 2996 const char* extra_request_headers) {
2548 MockHttpCache cache; 2997 MockHttpCache cache;
2549 2998
2550 // The URL we will be requesting. 2999 // The URL we will be requesting.
2551 const char kUrl[] = "http://foobar.com/main.css"; 3000 const char kUrl[] = "http://foobar.com/main.css";
2552 3001
(...skipping 3555 matching lines...) Expand 10 before | Expand all | Expand 10 after
6108 pending->trans->Start(&request, pending->callback.callback(), 6557 pending->trans->Start(&request, pending->callback.callback(),
6109 NetLogWithSource())); 6558 NetLogWithSource()));
6110 EXPECT_THAT(c->callback.GetResult(rv), IsOk()); 6559 EXPECT_THAT(c->callback.GetResult(rv), IsOk());
6111 6560
6112 // Make sure that the entry has some data stored. 6561 // Make sure that the entry has some data stored.
6113 scoped_refptr<IOBufferWithSize> buf(new IOBufferWithSize(5)); 6562 scoped_refptr<IOBufferWithSize> buf(new IOBufferWithSize(5));
6114 rv = c->trans->Read(buf.get(), buf->size(), c->callback.callback()); 6563 rv = c->trans->Read(buf.get(), buf->size(), c->callback.callback());
6115 EXPECT_EQ(5, c->callback.GetResult(rv)); 6564 EXPECT_EQ(5, c->callback.GetResult(rv));
6116 6565
6117 // Cancel the requests. 6566 // Cancel the requests.
6567 // Since |pending| is currently vaidating the already written headers
6568 // it will be restarted as well.
6118 delete c; 6569 delete c;
6119 delete pending; 6570 delete pending;
6120 6571
6121 EXPECT_EQ(1, cache.network_layer()->transaction_count()); 6572 EXPECT_EQ(1, cache.network_layer()->transaction_count());
6122 EXPECT_EQ(1, cache.disk_cache()->open_count()); 6573 EXPECT_EQ(1, cache.disk_cache()->open_count());
6123 EXPECT_EQ(2, cache.disk_cache()->create_count()); 6574 EXPECT_EQ(1, cache.disk_cache()->create_count());
6124 6575
6125 base::RunLoop().RunUntilIdle(); 6576 base::RunLoop().RunUntilIdle();
6126 RemoveMockTransaction(&transaction); 6577 RemoveMockTransaction(&transaction);
6127 } 6578 }
6128 6579
6129 // Tests that we delete truncated entries if the server changes its mind midway. 6580 // Tests that we delete truncated entries if the server changes its mind midway.
6130 TEST(HttpCache, GET_IncompleteResource2) { 6581 TEST(HttpCache, GET_IncompleteResource2) {
6131 MockHttpCache cache; 6582 MockHttpCache cache;
6132 AddMockTransaction(&kRangeGET_TransactionOK); 6583 AddMockTransaction(&kRangeGET_TransactionOK);
6133 6584
(...skipping 704 matching lines...) Expand 10 before | Expand all | Expand 10 after
6838 7289
6839 RunTransactionTestWithResponseInfo(cache.http_cache(), kSimpleGET_Transaction, 7290 RunTransactionTestWithResponseInfo(cache.http_cache(), kSimpleGET_Transaction,
6840 &response); 7291 &response);
6841 EXPECT_TRUE(response.metadata.get() == NULL); 7292 EXPECT_TRUE(response.metadata.get() == NULL);
6842 7293
6843 EXPECT_EQ(1, cache.network_layer()->transaction_count()); 7294 EXPECT_EQ(1, cache.network_layer()->transaction_count());
6844 EXPECT_EQ(2, cache.disk_cache()->open_count()); 7295 EXPECT_EQ(2, cache.disk_cache()->open_count());
6845 EXPECT_EQ(1, cache.disk_cache()->create_count()); 7296 EXPECT_EQ(1, cache.disk_cache()->create_count());
6846 } 7297 }
6847 7298
6848 // Tests that if a metadata writer transaction hits cache lock timeout, it will
6849 // error out.
6850 TEST(HttpCache, WriteMetadata_CacheLockTimeout) {
6851 MockHttpCache cache;
6852
6853 // Write to the cache
6854 HttpResponseInfo response;
6855 RunTransactionTestWithResponseInfo(cache.http_cache(), kSimpleGET_Transaction,
6856 &response);
6857 EXPECT_FALSE(response.metadata.get());
6858
6859 MockHttpRequest request(kSimpleGET_Transaction);
6860 Context c1;
6861 ASSERT_THAT(cache.CreateTransaction(&c1.trans), IsOk());
6862 ASSERT_EQ(ERR_IO_PENDING, c1.trans->Start(&request, c1.callback.callback(),
6863 NetLogWithSource()));
6864
6865 cache.SimulateCacheLockTimeout();
6866
6867 // Write meta data to the same entry.
6868 scoped_refptr<IOBufferWithSize> buf(new IOBufferWithSize(50));
6869 memset(buf->data(), 0, buf->size());
6870 base::strlcpy(buf->data(), "Hi there", buf->size());
6871 cache.http_cache()->WriteMetadata(GURL(kSimpleGET_Transaction.url),
6872 DEFAULT_PRIORITY, response.response_time,
6873 buf.get(), buf->size());
6874
6875 // Release the buffer before the operation takes place.
6876 buf = NULL;
6877
6878 // Makes sure we finish pending operations.
6879 base::RunLoop().RunUntilIdle();
6880
6881 RunTransactionTestWithResponseInfo(cache.http_cache(), kSimpleGET_Transaction,
6882 &response);
6883
6884 // The writer transaction should fail due to cache lock timeout.
6885 ASSERT_FALSE(response.metadata.get());
6886 }
6887
6888 // Tests that we ignore VARY checks when writing metadata since the request 7299 // Tests that we ignore VARY checks when writing metadata since the request
6889 // headers for the WriteMetadata transaction are made up. 7300 // headers for the WriteMetadata transaction are made up.
6890 TEST(HttpCache, WriteMetadata_IgnoreVary) { 7301 TEST(HttpCache, WriteMetadata_IgnoreVary) {
6891 MockHttpCache cache; 7302 MockHttpCache cache;
6892 7303
6893 // Write to the cache 7304 // Write to the cache
6894 HttpResponseInfo response; 7305 HttpResponseInfo response;
6895 ScopedMockTransaction transaction(kSimpleGET_Transaction); 7306 ScopedMockTransaction transaction(kSimpleGET_Transaction);
6896 transaction.request_headers = "accept-encoding: gzip\r\n"; 7307 transaction.request_headers = "accept-encoding: gzip\r\n";
6897 transaction.response_headers = 7308 transaction.response_headers =
(...skipping 307 matching lines...) Expand 10 before | Expand all | Expand 10 after
7205 MockHttpRequest request(mock_transaction); 7616 MockHttpRequest request(mock_transaction);
7206 7617
7207 { 7618 {
7208 std::unique_ptr<HttpTransaction> trans; 7619 std::unique_ptr<HttpTransaction> trans;
7209 ASSERT_THAT(cache.CreateTransaction(&trans), IsOk()); 7620 ASSERT_THAT(cache.CreateTransaction(&trans), IsOk());
7210 7621
7211 int rv = trans->Start(&request, callback.callback(), NetLogWithSource()); 7622 int rv = trans->Start(&request, callback.callback(), NetLogWithSource());
7212 EXPECT_THAT(callback.GetResult(rv), IsOk()); 7623 EXPECT_THAT(callback.GetResult(rv), IsOk());
7213 7624
7214 trans->StopCaching(); 7625 trans->StopCaching();
7215
7216 scoped_refptr<IOBuffer> buf(new IOBuffer(256));
7217 rv = trans->Read(buf.get(), 10, callback.callback());
7218 EXPECT_EQ(callback.GetResult(rv), 10);
7219 } 7626 }
7220 RemoveMockTransaction(&mock_transaction); 7627 RemoveMockTransaction(&mock_transaction);
7221 7628
7222 // Make sure that the ActiveEntry is gone. 7629 // Make sure that the ActiveEntry is gone.
7223 base::RunLoop().RunUntilIdle(); 7630 base::RunLoop().RunUntilIdle();
7224 7631
7225 // Verify that the entry is gone. 7632 // Verify that the entry is gone.
7226 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); 7633 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction);
7227 7634
7228 EXPECT_EQ(2, cache.network_layer()->transaction_count()); 7635 EXPECT_EQ(2, cache.network_layer()->transaction_count());
(...skipping 1031 matching lines...) Expand 10 before | Expand all | Expand 10 after
8260 ASSERT_TRUE(attrs->GetDictionary( 8667 ASSERT_TRUE(attrs->GetDictionary(
8261 base::trace_event::MemoryAllocatorDump::kNameSize, &size_attrs)); 8668 base::trace_event::MemoryAllocatorDump::kNameSize, &size_attrs));
8262 std::string size; 8669 std::string size;
8263 ASSERT_TRUE(size_attrs->GetString("value", &size)); 8670 ASSERT_TRUE(size_attrs->GetString("value", &size));
8264 int actual_size = 0; 8671 int actual_size = 0;
8265 ASSERT_TRUE(base::HexStringToInt(size, &actual_size)); 8672 ASSERT_TRUE(base::HexStringToInt(size, &actual_size));
8266 ASSERT_LT(0, actual_size); 8673 ASSERT_LT(0, actual_size);
8267 } 8674 }
8268 8675
8269 } // namespace net 8676 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698