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 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
140 // same value. | 140 // same value. |
141 EXPECT_FALSE(load_timing_info.send_start.is_null()); | 141 EXPECT_FALSE(load_timing_info.send_start.is_null()); |
142 EXPECT_EQ(load_timing_info.send_start, load_timing_info.send_end); | 142 EXPECT_EQ(load_timing_info.send_start, load_timing_info.send_end); |
143 | 143 |
144 // Set by URLRequest / URLRequestHttpJob, at a higher level. | 144 // Set by URLRequest / URLRequestHttpJob, at a higher level. |
145 EXPECT_TRUE(load_timing_info.request_start_time.is_null()); | 145 EXPECT_TRUE(load_timing_info.request_start_time.is_null()); |
146 EXPECT_TRUE(load_timing_info.request_start.is_null()); | 146 EXPECT_TRUE(load_timing_info.request_start.is_null()); |
147 EXPECT_TRUE(load_timing_info.receive_headers_end.is_null()); | 147 EXPECT_TRUE(load_timing_info.receive_headers_end.is_null()); |
148 } | 148 } |
149 | 149 |
| 150 void DeferNetworkStart(bool* defer) { |
| 151 *defer = true; |
| 152 } |
| 153 |
150 class DeleteCacheCompletionCallback : public TestCompletionCallbackBase { | 154 class DeleteCacheCompletionCallback : public TestCompletionCallbackBase { |
151 public: | 155 public: |
152 explicit DeleteCacheCompletionCallback(MockHttpCache* cache) | 156 explicit DeleteCacheCompletionCallback(MockHttpCache* cache) |
153 : cache_(cache), | 157 : cache_(cache), |
154 callback_(base::Bind(&DeleteCacheCompletionCallback::OnComplete, | 158 callback_(base::Bind(&DeleteCacheCompletionCallback::OnComplete, |
155 base::Unretained(this))) { | 159 base::Unretained(this))) { |
156 } | 160 } |
157 | 161 |
158 const CompletionCallback& callback() const { return callback_; } | 162 const CompletionCallback& callback() const { return callback_; } |
159 | 163 |
(...skipping 1183 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1343 EXPECT_EQ(1, cache.disk_cache()->open_count()); | 1347 EXPECT_EQ(1, cache.disk_cache()->open_count()); |
1344 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 1348 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
1345 RemoveMockTransaction(&transaction); | 1349 RemoveMockTransaction(&transaction); |
1346 } | 1350 } |
1347 | 1351 |
1348 TEST(HttpCache, SimpleGET_ManyReaders) { | 1352 TEST(HttpCache, SimpleGET_ManyReaders) { |
1349 MockHttpCache cache; | 1353 MockHttpCache cache; |
1350 | 1354 |
1351 MockHttpRequest request(kSimpleGET_Transaction); | 1355 MockHttpRequest request(kSimpleGET_Transaction); |
1352 | 1356 |
1353 std::vector<Context*> context_list; | 1357 std::vector<std::unique_ptr<Context>> context_list; |
1354 const int kNumTransactions = 5; | 1358 const int kNumTransactions = 5; |
1355 | 1359 |
1356 for (int i = 0; i < kNumTransactions; ++i) { | 1360 for (int i = 0; i < kNumTransactions; ++i) { |
1357 context_list.push_back(new Context()); | 1361 context_list.push_back(base::MakeUnique<Context>()); |
1358 Context* c = context_list[i]; | 1362 auto& c = context_list[i]; |
1359 | 1363 |
1360 c->result = cache.CreateTransaction(&c->trans); | 1364 c->result = cache.CreateTransaction(&c->trans); |
1361 ASSERT_THAT(c->result, IsOk()); | 1365 ASSERT_THAT(c->result, IsOk()); |
1362 EXPECT_EQ(LOAD_STATE_IDLE, c->trans->GetLoadState()); | 1366 EXPECT_EQ(LOAD_STATE_IDLE, c->trans->GetLoadState()); |
1363 | 1367 |
1364 c->result = | 1368 c->result = |
1365 c->trans->Start(&request, c->callback.callback(), NetLogWithSource()); | 1369 c->trans->Start(&request, c->callback.callback(), NetLogWithSource()); |
1366 } | 1370 } |
1367 | 1371 |
1368 // All requests are waiting for the active entry. | 1372 // All requests are waiting for the active entry. |
1369 for (int i = 0; i < kNumTransactions; ++i) { | 1373 for (auto& context : context_list) { |
1370 Context* c = context_list[i]; | 1374 EXPECT_EQ(LOAD_STATE_WAITING_FOR_CACHE, context->trans->GetLoadState()); |
1371 EXPECT_EQ(LOAD_STATE_WAITING_FOR_CACHE, c->trans->GetLoadState()); | |
1372 } | 1375 } |
1373 | 1376 |
1374 // Allow all requests to move from the Create queue to the active entry. | 1377 // Allow all requests to move from the Create queue to the active entry. |
1375 base::RunLoop().RunUntilIdle(); | 1378 base::RunLoop().RunUntilIdle(); |
1376 | 1379 |
1377 // The first request should be a writer at this point, and the subsequent | 1380 // The first request should be a writer at this point, and the subsequent |
1378 // requests should be pending. | 1381 // requests should have passed the validation phase and waiting for the |
| 1382 // response to be written to the cache before they can read. |
| 1383 EXPECT_TRUE(cache.IsWriterPresent(kSimpleGET_Transaction.url)); |
| 1384 EXPECT_EQ(kNumTransactions - 1, |
| 1385 cache.GetCountDoneHeadersQueue(kSimpleGET_Transaction.url)); |
1379 | 1386 |
1380 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 1387 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
1381 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 1388 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
1382 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 1389 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
1383 | 1390 |
1384 // All requests depend on the writer, and the writer is between Start and | 1391 // All requests depend on the writer, and the writer is between Start and |
1385 // Read, i.e. idle. | 1392 // Read, i.e. idle. |
1386 for (int i = 0; i < kNumTransactions; ++i) { | 1393 for (auto& context : context_list) { |
1387 Context* c = context_list[i]; | 1394 EXPECT_EQ(LOAD_STATE_IDLE, context->trans->GetLoadState()); |
1388 EXPECT_EQ(LOAD_STATE_IDLE, c->trans->GetLoadState()); | |
1389 } | 1395 } |
1390 | 1396 |
1391 for (int i = 0; i < kNumTransactions; ++i) { | 1397 for (int i = 0; i < kNumTransactions; ++i) { |
1392 Context* c = context_list[i]; | 1398 auto& c = context_list[i]; |
1393 if (c->result == ERR_IO_PENDING) | 1399 if (c->result == ERR_IO_PENDING) |
1394 c->result = c->callback.WaitForResult(); | 1400 c->result = c->callback.WaitForResult(); |
| 1401 |
| 1402 if (i > 0) { |
| 1403 EXPECT_FALSE(cache.IsWriterPresent(kSimpleGET_Transaction.url)); |
| 1404 EXPECT_EQ(kNumTransactions - i, |
| 1405 cache.GetCountReaders(kSimpleGET_Transaction.url)); |
| 1406 } |
| 1407 |
1395 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction); | 1408 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction); |
1396 } | 1409 } |
1397 | 1410 |
1398 // We should not have had to re-open the disk entry | 1411 // We should not have had to re-open the disk entry |
1399 | 1412 |
1400 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 1413 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
1401 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 1414 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
1402 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 1415 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
1403 | 1416 } |
1404 for (int i = 0; i < kNumTransactions; ++i) { | 1417 |
1405 Context* c = context_list[i]; | 1418 // Parallel validation results in 200. |
1406 delete c; | 1419 TEST(HttpCache, SimpleGET_ParallelValidationNoMatch) { |
1407 } | 1420 MockHttpCache cache; |
| 1421 |
| 1422 MockHttpRequest request(kSimpleGET_Transaction); |
| 1423 request.load_flags |= LOAD_VALIDATE_CACHE; |
| 1424 |
| 1425 std::vector<std::unique_ptr<Context>> context_list; |
| 1426 const int kNumTransactions = 5; |
| 1427 |
| 1428 for (int i = 0; i < kNumTransactions; ++i) { |
| 1429 context_list.push_back(base::MakeUnique<Context>()); |
| 1430 auto& c = context_list[i]; |
| 1431 |
| 1432 c->result = cache.CreateTransaction(&c->trans); |
| 1433 ASSERT_THAT(c->result, IsOk()); |
| 1434 EXPECT_EQ(LOAD_STATE_IDLE, c->trans->GetLoadState()); |
| 1435 |
| 1436 c->result = |
| 1437 c->trans->Start(&request, c->callback.callback(), NetLogWithSource()); |
| 1438 } |
| 1439 |
| 1440 // All requests are waiting for the active entry. |
| 1441 for (auto& context : context_list) { |
| 1442 EXPECT_EQ(LOAD_STATE_WAITING_FOR_CACHE, context->trans->GetLoadState()); |
| 1443 } |
| 1444 |
| 1445 // Allow all requests to move from the Create queue to the active entry. |
| 1446 base::RunLoop().RunUntilIdle(); |
| 1447 |
| 1448 // 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 // entries since none of them matched the headers of the earlier one. |
| 1451 EXPECT_TRUE(cache.IsWriterPresent(kSimpleGET_Transaction.url)); |
| 1452 |
| 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()); |
| 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 (auto& context : context_list) { |
| 1462 EXPECT_EQ(LOAD_STATE_IDLE, context->trans->GetLoadState()); |
| 1463 } |
| 1464 |
| 1465 for (auto& context : context_list) { |
| 1466 if (context->result == ERR_IO_PENDING) |
| 1467 context->result = context->callback.WaitForResult(); |
| 1468 ReadAndVerifyTransaction(context->trans.get(), kSimpleGET_Transaction); |
| 1469 } |
| 1470 |
| 1471 EXPECT_EQ(5, cache.network_layer()->transaction_count()); |
| 1472 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 1473 EXPECT_EQ(3, cache.disk_cache()->create_count()); |
| 1474 } |
| 1475 |
| 1476 // Tests that a GET followed by a DELETE results in DELETE immediately starting |
| 1477 // the headers phase and the entry is doomed. |
| 1478 TEST(HttpCache, SimpleGET_ParallelValidationDelete) { |
| 1479 MockHttpCache cache; |
| 1480 |
| 1481 MockHttpRequest request(kSimpleGET_Transaction); |
| 1482 request.load_flags |= LOAD_VALIDATE_CACHE; |
| 1483 |
| 1484 MockHttpRequest delete_request(kSimpleGET_Transaction); |
| 1485 delete_request.method = "DELETE"; |
| 1486 |
| 1487 std::vector<std::unique_ptr<Context>> context_list; |
| 1488 const int kNumTransactions = 2; |
| 1489 |
| 1490 for (int i = 0; i < kNumTransactions; ++i) { |
| 1491 context_list.push_back(base::MakeUnique<Context>()); |
| 1492 auto& c = context_list[i]; |
| 1493 |
| 1494 MockHttpRequest* this_request = &request; |
| 1495 if (i == 1) |
| 1496 this_request = &delete_request; |
| 1497 |
| 1498 c->result = cache.CreateTransaction(&c->trans); |
| 1499 ASSERT_THAT(c->result, IsOk()); |
| 1500 EXPECT_EQ(LOAD_STATE_IDLE, c->trans->GetLoadState()); |
| 1501 |
| 1502 c->result = c->trans->Start(this_request, c->callback.callback(), |
| 1503 NetLogWithSource()); |
| 1504 } |
| 1505 |
| 1506 // All requests are waiting for the active entry. |
| 1507 for (auto& context : context_list) { |
| 1508 EXPECT_EQ(LOAD_STATE_WAITING_FOR_CACHE, context->trans->GetLoadState()); |
| 1509 } |
| 1510 |
| 1511 // Allow all requests to move from the Create queue to the active entry. |
| 1512 base::RunLoop().RunUntilIdle(); |
| 1513 |
| 1514 // The first request should be a writer at this point, and the subsequent |
| 1515 // request should have passed the validation phase and doomed the existing |
| 1516 // entry. |
| 1517 EXPECT_TRUE( |
| 1518 cache.disk_cache()->IsDiskEntryDoomed(kSimpleGET_Transaction.url)); |
| 1519 |
| 1520 EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 1521 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 1522 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 1523 |
| 1524 // All requests depend on the writer, and the writer is between Start and |
| 1525 // Read, i.e. idle. |
| 1526 for (auto& context : context_list) { |
| 1527 EXPECT_EQ(LOAD_STATE_IDLE, context->trans->GetLoadState()); |
| 1528 } |
| 1529 |
| 1530 for (auto& context : context_list) { |
| 1531 if (context->result == ERR_IO_PENDING) |
| 1532 context->result = context->callback.WaitForResult(); |
| 1533 ReadAndVerifyTransaction(context->trans.get(), kSimpleGET_Transaction); |
| 1534 } |
| 1535 |
| 1536 EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 1537 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 1538 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 1539 } |
| 1540 |
| 1541 // Tests that a transaction which is in validated queue can be destroyed without |
| 1542 // any impact to other transactions. |
| 1543 TEST(HttpCache, SimpleGET_ParallelValidationCancelValidated) { |
| 1544 MockHttpCache cache; |
| 1545 |
| 1546 MockHttpRequest request(kSimpleGET_Transaction); |
| 1547 |
| 1548 std::vector<std::unique_ptr<Context>> context_list; |
| 1549 const int kNumTransactions = 2; |
| 1550 |
| 1551 for (int i = 0; i < kNumTransactions; ++i) { |
| 1552 context_list.push_back(base::MakeUnique<Context>()); |
| 1553 auto& c = context_list[i]; |
| 1554 |
| 1555 c->result = cache.CreateTransaction(&c->trans); |
| 1556 ASSERT_THAT(c->result, IsOk()); |
| 1557 |
| 1558 c->result = |
| 1559 c->trans->Start(&request, c->callback.callback(), NetLogWithSource()); |
| 1560 } |
| 1561 |
| 1562 // Allow all requests to move from the Create queue to the active entry. |
| 1563 base::RunLoop().RunUntilIdle(); |
| 1564 |
| 1565 // The first request should be a writer at this point, and the subsequent |
| 1566 // requests should have completed validation. |
| 1567 |
| 1568 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 1569 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 1570 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 1571 |
| 1572 EXPECT_TRUE(cache.IsWriterPresent(kSimpleGET_Transaction.url)); |
| 1573 EXPECT_EQ(1, cache.GetCountDoneHeadersQueue(kSimpleGET_Transaction.url)); |
| 1574 |
| 1575 context_list[1].reset(); |
| 1576 |
| 1577 EXPECT_EQ(0, cache.GetCountDoneHeadersQueue(kSimpleGET_Transaction.url)); |
| 1578 |
| 1579 // Complete the rest of the transactions. |
| 1580 for (auto& context : context_list) { |
| 1581 if (!context) |
| 1582 continue; |
| 1583 ReadAndVerifyTransaction(context->trans.get(), kSimpleGET_Transaction); |
| 1584 } |
| 1585 |
| 1586 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 1587 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 1588 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 1589 } |
| 1590 |
| 1591 // Tests that a transaction which is in readers can be destroyed without |
| 1592 // any impact to other transactions. |
| 1593 TEST(HttpCache, SimpleGET_ParallelValidationCancelReader) { |
| 1594 MockHttpCache cache; |
| 1595 |
| 1596 MockHttpRequest request(kSimpleGET_Transaction); |
| 1597 |
| 1598 MockTransaction transaction(kSimpleGET_Transaction); |
| 1599 transaction.load_flags |= LOAD_VALIDATE_CACHE; |
| 1600 MockHttpRequest validate_request(transaction); |
| 1601 |
| 1602 int kNumTransactions = 4; |
| 1603 std::vector<std::unique_ptr<Context>> context_list; |
| 1604 |
| 1605 for (int i = 0; i < kNumTransactions; ++i) { |
| 1606 context_list.push_back(base::MakeUnique<Context>()); |
| 1607 auto& c = context_list[i]; |
| 1608 |
| 1609 c->result = cache.CreateTransaction(&c->trans); |
| 1610 ASSERT_THAT(c->result, IsOk()); |
| 1611 |
| 1612 MockHttpRequest* this_request = &request; |
| 1613 if (i == 3) { |
| 1614 this_request = &validate_request; |
| 1615 c->trans->SetBeforeNetworkStartCallback(base::Bind(&DeferNetworkStart)); |
| 1616 } |
| 1617 |
| 1618 c->result = c->trans->Start(this_request, c->callback.callback(), |
| 1619 NetLogWithSource()); |
| 1620 } |
| 1621 |
| 1622 // Allow all requests to move from the Create queue to the active entry. |
| 1623 base::RunLoop().RunUntilIdle(); |
| 1624 |
| 1625 EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 1626 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 1627 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 1628 |
| 1629 EXPECT_TRUE(cache.IsWriterPresent(kSimpleGET_Transaction.url)); |
| 1630 EXPECT_EQ(2, cache.GetCountDoneHeadersQueue(kSimpleGET_Transaction.url)); |
| 1631 EXPECT_TRUE(cache.IsHeadersTransactionPresent(kSimpleGET_Transaction.url)); |
| 1632 |
| 1633 // Complete the response body. |
| 1634 auto& c = context_list[0]; |
| 1635 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction); |
| 1636 |
| 1637 // Rest of the transactions should move to readers. |
| 1638 EXPECT_FALSE(cache.IsWriterPresent(kSimpleGET_Transaction.url)); |
| 1639 EXPECT_EQ(2, cache.GetCountReaders(kSimpleGET_Transaction.url)); |
| 1640 EXPECT_EQ(0, cache.GetCountDoneHeadersQueue(kSimpleGET_Transaction.url)); |
| 1641 EXPECT_TRUE(cache.IsHeadersTransactionPresent(kSimpleGET_Transaction.url)); |
| 1642 |
| 1643 // Add 2 new transactions. |
| 1644 kNumTransactions = 6; |
| 1645 |
| 1646 for (int i = 4; i < kNumTransactions; ++i) { |
| 1647 context_list.push_back(base::MakeUnique<Context>()); |
| 1648 auto& c = context_list[i]; |
| 1649 |
| 1650 c->result = cache.CreateTransaction(&c->trans); |
| 1651 ASSERT_THAT(c->result, IsOk()); |
| 1652 |
| 1653 c->result = |
| 1654 c->trans->Start(&request, c->callback.callback(), NetLogWithSource()); |
| 1655 } |
| 1656 |
| 1657 EXPECT_EQ(2, cache.GetCountAddToEntryQueue(kSimpleGET_Transaction.url)); |
| 1658 |
| 1659 // Delete a reader. |
| 1660 context_list[1].reset(); |
| 1661 |
| 1662 // Deleting the reader did not impact any other transaction. |
| 1663 EXPECT_EQ(1, cache.GetCountReaders(kSimpleGET_Transaction.url)); |
| 1664 EXPECT_EQ(2, cache.GetCountAddToEntryQueue(kSimpleGET_Transaction.url)); |
| 1665 EXPECT_TRUE(cache.IsHeadersTransactionPresent(kSimpleGET_Transaction.url)); |
| 1666 |
| 1667 // 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. |
| 1669 auto& context = context_list[3]; |
| 1670 context->trans->ResumeNetworkStart(); |
| 1671 |
| 1672 // The pending transactions will be added to a new entry. |
| 1673 base::RunLoop().RunUntilIdle(); |
| 1674 |
| 1675 EXPECT_EQ(1, cache.GetCountDoneHeadersQueue(kSimpleGET_Transaction.url)); |
| 1676 EXPECT_TRUE(cache.IsWriterPresent(kSimpleGET_Transaction.url)); |
| 1677 |
| 1678 // Complete the rest of the transactions. |
| 1679 for (int i = 2; i < kNumTransactions; ++i) { |
| 1680 auto& c = context_list[i]; |
| 1681 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction); |
| 1682 } |
| 1683 |
| 1684 EXPECT_EQ(3, cache.network_layer()->transaction_count()); |
| 1685 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 1686 EXPECT_EQ(2, cache.disk_cache()->create_count()); |
| 1687 } |
| 1688 |
| 1689 // Tests that a transaction is in validated queue and writer is destroyed |
| 1690 // leading to restarting the validated transaction. |
| 1691 TEST(HttpCache, SimpleGET_ParallelValidationCancelWriter) { |
| 1692 MockHttpCache cache; |
| 1693 |
| 1694 MockHttpRequest request(kSimpleGET_Transaction); |
| 1695 |
| 1696 MockTransaction transaction(kSimpleGET_Transaction); |
| 1697 transaction.load_flags |= LOAD_VALIDATE_CACHE; |
| 1698 MockHttpRequest validate_request(transaction); |
| 1699 |
| 1700 const int kNumTransactions = 3; |
| 1701 std::vector<std::unique_ptr<Context>> context_list; |
| 1702 |
| 1703 for (int i = 0; i < kNumTransactions; ++i) { |
| 1704 context_list.push_back(base::MakeUnique<Context>()); |
| 1705 auto& c = context_list[i]; |
| 1706 |
| 1707 c->result = cache.CreateTransaction(&c->trans); |
| 1708 ASSERT_THAT(c->result, IsOk()); |
| 1709 |
| 1710 MockHttpRequest* this_request = &request; |
| 1711 if (i == 2) { |
| 1712 this_request = &validate_request; |
| 1713 c->trans->SetBeforeNetworkStartCallback(base::Bind(&DeferNetworkStart)); |
| 1714 } |
| 1715 |
| 1716 c->result = c->trans->Start(this_request, c->callback.callback(), |
| 1717 NetLogWithSource()); |
| 1718 } |
| 1719 |
| 1720 // Allow all requests to move from the Create queue to the active entry. |
| 1721 base::RunLoop().RunUntilIdle(); |
| 1722 |
| 1723 EXPECT_EQ(2, 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 EXPECT_TRUE(cache.IsWriterPresent(kSimpleGET_Transaction.url)); |
| 1728 EXPECT_TRUE(cache.IsHeadersTransactionPresent(kSimpleGET_Transaction.url)); |
| 1729 EXPECT_EQ(1, cache.GetCountDoneHeadersQueue(kSimpleGET_Transaction.url)); |
| 1730 |
| 1731 // Deleting the writer at this point will lead to destroying the entry and |
| 1732 // restarting the remaining transactions which will then create a new entry. |
| 1733 context_list[0].reset(); |
| 1734 |
| 1735 // Resume network start for headers_transaction. It should be restarted due to |
| 1736 // writer cancellation. |
| 1737 auto& c = context_list[2]; |
| 1738 c->trans->ResumeNetworkStart(); |
| 1739 |
| 1740 base::RunLoop().RunUntilIdle(); |
| 1741 |
| 1742 EXPECT_TRUE(cache.IsWriterPresent(kSimpleGET_Transaction.url)); |
| 1743 EXPECT_TRUE(cache.IsHeadersTransactionPresent(kSimpleGET_Transaction.url)); |
| 1744 |
| 1745 // Resume network start for the transaction the second time. |
| 1746 c->trans->ResumeNetworkStart(); |
| 1747 base::RunLoop().RunUntilIdle(); |
| 1748 |
| 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. |
| 1754 for (auto& context : context_list) { |
| 1755 if (!context) |
| 1756 continue; |
| 1757 ReadAndVerifyTransaction(context->trans.get(), kSimpleGET_Transaction); |
| 1758 } |
| 1759 |
| 1760 EXPECT_EQ(4, cache.network_layer()->transaction_count()); |
| 1761 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 1762 EXPECT_EQ(2, cache.disk_cache()->create_count()); |
| 1763 } |
| 1764 |
| 1765 // Tests that a transaction is currently in headers phase and is destroyed |
| 1766 // leading to destroying the entry. |
| 1767 TEST(HttpCache, SimpleGET_ParallelValidationCancelHeaders) { |
| 1768 MockHttpCache cache; |
| 1769 |
| 1770 MockHttpRequest request(kSimpleGET_Transaction); |
| 1771 |
| 1772 const int kNumTransactions = 2; |
| 1773 std::vector<std::unique_ptr<Context>> context_list; |
| 1774 |
| 1775 for (int i = 0; i < kNumTransactions; ++i) { |
| 1776 context_list.push_back(base::MakeUnique<Context>()); |
| 1777 auto& c = context_list[i]; |
| 1778 |
| 1779 c->result = cache.CreateTransaction(&c->trans); |
| 1780 ASSERT_THAT(c->result, IsOk()); |
| 1781 |
| 1782 if (i == 0) |
| 1783 c->trans->SetBeforeNetworkStartCallback(base::Bind(&DeferNetworkStart)); |
| 1784 |
| 1785 c->result = |
| 1786 c->trans->Start(&request, c->callback.callback(), NetLogWithSource()); |
| 1787 } |
| 1788 |
| 1789 base::RunLoop().RunUntilIdle(); |
| 1790 |
| 1791 EXPECT_TRUE(cache.IsHeadersTransactionPresent(kSimpleGET_Transaction.url)); |
| 1792 EXPECT_EQ(1, cache.GetCountAddToEntryQueue(kSimpleGET_Transaction.url)); |
| 1793 |
| 1794 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 1795 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 1796 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 1797 |
| 1798 // Delete the headers transaction. |
| 1799 context_list[0].reset(); |
| 1800 |
| 1801 base::RunLoop().RunUntilIdle(); |
| 1802 |
| 1803 // Complete the rest of the transactions. |
| 1804 for (auto& context : context_list) { |
| 1805 if (!context) |
| 1806 continue; |
| 1807 ReadAndVerifyTransaction(context->trans.get(), kSimpleGET_Transaction); |
| 1808 } |
| 1809 |
| 1810 EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 1811 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 1812 EXPECT_EQ(2, cache.disk_cache()->create_count()); |
| 1813 } |
| 1814 |
| 1815 // Similar to the above test, except here cache write fails and the |
| 1816 // validated transactions should be restarted. |
| 1817 TEST(HttpCache, SimpleGET_ParallelValidationFailWrite) { |
| 1818 MockHttpCache cache; |
| 1819 |
| 1820 MockHttpRequest request(kSimpleGET_Transaction); |
| 1821 |
| 1822 const int kNumTransactions = 5; |
| 1823 std::vector<std::unique_ptr<Context>> context_list; |
| 1824 |
| 1825 for (int i = 0; i < kNumTransactions; ++i) { |
| 1826 context_list.push_back(base::MakeUnique<Context>()); |
| 1827 auto& c = context_list[i]; |
| 1828 |
| 1829 c->result = cache.CreateTransaction(&c->trans); |
| 1830 ASSERT_THAT(c->result, IsOk()); |
| 1831 EXPECT_EQ(LOAD_STATE_IDLE, c->trans->GetLoadState()); |
| 1832 |
| 1833 c->result = |
| 1834 c->trans->Start(&request, c->callback.callback(), NetLogWithSource()); |
| 1835 } |
| 1836 |
| 1837 // All requests are waiting for the active entry. |
| 1838 for (auto& context : context_list) { |
| 1839 EXPECT_EQ(LOAD_STATE_WAITING_FOR_CACHE, context->trans->GetLoadState()); |
| 1840 } |
| 1841 |
| 1842 // Allow all requests to move from the Create queue to the active entry. |
| 1843 base::RunLoop().RunUntilIdle(); |
| 1844 |
| 1845 // The first request should be a writer at this point, and the subsequent |
| 1846 // requests should have passed the validation phase and waiting for the |
| 1847 // response to be written to the cache before they can read. |
| 1848 EXPECT_TRUE(cache.IsWriterPresent(kSimpleGET_Transaction.url)); |
| 1849 EXPECT_EQ(4, cache.GetCountDoneHeadersQueue(kSimpleGET_Transaction.url)); |
| 1850 |
| 1851 // All requests depend on the writer, and the writer is between Start and |
| 1852 // Read, i.e. idle. |
| 1853 for (auto& context : context_list) { |
| 1854 EXPECT_EQ(LOAD_STATE_IDLE, context->trans->GetLoadState()); |
| 1855 } |
| 1856 |
| 1857 // The first request should be a writer at this point, and the subsequent |
| 1858 // requests should have passed the validation phase and waiting for the |
| 1859 // response to be written to the cache before they can read. |
| 1860 |
| 1861 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 1862 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 1863 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 1864 |
| 1865 // Fail the request. |
| 1866 cache.disk_cache()->set_soft_failures(true); |
| 1867 // We have to open the entry again to propagate the failure flag. |
| 1868 disk_cache::Entry* en; |
| 1869 cache.OpenBackendEntry(kSimpleGET_Transaction.url, &en); |
| 1870 en->Close(); |
| 1871 |
| 1872 for (int i = 0; i < kNumTransactions; ++i) { |
| 1873 auto& c = context_list[i]; |
| 1874 if (c->result == ERR_IO_PENDING) |
| 1875 c->result = c->callback.WaitForResult(); |
| 1876 if (i == 1) { |
| 1877 // The earlier entry must be destroyed and its disk entry doomed. |
| 1878 EXPECT_TRUE( |
| 1879 cache.disk_cache()->IsDiskEntryDoomed(kSimpleGET_Transaction.url)); |
| 1880 } |
| 1881 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction); |
| 1882 } |
| 1883 |
| 1884 // Since validated transactions were restarted and new entry read/write |
| 1885 // operations would also fail, all requests would have gone to the network. |
| 1886 EXPECT_EQ(5, cache.network_layer()->transaction_count()); |
| 1887 EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 1888 EXPECT_EQ(5, cache.disk_cache()->create_count()); |
1408 } | 1889 } |
1409 | 1890 |
1410 // This is a test for http://code.google.com/p/chromium/issues/detail?id=4769. | 1891 // 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 | 1892 // 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 | 1893 // finishing, we have to make sure that we remove both transactions from the |
1413 // entry. | 1894 // entry. |
1414 TEST(HttpCache, SimpleGET_RacingReaders) { | 1895 TEST(HttpCache, SimpleGET_RacingReaders) { |
1415 MockHttpCache cache; | 1896 MockHttpCache cache; |
1416 | 1897 |
1417 MockHttpRequest request(kSimpleGET_Transaction); | 1898 MockHttpRequest request(kSimpleGET_Transaction); |
(...skipping 26 matching lines...) Expand all Loading... |
1444 | 1925 |
1445 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 1926 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
1446 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 1927 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
1447 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 1928 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
1448 | 1929 |
1449 Context* c = context_list[0]; | 1930 Context* c = context_list[0]; |
1450 ASSERT_THAT(c->result, IsError(ERR_IO_PENDING)); | 1931 ASSERT_THAT(c->result, IsError(ERR_IO_PENDING)); |
1451 c->result = c->callback.WaitForResult(); | 1932 c->result = c->callback.WaitForResult(); |
1452 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction); | 1933 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction); |
1453 | 1934 |
1454 // Now we have 2 active readers and two queued transactions. | 1935 // Now all transactions should be waiting for read to be invoked. Two readers |
1455 | 1936 // are because of the load flags and remaining two transactions were converted |
| 1937 // to readers after skipping validation. Note that the remaining two went on |
| 1938 // to process the headers in parallel with readers present on the entry. |
1456 EXPECT_EQ(LOAD_STATE_IDLE, context_list[2]->trans->GetLoadState()); | 1939 EXPECT_EQ(LOAD_STATE_IDLE, context_list[2]->trans->GetLoadState()); |
1457 EXPECT_EQ(LOAD_STATE_WAITING_FOR_CACHE, | 1940 EXPECT_EQ(LOAD_STATE_IDLE, context_list[3]->trans->GetLoadState()); |
1458 context_list[3]->trans->GetLoadState()); | |
1459 | 1941 |
1460 c = context_list[1]; | 1942 c = context_list[1]; |
1461 ASSERT_THAT(c->result, IsError(ERR_IO_PENDING)); | 1943 ASSERT_THAT(c->result, IsError(ERR_IO_PENDING)); |
1462 c->result = c->callback.WaitForResult(); | 1944 c->result = c->callback.WaitForResult(); |
1463 if (c->result == OK) | 1945 if (c->result == OK) |
1464 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction); | 1946 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction); |
1465 | 1947 |
1466 // At this point we have one reader, two pending transactions and a task on | 1948 // 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 | 1949 // 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 | 1950 // 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()); | 1995 ASSERT_THAT(c->result, IsOk()); |
1514 | 1996 |
1515 MockHttpRequest* this_request = &request; | 1997 MockHttpRequest* this_request = &request; |
1516 if (i == 3) | 1998 if (i == 3) |
1517 this_request = &writer_request; | 1999 this_request = &writer_request; |
1518 | 2000 |
1519 c->result = c->trans->Start(this_request, c->callback.callback(), | 2001 c->result = c->trans->Start(this_request, c->callback.callback(), |
1520 NetLogWithSource()); | 2002 NetLogWithSource()); |
1521 } | 2003 } |
1522 | 2004 |
| 2005 base::RunLoop().RunUntilIdle(); |
| 2006 |
1523 // The first request should be a writer at this point, and the two subsequent | 2007 // 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. | 2008 // requests should be pending. The last request doomed the first entry. |
1525 | 2009 |
1526 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | 2010 EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
1527 | 2011 |
1528 // Cancel the first queued transaction. | 2012 // Cancel the second transaction. Note that this and the 3rd transactions |
| 2013 // would have completed their headers phase and would be waiting in the |
| 2014 // done_headers_queue when the 2nd transaction is cancelled. |
1529 context_list[1].reset(); | 2015 context_list[1].reset(); |
1530 | 2016 |
1531 for (int i = 0; i < kNumTransactions; ++i) { | 2017 for (int i = 0; i < kNumTransactions; ++i) { |
1532 if (i == 1) | 2018 if (i == 1) |
1533 continue; | 2019 continue; |
1534 Context* c = context_list[i].get(); | 2020 Context* c = context_list[i].get(); |
1535 ASSERT_THAT(c->result, IsError(ERR_IO_PENDING)); | 2021 ASSERT_THAT(c->result, IsError(ERR_IO_PENDING)); |
1536 c->result = c->callback.WaitForResult(); | 2022 c->result = c->callback.WaitForResult(); |
1537 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction); | 2023 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction); |
1538 } | 2024 } |
(...skipping 21 matching lines...) Expand all Loading... |
1560 ASSERT_THAT(c->result, IsOk()); | 2046 ASSERT_THAT(c->result, IsOk()); |
1561 | 2047 |
1562 c->result = | 2048 c->result = |
1563 c->trans->Start(&request, c->callback.callback(), NetLogWithSource()); | 2049 c->trans->Start(&request, c->callback.callback(), NetLogWithSource()); |
1564 } | 2050 } |
1565 | 2051 |
1566 // Allow all requests to move from the Create queue to the active entry. | 2052 // Allow all requests to move from the Create queue to the active entry. |
1567 base::RunLoop().RunUntilIdle(); | 2053 base::RunLoop().RunUntilIdle(); |
1568 | 2054 |
1569 // The first request should be a writer at this point, and the subsequent | 2055 // The first request should be a writer at this point, and the subsequent |
1570 // requests should be pending. | 2056 // requests should have completed validation. Since the validation does not |
| 2057 // result in a match, a new entry would be created. |
1571 | 2058 |
1572 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 2059 EXPECT_EQ(3, cache.network_layer()->transaction_count()); |
1573 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 2060 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
1574 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 2061 EXPECT_EQ(2, cache.disk_cache()->create_count()); |
1575 | 2062 |
1576 // Now, make sure that the second request asks for the entry not to be stored. | 2063 // Now, make sure that the second request asks for the entry not to be stored. |
1577 request_handler.set_no_store(true); | 2064 request_handler.set_no_store(true); |
1578 | 2065 |
1579 for (int i = 0; i < kNumTransactions; ++i) { | 2066 for (int i = 0; i < kNumTransactions; ++i) { |
1580 Context* c = context_list[i]; | 2067 Context* c = context_list[i]; |
1581 if (c->result == ERR_IO_PENDING) | 2068 if (c->result == ERR_IO_PENDING) |
1582 c->result = c->callback.WaitForResult(); | 2069 c->result = c->callback.WaitForResult(); |
1583 ReadAndVerifyTransaction(c->trans.get(), kFastNoStoreGET_Transaction); | 2070 ReadAndVerifyTransaction(c->trans.get(), kFastNoStoreGET_Transaction); |
1584 delete c; | 2071 delete c; |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1618 | 2105 |
1619 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 2106 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
1620 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 2107 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
1621 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 2108 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
1622 | 2109 |
1623 for (int i = 0; i < kNumTransactions; ++i) { | 2110 for (int i = 0; i < kNumTransactions; ++i) { |
1624 Context* c = context_list[i]; | 2111 Context* c = context_list[i]; |
1625 if (c->result == ERR_IO_PENDING) | 2112 if (c->result == ERR_IO_PENDING) |
1626 c->result = c->callback.WaitForResult(); | 2113 c->result = c->callback.WaitForResult(); |
1627 // Destroy only the first transaction. | 2114 // Destroy only the first transaction. |
| 2115 // This should lead to all transactions to restart, even those that have |
| 2116 // validated themselves and were waiting for the writer transaction to |
| 2117 // complete writing to the cache. |
1628 if (i == 0) { | 2118 if (i == 0) { |
1629 delete c; | 2119 delete c; |
1630 context_list[i] = NULL; | 2120 context_list[i] = NULL; |
1631 } | 2121 } |
1632 } | 2122 } |
1633 | 2123 |
1634 // Complete the rest of the transactions. | 2124 // Complete the rest of the transactions. |
1635 for (int i = 1; i < kNumTransactions; ++i) { | 2125 for (int i = 1; i < kNumTransactions; ++i) { |
1636 Context* c = context_list[i]; | 2126 Context* c = context_list[i]; |
1637 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction); | 2127 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction); |
(...skipping 895 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2533 } | 3023 } |
2534 | 3024 |
2535 // Helper that does 4 requests using HttpCache: | 3025 // Helper that does 4 requests using HttpCache: |
2536 // | 3026 // |
2537 // (1) loads |kUrl| -- expects |net_response_1| to be returned. | 3027 // (1) loads |kUrl| -- expects |net_response_1| to be returned. |
2538 // (2) loads |kUrl| from cache only -- expects |net_response_1| to be returned. | 3028 // (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 | 3029 // (3) loads |kUrl| using |extra_request_headers| -- expects |net_response_2| to |
2540 // be returned. | 3030 // be returned. |
2541 // (4) loads |kUrl| from cache only -- expects |cached_response_2| to be | 3031 // (4) loads |kUrl| from cache only -- expects |cached_response_2| to be |
2542 // returned. | 3032 // returned. |
| 3033 // The entry will be created once and will be opened for the 3 subsequent |
| 3034 // requests. |
2543 static void ConditionalizedRequestUpdatesCacheHelper( | 3035 static void ConditionalizedRequestUpdatesCacheHelper( |
2544 const Response& net_response_1, | 3036 const Response& net_response_1, |
2545 const Response& net_response_2, | 3037 const Response& net_response_2, |
2546 const Response& cached_response_2, | 3038 const Response& cached_response_2, |
2547 const char* extra_request_headers) { | 3039 const char* extra_request_headers) { |
2548 MockHttpCache cache; | 3040 MockHttpCache cache; |
2549 | 3041 |
2550 // The URL we will be requesting. | 3042 // The URL we will be requesting. |
2551 const char kUrl[] = "http://foobar.com/main.css"; | 3043 const char kUrl[] = "http://foobar.com/main.css"; |
2552 | 3044 |
(...skipping 3555 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
6108 pending->trans->Start(&request, pending->callback.callback(), | 6600 pending->trans->Start(&request, pending->callback.callback(), |
6109 NetLogWithSource())); | 6601 NetLogWithSource())); |
6110 EXPECT_THAT(c->callback.GetResult(rv), IsOk()); | 6602 EXPECT_THAT(c->callback.GetResult(rv), IsOk()); |
6111 | 6603 |
6112 // Make sure that the entry has some data stored. | 6604 // Make sure that the entry has some data stored. |
6113 scoped_refptr<IOBufferWithSize> buf(new IOBufferWithSize(5)); | 6605 scoped_refptr<IOBufferWithSize> buf(new IOBufferWithSize(5)); |
6114 rv = c->trans->Read(buf.get(), buf->size(), c->callback.callback()); | 6606 rv = c->trans->Read(buf.get(), buf->size(), c->callback.callback()); |
6115 EXPECT_EQ(5, c->callback.GetResult(rv)); | 6607 EXPECT_EQ(5, c->callback.GetResult(rv)); |
6116 | 6608 |
6117 // Cancel the requests. | 6609 // Cancel the requests. |
| 6610 // Since |pending| is currently validating the already written headers |
| 6611 // it will be restarted as well. |
6118 delete c; | 6612 delete c; |
6119 delete pending; | 6613 delete pending; |
6120 | 6614 |
6121 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 6615 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
6122 EXPECT_EQ(1, cache.disk_cache()->open_count()); | 6616 EXPECT_EQ(1, cache.disk_cache()->open_count()); |
6123 EXPECT_EQ(2, cache.disk_cache()->create_count()); | 6617 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
6124 | 6618 |
6125 base::RunLoop().RunUntilIdle(); | 6619 base::RunLoop().RunUntilIdle(); |
6126 RemoveMockTransaction(&transaction); | 6620 RemoveMockTransaction(&transaction); |
6127 } | 6621 } |
6128 | 6622 |
6129 // Tests that we delete truncated entries if the server changes its mind midway. | 6623 // Tests that we delete truncated entries if the server changes its mind midway. |
6130 TEST(HttpCache, GET_IncompleteResource2) { | 6624 TEST(HttpCache, GET_IncompleteResource2) { |
6131 MockHttpCache cache; | 6625 MockHttpCache cache; |
6132 AddMockTransaction(&kRangeGET_TransactionOK); | 6626 AddMockTransaction(&kRangeGET_TransactionOK); |
6133 | 6627 |
(...skipping 704 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
6838 | 7332 |
6839 RunTransactionTestWithResponseInfo(cache.http_cache(), kSimpleGET_Transaction, | 7333 RunTransactionTestWithResponseInfo(cache.http_cache(), kSimpleGET_Transaction, |
6840 &response); | 7334 &response); |
6841 EXPECT_TRUE(response.metadata.get() == NULL); | 7335 EXPECT_TRUE(response.metadata.get() == NULL); |
6842 | 7336 |
6843 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 7337 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
6844 EXPECT_EQ(2, cache.disk_cache()->open_count()); | 7338 EXPECT_EQ(2, cache.disk_cache()->open_count()); |
6845 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 7339 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
6846 } | 7340 } |
6847 | 7341 |
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 | 7342 // Tests that we ignore VARY checks when writing metadata since the request |
6889 // headers for the WriteMetadata transaction are made up. | 7343 // headers for the WriteMetadata transaction are made up. |
6890 TEST(HttpCache, WriteMetadata_IgnoreVary) { | 7344 TEST(HttpCache, WriteMetadata_IgnoreVary) { |
6891 MockHttpCache cache; | 7345 MockHttpCache cache; |
6892 | 7346 |
6893 // Write to the cache | 7347 // Write to the cache |
6894 HttpResponseInfo response; | 7348 HttpResponseInfo response; |
6895 ScopedMockTransaction transaction(kSimpleGET_Transaction); | 7349 ScopedMockTransaction transaction(kSimpleGET_Transaction); |
6896 transaction.request_headers = "accept-encoding: gzip\r\n"; | 7350 transaction.request_headers = "accept-encoding: gzip\r\n"; |
6897 transaction.response_headers = | 7351 transaction.response_headers = |
(...skipping 307 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
7205 MockHttpRequest request(mock_transaction); | 7659 MockHttpRequest request(mock_transaction); |
7206 | 7660 |
7207 { | 7661 { |
7208 std::unique_ptr<HttpTransaction> trans; | 7662 std::unique_ptr<HttpTransaction> trans; |
7209 ASSERT_THAT(cache.CreateTransaction(&trans), IsOk()); | 7663 ASSERT_THAT(cache.CreateTransaction(&trans), IsOk()); |
7210 | 7664 |
7211 int rv = trans->Start(&request, callback.callback(), NetLogWithSource()); | 7665 int rv = trans->Start(&request, callback.callback(), NetLogWithSource()); |
7212 EXPECT_THAT(callback.GetResult(rv), IsOk()); | 7666 EXPECT_THAT(callback.GetResult(rv), IsOk()); |
7213 | 7667 |
7214 trans->StopCaching(); | 7668 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 } | 7669 } |
7220 RemoveMockTransaction(&mock_transaction); | 7670 RemoveMockTransaction(&mock_transaction); |
7221 | 7671 |
7222 // Make sure that the ActiveEntry is gone. | 7672 // Make sure that the ActiveEntry is gone. |
7223 base::RunLoop().RunUntilIdle(); | 7673 base::RunLoop().RunUntilIdle(); |
7224 | 7674 |
7225 // Verify that the entry is gone. | 7675 // Verify that the entry is gone. |
7226 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); | 7676 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); |
7227 | 7677 |
7228 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | 7678 EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
(...skipping 1031 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
8260 ASSERT_TRUE(attrs->GetDictionary( | 8710 ASSERT_TRUE(attrs->GetDictionary( |
8261 base::trace_event::MemoryAllocatorDump::kNameSize, &size_attrs)); | 8711 base::trace_event::MemoryAllocatorDump::kNameSize, &size_attrs)); |
8262 std::string size; | 8712 std::string size; |
8263 ASSERT_TRUE(size_attrs->GetString("value", &size)); | 8713 ASSERT_TRUE(size_attrs->GetString("value", &size)); |
8264 int actual_size = 0; | 8714 int actual_size = 0; |
8265 ASSERT_TRUE(base::HexStringToInt(size, &actual_size)); | 8715 ASSERT_TRUE(base::HexStringToInt(size, &actual_size)); |
8266 ASSERT_LT(0, actual_size); | 8716 ASSERT_LT(0, actual_size); |
8267 } | 8717 } |
8268 | 8718 |
8269 } // namespace net | 8719 } // namespace net |
OLD | NEW |