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 1869 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1880 } | 1880 } |
1881 | 1881 |
1882 // Allow all requests to move from the Create queue to the active entry. | 1882 // Allow all requests to move from the Create queue to the active entry. |
1883 base::RunLoop().RunUntilIdle(); | 1883 base::RunLoop().RunUntilIdle(); |
1884 | 1884 |
1885 // The first request should be a writer at this point, and the subsequent | 1885 // The first request should be a writer at this point, and the subsequent |
1886 // requests should have passed the validation phase and created their own | 1886 // requests should have passed the validation phase and created their own |
1887 // entries since none of them matched the headers of the earlier one. | 1887 // entries since none of them matched the headers of the earlier one. |
1888 EXPECT_TRUE(cache.IsWriterPresent(kSimpleGET_Transaction.url)); | 1888 EXPECT_TRUE(cache.IsWriterPresent(kSimpleGET_Transaction.url)); |
1889 | 1889 |
1890 // Note that there are only 3 entries created and not 5 since every other | |
1891 // transaction would have gone to the network. | |
1892 EXPECT_EQ(5, cache.network_layer()->transaction_count()); | 1890 EXPECT_EQ(5, cache.network_layer()->transaction_count()); |
1893 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 1891 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
1894 EXPECT_EQ(3, cache.disk_cache()->create_count()); | 1892 EXPECT_EQ(5, cache.disk_cache()->create_count()); |
1895 | 1893 |
1896 // All requests depend on the writer, and the writer is between Start and | 1894 // All requests depend on the writer, and the writer is between Start and |
1897 // Read, i.e. idle. | 1895 // Read, i.e. idle. |
1898 for (auto& context : context_list) { | 1896 for (auto& context : context_list) { |
1899 EXPECT_EQ(LOAD_STATE_IDLE, context->trans->GetLoadState()); | 1897 EXPECT_EQ(LOAD_STATE_IDLE, context->trans->GetLoadState()); |
1900 } | 1898 } |
1901 | 1899 |
1902 for (auto& context : context_list) { | 1900 for (auto& context : context_list) { |
1903 if (context->result == ERR_IO_PENDING) | 1901 if (context->result == ERR_IO_PENDING) |
1904 context->result = context->callback.WaitForResult(); | 1902 context->result = context->callback.WaitForResult(); |
1905 ReadAndVerifyTransaction(context->trans.get(), kSimpleGET_Transaction); | 1903 ReadAndVerifyTransaction(context->trans.get(), kSimpleGET_Transaction); |
1906 } | 1904 } |
1907 | 1905 |
1908 EXPECT_EQ(5, cache.network_layer()->transaction_count()); | 1906 EXPECT_EQ(5, cache.network_layer()->transaction_count()); |
1909 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 1907 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
1910 EXPECT_EQ(3, cache.disk_cache()->create_count()); | 1908 EXPECT_EQ(5, cache.disk_cache()->create_count()); |
| 1909 } |
| 1910 |
| 1911 // Parallel validation results in 200 for 1 transaction and validation matches |
| 1912 // for subsequent transactions. |
| 1913 TEST(HttpCache, SimpleGET_ParallelValidationNoMatch1) { |
| 1914 MockHttpCache cache; |
| 1915 MockHttpRequest request(kSimpleGET_Transaction); |
| 1916 |
| 1917 MockTransaction transaction(kSimpleGET_Transaction); |
| 1918 transaction.load_flags |= LOAD_VALIDATE_CACHE; |
| 1919 MockHttpRequest validate_request(transaction); |
| 1920 std::vector<std::unique_ptr<Context>> context_list; |
| 1921 const int kNumTransactions = 5; |
| 1922 for (int i = 0; i < kNumTransactions; ++i) { |
| 1923 context_list.push_back(base::MakeUnique<Context>()); |
| 1924 auto& c = context_list[i]; |
| 1925 c->result = cache.CreateTransaction(&c->trans); |
| 1926 ASSERT_THAT(c->result, IsOk()); |
| 1927 EXPECT_EQ(LOAD_STATE_IDLE, c->trans->GetLoadState()); |
| 1928 |
| 1929 MockHttpRequest* this_request = &request; |
| 1930 if (i == 1) |
| 1931 this_request = &validate_request; |
| 1932 |
| 1933 c->result = c->trans->Start(this_request, c->callback.callback(), |
| 1934 NetLogWithSource()); |
| 1935 } |
| 1936 |
| 1937 // All requests are waiting for the active entry. |
| 1938 for (auto& context : context_list) { |
| 1939 EXPECT_EQ(LOAD_STATE_WAITING_FOR_CACHE, context->trans->GetLoadState()); |
| 1940 } |
| 1941 |
| 1942 // Allow all requests to move from the Create queue to the active entry. |
| 1943 base::RunLoop().RunUntilIdle(); |
| 1944 |
| 1945 // The first request should be a writer at this point, and the second |
| 1946 // request should have passed the validation phase and created a new |
| 1947 // entry. Rest of them should be added to the new entry. |
| 1948 EXPECT_TRUE(cache.IsWriterPresent(kSimpleGET_Transaction.url)); |
| 1949 EXPECT_EQ(3, cache.GetCountDoneHeadersQueue(kSimpleGET_Transaction.url)); |
| 1950 |
| 1951 EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 1952 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 1953 EXPECT_EQ(2, cache.disk_cache()->create_count()); |
| 1954 |
| 1955 // All requests depend on the writer, and the writer is between Start and |
| 1956 // Read, i.e. idle. |
| 1957 for (auto& context : context_list) { |
| 1958 EXPECT_EQ(LOAD_STATE_IDLE, context->trans->GetLoadState()); |
| 1959 } |
| 1960 |
| 1961 EXPECT_EQ(1, cache.disk_cache()->doomed_count()); |
| 1962 |
| 1963 for (size_t i = 0; i < context_list.size(); i++) { |
| 1964 if (context_list[i]->result == ERR_IO_PENDING) |
| 1965 context_list[i]->result = context_list[i]->callback.WaitForResult(); |
| 1966 |
| 1967 ReadAndVerifyTransaction(context_list[i]->trans.get(), |
| 1968 kSimpleGET_Transaction); |
| 1969 } |
| 1970 |
| 1971 EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 1972 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 1973 EXPECT_EQ(2, cache.disk_cache()->create_count()); |
1911 } | 1974 } |
1912 | 1975 |
1913 // Tests that a GET followed by a DELETE results in DELETE immediately starting | 1976 // Tests that a GET followed by a DELETE results in DELETE immediately starting |
1914 // the headers phase and the entry is doomed. | 1977 // the headers phase and the entry is doomed. |
1915 TEST(HttpCache, SimpleGET_ParallelValidationDelete) { | 1978 TEST(HttpCache, SimpleGET_ParallelValidationDelete) { |
1916 MockHttpCache cache; | 1979 MockHttpCache cache; |
1917 | 1980 |
1918 MockHttpRequest request(kSimpleGET_Transaction); | 1981 MockHttpRequest request(kSimpleGET_Transaction); |
1919 request.load_flags |= LOAD_VALIDATE_CACHE; | 1982 request.load_flags |= LOAD_VALIDATE_CACHE; |
1920 | 1983 |
(...skipping 228 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2149 EXPECT_TRUE(cache.IsHeadersTransactionPresent(kSimpleGET_Transaction.url)); | 2212 EXPECT_TRUE(cache.IsHeadersTransactionPresent(kSimpleGET_Transaction.url)); |
2150 | 2213 |
2151 // Resume network start for headers_transaction. It will doom the entry as it | 2214 // Resume network start for headers_transaction. It will doom the entry as it |
2152 // will be a 200 and will go to network for the response body. | 2215 // will be a 200 and will go to network for the response body. |
2153 auto& context = context_list[3]; | 2216 auto& context = context_list[3]; |
2154 context->trans->ResumeNetworkStart(); | 2217 context->trans->ResumeNetworkStart(); |
2155 | 2218 |
2156 // The pending transactions will be added to a new entry. | 2219 // The pending transactions will be added to a new entry. |
2157 base::RunLoop().RunUntilIdle(); | 2220 base::RunLoop().RunUntilIdle(); |
2158 | 2221 |
2159 EXPECT_EQ(1, cache.GetCountDoneHeadersQueue(kSimpleGET_Transaction.url)); | 2222 EXPECT_EQ(2, cache.GetCountDoneHeadersQueue(kSimpleGET_Transaction.url)); |
2160 EXPECT_TRUE(cache.IsWriterPresent(kSimpleGET_Transaction.url)); | 2223 EXPECT_TRUE(cache.IsWriterPresent(kSimpleGET_Transaction.url)); |
2161 | 2224 |
2162 // Complete the rest of the transactions. | 2225 // Complete the rest of the transactions. |
2163 for (int i = 2; i < kNumTransactions; ++i) { | 2226 for (int i = 2; i < kNumTransactions; ++i) { |
2164 auto& c = context_list[i]; | 2227 auto& c = context_list[i]; |
2165 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction); | 2228 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction); |
2166 } | 2229 } |
2167 | 2230 |
2168 EXPECT_EQ(3, cache.network_layer()->transaction_count()); | 2231 EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
2169 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 2232 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
2170 EXPECT_EQ(2, cache.disk_cache()->create_count()); | 2233 EXPECT_EQ(2, cache.disk_cache()->create_count()); |
2171 } | 2234 } |
2172 | 2235 |
2173 // Tests that a transaction is in validated queue and writer is destroyed | 2236 // Tests that a transaction is in validated queue and writer is destroyed |
2174 // leading to restarting the validated transaction. | 2237 // leading to restarting the validated transaction. |
2175 TEST(HttpCache, SimpleGET_ParallelValidationCancelWriter) { | 2238 TEST(HttpCache, SimpleGET_ParallelValidationCancelWriter) { |
2176 MockHttpCache cache; | 2239 MockHttpCache cache; |
2177 | 2240 |
2178 MockHttpRequest request(kSimpleGET_Transaction); | 2241 MockHttpRequest request(kSimpleGET_Transaction); |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2223 | 2286 |
2224 base::RunLoop().RunUntilIdle(); | 2287 base::RunLoop().RunUntilIdle(); |
2225 | 2288 |
2226 EXPECT_TRUE(cache.IsWriterPresent(kSimpleGET_Transaction.url)); | 2289 EXPECT_TRUE(cache.IsWriterPresent(kSimpleGET_Transaction.url)); |
2227 EXPECT_TRUE(cache.IsHeadersTransactionPresent(kSimpleGET_Transaction.url)); | 2290 EXPECT_TRUE(cache.IsHeadersTransactionPresent(kSimpleGET_Transaction.url)); |
2228 | 2291 |
2229 // Resume network start for the transaction the second time. | 2292 // Resume network start for the transaction the second time. |
2230 c->trans->ResumeNetworkStart(); | 2293 c->trans->ResumeNetworkStart(); |
2231 base::RunLoop().RunUntilIdle(); | 2294 base::RunLoop().RunUntilIdle(); |
2232 | 2295 |
2233 // Headers transaction would have doomed the new entry created. | |
2234 EXPECT_TRUE( | |
2235 cache.disk_cache()->IsDiskEntryDoomed(kSimpleGET_Transaction.url)); | |
2236 | |
2237 // Complete the rest of the transactions. | 2296 // Complete the rest of the transactions. |
2238 for (auto& context : context_list) { | 2297 for (auto& context : context_list) { |
2239 if (!context) | 2298 if (!context) |
2240 continue; | 2299 continue; |
2241 ReadAndVerifyTransaction(context->trans.get(), kSimpleGET_Transaction); | 2300 ReadAndVerifyTransaction(context->trans.get(), kSimpleGET_Transaction); |
2242 } | 2301 } |
2243 | 2302 |
2244 EXPECT_EQ(4, cache.network_layer()->transaction_count()); | 2303 EXPECT_EQ(4, cache.network_layer()->transaction_count()); |
2245 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 2304 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
2246 EXPECT_EQ(2, cache.disk_cache()->create_count()); | 2305 EXPECT_EQ(3, cache.disk_cache()->create_count()); |
2247 } | 2306 } |
2248 | 2307 |
2249 // Tests when a writer is destroyed mid-read and entry is marked as truncated, | 2308 // Tests when a writer is destroyed mid-read and entry is marked as truncated, |
2250 // it should lead to restarting the dependent transactions. | 2309 // it should lead to restarting the dependent transactions. |
2251 TEST(HttpCache, SimpleGET_ParallelValidationCancelWriterTruncateEntry) { | 2310 TEST(HttpCache, SimpleGET_ParallelValidationCancelWriterTruncateEntry) { |
2252 MockHttpCache cache; | 2311 MockHttpCache cache; |
2253 | 2312 |
2254 ScopedMockTransaction transaction(kSimpleGET_Transaction); | 2313 ScopedMockTransaction transaction(kSimpleGET_Transaction); |
2255 transaction.response_headers = | 2314 transaction.response_headers = |
2256 "Last-Modified: Wed, 28 Nov 2007 00:40:09 GMT\n" | 2315 "Last-Modified: Wed, 28 Nov 2007 00:40:09 GMT\n" |
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2365 | 2424 |
2366 base::RunLoop().RunUntilIdle(); | 2425 base::RunLoop().RunUntilIdle(); |
2367 | 2426 |
2368 EXPECT_TRUE(cache.IsWriterPresent(kSimpleGET_Transaction.url)); | 2427 EXPECT_TRUE(cache.IsWriterPresent(kSimpleGET_Transaction.url)); |
2369 EXPECT_TRUE(cache.IsHeadersTransactionPresent(kSimpleGET_Transaction.url)); | 2428 EXPECT_TRUE(cache.IsHeadersTransactionPresent(kSimpleGET_Transaction.url)); |
2370 | 2429 |
2371 // Resume network start for the transaction the second time. | 2430 // Resume network start for the transaction the second time. |
2372 c->trans->ResumeNetworkStart(); | 2431 c->trans->ResumeNetworkStart(); |
2373 base::RunLoop().RunUntilIdle(); | 2432 base::RunLoop().RunUntilIdle(); |
2374 | 2433 |
2375 // Headers transaction would have doomed the new entry created. | |
2376 EXPECT_TRUE( | |
2377 cache.disk_cache()->IsDiskEntryDoomed(kSimpleGET_Transaction.url)); | |
2378 | |
2379 // Complete the rest of the transactions. | 2434 // Complete the rest of the transactions. |
2380 for (auto& context : context_list) { | 2435 for (auto& context : context_list) { |
2381 if (!context) | 2436 if (!context) |
2382 continue; | 2437 continue; |
2383 ReadAndVerifyTransaction(context->trans.get(), kSimpleGET_Transaction); | 2438 ReadAndVerifyTransaction(context->trans.get(), kSimpleGET_Transaction); |
2384 } | 2439 } |
2385 | 2440 |
2386 EXPECT_EQ(4, cache.network_layer()->transaction_count()); | 2441 EXPECT_EQ(4, cache.network_layer()->transaction_count()); |
2387 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 2442 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
2388 EXPECT_EQ(2, cache.disk_cache()->create_count()); | 2443 EXPECT_EQ(3, cache.disk_cache()->create_count()); |
2389 } | 2444 } |
2390 | 2445 |
2391 // Tests that a transaction is currently in headers phase and is destroyed | 2446 // Tests that a transaction is currently in headers phase and is destroyed |
2392 // leading to destroying the entry. | 2447 // leading to destroying the entry. |
2393 TEST(HttpCache, SimpleGET_ParallelValidationCancelHeaders) { | 2448 TEST(HttpCache, SimpleGET_ParallelValidationCancelHeaders) { |
2394 MockHttpCache cache; | 2449 MockHttpCache cache; |
2395 | 2450 |
2396 MockHttpRequest request(kSimpleGET_Transaction); | 2451 MockHttpRequest request(kSimpleGET_Transaction); |
2397 | 2452 |
2398 const int kNumTransactions = 2; | 2453 const int kNumTransactions = 2; |
(...skipping 273 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2672 | 2727 |
2673 // Allow all requests to move from the Create queue to the active entry. | 2728 // Allow all requests to move from the Create queue to the active entry. |
2674 base::RunLoop().RunUntilIdle(); | 2729 base::RunLoop().RunUntilIdle(); |
2675 | 2730 |
2676 // The first request should be a writer at this point, and the subsequent | 2731 // The first request should be a writer at this point, and the subsequent |
2677 // requests should have completed validation. Since the validation does not | 2732 // requests should have completed validation. Since the validation does not |
2678 // result in a match, a new entry would be created. | 2733 // result in a match, a new entry would be created. |
2679 | 2734 |
2680 EXPECT_EQ(3, cache.network_layer()->transaction_count()); | 2735 EXPECT_EQ(3, cache.network_layer()->transaction_count()); |
2681 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 2736 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
2682 EXPECT_EQ(2, cache.disk_cache()->create_count()); | 2737 EXPECT_EQ(3, cache.disk_cache()->create_count()); |
2683 | 2738 |
2684 // Now, make sure that the second request asks for the entry not to be stored. | 2739 // Now, make sure that the second request asks for the entry not to be stored. |
2685 request_handler.set_no_store(true); | 2740 request_handler.set_no_store(true); |
2686 | 2741 |
2687 for (int i = 0; i < kNumTransactions; ++i) { | 2742 for (int i = 0; i < kNumTransactions; ++i) { |
2688 Context* c = context_list[i].get(); | 2743 Context* c = context_list[i].get(); |
2689 if (c->result == ERR_IO_PENDING) | 2744 if (c->result == ERR_IO_PENDING) |
2690 c->result = c->callback.WaitForResult(); | 2745 c->result = c->callback.WaitForResult(); |
2691 ReadAndVerifyTransaction(c->trans.get(), kFastNoStoreGET_Transaction); | 2746 ReadAndVerifyTransaction(c->trans.get(), kFastNoStoreGET_Transaction); |
2692 context_list[i].reset(); | 2747 context_list[i].reset(); |
2693 } | 2748 } |
2694 | 2749 |
2695 EXPECT_EQ(3, cache.network_layer()->transaction_count()); | 2750 EXPECT_EQ(3, cache.network_layer()->transaction_count()); |
2696 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 2751 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
2697 EXPECT_EQ(2, cache.disk_cache()->create_count()); | 2752 EXPECT_EQ(3, cache.disk_cache()->create_count()); |
2698 | 2753 |
2699 RemoveMockTransaction(&kFastNoStoreGET_Transaction); | 2754 RemoveMockTransaction(&kFastNoStoreGET_Transaction); |
2700 } | 2755 } |
2701 | 2756 |
2702 TEST(HttpCache, SimpleGET_ManyWriters_CancelFirst) { | 2757 TEST(HttpCache, SimpleGET_ManyWriters_CancelFirst) { |
2703 MockHttpCache cache; | 2758 MockHttpCache cache; |
2704 | 2759 |
2705 MockHttpRequest request(kSimpleGET_Transaction); | 2760 MockHttpRequest request(kSimpleGET_Transaction); |
2706 | 2761 |
2707 std::vector<std::unique_ptr<Context>> context_list; | 2762 std::vector<std::unique_ptr<Context>> context_list; |
(...skipping 6596 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
9304 ASSERT_TRUE(attrs->GetDictionary( | 9359 ASSERT_TRUE(attrs->GetDictionary( |
9305 base::trace_event::MemoryAllocatorDump::kNameSize, &size_attrs)); | 9360 base::trace_event::MemoryAllocatorDump::kNameSize, &size_attrs)); |
9306 std::string size; | 9361 std::string size; |
9307 ASSERT_TRUE(size_attrs->GetString("value", &size)); | 9362 ASSERT_TRUE(size_attrs->GetString("value", &size)); |
9308 int actual_size = 0; | 9363 int actual_size = 0; |
9309 ASSERT_TRUE(base::HexStringToInt(size, &actual_size)); | 9364 ASSERT_TRUE(base::HexStringToInt(size, &actual_size)); |
9310 ASSERT_LT(0, actual_size); | 9365 ASSERT_LT(0, actual_size); |
9311 } | 9366 } |
9312 | 9367 |
9313 } // namespace net | 9368 } // namespace net |
OLD | NEW |