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 1399 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1410 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 1410 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
1411 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 1411 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
1412 | 1412 |
1413 for (int i = 0; i < kNumTransactions; ++i) { | 1413 for (int i = 0; i < kNumTransactions; ++i) { |
1414 Context* c = context_list[i]; | 1414 Context* c = context_list[i]; |
1415 delete c; | 1415 delete c; |
1416 } | 1416 } |
1417 } | 1417 } |
1418 | 1418 |
1419 // Parallel validation results in 200. | 1419 // Parallel validation results in 200. |
1420 TEST(HttpCache, SimpleGET_ParallelValidationNoMatch) { | 1420 TEST(HttpCache, SimpleGET_ParallelValidationNoMatchCacheCreateRace) { |
1421 MockHttpCache cache; | 1421 MockHttpCache cache; |
1422 | 1422 |
1423 MockHttpRequest request(kSimpleGET_Transaction); | 1423 MockHttpRequest request(kSimpleGET_Transaction); |
1424 request.load_flags |= LOAD_VALIDATE_CACHE; | 1424 request.load_flags |= LOAD_VALIDATE_CACHE; |
1425 | 1425 |
1426 std::vector<Context*> context_list; | 1426 std::vector<Context*> context_list; |
1427 const int kNumTransactions = 5; | 1427 const int kNumTransactions = 5; |
1428 | 1428 |
1429 for (int i = 0; i < kNumTransactions; ++i) { | 1429 for (int i = 0; i < kNumTransactions; ++i) { |
1430 context_list.push_back(new Context()); | 1430 context_list.push_back(new Context()); |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1465 | 1465 |
1466 for (int i = 0; i < kNumTransactions; ++i) { | 1466 for (int i = 0; i < kNumTransactions; ++i) { |
1467 Context* c = context_list[i]; | 1467 Context* c = context_list[i]; |
1468 if (c->result == ERR_IO_PENDING) | 1468 if (c->result == ERR_IO_PENDING) |
1469 c->result = c->callback.WaitForResult(); | 1469 c->result = c->callback.WaitForResult(); |
1470 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction); | 1470 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction); |
1471 } | 1471 } |
1472 | 1472 |
1473 EXPECT_EQ(5, cache.network_layer()->transaction_count()); | 1473 EXPECT_EQ(5, cache.network_layer()->transaction_count()); |
1474 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 1474 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 1475 |
| 1476 // Note only 3 entries got created instead of 5 since every other transaction |
| 1477 // received a ERR_CACHE_CREATE_FAILURE due to racing with another transaction |
| 1478 // to create the entry. |
1475 EXPECT_EQ(3, cache.disk_cache()->create_count()); | 1479 EXPECT_EQ(3, cache.disk_cache()->create_count()); |
1476 | 1480 |
1477 for (int i = 0; i < kNumTransactions; ++i) { | 1481 for (int i = 0; i < kNumTransactions; ++i) { |
1478 Context* c = context_list[i]; | 1482 Context* c = context_list[i]; |
1479 delete c; | 1483 delete c; |
1480 } | 1484 } |
1481 } | 1485 } |
1482 | 1486 |
| 1487 // Parallel validation results in 200. Similar to above except here there are |
| 1488 // only 2 transactions so no cache create race happens. |
| 1489 TEST(HttpCache, SimpleGET_ParallelValidationNoMatch) { |
| 1490 MockHttpCache cache; |
| 1491 |
| 1492 MockHttpRequest request(kSimpleGET_Transaction); |
| 1493 request.load_flags |= LOAD_VALIDATE_CACHE; |
| 1494 |
| 1495 std::vector<Context*> context_list; |
| 1496 const int kNumTransactions = 2; |
| 1497 |
| 1498 for (int i = 0; i < kNumTransactions; ++i) { |
| 1499 context_list.push_back(new Context()); |
| 1500 Context* c = context_list[i]; |
| 1501 |
| 1502 c->result = cache.CreateTransaction(&c->trans); |
| 1503 ASSERT_THAT(c->result, IsOk()); |
| 1504 EXPECT_EQ(LOAD_STATE_IDLE, c->trans->GetLoadState()); |
| 1505 |
| 1506 c->result = |
| 1507 c->trans->Start(&request, c->callback.callback(), NetLogWithSource()); |
| 1508 } |
| 1509 |
| 1510 // All requests are waiting for the active entry. |
| 1511 for (int i = 0; i < kNumTransactions; ++i) { |
| 1512 Context* c = context_list[i]; |
| 1513 EXPECT_EQ(LOAD_STATE_WAITING_FOR_CACHE, c->trans->GetLoadState()); |
| 1514 } |
| 1515 |
| 1516 // Allow all requests to move from the Create queue to the active entry. |
| 1517 base::RunLoop().RunUntilIdle(); |
| 1518 |
| 1519 // The first request should be a writer at this point, and the subsequent |
| 1520 // requests should have passed the validation phase and created their own |
| 1521 // entries since none of them matched the headers of the earlier one. |
| 1522 EXPECT_TRUE(cache.IsWriterPresent(kSimpleGET_Transaction.url)); |
| 1523 |
| 1524 EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 1525 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 1526 EXPECT_EQ(2, cache.disk_cache()->create_count()); |
| 1527 |
| 1528 // All requests depend on the writer, and the writer is between Start and |
| 1529 // Read, i.e. idle. |
| 1530 for (int i = 0; i < kNumTransactions; ++i) { |
| 1531 Context* c = context_list[i]; |
| 1532 EXPECT_EQ(LOAD_STATE_IDLE, c->trans->GetLoadState()); |
| 1533 } |
| 1534 |
| 1535 for (int i = 0; i < kNumTransactions; ++i) { |
| 1536 Context* c = context_list[i]; |
| 1537 if (c->result == ERR_IO_PENDING) |
| 1538 c->result = c->callback.WaitForResult(); |
| 1539 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction); |
| 1540 } |
| 1541 |
| 1542 EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 1543 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 1544 EXPECT_EQ(2, cache.disk_cache()->create_count()); |
| 1545 |
| 1546 for (int i = 0; i < kNumTransactions; ++i) { |
| 1547 Context* c = context_list[i]; |
| 1548 delete c; |
| 1549 } |
| 1550 } |
| 1551 |
1483 // Tests that a GET followed by a DELETE results in DELETE immediately starting | 1552 // Tests that a GET followed by a DELETE results in DELETE immediately starting |
1484 // the headers phase and the entry is doomed. | 1553 // the headers phase and the entry is doomed. |
1485 TEST(HttpCache, SimpleGET_ParallelValidationDelete) { | 1554 TEST(HttpCache, SimpleGET_ParallelValidationDelete) { |
1486 MockHttpCache cache; | 1555 MockHttpCache cache; |
1487 | 1556 |
1488 MockHttpRequest request(kSimpleGET_Transaction); | 1557 MockHttpRequest request(kSimpleGET_Transaction); |
1489 request.load_flags |= LOAD_VALIDATE_CACHE; | 1558 request.load_flags |= LOAD_VALIDATE_CACHE; |
1490 | 1559 |
1491 MockHttpRequest delete_request(kSimpleGET_Transaction); | 1560 MockHttpRequest delete_request(kSimpleGET_Transaction); |
1492 delete_request.method = "DELETE"; | 1561 delete_request.method = "DELETE"; |
(...skipping 284 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1777 | 1846 |
1778 base::RunLoop().RunUntilIdle(); | 1847 base::RunLoop().RunUntilIdle(); |
1779 | 1848 |
1780 EXPECT_TRUE(cache.IsWriterPresent(kSimpleGET_Transaction.url)); | 1849 EXPECT_TRUE(cache.IsWriterPresent(kSimpleGET_Transaction.url)); |
1781 EXPECT_TRUE(cache.IsHeadersTransactionPresent(kSimpleGET_Transaction.url)); | 1850 EXPECT_TRUE(cache.IsHeadersTransactionPresent(kSimpleGET_Transaction.url)); |
1782 | 1851 |
1783 // Resume network start for the transaction the second time. | 1852 // Resume network start for the transaction the second time. |
1784 c->trans->ResumeNetworkStart(); | 1853 c->trans->ResumeNetworkStart(); |
1785 base::RunLoop().RunUntilIdle(); | 1854 base::RunLoop().RunUntilIdle(); |
1786 | 1855 |
1787 // Headers transaction would have doomed the new entry created. | |
1788 EXPECT_TRUE( | |
1789 cache.disk_cache()->IsDiskEntryDoomed(kSimpleGET_Transaction.url)); | |
1790 | |
1791 // Complete the rest of the transactions. | 1856 // Complete the rest of the transactions. |
1792 for (int i = 1; i < kNumTransactions; ++i) { | 1857 for (int i = 1; i < kNumTransactions; ++i) { |
1793 Context* c = context_list[i]; | 1858 Context* c = context_list[i]; |
1794 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction); | 1859 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction); |
1795 } | 1860 } |
1796 | 1861 |
1797 EXPECT_EQ(4, cache.network_layer()->transaction_count()); | 1862 EXPECT_EQ(4, cache.network_layer()->transaction_count()); |
1798 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 1863 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
1799 EXPECT_EQ(2, cache.disk_cache()->create_count()); | 1864 |
| 1865 // Headers transaction would have doomed the new entry created and created |
| 1866 // another new entry. |
| 1867 EXPECT_EQ(3, cache.disk_cache()->create_count()); |
1800 | 1868 |
1801 for (int i = 1; i < kNumTransactions; ++i) { | 1869 for (int i = 1; i < kNumTransactions; ++i) { |
1802 Context* c = context_list[i]; | 1870 Context* c = context_list[i]; |
1803 delete c; | 1871 delete c; |
1804 } | 1872 } |
1805 } | 1873 } |
1806 | 1874 |
1807 // Tests that a transaction is currently in headers phase and is destroyed | 1875 // Tests that a transaction is currently in headers phase and is destroyed |
1808 // leading to destroying the entry. | 1876 // leading to destroying the entry. |
1809 TEST(HttpCache, SimpleGET_ParallelValidationCancelHeaders) { | 1877 TEST(HttpCache, SimpleGET_ParallelValidationCancelHeaders) { |
(...skipping 6958 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
8768 ASSERT_TRUE(attrs->GetDictionary( | 8836 ASSERT_TRUE(attrs->GetDictionary( |
8769 base::trace_event::MemoryAllocatorDump::kNameSize, &size_attrs)); | 8837 base::trace_event::MemoryAllocatorDump::kNameSize, &size_attrs)); |
8770 std::string size; | 8838 std::string size; |
8771 ASSERT_TRUE(size_attrs->GetString("value", &size)); | 8839 ASSERT_TRUE(size_attrs->GetString("value", &size)); |
8772 int actual_size = 0; | 8840 int actual_size = 0; |
8773 ASSERT_TRUE(base::HexStringToInt(size, &actual_size)); | 8841 ASSERT_TRUE(base::HexStringToInt(size, &actual_size)); |
8774 ASSERT_LT(0, actual_size); | 8842 ASSERT_LT(0, actual_size); |
8775 } | 8843 } |
8776 | 8844 |
8777 } // namespace net | 8845 } // namespace net |
OLD | NEW |