| OLD | NEW |
| 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 1397 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1408 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction); | 1408 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction); |
| 1409 } | 1409 } |
| 1410 | 1410 |
| 1411 // We should not have had to re-open the disk entry | 1411 // We should not have had to re-open the disk entry |
| 1412 | 1412 |
| 1413 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 1413 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 1414 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 1414 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 1415 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 1415 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 1416 } | 1416 } |
| 1417 | 1417 |
| 1418 // Parallel validation results in 200. | 1418 // Parallel validation results in 200. Creation of a new entry after dooming |
| 1419 TEST(HttpCache, SimpleGET_ParallelValidationNoMatch) { | 1419 // leads to a race between 2 transactions trying to create the new entry. |
| 1420 TEST(HttpCache, SimpleGET_ParallelValidationNoMatchCacheCreateRace) { |
| 1420 MockHttpCache cache; | 1421 MockHttpCache cache; |
| 1421 | 1422 |
| 1422 MockHttpRequest request(kSimpleGET_Transaction); | 1423 MockHttpRequest request(kSimpleGET_Transaction); |
| 1423 request.load_flags |= LOAD_VALIDATE_CACHE; | 1424 request.load_flags |= LOAD_VALIDATE_CACHE; |
| 1424 | 1425 |
| 1425 std::vector<std::unique_ptr<Context>> context_list; | 1426 std::vector<std::unique_ptr<Context>> context_list; |
| 1426 const int kNumTransactions = 5; | 1427 const int kNumTransactions = 5; |
| 1427 | 1428 |
| 1428 for (int i = 0; i < kNumTransactions; ++i) { | 1429 for (int i = 0; i < kNumTransactions; ++i) { |
| 1429 context_list.push_back(base::MakeUnique<Context>()); | 1430 context_list.push_back(base::MakeUnique<Context>()); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 1443 } | 1444 } |
| 1444 | 1445 |
| 1445 // Allow all requests to move from the Create queue to the active entry. | 1446 // Allow all requests to move from the Create queue to the active entry. |
| 1446 base::RunLoop().RunUntilIdle(); | 1447 base::RunLoop().RunUntilIdle(); |
| 1447 | 1448 |
| 1448 // The first request should be a writer at this point, and the subsequent | 1449 // The first request should be a writer at this point, and the subsequent |
| 1449 // requests should have passed the validation phase and created their own | 1450 // requests should have passed the validation phase and created their own |
| 1450 // entries since none of them matched the headers of the earlier one. | 1451 // entries since none of them matched the headers of the earlier one. |
| 1451 EXPECT_TRUE(cache.IsWriterPresent(kSimpleGET_Transaction.url)); | 1452 EXPECT_TRUE(cache.IsWriterPresent(kSimpleGET_Transaction.url)); |
| 1452 | 1453 |
| 1453 // Note that there are only 3 entries created and not 5 since every other | |
| 1454 // transaction would have gone to the network. | |
| 1455 EXPECT_EQ(5, cache.network_layer()->transaction_count()); | 1454 EXPECT_EQ(5, cache.network_layer()->transaction_count()); |
| 1456 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 1455 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 1457 EXPECT_EQ(3, cache.disk_cache()->create_count()); | 1456 EXPECT_EQ(5, cache.disk_cache()->create_count()); |
| 1458 | 1457 |
| 1459 // All requests depend on the writer, and the writer is between Start and | 1458 // All requests depend on the writer, and the writer is between Start and |
| 1460 // Read, i.e. idle. | 1459 // Read, i.e. idle. |
| 1461 for (auto& context : context_list) { | 1460 for (auto& context : context_list) { |
| 1462 EXPECT_EQ(LOAD_STATE_IDLE, context->trans->GetLoadState()); | 1461 EXPECT_EQ(LOAD_STATE_IDLE, context->trans->GetLoadState()); |
| 1463 } | 1462 } |
| 1464 | 1463 |
| 1465 for (auto& context : context_list) { | 1464 for (auto& context : context_list) { |
| 1466 if (context->result == ERR_IO_PENDING) | 1465 if (context->result == ERR_IO_PENDING) |
| 1467 context->result = context->callback.WaitForResult(); | 1466 context->result = context->callback.WaitForResult(); |
| 1468 ReadAndVerifyTransaction(context->trans.get(), kSimpleGET_Transaction); | 1467 ReadAndVerifyTransaction(context->trans.get(), kSimpleGET_Transaction); |
| 1469 } | 1468 } |
| 1470 | 1469 |
| 1471 EXPECT_EQ(5, cache.network_layer()->transaction_count()); | 1470 EXPECT_EQ(5, cache.network_layer()->transaction_count()); |
| 1472 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 1471 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 1473 EXPECT_EQ(3, cache.disk_cache()->create_count()); | 1472 EXPECT_EQ(5, cache.disk_cache()->create_count()); |
| 1473 } |
| 1474 |
| 1475 // Parallel validation results in 200. Similar to above except here there are |
| 1476 // only 2 transactions so no cache create race happens. |
| 1477 TEST(HttpCache, SimpleGET_ParallelValidationNoMatch) { |
| 1478 MockHttpCache cache; |
| 1479 |
| 1480 MockHttpRequest request(kSimpleGET_Transaction); |
| 1481 request.load_flags |= LOAD_VALIDATE_CACHE; |
| 1482 |
| 1483 std::vector<std::unique_ptr<Context>> context_list; |
| 1484 const int kNumTransactions = 2; |
| 1485 |
| 1486 for (int i = 0; i < kNumTransactions; ++i) { |
| 1487 context_list.push_back(base::MakeUnique<Context>()); |
| 1488 auto& c = context_list[i]; |
| 1489 |
| 1490 c->result = cache.CreateTransaction(&c->trans); |
| 1491 ASSERT_THAT(c->result, IsOk()); |
| 1492 EXPECT_EQ(LOAD_STATE_IDLE, c->trans->GetLoadState()); |
| 1493 |
| 1494 c->result = |
| 1495 c->trans->Start(&request, c->callback.callback(), NetLogWithSource()); |
| 1496 } |
| 1497 |
| 1498 // All requests are waiting for the active entry. |
| 1499 for (auto& context : context_list) { |
| 1500 EXPECT_EQ(LOAD_STATE_WAITING_FOR_CACHE, context->trans->GetLoadState()); |
| 1501 } |
| 1502 |
| 1503 // Allow all requests to move from the Create queue to the active entry. |
| 1504 base::RunLoop().RunUntilIdle(); |
| 1505 |
| 1506 // The first request should be a writer at this point, and the subsequent |
| 1507 // requests should have passed the validation phase and created their own |
| 1508 // entries since none of them matched the headers of the earlier one. |
| 1509 EXPECT_TRUE(cache.IsWriterPresent(kSimpleGET_Transaction.url)); |
| 1510 |
| 1511 EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 1512 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 1513 EXPECT_EQ(2, cache.disk_cache()->create_count()); |
| 1514 |
| 1515 // All requests depend on the writer, and the writer is between Start and |
| 1516 // Read, i.e. idle. |
| 1517 for (auto& context : context_list) { |
| 1518 EXPECT_EQ(LOAD_STATE_IDLE, context->trans->GetLoadState()); |
| 1519 } |
| 1520 |
| 1521 for (auto& context : context_list) { |
| 1522 if (context->result == ERR_IO_PENDING) |
| 1523 context->result = context->callback.WaitForResult(); |
| 1524 ReadAndVerifyTransaction(context->trans.get(), kSimpleGET_Transaction); |
| 1525 } |
| 1526 |
| 1527 EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 1528 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 1529 EXPECT_EQ(2, cache.disk_cache()->create_count()); |
| 1474 } | 1530 } |
| 1475 | 1531 |
| 1476 // Tests that a GET followed by a DELETE results in DELETE immediately starting | 1532 // Tests that a GET followed by a DELETE results in DELETE immediately starting |
| 1477 // the headers phase and the entry is doomed. | 1533 // the headers phase and the entry is doomed. |
| 1478 TEST(HttpCache, SimpleGET_ParallelValidationDelete) { | 1534 TEST(HttpCache, SimpleGET_ParallelValidationDelete) { |
| 1479 MockHttpCache cache; | 1535 MockHttpCache cache; |
| 1480 | 1536 |
| 1481 MockHttpRequest request(kSimpleGET_Transaction); | 1537 MockHttpRequest request(kSimpleGET_Transaction); |
| 1482 request.load_flags |= LOAD_VALIDATE_CACHE; | 1538 request.load_flags |= LOAD_VALIDATE_CACHE; |
| 1483 | 1539 |
| (...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1665 EXPECT_TRUE(cache.IsHeadersTransactionPresent(kSimpleGET_Transaction.url)); | 1721 EXPECT_TRUE(cache.IsHeadersTransactionPresent(kSimpleGET_Transaction.url)); |
| 1666 | 1722 |
| 1667 // Resume network start for headers_transaction. It will doom the entry as it | 1723 // Resume network start for headers_transaction. It will doom the entry as it |
| 1668 // will be a 200 and will go to network for the response body. | 1724 // will be a 200 and will go to network for the response body. |
| 1669 auto& context = context_list[3]; | 1725 auto& context = context_list[3]; |
| 1670 context->trans->ResumeNetworkStart(); | 1726 context->trans->ResumeNetworkStart(); |
| 1671 | 1727 |
| 1672 // The pending transactions will be added to a new entry. | 1728 // The pending transactions will be added to a new entry. |
| 1673 base::RunLoop().RunUntilIdle(); | 1729 base::RunLoop().RunUntilIdle(); |
| 1674 | 1730 |
| 1675 EXPECT_EQ(1, cache.GetCountDoneHeadersQueue(kSimpleGET_Transaction.url)); | 1731 EXPECT_EQ(2, cache.GetCountDoneHeadersQueue(kSimpleGET_Transaction.url)); |
| 1676 EXPECT_TRUE(cache.IsWriterPresent(kSimpleGET_Transaction.url)); | 1732 EXPECT_TRUE(cache.IsWriterPresent(kSimpleGET_Transaction.url)); |
| 1677 | 1733 |
| 1678 // Complete the rest of the transactions. | 1734 // Complete the rest of the transactions. |
| 1679 for (int i = 2; i < kNumTransactions; ++i) { | 1735 for (int i = 2; i < kNumTransactions; ++i) { |
| 1680 auto& c = context_list[i]; | 1736 auto& c = context_list[i]; |
| 1681 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction); | 1737 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction); |
| 1682 } | 1738 } |
| 1683 | 1739 |
| 1684 EXPECT_EQ(3, cache.network_layer()->transaction_count()); | 1740 EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 1685 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 1741 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 1686 EXPECT_EQ(2, cache.disk_cache()->create_count()); | 1742 EXPECT_EQ(2, cache.disk_cache()->create_count()); |
| 1687 } | 1743 } |
| 1688 | 1744 |
| 1689 // Tests that a transaction is in validated queue and writer is destroyed | 1745 // Tests that a transaction is in validated queue and writer is destroyed |
| 1690 // leading to restarting the validated transaction. | 1746 // leading to restarting the validated transaction. |
| 1691 TEST(HttpCache, SimpleGET_ParallelValidationCancelWriter) { | 1747 TEST(HttpCache, SimpleGET_ParallelValidationCancelWriter) { |
| 1692 MockHttpCache cache; | 1748 MockHttpCache cache; |
| 1693 | 1749 |
| 1694 MockHttpRequest request(kSimpleGET_Transaction); | 1750 MockHttpRequest request(kSimpleGET_Transaction); |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1739 | 1795 |
| 1740 base::RunLoop().RunUntilIdle(); | 1796 base::RunLoop().RunUntilIdle(); |
| 1741 | 1797 |
| 1742 EXPECT_TRUE(cache.IsWriterPresent(kSimpleGET_Transaction.url)); | 1798 EXPECT_TRUE(cache.IsWriterPresent(kSimpleGET_Transaction.url)); |
| 1743 EXPECT_TRUE(cache.IsHeadersTransactionPresent(kSimpleGET_Transaction.url)); | 1799 EXPECT_TRUE(cache.IsHeadersTransactionPresent(kSimpleGET_Transaction.url)); |
| 1744 | 1800 |
| 1745 // Resume network start for the transaction the second time. | 1801 // Resume network start for the transaction the second time. |
| 1746 c->trans->ResumeNetworkStart(); | 1802 c->trans->ResumeNetworkStart(); |
| 1747 base::RunLoop().RunUntilIdle(); | 1803 base::RunLoop().RunUntilIdle(); |
| 1748 | 1804 |
| 1749 // Headers transaction would have doomed the new entry created. | |
| 1750 EXPECT_TRUE( | |
| 1751 cache.disk_cache()->IsDiskEntryDoomed(kSimpleGET_Transaction.url)); | |
| 1752 | |
| 1753 // Complete the rest of the transactions. | 1805 // Complete the rest of the transactions. |
| 1754 for (auto& context : context_list) { | 1806 for (auto& context : context_list) { |
| 1755 if (!context) | 1807 if (!context) |
| 1756 continue; | 1808 continue; |
| 1757 ReadAndVerifyTransaction(context->trans.get(), kSimpleGET_Transaction); | 1809 ReadAndVerifyTransaction(context->trans.get(), kSimpleGET_Transaction); |
| 1758 } | 1810 } |
| 1759 | 1811 |
| 1760 EXPECT_EQ(4, cache.network_layer()->transaction_count()); | 1812 EXPECT_EQ(4, cache.network_layer()->transaction_count()); |
| 1761 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 1813 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 1762 EXPECT_EQ(2, cache.disk_cache()->create_count()); | 1814 // Headers transaction would have doomed the new entry created and created |
| 1815 // another new entry. |
| 1816 EXPECT_EQ(3, cache.disk_cache()->create_count()); |
| 1763 } | 1817 } |
| 1764 | 1818 |
| 1765 // Tests that a transaction is currently in headers phase and is destroyed | 1819 // Tests that a transaction is currently in headers phase and is destroyed |
| 1766 // leading to destroying the entry. | 1820 // leading to destroying the entry. |
| 1767 TEST(HttpCache, SimpleGET_ParallelValidationCancelHeaders) { | 1821 TEST(HttpCache, SimpleGET_ParallelValidationCancelHeaders) { |
| 1768 MockHttpCache cache; | 1822 MockHttpCache cache; |
| 1769 | 1823 |
| 1770 MockHttpRequest request(kSimpleGET_Transaction); | 1824 MockHttpRequest request(kSimpleGET_Transaction); |
| 1771 | 1825 |
| 1772 const int kNumTransactions = 2; | 1826 const int kNumTransactions = 2; |
| (...skipping 278 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2051 | 2105 |
| 2052 // Allow all requests to move from the Create queue to the active entry. | 2106 // Allow all requests to move from the Create queue to the active entry. |
| 2053 base::RunLoop().RunUntilIdle(); | 2107 base::RunLoop().RunUntilIdle(); |
| 2054 | 2108 |
| 2055 // The first request should be a writer at this point, and the subsequent | 2109 // The first request should be a writer at this point, and the subsequent |
| 2056 // requests should have completed validation. Since the validation does not | 2110 // requests should have completed validation. Since the validation does not |
| 2057 // result in a match, a new entry would be created. | 2111 // result in a match, a new entry would be created. |
| 2058 | 2112 |
| 2059 EXPECT_EQ(3, cache.network_layer()->transaction_count()); | 2113 EXPECT_EQ(3, cache.network_layer()->transaction_count()); |
| 2060 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 2114 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 2061 EXPECT_EQ(2, cache.disk_cache()->create_count()); | 2115 EXPECT_EQ(3, cache.disk_cache()->create_count()); |
| 2062 | 2116 |
| 2063 // Now, make sure that the second request asks for the entry not to be stored. | 2117 // Now, make sure that the second request asks for the entry not to be stored. |
| 2064 request_handler.set_no_store(true); | 2118 request_handler.set_no_store(true); |
| 2065 | 2119 |
| 2066 for (int i = 0; i < kNumTransactions; ++i) { | 2120 for (int i = 0; i < kNumTransactions; ++i) { |
| 2067 Context* c = context_list[i]; | 2121 Context* c = context_list[i]; |
| 2068 if (c->result == ERR_IO_PENDING) | 2122 if (c->result == ERR_IO_PENDING) |
| 2069 c->result = c->callback.WaitForResult(); | 2123 c->result = c->callback.WaitForResult(); |
| 2070 ReadAndVerifyTransaction(c->trans.get(), kFastNoStoreGET_Transaction); | 2124 ReadAndVerifyTransaction(c->trans.get(), kFastNoStoreGET_Transaction); |
| 2071 delete c; | 2125 delete c; |
| 2072 } | 2126 } |
| 2073 | 2127 |
| 2074 EXPECT_EQ(3, cache.network_layer()->transaction_count()); | 2128 EXPECT_EQ(3, cache.network_layer()->transaction_count()); |
| 2075 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 2129 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 2076 EXPECT_EQ(2, cache.disk_cache()->create_count()); | 2130 EXPECT_EQ(3, cache.disk_cache()->create_count()); |
| 2077 | 2131 |
| 2078 RemoveMockTransaction(&kFastNoStoreGET_Transaction); | 2132 RemoveMockTransaction(&kFastNoStoreGET_Transaction); |
| 2079 } | 2133 } |
| 2080 | 2134 |
| 2081 TEST(HttpCache, SimpleGET_ManyWriters_CancelFirst) { | 2135 TEST(HttpCache, SimpleGET_ManyWriters_CancelFirst) { |
| 2082 MockHttpCache cache; | 2136 MockHttpCache cache; |
| 2083 | 2137 |
| 2084 MockHttpRequest request(kSimpleGET_Transaction); | 2138 MockHttpRequest request(kSimpleGET_Transaction); |
| 2085 | 2139 |
| 2086 std::vector<Context*> context_list; | 2140 std::vector<Context*> context_list; |
| (...skipping 6623 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 8710 ASSERT_TRUE(attrs->GetDictionary( | 8764 ASSERT_TRUE(attrs->GetDictionary( |
| 8711 base::trace_event::MemoryAllocatorDump::kNameSize, &size_attrs)); | 8765 base::trace_event::MemoryAllocatorDump::kNameSize, &size_attrs)); |
| 8712 std::string size; | 8766 std::string size; |
| 8713 ASSERT_TRUE(size_attrs->GetString("value", &size)); | 8767 ASSERT_TRUE(size_attrs->GetString("value", &size)); |
| 8714 int actual_size = 0; | 8768 int actual_size = 0; |
| 8715 ASSERT_TRUE(base::HexStringToInt(size, &actual_size)); | 8769 ASSERT_TRUE(base::HexStringToInt(size, &actual_size)); |
| 8716 ASSERT_LT(0, actual_size); | 8770 ASSERT_LT(0, actual_size); |
| 8717 } | 8771 } |
| 8718 | 8772 |
| 8719 } // namespace net | 8773 } // namespace net |
| OLD | NEW |