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 1558 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1569 std::string headers; | 1569 std::string headers; |
1570 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); | 1570 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); |
1571 Verify206Response(headers, 30, 49); | 1571 Verify206Response(headers, 30, 49); |
1572 } | 1572 } |
1573 | 1573 |
1574 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | 1574 EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
1575 EXPECT_EQ(1, cache.disk_cache()->open_count()); | 1575 EXPECT_EQ(1, cache.disk_cache()->open_count()); |
1576 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 1576 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
1577 } | 1577 } |
1578 | 1578 |
| 1579 // Tests parallel validation on range requests can be successfully restarted |
| 1580 // when there is a cache lock timeout. |
| 1581 TEST(HttpCache, RangeGET_ParallelValidationCacheLockTimeout) { |
| 1582 MockHttpCache cache; |
| 1583 |
| 1584 ScopedMockTransaction transaction(kRangeGET_TransactionOK); |
| 1585 |
| 1586 std::vector<std::unique_ptr<Context>> context_list; |
| 1587 const int kNumTransactions = 2; |
| 1588 |
| 1589 for (int i = 0; i < kNumTransactions; ++i) { |
| 1590 context_list.push_back(base::MakeUnique<Context>()); |
| 1591 } |
| 1592 |
| 1593 // Let 1st transaction complete headers phase for ranges 40-49. |
| 1594 std::string first_read; |
| 1595 MockHttpRequest request1(transaction); |
| 1596 { |
| 1597 auto& c = context_list[0]; |
| 1598 c->result = cache.CreateTransaction(&c->trans); |
| 1599 ASSERT_THAT(c->result, IsOk()); |
| 1600 EXPECT_EQ(LOAD_STATE_IDLE, c->trans->GetLoadState()); |
| 1601 |
| 1602 c->result = |
| 1603 c->trans->Start(&request1, c->callback.callback(), NetLogWithSource()); |
| 1604 base::RunLoop().RunUntilIdle(); |
| 1605 |
| 1606 // Start writing to the cache so that MockDiskEntry::CouldBeSparse() returns |
| 1607 // true. |
| 1608 const int kBufferSize = 5; |
| 1609 scoped_refptr<IOBuffer> buffer(new IOBuffer(kBufferSize)); |
| 1610 ReleaseBufferCompletionCallback cb(buffer.get()); |
| 1611 c->result = c->trans->Read(buffer.get(), kBufferSize, cb.callback()); |
| 1612 EXPECT_EQ(kBufferSize, cb.GetResult(c->result)); |
| 1613 |
| 1614 std::string data_read(buffer->data(), kBufferSize); |
| 1615 first_read = data_read; |
| 1616 |
| 1617 EXPECT_EQ(LOAD_STATE_READING_RESPONSE, c->trans->GetLoadState()); |
| 1618 } |
| 1619 |
| 1620 cache.SimulateCacheLockTimeoutAfterHeaders(); |
| 1621 |
| 1622 // 2nd transaction requests ranges 30-39. |
| 1623 transaction.request_headers = "Range: bytes = 30-39\r\n" EXTRA_HEADER; |
| 1624 MockHttpRequest request2(transaction); |
| 1625 { |
| 1626 auto& c = context_list[1]; |
| 1627 c->result = cache.CreateTransaction(&c->trans); |
| 1628 ASSERT_THAT(c->result, IsOk()); |
| 1629 EXPECT_EQ(LOAD_STATE_IDLE, c->trans->GetLoadState()); |
| 1630 |
| 1631 c->result = |
| 1632 c->trans->Start(&request2, c->callback.callback(), NetLogWithSource()); |
| 1633 base::RunLoop().RunUntilIdle(); |
| 1634 |
| 1635 EXPECT_EQ(LOAD_STATE_IDLE, c->trans->GetLoadState()); |
| 1636 } |
| 1637 |
| 1638 EXPECT_TRUE(cache.IsWriterPresent(kRangeGET_TransactionOK.url)); |
| 1639 EXPECT_EQ(0, cache.GetCountDoneHeadersQueue(kRangeGET_TransactionOK.url)); |
| 1640 |
| 1641 EXPECT_EQ(3, cache.network_layer()->transaction_count()); |
| 1642 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 1643 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 1644 |
| 1645 for (int i = 0; i < kNumTransactions; ++i) { |
| 1646 auto& c = context_list[i]; |
| 1647 if (c->result == ERR_IO_PENDING) |
| 1648 c->result = c->callback.WaitForResult(); |
| 1649 |
| 1650 if (i == 0) { |
| 1651 ReadRemainingAndVerifyTransaction(c->trans.get(), first_read, |
| 1652 transaction); |
| 1653 continue; |
| 1654 } |
| 1655 |
| 1656 transaction.data = "rg: 30-39 "; |
| 1657 ReadAndVerifyTransaction(c->trans.get(), transaction); |
| 1658 } |
| 1659 |
| 1660 EXPECT_EQ(3, cache.network_layer()->transaction_count()); |
| 1661 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 1662 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 1663 } |
| 1664 |
1579 // Tests parallel validation on range requests with overlapping ranges. | 1665 // Tests parallel validation on range requests with overlapping ranges. |
1580 TEST(HttpCache, RangeGET_ParallelValidationOverlappingRanges) { | 1666 TEST(HttpCache, RangeGET_ParallelValidationOverlappingRanges) { |
1581 MockHttpCache cache; | 1667 MockHttpCache cache; |
1582 | 1668 |
1583 ScopedMockTransaction transaction(kRangeGET_TransactionOK); | 1669 ScopedMockTransaction transaction(kRangeGET_TransactionOK); |
1584 | 1670 |
1585 std::vector<std::unique_ptr<Context>> context_list; | 1671 std::vector<std::unique_ptr<Context>> context_list; |
1586 const int kNumTransactions = 2; | 1672 const int kNumTransactions = 2; |
1587 | 1673 |
1588 for (int i = 0; i < kNumTransactions; ++i) { | 1674 for (int i = 0; i < kNumTransactions; ++i) { |
(...skipping 343 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1932 if (!context) | 2018 if (!context) |
1933 continue; | 2019 continue; |
1934 ReadAndVerifyTransaction(context->trans.get(), kSimpleGET_Transaction); | 2020 ReadAndVerifyTransaction(context->trans.get(), kSimpleGET_Transaction); |
1935 } | 2021 } |
1936 | 2022 |
1937 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 2023 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
1938 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 2024 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
1939 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 2025 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
1940 } | 2026 } |
1941 | 2027 |
| 2028 // Tests that a transaction which is in validated queue can timeout and start |
| 2029 // reading from the network without writing to the cache. |
| 2030 TEST(HttpCache, SimpleGET_ParallelValidationValidatedTimeout) { |
| 2031 MockHttpCache cache; |
| 2032 |
| 2033 MockHttpRequest request(kSimpleGET_Transaction); |
| 2034 |
| 2035 std::vector<std::unique_ptr<Context>> context_list; |
| 2036 const int kNumTransactions = 2; |
| 2037 |
| 2038 for (int i = 0; i < kNumTransactions; ++i) { |
| 2039 context_list.push_back(base::MakeUnique<Context>()); |
| 2040 auto& c = context_list[i]; |
| 2041 |
| 2042 if (i == 1) |
| 2043 cache.SimulateCacheLockTimeoutAfterHeaders(); |
| 2044 |
| 2045 c->result = cache.CreateTransaction(&c->trans); |
| 2046 ASSERT_THAT(c->result, IsOk()); |
| 2047 |
| 2048 c->result = |
| 2049 c->trans->Start(&request, c->callback.callback(), NetLogWithSource()); |
| 2050 } |
| 2051 |
| 2052 // Allow all requests to move from the Create queue to the active entry. |
| 2053 base::RunLoop().RunUntilIdle(); |
| 2054 |
| 2055 // The first request should be a writer at this point, and the subsequent |
| 2056 // requests should have completed validation, timed out and restarted. |
| 2057 |
| 2058 EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 2059 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 2060 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 2061 |
| 2062 EXPECT_TRUE(cache.IsWriterPresent(kSimpleGET_Transaction.url)); |
| 2063 EXPECT_EQ(0, cache.GetCountDoneHeadersQueue(kSimpleGET_Transaction.url)); |
| 2064 |
| 2065 // Complete the rest of the transactions. |
| 2066 for (auto& context : context_list) { |
| 2067 ReadAndVerifyTransaction(context->trans.get(), kSimpleGET_Transaction); |
| 2068 } |
| 2069 |
| 2070 EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 2071 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 2072 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 2073 } |
| 2074 |
1942 // Tests that a transaction which is in readers can be destroyed without | 2075 // Tests that a transaction which is in readers can be destroyed without |
1943 // any impact to other transactions. | 2076 // any impact to other transactions. |
1944 TEST(HttpCache, SimpleGET_ParallelValidationCancelReader) { | 2077 TEST(HttpCache, SimpleGET_ParallelValidationCancelReader) { |
1945 MockHttpCache cache; | 2078 MockHttpCache cache; |
1946 | 2079 |
1947 MockHttpRequest request(kSimpleGET_Transaction); | 2080 MockHttpRequest request(kSimpleGET_Transaction); |
1948 | 2081 |
1949 MockTransaction transaction(kSimpleGET_Transaction); | 2082 MockTransaction transaction(kSimpleGET_Transaction); |
1950 transaction.load_flags |= LOAD_VALIDATE_CACHE; | 2083 transaction.load_flags |= LOAD_VALIDATE_CACHE; |
1951 MockHttpRequest validate_request(transaction); | 2084 MockHttpRequest validate_request(transaction); |
(...skipping 7219 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
9171 ASSERT_TRUE(attrs->GetDictionary( | 9304 ASSERT_TRUE(attrs->GetDictionary( |
9172 base::trace_event::MemoryAllocatorDump::kNameSize, &size_attrs)); | 9305 base::trace_event::MemoryAllocatorDump::kNameSize, &size_attrs)); |
9173 std::string size; | 9306 std::string size; |
9174 ASSERT_TRUE(size_attrs->GetString("value", &size)); | 9307 ASSERT_TRUE(size_attrs->GetString("value", &size)); |
9175 int actual_size = 0; | 9308 int actual_size = 0; |
9176 ASSERT_TRUE(base::HexStringToInt(size, &actual_size)); | 9309 ASSERT_TRUE(base::HexStringToInt(size, &actual_size)); |
9177 ASSERT_LT(0, actual_size); | 9310 ASSERT_LT(0, actual_size); |
9178 } | 9311 } |
9179 | 9312 |
9180 } // namespace net | 9313 } // namespace net |
OLD | NEW |