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 1357 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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. |
1379 | 1380 |
1380 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 1381 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
1381 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 1382 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
1382 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 1383 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
1383 | 1384 |
1384 // All requests depend on the writer, and the writer is between Start and | 1385 // All requests depend on the writer, and the writer is between Start and |
1385 // Read, i.e. idle. | 1386 // Read, i.e. idle. |
1386 for (int i = 0; i < kNumTransactions; ++i) { | 1387 for (int i = 0; i < kNumTransactions; ++i) { |
1387 Context* c = context_list[i]; | 1388 Context* c = context_list[i]; |
1388 EXPECT_EQ(LOAD_STATE_IDLE, c->trans->GetLoadState()); | 1389 EXPECT_EQ(LOAD_STATE_IDLE, c->trans->GetLoadState()); |
(...skipping 11 matching lines...) Expand all Loading... |
1400 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 1401 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
1401 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 1402 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
1402 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 1403 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
1403 | 1404 |
1404 for (int i = 0; i < kNumTransactions; ++i) { | 1405 for (int i = 0; i < kNumTransactions; ++i) { |
1405 Context* c = context_list[i]; | 1406 Context* c = context_list[i]; |
1406 delete c; | 1407 delete c; |
1407 } | 1408 } |
1408 } | 1409 } |
1409 | 1410 |
| 1411 // Tests parallel validation similar to the above test, except here validation |
| 1412 // request is sent out to the network and results in 304. |
| 1413 TEST(HttpCache, SimpleGET_ParallelValidation) { |
| 1414 MockHttpCache cache; |
| 1415 |
| 1416 std::vector<MockHttpRequest> request; |
| 1417 ScopedMockTransaction transaction(kTypicalGET_Transaction); |
| 1418 transaction.response_headers = |
| 1419 "Etag: \"foopy\"\n" |
| 1420 "Cache-Control: max-age=0\n"; |
| 1421 MockHttpRequest request0(transaction); |
| 1422 request.push_back(request0); |
| 1423 |
| 1424 ScopedMockTransaction transaction1(kTypicalGET_Transaction); |
| 1425 transaction1.status = "HTTP/1.1 304 Not Modified"; |
| 1426 transaction1.load_flags |= LOAD_VALIDATE_CACHE; |
| 1427 transaction1.response_headers = |
| 1428 "Etag: \"foopy\"\n" |
| 1429 "Cache-Control: max-age=3600\n"; |
| 1430 MockHttpRequest request1(transaction1); |
| 1431 request.push_back(request1); |
| 1432 |
| 1433 std::vector<Context*> context_list; |
| 1434 const int kNumTransactions = 2; |
| 1435 |
| 1436 for (int i = 0; i < kNumTransactions; ++i) { |
| 1437 context_list.push_back(new Context()); |
| 1438 Context* c = context_list[i]; |
| 1439 |
| 1440 c->result = cache.CreateTransaction(&c->trans); |
| 1441 ASSERT_THAT(c->result, IsOk()); |
| 1442 EXPECT_EQ(LOAD_STATE_IDLE, c->trans->GetLoadState()); |
| 1443 |
| 1444 c->result = c->trans->Start(&request[i], c->callback.callback(), |
| 1445 NetLogWithSource()); |
| 1446 } |
| 1447 |
| 1448 // All requests are waiting for the active entry. |
| 1449 for (int i = 0; i < kNumTransactions; ++i) { |
| 1450 Context* c = context_list[i]; |
| 1451 EXPECT_EQ(LOAD_STATE_WAITING_FOR_CACHE, c->trans->GetLoadState()); |
| 1452 } |
| 1453 |
| 1454 // Allow all requests to move from the Create queue to the active entry. |
| 1455 base::RunLoop().RunUntilIdle(); |
| 1456 |
| 1457 // The first request should be a writer at this point, and the subsequent |
| 1458 // requests should have passed the validation phase and waiting for the |
| 1459 // response to be written to the cache before they can read. |
| 1460 |
| 1461 EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 1462 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 1463 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 1464 |
| 1465 // All requests depend on the writer, and the writer is between Start and |
| 1466 // Read, i.e. idle. |
| 1467 for (int i = 0; i < kNumTransactions; ++i) { |
| 1468 Context* c = context_list[i]; |
| 1469 EXPECT_EQ(LOAD_STATE_IDLE, c->trans->GetLoadState()); |
| 1470 } |
| 1471 |
| 1472 for (int i = 0; i < kNumTransactions; ++i) { |
| 1473 Context* c = context_list[i]; |
| 1474 if (c->result == ERR_IO_PENDING) |
| 1475 c->result = c->callback.WaitForResult(); |
| 1476 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction); |
| 1477 } |
| 1478 |
| 1479 // We should not have had to re-open the disk entry |
| 1480 |
| 1481 EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 1482 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 1483 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 1484 |
| 1485 for (int i = 0; i < kNumTransactions; ++i) { |
| 1486 Context* c = context_list[i]; |
| 1487 delete c; |
| 1488 } |
| 1489 } |
| 1490 |
| 1491 // Tests parallel validation similar to the above test, except here cache write |
| 1492 // fails and the validated transactions should be restarted. |
| 1493 TEST(HttpCache, SimpleGET_ParallelValidationFailWrite) { |
| 1494 MockHttpCache cache; |
| 1495 |
| 1496 std::vector<MockHttpRequest> request; |
| 1497 ScopedMockTransaction transaction(kTypicalGET_Transaction); |
| 1498 transaction.response_headers = |
| 1499 "Etag: \"foopy\"\n" |
| 1500 "Cache-Control: max-age=0\n"; |
| 1501 MockHttpRequest request0(transaction); |
| 1502 request.push_back(request0); |
| 1503 |
| 1504 ScopedMockTransaction transaction1(kTypicalGET_Transaction); |
| 1505 transaction1.status = "HTTP/1.1 304 Not Modified"; |
| 1506 transaction1.load_flags |= LOAD_VALIDATE_CACHE; |
| 1507 transaction1.response_headers = |
| 1508 "Etag: \"foopy\"\n" |
| 1509 "Cache-Control: max-age=3600\n"; |
| 1510 MockHttpRequest request1(transaction1); |
| 1511 request.push_back(request1); |
| 1512 |
| 1513 std::vector<Context*> context_list; |
| 1514 const int kNumTransactions = 2; |
| 1515 |
| 1516 for (int i = 0; i < kNumTransactions; ++i) { |
| 1517 context_list.push_back(new Context()); |
| 1518 Context* c = context_list[i]; |
| 1519 |
| 1520 c->result = cache.CreateTransaction(&c->trans); |
| 1521 ASSERT_THAT(c->result, IsOk()); |
| 1522 EXPECT_EQ(LOAD_STATE_IDLE, c->trans->GetLoadState()); |
| 1523 |
| 1524 c->result = c->trans->Start(&request[i], c->callback.callback(), |
| 1525 NetLogWithSource()); |
| 1526 } |
| 1527 |
| 1528 // All requests are waiting for the active entry. |
| 1529 for (int i = 0; i < kNumTransactions; ++i) { |
| 1530 Context* c = context_list[i]; |
| 1531 EXPECT_EQ(LOAD_STATE_WAITING_FOR_CACHE, c->trans->GetLoadState()); |
| 1532 } |
| 1533 |
| 1534 // Allow all requests to move from the Create queue to the active entry. |
| 1535 base::RunLoop().RunUntilIdle(); |
| 1536 |
| 1537 // The first request should be a writer at this point, and the subsequent |
| 1538 // requests should have passed the validation phase and waiting for the |
| 1539 // response to be written to the cache before they can read. |
| 1540 |
| 1541 EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 1542 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 1543 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 1544 |
| 1545 // All requests depend on the writer, and the writer is between Start and |
| 1546 // Read, i.e. idle. |
| 1547 for (int i = 0; i < kNumTransactions; ++i) { |
| 1548 Context* c = context_list[i]; |
| 1549 EXPECT_EQ(LOAD_STATE_IDLE, c->trans->GetLoadState()); |
| 1550 } |
| 1551 |
| 1552 // Fail the request. |
| 1553 cache.disk_cache()->set_soft_failures(true); |
| 1554 // We have to open the entry again to propagate the failure flag. |
| 1555 disk_cache::Entry* en; |
| 1556 cache.OpenBackendEntry(kTypicalGET_Transaction.url, &en); |
| 1557 en->Close(); |
| 1558 |
| 1559 for (int i = 0; i < kNumTransactions; ++i) { |
| 1560 Context* c = context_list[i]; |
| 1561 if (c->result == ERR_IO_PENDING) |
| 1562 c->result = c->callback.WaitForResult(); |
| 1563 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction); |
| 1564 } |
| 1565 |
| 1566 // We should not have had to re-open the disk entry |
| 1567 |
| 1568 // Since validated transaction was restarted, there is an additional network |
| 1569 // transaction created. |
| 1570 EXPECT_EQ(3, cache.network_layer()->transaction_count()); |
| 1571 EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 1572 EXPECT_EQ(2, cache.disk_cache()->create_count()); |
| 1573 |
| 1574 for (int i = 0; i < kNumTransactions; ++i) { |
| 1575 Context* c = context_list[i]; |
| 1576 delete c; |
| 1577 } |
| 1578 } |
| 1579 |
1410 // 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. |
1411 // 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 |
1412 // 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 |
1413 // entry. | 1583 // entry. |
1414 TEST(HttpCache, SimpleGET_RacingReaders) { | 1584 TEST(HttpCache, SimpleGET_RacingReaders) { |
1415 MockHttpCache cache; | 1585 MockHttpCache cache; |
1416 | 1586 |
1417 MockHttpRequest request(kSimpleGET_Transaction); | 1587 MockHttpRequest request(kSimpleGET_Transaction); |
1418 MockHttpRequest reader_request(kSimpleGET_Transaction); | 1588 MockHttpRequest reader_request(kSimpleGET_Transaction); |
1419 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 Loading... |
1444 | 1614 |
1445 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 1615 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
1446 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 1616 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
1447 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 1617 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
1448 | 1618 |
1449 Context* c = context_list[0]; | 1619 Context* c = context_list[0]; |
1450 ASSERT_THAT(c->result, IsError(ERR_IO_PENDING)); | 1620 ASSERT_THAT(c->result, IsError(ERR_IO_PENDING)); |
1451 c->result = c->callback.WaitForResult(); | 1621 c->result = c->callback.WaitForResult(); |
1452 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction); | 1622 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction); |
1453 | 1623 |
1454 // Now we have 2 active readers and two queued transactions. | 1624 // Now all transactions should be waiting for read to be invoked. Two readers |
1455 | 1625 // are because of the load flags and remaining two transactions were converted |
| 1626 // to readers after skipping validation. Note that the remaining two went on |
| 1627 // to process the headers in parallel with readers presnt on the entry. |
1456 EXPECT_EQ(LOAD_STATE_IDLE, context_list[2]->trans->GetLoadState()); | 1628 EXPECT_EQ(LOAD_STATE_IDLE, context_list[2]->trans->GetLoadState()); |
1457 EXPECT_EQ(LOAD_STATE_WAITING_FOR_CACHE, | 1629 EXPECT_EQ(LOAD_STATE_IDLE, context_list[3]->trans->GetLoadState()); |
1458 context_list[3]->trans->GetLoadState()); | |
1459 | 1630 |
1460 c = context_list[1]; | 1631 c = context_list[1]; |
1461 ASSERT_THAT(c->result, IsError(ERR_IO_PENDING)); | 1632 ASSERT_THAT(c->result, IsError(ERR_IO_PENDING)); |
1462 c->result = c->callback.WaitForResult(); | 1633 c->result = c->callback.WaitForResult(); |
1463 if (c->result == OK) | 1634 if (c->result == OK) |
1464 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction); | 1635 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction); |
1465 | 1636 |
1466 // At this point we have one reader, two pending transactions and a task on | 1637 // 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 | 1638 // 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 | 1639 // 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 Loading... |
1513 ASSERT_THAT(c->result, IsOk()); | 1684 ASSERT_THAT(c->result, IsOk()); |
1514 | 1685 |
1515 MockHttpRequest* this_request = &request; | 1686 MockHttpRequest* this_request = &request; |
1516 if (i == 3) | 1687 if (i == 3) |
1517 this_request = &writer_request; | 1688 this_request = &writer_request; |
1518 | 1689 |
1519 c->result = c->trans->Start(this_request, c->callback.callback(), | 1690 c->result = c->trans->Start(this_request, c->callback.callback(), |
1520 NetLogWithSource()); | 1691 NetLogWithSource()); |
1521 } | 1692 } |
1522 | 1693 |
| 1694 base::RunLoop().RunUntilIdle(); |
| 1695 |
1523 // The first request should be a writer at this point, and the two subsequent | 1696 // 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. | 1697 // requests should be pending. The last request doomed the first entry. |
1525 | 1698 |
1526 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | 1699 EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
1527 | 1700 |
1528 // Cancel the first queued transaction. | 1701 // Cancel the second transaction. Note that this and the 3rd transactions |
| 1702 // would have completed their headers phase and would be waiting in the |
| 1703 // done_headers_queue when the 2nd transaction is cancelled. |
1529 context_list[1].reset(); | 1704 context_list[1].reset(); |
1530 | 1705 |
1531 for (int i = 0; i < kNumTransactions; ++i) { | 1706 for (int i = 0; i < kNumTransactions; ++i) { |
1532 if (i == 1) | 1707 if (i == 1) |
1533 continue; | 1708 continue; |
1534 Context* c = context_list[i].get(); | 1709 Context* c = context_list[i].get(); |
1535 ASSERT_THAT(c->result, IsError(ERR_IO_PENDING)); | 1710 ASSERT_THAT(c->result, IsError(ERR_IO_PENDING)); |
1536 c->result = c->callback.WaitForResult(); | 1711 c->result = c->callback.WaitForResult(); |
1537 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction); | 1712 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction); |
1538 } | 1713 } |
(...skipping 21 matching lines...) Expand all Loading... |
1560 ASSERT_THAT(c->result, IsOk()); | 1735 ASSERT_THAT(c->result, IsOk()); |
1561 | 1736 |
1562 c->result = | 1737 c->result = |
1563 c->trans->Start(&request, c->callback.callback(), NetLogWithSource()); | 1738 c->trans->Start(&request, c->callback.callback(), NetLogWithSource()); |
1564 } | 1739 } |
1565 | 1740 |
1566 // Allow all requests to move from the Create queue to the active entry. | 1741 // Allow all requests to move from the Create queue to the active entry. |
1567 base::RunLoop().RunUntilIdle(); | 1742 base::RunLoop().RunUntilIdle(); |
1568 | 1743 |
1569 // The first request should be a writer at this point, and the subsequent | 1744 // The first request should be a writer at this point, and the subsequent |
1570 // requests should be pending. | 1745 // requests should have completed validation. Since the validation does not |
| 1746 // result in a match, a new entry would be created. |
1571 | 1747 |
1572 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 1748 EXPECT_EQ(3, cache.network_layer()->transaction_count()); |
1573 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 1749 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
1574 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 1750 EXPECT_EQ(2, cache.disk_cache()->create_count()); |
1575 | 1751 |
1576 // Now, make sure that the second request asks for the entry not to be stored. | 1752 // Now, make sure that the second request asks for the entry not to be stored. |
1577 request_handler.set_no_store(true); | 1753 request_handler.set_no_store(true); |
1578 | 1754 |
1579 for (int i = 0; i < kNumTransactions; ++i) { | 1755 for (int i = 0; i < kNumTransactions; ++i) { |
1580 Context* c = context_list[i]; | 1756 Context* c = context_list[i]; |
1581 if (c->result == ERR_IO_PENDING) | 1757 if (c->result == ERR_IO_PENDING) |
1582 c->result = c->callback.WaitForResult(); | 1758 c->result = c->callback.WaitForResult(); |
1583 ReadAndVerifyTransaction(c->trans.get(), kFastNoStoreGET_Transaction); | 1759 ReadAndVerifyTransaction(c->trans.get(), kFastNoStoreGET_Transaction); |
1584 delete c; | 1760 delete c; |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1618 | 1794 |
1619 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 1795 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
1620 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 1796 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
1621 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 1797 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
1622 | 1798 |
1623 for (int i = 0; i < kNumTransactions; ++i) { | 1799 for (int i = 0; i < kNumTransactions; ++i) { |
1624 Context* c = context_list[i]; | 1800 Context* c = context_list[i]; |
1625 if (c->result == ERR_IO_PENDING) | 1801 if (c->result == ERR_IO_PENDING) |
1626 c->result = c->callback.WaitForResult(); | 1802 c->result = c->callback.WaitForResult(); |
1627 // Destroy only the first transaction. | 1803 // Destroy only the first transaction. |
| 1804 // This should lead to all transactions to restart, even those that have |
| 1805 // validated themselves and were waiting for the writer transaction to |
| 1806 // complete writing to the cache. |
1628 if (i == 0) { | 1807 if (i == 0) { |
1629 delete c; | 1808 delete c; |
1630 context_list[i] = NULL; | 1809 context_list[i] = NULL; |
1631 } | 1810 } |
1632 } | 1811 } |
1633 | 1812 |
1634 // Complete the rest of the transactions. | 1813 // Complete the rest of the transactions. |
1635 for (int i = 1; i < kNumTransactions; ++i) { | 1814 for (int i = 1; i < kNumTransactions; ++i) { |
1636 Context* c = context_list[i]; | 1815 Context* c = context_list[i]; |
1637 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction); | 1816 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction); |
1638 } | 1817 } |
1639 | 1818 |
1640 // We should have had to re-open the disk entry. | 1819 // We should have had to re-open the disk entry. |
1641 | 1820 |
1642 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | 1821 EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
1643 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 1822 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
1644 EXPECT_EQ(2, cache.disk_cache()->create_count()); | 1823 EXPECT_EQ(2, cache.disk_cache()->create_count()); |
1645 | 1824 |
1646 for (int i = 1; i < kNumTransactions; ++i) { | 1825 for (int i = 1; i < kNumTransactions; ++i) { |
1647 Context* c = context_list[i]; | 1826 Context* c = context_list[i]; |
1648 delete c; | 1827 delete c; |
1649 } | 1828 } |
1650 } | 1829 } |
1651 | 1830 |
| 1831 // Tests that a transaction which is in validated queue can be destroyed without |
| 1832 // any impact to other transactions. |
| 1833 TEST(HttpCache, SimpleGET_ManyWriters_CancelValidated) { |
| 1834 MockHttpCache cache; |
| 1835 |
| 1836 MockHttpRequest request(kSimpleGET_Transaction); |
| 1837 |
| 1838 std::vector<Context*> context_list; |
| 1839 const int kNumTransactions = 2; |
| 1840 |
| 1841 for (int i = 0; i < kNumTransactions; ++i) { |
| 1842 context_list.push_back(new Context()); |
| 1843 Context* c = context_list[i]; |
| 1844 |
| 1845 c->result = cache.CreateTransaction(&c->trans); |
| 1846 ASSERT_THAT(c->result, IsOk()); |
| 1847 |
| 1848 c->result = |
| 1849 c->trans->Start(&request, c->callback.callback(), NetLogWithSource()); |
| 1850 } |
| 1851 |
| 1852 // Allow all requests to move from the Create queue to the active entry. |
| 1853 base::RunLoop().RunUntilIdle(); |
| 1854 |
| 1855 // The first request should be a writer at this point, and the subsequent |
| 1856 // requests should be pending. |
| 1857 |
| 1858 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 1859 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 1860 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 1861 |
| 1862 for (int i = 0; i < kNumTransactions; ++i) { |
| 1863 Context* c = context_list[i]; |
| 1864 if (i == 0 && c->result == ERR_IO_PENDING) |
| 1865 c->result = c->callback.WaitForResult(); |
| 1866 // Destroy one of the transactions waiting after the validated stage. |
| 1867 if (i == 1) { |
| 1868 delete c; |
| 1869 context_list[i] = NULL; |
| 1870 } |
| 1871 } |
| 1872 |
| 1873 // Complete the rest of the transactions. |
| 1874 for (int i = 0; i < kNumTransactions; ++i) { |
| 1875 if (i == 1) |
| 1876 continue; |
| 1877 Context* c = context_list[i]; |
| 1878 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction); |
| 1879 } |
| 1880 |
| 1881 // We should have had to re-open the disk entry. |
| 1882 |
| 1883 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 1884 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 1885 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 1886 |
| 1887 for (int i = 0; i < kNumTransactions; ++i) { |
| 1888 if (i == 1) |
| 1889 continue; |
| 1890 Context* c = context_list[i]; |
| 1891 delete c; |
| 1892 } |
| 1893 } |
| 1894 |
| 1895 // Tests that a transaction which is currently validating can be destroyed |
| 1896 // without any impact to other transactions. |
| 1897 TEST(HttpCache, SimpleGET_ManyWriters_CancelValidating) { |
| 1898 MockHttpCache cache; |
| 1899 |
| 1900 MockHttpRequest request(kSimpleGET_Transaction); |
| 1901 |
| 1902 std::vector<Context*> context_list; |
| 1903 const int kNumTransactions = 2; |
| 1904 |
| 1905 for (int i = 0; i < kNumTransactions; ++i) { |
| 1906 context_list.push_back(new Context()); |
| 1907 Context* c = context_list[i]; |
| 1908 |
| 1909 c->result = cache.CreateTransaction(&c->trans); |
| 1910 ASSERT_THAT(c->result, IsOk()); |
| 1911 |
| 1912 c->result = |
| 1913 c->trans->Start(&request, c->callback.callback(), NetLogWithSource()); |
| 1914 } |
| 1915 |
| 1916 // Delete the currently validating transaction. |
| 1917 Context* c = context_list[1]; |
| 1918 EXPECT_EQ(ERR_IO_PENDING, c->result); |
| 1919 delete c; |
| 1920 context_list[1] = nullptr; |
| 1921 |
| 1922 // Allow all requests to move from the Create queue to the active entry. |
| 1923 base::RunLoop().RunUntilIdle(); |
| 1924 |
| 1925 // The first request should be a writer at this point, and the subsequent |
| 1926 // requests should be pending. |
| 1927 |
| 1928 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 1929 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 1930 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 1931 |
| 1932 for (int i = 0; i < kNumTransactions; ++i) { |
| 1933 if (i == 1) |
| 1934 continue; |
| 1935 Context* c = context_list[i]; |
| 1936 if (c->result == ERR_IO_PENDING) |
| 1937 c->result = c->callback.WaitForResult(); |
| 1938 } |
| 1939 |
| 1940 // Complete the rest of the transactions. |
| 1941 for (int i = 0; i < kNumTransactions; ++i) { |
| 1942 if (i == 1) |
| 1943 continue; |
| 1944 Context* c = context_list[i]; |
| 1945 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction); |
| 1946 } |
| 1947 |
| 1948 // We should have had to re-open the disk entry. |
| 1949 |
| 1950 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 1951 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 1952 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 1953 |
| 1954 for (int i = 0; i < kNumTransactions; ++i) { |
| 1955 if (i == 1) |
| 1956 continue; |
| 1957 Context* c = context_list[i]; |
| 1958 delete c; |
| 1959 } |
| 1960 } |
| 1961 |
1652 // Tests that we can cancel requests that are queued waiting to open the disk | 1962 // Tests that we can cancel requests that are queued waiting to open the disk |
1653 // cache entry. | 1963 // cache entry. |
1654 TEST(HttpCache, SimpleGET_ManyWriters_CancelCreate) { | 1964 TEST(HttpCache, SimpleGET_ManyWriters_CancelCreate) { |
1655 MockHttpCache cache; | 1965 MockHttpCache cache; |
1656 | 1966 |
1657 MockHttpRequest request(kSimpleGET_Transaction); | 1967 MockHttpRequest request(kSimpleGET_Transaction); |
1658 | 1968 |
1659 std::vector<Context*> context_list; | 1969 std::vector<Context*> context_list; |
1660 const int kNumTransactions = 5; | 1970 const int kNumTransactions = 5; |
1661 | 1971 |
(...skipping 871 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2533 } | 2843 } |
2534 | 2844 |
2535 // Helper that does 4 requests using HttpCache: | 2845 // Helper that does 4 requests using HttpCache: |
2536 // | 2846 // |
2537 // (1) loads |kUrl| -- expects |net_response_1| to be returned. | 2847 // (1) loads |kUrl| -- expects |net_response_1| to be returned. |
2538 // (2) loads |kUrl| from cache only -- expects |net_response_1| to be returned. | 2848 // (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 | 2849 // (3) loads |kUrl| using |extra_request_headers| -- expects |net_response_2| to |
2540 // be returned. | 2850 // be returned. |
2541 // (4) loads |kUrl| from cache only -- expects |cached_response_2| to be | 2851 // (4) loads |kUrl| from cache only -- expects |cached_response_2| to be |
2542 // returned. | 2852 // returned. |
| 2853 // The entry will be created once and will be opened for the 3 subsequent |
| 2854 // requests. |
2543 static void ConditionalizedRequestUpdatesCacheHelper( | 2855 static void ConditionalizedRequestUpdatesCacheHelper( |
2544 const Response& net_response_1, | 2856 const Response& net_response_1, |
2545 const Response& net_response_2, | 2857 const Response& net_response_2, |
2546 const Response& cached_response_2, | 2858 const Response& cached_response_2, |
2547 const char* extra_request_headers) { | 2859 const char* extra_request_headers) { |
2548 MockHttpCache cache; | 2860 MockHttpCache cache; |
2549 | 2861 |
2550 // The URL we will be requesting. | 2862 // The URL we will be requesting. |
2551 const char kUrl[] = "http://foobar.com/main.css"; | 2863 const char kUrl[] = "http://foobar.com/main.css"; |
2552 | 2864 |
(...skipping 3555 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
6108 pending->trans->Start(&request, pending->callback.callback(), | 6420 pending->trans->Start(&request, pending->callback.callback(), |
6109 NetLogWithSource())); | 6421 NetLogWithSource())); |
6110 EXPECT_THAT(c->callback.GetResult(rv), IsOk()); | 6422 EXPECT_THAT(c->callback.GetResult(rv), IsOk()); |
6111 | 6423 |
6112 // Make sure that the entry has some data stored. | 6424 // Make sure that the entry has some data stored. |
6113 scoped_refptr<IOBufferWithSize> buf(new IOBufferWithSize(5)); | 6425 scoped_refptr<IOBufferWithSize> buf(new IOBufferWithSize(5)); |
6114 rv = c->trans->Read(buf.get(), buf->size(), c->callback.callback()); | 6426 rv = c->trans->Read(buf.get(), buf->size(), c->callback.callback()); |
6115 EXPECT_EQ(5, c->callback.GetResult(rv)); | 6427 EXPECT_EQ(5, c->callback.GetResult(rv)); |
6116 | 6428 |
6117 // Cancel the requests. | 6429 // Cancel the requests. |
| 6430 // Since |pending| is currently vaidating the already written headers |
| 6431 // it will be restarted as well. |
6118 delete c; | 6432 delete c; |
6119 delete pending; | 6433 delete pending; |
6120 | 6434 |
6121 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 6435 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
6122 EXPECT_EQ(1, cache.disk_cache()->open_count()); | 6436 EXPECT_EQ(1, cache.disk_cache()->open_count()); |
6123 EXPECT_EQ(2, cache.disk_cache()->create_count()); | 6437 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
6124 | 6438 |
6125 base::RunLoop().RunUntilIdle(); | 6439 base::RunLoop().RunUntilIdle(); |
6126 RemoveMockTransaction(&transaction); | 6440 RemoveMockTransaction(&transaction); |
6127 } | 6441 } |
6128 | 6442 |
6129 // Tests that we delete truncated entries if the server changes its mind midway. | 6443 // Tests that we delete truncated entries if the server changes its mind midway. |
6130 TEST(HttpCache, GET_IncompleteResource2) { | 6444 TEST(HttpCache, GET_IncompleteResource2) { |
6131 MockHttpCache cache; | 6445 MockHttpCache cache; |
6132 AddMockTransaction(&kRangeGET_TransactionOK); | 6446 AddMockTransaction(&kRangeGET_TransactionOK); |
6133 | 6447 |
(...skipping 704 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
6838 | 7152 |
6839 RunTransactionTestWithResponseInfo(cache.http_cache(), kSimpleGET_Transaction, | 7153 RunTransactionTestWithResponseInfo(cache.http_cache(), kSimpleGET_Transaction, |
6840 &response); | 7154 &response); |
6841 EXPECT_TRUE(response.metadata.get() == NULL); | 7155 EXPECT_TRUE(response.metadata.get() == NULL); |
6842 | 7156 |
6843 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 7157 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
6844 EXPECT_EQ(2, cache.disk_cache()->open_count()); | 7158 EXPECT_EQ(2, cache.disk_cache()->open_count()); |
6845 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 7159 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
6846 } | 7160 } |
6847 | 7161 |
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 | 7162 // Tests that we ignore VARY checks when writing metadata since the request |
6889 // headers for the WriteMetadata transaction are made up. | 7163 // headers for the WriteMetadata transaction are made up. |
6890 TEST(HttpCache, WriteMetadata_IgnoreVary) { | 7164 TEST(HttpCache, WriteMetadata_IgnoreVary) { |
6891 MockHttpCache cache; | 7165 MockHttpCache cache; |
6892 | 7166 |
6893 // Write to the cache | 7167 // Write to the cache |
6894 HttpResponseInfo response; | 7168 HttpResponseInfo response; |
6895 ScopedMockTransaction transaction(kSimpleGET_Transaction); | 7169 ScopedMockTransaction transaction(kSimpleGET_Transaction); |
6896 transaction.request_headers = "accept-encoding: gzip\r\n"; | 7170 transaction.request_headers = "accept-encoding: gzip\r\n"; |
6897 transaction.response_headers = | 7171 transaction.response_headers = |
(...skipping 307 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
7205 MockHttpRequest request(mock_transaction); | 7479 MockHttpRequest request(mock_transaction); |
7206 | 7480 |
7207 { | 7481 { |
7208 std::unique_ptr<HttpTransaction> trans; | 7482 std::unique_ptr<HttpTransaction> trans; |
7209 ASSERT_THAT(cache.CreateTransaction(&trans), IsOk()); | 7483 ASSERT_THAT(cache.CreateTransaction(&trans), IsOk()); |
7210 | 7484 |
7211 int rv = trans->Start(&request, callback.callback(), NetLogWithSource()); | 7485 int rv = trans->Start(&request, callback.callback(), NetLogWithSource()); |
7212 EXPECT_THAT(callback.GetResult(rv), IsOk()); | 7486 EXPECT_THAT(callback.GetResult(rv), IsOk()); |
7213 | 7487 |
7214 trans->StopCaching(); | 7488 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 } | 7489 } |
7220 RemoveMockTransaction(&mock_transaction); | 7490 RemoveMockTransaction(&mock_transaction); |
7221 | 7491 |
7222 // Make sure that the ActiveEntry is gone. | 7492 // Make sure that the ActiveEntry is gone. |
7223 base::RunLoop().RunUntilIdle(); | 7493 base::RunLoop().RunUntilIdle(); |
7224 | 7494 |
7225 // Verify that the entry is gone. | 7495 // Verify that the entry is gone. |
7226 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); | 7496 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); |
7227 | 7497 |
7228 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | 7498 EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
(...skipping 1173 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
8402 ASSERT_TRUE(attrs->GetDictionary( | 8672 ASSERT_TRUE(attrs->GetDictionary( |
8403 base::trace_event::MemoryAllocatorDump::kNameSize, &size_attrs)); | 8673 base::trace_event::MemoryAllocatorDump::kNameSize, &size_attrs)); |
8404 std::string size; | 8674 std::string size; |
8405 ASSERT_TRUE(size_attrs->GetString("value", &size)); | 8675 ASSERT_TRUE(size_attrs->GetString("value", &size)); |
8406 int actual_size = 0; | 8676 int actual_size = 0; |
8407 ASSERT_TRUE(base::HexStringToInt(size, &actual_size)); | 8677 ASSERT_TRUE(base::HexStringToInt(size, &actual_size)); |
8408 ASSERT_LT(0, actual_size); | 8678 ASSERT_LT(0, actual_size); |
8409 } | 8679 } |
8410 | 8680 |
8411 } // namespace net | 8681 } // namespace net |
OLD | NEW |