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

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

Issue 2721933002: HttpCache::Transaction layer allowing parallel validation (Closed)
Patch Set: Josh's latest feedback addressed 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.
Randy Smith (Not in Mondays) 2017/04/04 21:21:16 What if the currently active headers transaction i
shivanisha 2017/04/05 15:00:37 It seems there doesn't exist a way to pause and re
Randy Smith (Not in Mondays) 2017/04/05 18:39:17 I'm not sure that's true. Did you look at the Bef
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 is in validated queue and writer is destroyed
1617 // leading to restarting the validated transaction.
1618 TEST(HttpCache, SimpleGET_ParallelValidationCancelWriter) {
1619 MockHttpCache cache;
1620
1621 MockHttpRequest request(kSimpleGET_Transaction);
1622
1623 std::vector<Context*> context_list;
1624 const int kNumTransactions = 2;
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(1, cache.GetCountDoneHeadersQueue(kSimpleGET_Transaction.url));
1649
1650 Context* c = context_list[0];
1651 delete c;
1652 context_list[0] = nullptr;
1653
1654 base::RunLoop().RunUntilIdle();
1655
1656 // Complete the rest of the transactions.
1657 for (int i = 0; i < kNumTransactions; ++i) {
1658 if (i == 0)
1659 continue;
1660 Context* c = context_list[i];
1661 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction);
1662 }
1663
1664 EXPECT_EQ(2, cache.network_layer()->transaction_count());
1665 EXPECT_EQ(0, cache.disk_cache()->open_count());
1666 EXPECT_EQ(2, cache.disk_cache()->create_count());
1667
1668 for (int i = 0; i < kNumTransactions; ++i) {
1669 if (i == 1)
1670 continue;
1671 Context* c = context_list[i];
1672 delete c;
1673 }
1674 }
1675 // Similar to the above test, except here cache write fails and the
1676 // validated transactions should be restarted.
1677 TEST(HttpCache, SimpleGET_ParallelValidationFailWrite) {
1678 MockHttpCache cache;
1679
1680 MockHttpRequest request(kSimpleGET_Transaction);
1681
1682 std::vector<Context*> context_list;
1683 const int kNumTransactions = 5;
1684
1685 for (int i = 0; i < kNumTransactions; ++i) {
1686 context_list.push_back(new Context());
1687 Context* c = context_list[i];
1688
1689 c->result = cache.CreateTransaction(&c->trans);
1690 ASSERT_THAT(c->result, IsOk());
1691 EXPECT_EQ(LOAD_STATE_IDLE, c->trans->GetLoadState());
1692
1693 c->result =
1694 c->trans->Start(&request, c->callback.callback(), NetLogWithSource());
1695 }
1696
1697 // All requests are waiting for the active entry.
1698 for (int i = 0; i < kNumTransactions; ++i) {
1699 Context* c = context_list[i];
1700 EXPECT_EQ(LOAD_STATE_WAITING_FOR_CACHE, c->trans->GetLoadState());
1701 }
1702
1703 // Allow all requests to move from the Create queue to the active entry.
1704 base::RunLoop().RunUntilIdle();
1705
1706 // The first request should be a writer at this point, and the subsequent
1707 // requests should have passed the validation phase and waiting for the
1708 // response to be written to the cache before they can read.
1709 EXPECT_TRUE(cache.IsWriterPresent(kSimpleGET_Transaction.url));
1710 EXPECT_EQ(4, cache.GetCountDoneHeadersQueue(kSimpleGET_Transaction.url));
1711
1712 // All requests depend on the writer, and the writer is between Start and
1713 // Read, i.e. idle.
1714 for (int i = 0; i < kNumTransactions; ++i) {
1715 Context* c = context_list[i];
1716 EXPECT_EQ(LOAD_STATE_IDLE, c->trans->GetLoadState());
1717 }
1718
1719 // The first request should be a writer at this point, and the subsequent
1720 // requests should have passed the validation phase and waiting for the
1721 // response to be written to the cache before they can read.
1722
1723 EXPECT_EQ(1, cache.network_layer()->transaction_count());
1724 EXPECT_EQ(0, cache.disk_cache()->open_count());
1725 EXPECT_EQ(1, cache.disk_cache()->create_count());
1726
1727 // Fail the request.
1728 cache.disk_cache()->set_soft_failures(true);
1729 // We have to open the entry again to propagate the failure flag.
1730 disk_cache::Entry* en;
1731 cache.OpenBackendEntry(kSimpleGET_Transaction.url, &en);
1732 en->Close();
1733
1734 for (int i = 0; i < kNumTransactions; ++i) {
1735 Context* c = context_list[i];
1736 if (c->result == ERR_IO_PENDING)
1737 c->result = c->callback.WaitForResult();
1738 if (i == 1) {
1739 // The earlier entry must be destroyed and its disk entry doomed.
1740 EXPECT_TRUE(
1741 cache.disk_cache()->IsDiskEntryDoomed(kSimpleGET_Transaction.url));
1742 }
1743 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction);
1744 }
1745
1746 // Since validated transactions were restarted and new entry read/write
1747 // operations would also fail, all requests would have gone to the network.
1748 EXPECT_EQ(5, cache.network_layer()->transaction_count());
1749 EXPECT_EQ(1, cache.disk_cache()->open_count());
1750 EXPECT_EQ(5, cache.disk_cache()->create_count());
1751
1752 for (int i = 0; i < kNumTransactions; ++i) {
1753 Context* c = context_list[i];
1754 delete c;
1755 }
1756 }
Randy Smith (Not in Mondays) 2017/04/04 21:21:16 Should we also confirm that destroying a reader do
shivanisha 2017/04/05 15:00:37 Added a test for that: HttpCache.SimpleGET_Paralle
Randy Smith (Not in Mondays) 2017/04/05 18:39:17 Acknowledged.
1757
1410 // This is a test for http://code.google.com/p/chromium/issues/detail?id=4769. 1758 // 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 1759 // 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 1760 // finishing, we have to make sure that we remove both transactions from the
1413 // entry. 1761 // entry.
1414 TEST(HttpCache, SimpleGET_RacingReaders) { 1762 TEST(HttpCache, SimpleGET_RacingReaders) {
1415 MockHttpCache cache; 1763 MockHttpCache cache;
1416 1764
1417 MockHttpRequest request(kSimpleGET_Transaction); 1765 MockHttpRequest request(kSimpleGET_Transaction);
1418 MockHttpRequest reader_request(kSimpleGET_Transaction); 1766 MockHttpRequest reader_request(kSimpleGET_Transaction);
1419 reader_request.load_flags = LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION; 1767 reader_request.load_flags = LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION;
(...skipping 24 matching lines...) Expand all
1444 1792
1445 EXPECT_EQ(1, cache.network_layer()->transaction_count()); 1793 EXPECT_EQ(1, cache.network_layer()->transaction_count());
1446 EXPECT_EQ(0, cache.disk_cache()->open_count()); 1794 EXPECT_EQ(0, cache.disk_cache()->open_count());
1447 EXPECT_EQ(1, cache.disk_cache()->create_count()); 1795 EXPECT_EQ(1, cache.disk_cache()->create_count());
1448 1796
1449 Context* c = context_list[0]; 1797 Context* c = context_list[0];
1450 ASSERT_THAT(c->result, IsError(ERR_IO_PENDING)); 1798 ASSERT_THAT(c->result, IsError(ERR_IO_PENDING));
1451 c->result = c->callback.WaitForResult(); 1799 c->result = c->callback.WaitForResult();
1452 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction); 1800 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction);
1453 1801
1454 // Now we have 2 active readers and two queued transactions. 1802 // Now all transactions should be waiting for read to be invoked. Two readers
1455 1803 // are because of the load flags and remaining two transactions were converted
1804 // to readers after skipping validation. Note that the remaining two went on
1805 // to process the headers in parallel with readers presnt on the entry.
1456 EXPECT_EQ(LOAD_STATE_IDLE, context_list[2]->trans->GetLoadState()); 1806 EXPECT_EQ(LOAD_STATE_IDLE, context_list[2]->trans->GetLoadState());
1457 EXPECT_EQ(LOAD_STATE_WAITING_FOR_CACHE, 1807 EXPECT_EQ(LOAD_STATE_IDLE, context_list[3]->trans->GetLoadState());
1458 context_list[3]->trans->GetLoadState());
1459 1808
1460 c = context_list[1]; 1809 c = context_list[1];
1461 ASSERT_THAT(c->result, IsError(ERR_IO_PENDING)); 1810 ASSERT_THAT(c->result, IsError(ERR_IO_PENDING));
1462 c->result = c->callback.WaitForResult(); 1811 c->result = c->callback.WaitForResult();
1463 if (c->result == OK) 1812 if (c->result == OK)
1464 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction); 1813 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction);
1465 1814
1466 // At this point we have one reader, two pending transactions and a task on 1815 // 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 1816 // 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 1817 // 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()); 1862 ASSERT_THAT(c->result, IsOk());
1514 1863
1515 MockHttpRequest* this_request = &request; 1864 MockHttpRequest* this_request = &request;
1516 if (i == 3) 1865 if (i == 3)
1517 this_request = &writer_request; 1866 this_request = &writer_request;
1518 1867
1519 c->result = c->trans->Start(this_request, c->callback.callback(), 1868 c->result = c->trans->Start(this_request, c->callback.callback(),
1520 NetLogWithSource()); 1869 NetLogWithSource());
1521 } 1870 }
1522 1871
1872 base::RunLoop().RunUntilIdle();
1873
1523 // The first request should be a writer at this point, and the two subsequent 1874 // 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. 1875 // requests should be pending. The last request doomed the first entry.
1525 1876
1526 EXPECT_EQ(2, cache.network_layer()->transaction_count()); 1877 EXPECT_EQ(2, cache.network_layer()->transaction_count());
1527 1878
1528 // Cancel the first queued transaction. 1879 // Cancel the second transaction. Note that this and the 3rd transactions
1880 // would have completed their headers phase and would be waiting in the
1881 // done_headers_queue when the 2nd transaction is cancelled.
1529 context_list[1].reset(); 1882 context_list[1].reset();
1530 1883
1531 for (int i = 0; i < kNumTransactions; ++i) { 1884 for (int i = 0; i < kNumTransactions; ++i) {
1532 if (i == 1) 1885 if (i == 1)
1533 continue; 1886 continue;
1534 Context* c = context_list[i].get(); 1887 Context* c = context_list[i].get();
1535 ASSERT_THAT(c->result, IsError(ERR_IO_PENDING)); 1888 ASSERT_THAT(c->result, IsError(ERR_IO_PENDING));
1536 c->result = c->callback.WaitForResult(); 1889 c->result = c->callback.WaitForResult();
1537 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction); 1890 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction);
1538 } 1891 }
(...skipping 21 matching lines...) Expand all
1560 ASSERT_THAT(c->result, IsOk()); 1913 ASSERT_THAT(c->result, IsOk());
1561 1914
1562 c->result = 1915 c->result =
1563 c->trans->Start(&request, c->callback.callback(), NetLogWithSource()); 1916 c->trans->Start(&request, c->callback.callback(), NetLogWithSource());
1564 } 1917 }
1565 1918
1566 // Allow all requests to move from the Create queue to the active entry. 1919 // Allow all requests to move from the Create queue to the active entry.
1567 base::RunLoop().RunUntilIdle(); 1920 base::RunLoop().RunUntilIdle();
1568 1921
1569 // The first request should be a writer at this point, and the subsequent 1922 // The first request should be a writer at this point, and the subsequent
1570 // requests should be pending. 1923 // requests should have completed validation. Since the validation does not
1924 // result in a match, a new entry would be created.
1571 1925
1572 EXPECT_EQ(1, cache.network_layer()->transaction_count()); 1926 EXPECT_EQ(3, cache.network_layer()->transaction_count());
1573 EXPECT_EQ(0, cache.disk_cache()->open_count()); 1927 EXPECT_EQ(0, cache.disk_cache()->open_count());
1574 EXPECT_EQ(1, cache.disk_cache()->create_count()); 1928 EXPECT_EQ(2, cache.disk_cache()->create_count());
1575 1929
1576 // Now, make sure that the second request asks for the entry not to be stored. 1930 // Now, make sure that the second request asks for the entry not to be stored.
1577 request_handler.set_no_store(true); 1931 request_handler.set_no_store(true);
1578 1932
1579 for (int i = 0; i < kNumTransactions; ++i) { 1933 for (int i = 0; i < kNumTransactions; ++i) {
1580 Context* c = context_list[i]; 1934 Context* c = context_list[i];
1581 if (c->result == ERR_IO_PENDING) 1935 if (c->result == ERR_IO_PENDING)
1582 c->result = c->callback.WaitForResult(); 1936 c->result = c->callback.WaitForResult();
1583 ReadAndVerifyTransaction(c->trans.get(), kFastNoStoreGET_Transaction); 1937 ReadAndVerifyTransaction(c->trans.get(), kFastNoStoreGET_Transaction);
1584 delete c; 1938 delete c;
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
1618 1972
1619 EXPECT_EQ(1, cache.network_layer()->transaction_count()); 1973 EXPECT_EQ(1, cache.network_layer()->transaction_count());
1620 EXPECT_EQ(0, cache.disk_cache()->open_count()); 1974 EXPECT_EQ(0, cache.disk_cache()->open_count());
1621 EXPECT_EQ(1, cache.disk_cache()->create_count()); 1975 EXPECT_EQ(1, cache.disk_cache()->create_count());
1622 1976
1623 for (int i = 0; i < kNumTransactions; ++i) { 1977 for (int i = 0; i < kNumTransactions; ++i) {
1624 Context* c = context_list[i]; 1978 Context* c = context_list[i];
1625 if (c->result == ERR_IO_PENDING) 1979 if (c->result == ERR_IO_PENDING)
1626 c->result = c->callback.WaitForResult(); 1980 c->result = c->callback.WaitForResult();
1627 // Destroy only the first transaction. 1981 // Destroy only the first transaction.
1982 // This should lead to all transactions to restart, even those that have
1983 // validated themselves and were waiting for the writer transaction to
1984 // complete writing to the cache.
1628 if (i == 0) { 1985 if (i == 0) {
1629 delete c; 1986 delete c;
1630 context_list[i] = NULL; 1987 context_list[i] = NULL;
1631 } 1988 }
1632 } 1989 }
1633 1990
1634 // Complete the rest of the transactions. 1991 // Complete the rest of the transactions.
1635 for (int i = 1; i < kNumTransactions; ++i) { 1992 for (int i = 1; i < kNumTransactions; ++i) {
1636 Context* c = context_list[i]; 1993 Context* c = context_list[i];
1637 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction); 1994 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction);
(...skipping 895 matching lines...) Expand 10 before | Expand all | Expand 10 after
2533 } 2890 }
2534 2891
2535 // Helper that does 4 requests using HttpCache: 2892 // Helper that does 4 requests using HttpCache:
2536 // 2893 //
2537 // (1) loads |kUrl| -- expects |net_response_1| to be returned. 2894 // (1) loads |kUrl| -- expects |net_response_1| to be returned.
2538 // (2) loads |kUrl| from cache only -- expects |net_response_1| to be returned. 2895 // (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 2896 // (3) loads |kUrl| using |extra_request_headers| -- expects |net_response_2| to
2540 // be returned. 2897 // be returned.
2541 // (4) loads |kUrl| from cache only -- expects |cached_response_2| to be 2898 // (4) loads |kUrl| from cache only -- expects |cached_response_2| to be
2542 // returned. 2899 // returned.
2900 // The entry will be created once and will be opened for the 3 subsequent
2901 // requests.
2543 static void ConditionalizedRequestUpdatesCacheHelper( 2902 static void ConditionalizedRequestUpdatesCacheHelper(
2544 const Response& net_response_1, 2903 const Response& net_response_1,
2545 const Response& net_response_2, 2904 const Response& net_response_2,
2546 const Response& cached_response_2, 2905 const Response& cached_response_2,
2547 const char* extra_request_headers) { 2906 const char* extra_request_headers) {
2548 MockHttpCache cache; 2907 MockHttpCache cache;
2549 2908
2550 // The URL we will be requesting. 2909 // The URL we will be requesting.
2551 const char kUrl[] = "http://foobar.com/main.css"; 2910 const char kUrl[] = "http://foobar.com/main.css";
2552 2911
(...skipping 3555 matching lines...) Expand 10 before | Expand all | Expand 10 after
6108 pending->trans->Start(&request, pending->callback.callback(), 6467 pending->trans->Start(&request, pending->callback.callback(),
6109 NetLogWithSource())); 6468 NetLogWithSource()));
6110 EXPECT_THAT(c->callback.GetResult(rv), IsOk()); 6469 EXPECT_THAT(c->callback.GetResult(rv), IsOk());
6111 6470
6112 // Make sure that the entry has some data stored. 6471 // Make sure that the entry has some data stored.
6113 scoped_refptr<IOBufferWithSize> buf(new IOBufferWithSize(5)); 6472 scoped_refptr<IOBufferWithSize> buf(new IOBufferWithSize(5));
6114 rv = c->trans->Read(buf.get(), buf->size(), c->callback.callback()); 6473 rv = c->trans->Read(buf.get(), buf->size(), c->callback.callback());
6115 EXPECT_EQ(5, c->callback.GetResult(rv)); 6474 EXPECT_EQ(5, c->callback.GetResult(rv));
6116 6475
6117 // Cancel the requests. 6476 // Cancel the requests.
6477 // Since |pending| is currently vaidating the already written headers
6478 // it will be restarted as well.
6118 delete c; 6479 delete c;
6119 delete pending; 6480 delete pending;
6120 6481
6121 EXPECT_EQ(1, cache.network_layer()->transaction_count()); 6482 EXPECT_EQ(1, cache.network_layer()->transaction_count());
6122 EXPECT_EQ(1, cache.disk_cache()->open_count()); 6483 EXPECT_EQ(1, cache.disk_cache()->open_count());
6123 EXPECT_EQ(2, cache.disk_cache()->create_count()); 6484 EXPECT_EQ(1, cache.disk_cache()->create_count());
6124 6485
6125 base::RunLoop().RunUntilIdle(); 6486 base::RunLoop().RunUntilIdle();
6126 RemoveMockTransaction(&transaction); 6487 RemoveMockTransaction(&transaction);
6127 } 6488 }
6128 6489
6129 // Tests that we delete truncated entries if the server changes its mind midway. 6490 // Tests that we delete truncated entries if the server changes its mind midway.
6130 TEST(HttpCache, GET_IncompleteResource2) { 6491 TEST(HttpCache, GET_IncompleteResource2) {
6131 MockHttpCache cache; 6492 MockHttpCache cache;
6132 AddMockTransaction(&kRangeGET_TransactionOK); 6493 AddMockTransaction(&kRangeGET_TransactionOK);
6133 6494
(...skipping 704 matching lines...) Expand 10 before | Expand all | Expand 10 after
6838 7199
6839 RunTransactionTestWithResponseInfo(cache.http_cache(), kSimpleGET_Transaction, 7200 RunTransactionTestWithResponseInfo(cache.http_cache(), kSimpleGET_Transaction,
6840 &response); 7201 &response);
6841 EXPECT_TRUE(response.metadata.get() == NULL); 7202 EXPECT_TRUE(response.metadata.get() == NULL);
6842 7203
6843 EXPECT_EQ(1, cache.network_layer()->transaction_count()); 7204 EXPECT_EQ(1, cache.network_layer()->transaction_count());
6844 EXPECT_EQ(2, cache.disk_cache()->open_count()); 7205 EXPECT_EQ(2, cache.disk_cache()->open_count());
6845 EXPECT_EQ(1, cache.disk_cache()->create_count()); 7206 EXPECT_EQ(1, cache.disk_cache()->create_count());
6846 } 7207 }
6847 7208
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 7209 // Tests that we ignore VARY checks when writing metadata since the request
6889 // headers for the WriteMetadata transaction are made up. 7210 // headers for the WriteMetadata transaction are made up.
6890 TEST(HttpCache, WriteMetadata_IgnoreVary) { 7211 TEST(HttpCache, WriteMetadata_IgnoreVary) {
6891 MockHttpCache cache; 7212 MockHttpCache cache;
6892 7213
6893 // Write to the cache 7214 // Write to the cache
6894 HttpResponseInfo response; 7215 HttpResponseInfo response;
6895 ScopedMockTransaction transaction(kSimpleGET_Transaction); 7216 ScopedMockTransaction transaction(kSimpleGET_Transaction);
6896 transaction.request_headers = "accept-encoding: gzip\r\n"; 7217 transaction.request_headers = "accept-encoding: gzip\r\n";
6897 transaction.response_headers = 7218 transaction.response_headers =
(...skipping 307 matching lines...) Expand 10 before | Expand all | Expand 10 after
7205 MockHttpRequest request(mock_transaction); 7526 MockHttpRequest request(mock_transaction);
7206 7527
7207 { 7528 {
7208 std::unique_ptr<HttpTransaction> trans; 7529 std::unique_ptr<HttpTransaction> trans;
7209 ASSERT_THAT(cache.CreateTransaction(&trans), IsOk()); 7530 ASSERT_THAT(cache.CreateTransaction(&trans), IsOk());
7210 7531
7211 int rv = trans->Start(&request, callback.callback(), NetLogWithSource()); 7532 int rv = trans->Start(&request, callback.callback(), NetLogWithSource());
7212 EXPECT_THAT(callback.GetResult(rv), IsOk()); 7533 EXPECT_THAT(callback.GetResult(rv), IsOk());
7213 7534
7214 trans->StopCaching(); 7535 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 } 7536 }
7220 RemoveMockTransaction(&mock_transaction); 7537 RemoveMockTransaction(&mock_transaction);
7221 7538
7222 // Make sure that the ActiveEntry is gone. 7539 // Make sure that the ActiveEntry is gone.
7223 base::RunLoop().RunUntilIdle(); 7540 base::RunLoop().RunUntilIdle();
7224 7541
7225 // Verify that the entry is gone. 7542 // Verify that the entry is gone.
7226 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); 7543 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction);
7227 7544
7228 EXPECT_EQ(2, cache.network_layer()->transaction_count()); 7545 EXPECT_EQ(2, cache.network_layer()->transaction_count());
(...skipping 1173 matching lines...) Expand 10 before | Expand all | Expand 10 after
8402 ASSERT_TRUE(attrs->GetDictionary( 8719 ASSERT_TRUE(attrs->GetDictionary(
8403 base::trace_event::MemoryAllocatorDump::kNameSize, &size_attrs)); 8720 base::trace_event::MemoryAllocatorDump::kNameSize, &size_attrs));
8404 std::string size; 8721 std::string size;
8405 ASSERT_TRUE(size_attrs->GetString("value", &size)); 8722 ASSERT_TRUE(size_attrs->GetString("value", &size));
8406 int actual_size = 0; 8723 int actual_size = 0;
8407 ASSERT_TRUE(base::HexStringToInt(size, &actual_size)); 8724 ASSERT_TRUE(base::HexStringToInt(size, &actual_size));
8408 ASSERT_LT(0, actual_size); 8725 ASSERT_LT(0, actual_size);
8409 } 8726 }
8410 8727
8411 } // namespace net 8728 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698