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

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

Issue 2886483002: Adds a new class HttpCache::Writers for multiple cache transactions reading from the network. (Closed)
Patch Set: Comment changed. Created 3 years, 5 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
« no previous file with comments | « net/http/http_cache_transaction.cc ('k') | net/http/http_cache_writers.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 200 matching lines...) Expand 10 before | Expand all | Expand 10 after
211 // write to the cache 211 // write to the cache
212 212
213 std::unique_ptr<HttpTransaction> trans; 213 std::unique_ptr<HttpTransaction> trans;
214 int rv = cache->CreateTransaction(DEFAULT_PRIORITY, &trans); 214 int rv = cache->CreateTransaction(DEFAULT_PRIORITY, &trans);
215 EXPECT_THAT(rv, IsOk()); 215 EXPECT_THAT(rv, IsOk());
216 ASSERT_TRUE(trans.get()); 216 ASSERT_TRUE(trans.get());
217 217
218 rv = trans->Start(&request, callback.callback(), net_log); 218 rv = trans->Start(&request, callback.callback(), net_log);
219 if (rv == ERR_IO_PENDING) 219 if (rv == ERR_IO_PENDING)
220 rv = callback.WaitForResult(); 220 rv = callback.WaitForResult();
221 ASSERT_EQ(trans_info.return_code, rv); 221 ASSERT_EQ(trans_info.start_return_code, rv);
222 222
223 if (OK != rv) 223 if (OK != rv)
224 return; 224 return;
225 225
226 const HttpResponseInfo* response = trans->GetResponseInfo(); 226 const HttpResponseInfo* response = trans->GetResponseInfo();
227 ASSERT_TRUE(response); 227 ASSERT_TRUE(response);
228 228
229 if (response_info) 229 if (response_info)
230 *response_info = *response; 230 *response_info = *response;
231 231
(...skipping 837 matching lines...) Expand 10 before | Expand all | Expand 10 after
1069 MockTransaction transaction(kSimpleGET_Transaction); 1069 MockTransaction transaction(kSimpleGET_Transaction);
1070 transaction.response_headers = "Cache-Control: no-cache\n"; 1070 transaction.response_headers = "Cache-Control: no-cache\n";
1071 1071
1072 AddMockTransaction(&transaction); 1072 AddMockTransaction(&transaction);
1073 RunTransactionTest(cache.http_cache(), transaction); 1073 RunTransactionTest(cache.http_cache(), transaction);
1074 EXPECT_EQ(1, cache.network_layer()->transaction_count()); 1074 EXPECT_EQ(1, cache.network_layer()->transaction_count());
1075 EXPECT_EQ(1, cache.disk_cache()->create_count()); 1075 EXPECT_EQ(1, cache.disk_cache()->create_count());
1076 RemoveMockTransaction(&transaction); 1076 RemoveMockTransaction(&transaction);
1077 1077
1078 // Network failure with error; should fail but have was_cached set. 1078 // Network failure with error; should fail but have was_cached set.
1079 transaction.return_code = ERR_FAILED; 1079 transaction.start_return_code = ERR_FAILED;
1080 AddMockTransaction(&transaction); 1080 AddMockTransaction(&transaction);
1081 1081
1082 MockHttpRequest request(transaction); 1082 MockHttpRequest request(transaction);
1083 TestCompletionCallback callback; 1083 TestCompletionCallback callback;
1084 std::unique_ptr<HttpTransaction> trans; 1084 std::unique_ptr<HttpTransaction> trans;
1085 int rv = cache.http_cache()->CreateTransaction(DEFAULT_PRIORITY, &trans); 1085 int rv = cache.http_cache()->CreateTransaction(DEFAULT_PRIORITY, &trans);
1086 EXPECT_THAT(rv, IsOk()); 1086 EXPECT_THAT(rv, IsOk());
1087 ASSERT_TRUE(trans.get()); 1087 ASSERT_TRUE(trans.get());
1088 rv = trans->Start(&request, callback.callback(), NetLogWithSource()); 1088 rv = trans->Start(&request, callback.callback(), NetLogWithSource());
1089 EXPECT_THAT(callback.GetResult(rv), IsError(ERR_FAILED)); 1089 EXPECT_THAT(callback.GetResult(rv), IsError(ERR_FAILED));
(...skipping 3416 matching lines...) Expand 10 before | Expand all | Expand 10 after
4506 MockTransaction transaction(kSimpleGET_Transaction); 4506 MockTransaction transaction(kSimpleGET_Transaction);
4507 AddMockTransaction(&transaction); 4507 AddMockTransaction(&transaction);
4508 4508
4509 // Populate the cache. 4509 // Populate the cache.
4510 RunTransactionTest(cache.http_cache(), transaction); 4510 RunTransactionTest(cache.http_cache(), transaction);
4511 4511
4512 // Load from cache. 4512 // Load from cache.
4513 transaction.method = "HEAD"; 4513 transaction.method = "HEAD";
4514 transaction.request_headers = "Range: bytes = 0-4\r\n"; 4514 transaction.request_headers = "Range: bytes = 0-4\r\n";
4515 transaction.load_flags |= LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION; 4515 transaction.load_flags |= LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION;
4516 transaction.return_code = ERR_CACHE_MISS; 4516 transaction.start_return_code = ERR_CACHE_MISS;
4517 RunTransactionTest(cache.http_cache(), transaction); 4517 RunTransactionTest(cache.http_cache(), transaction);
4518 4518
4519 EXPECT_EQ(0, cache.disk_cache()->open_count()); 4519 EXPECT_EQ(0, cache.disk_cache()->open_count());
4520 EXPECT_EQ(1, cache.disk_cache()->create_count()); 4520 EXPECT_EQ(1, cache.disk_cache()->create_count());
4521 RemoveMockTransaction(&transaction); 4521 RemoveMockTransaction(&transaction);
4522 } 4522 }
4523 4523
4524 // Tests that a HEAD request can be served from a partialy cached resource. 4524 // Tests that a HEAD request can be served from a partialy cached resource.
4525 TEST(HttpCache, SimpleHEAD_WithCachedRanges) { 4525 TEST(HttpCache, SimpleHEAD_WithCachedRanges) {
4526 MockHttpCache cache; 4526 MockHttpCache cache;
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after
4672 4672
4673 // Update the cache. 4673 // Update the cache.
4674 transaction.method = "HEAD"; 4674 transaction.method = "HEAD";
4675 transaction.data = ""; 4675 transaction.data = "";
4676 RunTransactionTest(cache.http_cache(), transaction); 4676 RunTransactionTest(cache.http_cache(), transaction);
4677 EXPECT_EQ(2, cache.network_layer()->transaction_count()); 4677 EXPECT_EQ(2, cache.network_layer()->transaction_count());
4678 4678
4679 // Load from the cache. 4679 // Load from the cache.
4680 transaction.method = "GET"; 4680 transaction.method = "GET";
4681 transaction.load_flags |= LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION; 4681 transaction.load_flags |= LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION;
4682 transaction.return_code = ERR_CACHE_MISS; 4682 transaction.start_return_code = ERR_CACHE_MISS;
4683 RunTransactionTest(cache.http_cache(), transaction); 4683 RunTransactionTest(cache.http_cache(), transaction);
4684 4684
4685 RemoveMockTransaction(&transaction); 4685 RemoveMockTransaction(&transaction);
4686 } 4686 }
4687 4687
4688 // Tests that we do not cache the response of a PUT. 4688 // Tests that we do not cache the response of a PUT.
4689 TEST(HttpCache, SimplePUT_Miss) { 4689 TEST(HttpCache, SimplePUT_Miss) {
4690 MockHttpCache cache; 4690 MockHttpCache cache;
4691 4691
4692 MockTransaction transaction(kSimplePOST_Transaction); 4692 MockTransaction transaction(kSimplePOST_Transaction);
(...skipping 256 matching lines...) Expand 10 before | Expand all | Expand 10 after
4949 // Tests that we don't invalidate entries after a failed network transaction. 4949 // Tests that we don't invalidate entries after a failed network transaction.
4950 TEST(HttpCache, SimpleGET_DontInvalidateOnFailure) { 4950 TEST(HttpCache, SimpleGET_DontInvalidateOnFailure) {
4951 MockHttpCache cache; 4951 MockHttpCache cache;
4952 4952
4953 // Populate the cache. 4953 // Populate the cache.
4954 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); 4954 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction);
4955 EXPECT_EQ(1, cache.network_layer()->transaction_count()); 4955 EXPECT_EQ(1, cache.network_layer()->transaction_count());
4956 4956
4957 // Fail the network request. 4957 // Fail the network request.
4958 MockTransaction transaction(kSimpleGET_Transaction); 4958 MockTransaction transaction(kSimpleGET_Transaction);
4959 transaction.return_code = ERR_FAILED; 4959 transaction.start_return_code = ERR_FAILED;
4960 transaction.load_flags |= LOAD_VALIDATE_CACHE; 4960 transaction.load_flags |= LOAD_VALIDATE_CACHE;
4961 4961
4962 AddMockTransaction(&transaction); 4962 AddMockTransaction(&transaction);
4963 RunTransactionTest(cache.http_cache(), transaction); 4963 RunTransactionTest(cache.http_cache(), transaction);
4964 EXPECT_EQ(2, cache.network_layer()->transaction_count()); 4964 EXPECT_EQ(2, cache.network_layer()->transaction_count());
4965 RemoveMockTransaction(&transaction); 4965 RemoveMockTransaction(&transaction);
4966 4966
4967 transaction.load_flags = LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION; 4967 transaction.load_flags = LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION;
4968 transaction.return_code = OK; 4968 transaction.start_return_code = OK;
4969 AddMockTransaction(&transaction); 4969 AddMockTransaction(&transaction);
4970 RunTransactionTest(cache.http_cache(), transaction); 4970 RunTransactionTest(cache.http_cache(), transaction);
4971 4971
4972 // Make sure the transaction didn't reach the network. 4972 // Make sure the transaction didn't reach the network.
4973 EXPECT_EQ(2, cache.network_layer()->transaction_count()); 4973 EXPECT_EQ(2, cache.network_layer()->transaction_count());
4974 RemoveMockTransaction(&transaction); 4974 RemoveMockTransaction(&transaction);
4975 } 4975 }
4976 4976
4977 TEST(HttpCache, RangeGET_SkipsCache) { 4977 TEST(HttpCache, RangeGET_SkipsCache) {
4978 MockHttpCache cache; 4978 MockHttpCache cache;
(...skipping 3054 matching lines...) Expand 10 before | Expand all | Expand 10 after
8033 transaction.request_headers = "accept-encoding: gzip\r\n"; 8033 transaction.request_headers = "accept-encoding: gzip\r\n";
8034 transaction.response_headers = 8034 transaction.response_headers =
8035 "Vary: accept-encoding\n" 8035 "Vary: accept-encoding\n"
8036 "Cache-Control: max-age=10000\n"; 8036 "Cache-Control: max-age=10000\n";
8037 RunTransactionTest(cache.http_cache(), transaction); 8037 RunTransactionTest(cache.http_cache(), transaction);
8038 8038
8039 // Change the request headers so that the request doesn't match due to vary. 8039 // Change the request headers so that the request doesn't match due to vary.
8040 // The request should fail. 8040 // The request should fail.
8041 transaction.load_flags = LOAD_ONLY_FROM_CACHE; 8041 transaction.load_flags = LOAD_ONLY_FROM_CACHE;
8042 transaction.request_headers = "accept-encoding: foo\r\n"; 8042 transaction.request_headers = "accept-encoding: foo\r\n";
8043 transaction.return_code = ERR_CACHE_MISS; 8043 transaction.start_return_code = ERR_CACHE_MISS;
8044 RunTransactionTest(cache.http_cache(), transaction); 8044 RunTransactionTest(cache.http_cache(), transaction);
8045 8045
8046 // Change the load flags to ignore vary checks, the request should now hit. 8046 // Change the load flags to ignore vary checks, the request should now hit.
8047 transaction.load_flags = LOAD_ONLY_FROM_CACHE | LOAD_SKIP_VARY_CHECK; 8047 transaction.load_flags = LOAD_ONLY_FROM_CACHE | LOAD_SKIP_VARY_CHECK;
8048 transaction.return_code = OK; 8048 transaction.start_return_code = OK;
8049 RunTransactionTest(cache.http_cache(), transaction); 8049 RunTransactionTest(cache.http_cache(), transaction);
8050 } 8050 }
8051 8051
8052 // Tests that we only return valid entries with LOAD_ONLY_FROM_CACHE 8052 // Tests that we only return valid entries with LOAD_ONLY_FROM_CACHE
8053 // transactions unless LOAD_SKIP_CACHE_VALIDATION is set. 8053 // transactions unless LOAD_SKIP_CACHE_VALIDATION is set.
8054 TEST(HttpCache, ValidLoadOnlyFromCache) { 8054 TEST(HttpCache, ValidLoadOnlyFromCache) {
8055 MockHttpCache cache; 8055 MockHttpCache cache;
8056 base::SimpleTestClock* clock = new base::SimpleTestClock(); 8056 base::SimpleTestClock* clock = new base::SimpleTestClock();
8057 cache.http_cache()->SetClockForTesting(base::WrapUnique(clock)); 8057 cache.http_cache()->SetClockForTesting(base::WrapUnique(clock));
8058 cache.network_layer()->SetClock(clock); 8058 cache.network_layer()->SetClock(clock);
8059 8059
8060 // Write a resource that will expire in 100 seconds. 8060 // Write a resource that will expire in 100 seconds.
8061 ScopedMockTransaction transaction(kSimpleGET_Transaction); 8061 ScopedMockTransaction transaction(kSimpleGET_Transaction);
8062 transaction.response_headers = "Cache-Control: max-age=100\n"; 8062 transaction.response_headers = "Cache-Control: max-age=100\n";
8063 RunTransactionTest(cache.http_cache(), transaction); 8063 RunTransactionTest(cache.http_cache(), transaction);
8064 8064
8065 // Move forward in time such that the cached response is no longer valid. 8065 // Move forward in time such that the cached response is no longer valid.
8066 clock->Advance(base::TimeDelta::FromSeconds(101)); 8066 clock->Advance(base::TimeDelta::FromSeconds(101));
8067 8067
8068 // Skipping cache validation should still return a response. 8068 // Skipping cache validation should still return a response.
8069 transaction.load_flags = LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION; 8069 transaction.load_flags = LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION;
8070 RunTransactionTest(cache.http_cache(), transaction); 8070 RunTransactionTest(cache.http_cache(), transaction);
8071 8071
8072 // If the cache entry is checked for validitiy, it should fail. 8072 // If the cache entry is checked for validitiy, it should fail.
8073 transaction.load_flags = LOAD_ONLY_FROM_CACHE; 8073 transaction.load_flags = LOAD_ONLY_FROM_CACHE;
8074 transaction.return_code = ERR_CACHE_MISS; 8074 transaction.start_return_code = ERR_CACHE_MISS;
8075 RunTransactionTest(cache.http_cache(), transaction); 8075 RunTransactionTest(cache.http_cache(), transaction);
8076 } 8076 }
8077 8077
8078 TEST(HttpCache, InvalidLoadFlagCombination) { 8078 TEST(HttpCache, InvalidLoadFlagCombination) {
8079 MockHttpCache cache; 8079 MockHttpCache cache;
8080 8080
8081 // Put the resource in the cache. 8081 // Put the resource in the cache.
8082 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); 8082 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction);
8083 8083
8084 // Now try to fetch it again, but with a flag combination disallowing both 8084 // Now try to fetch it again, but with a flag combination disallowing both
8085 // cache and network access. 8085 // cache and network access.
8086 ScopedMockTransaction transaction(kSimpleGET_Transaction); 8086 ScopedMockTransaction transaction(kSimpleGET_Transaction);
8087 // DevTools relies on this combination of flags for "disable cache" mode 8087 // DevTools relies on this combination of flags for "disable cache" mode
8088 // when a resource is only supposed to be loaded from cache. 8088 // when a resource is only supposed to be loaded from cache.
8089 transaction.load_flags = LOAD_ONLY_FROM_CACHE | LOAD_BYPASS_CACHE; 8089 transaction.load_flags = LOAD_ONLY_FROM_CACHE | LOAD_BYPASS_CACHE;
8090 transaction.return_code = ERR_CACHE_MISS; 8090 transaction.start_return_code = ERR_CACHE_MISS;
8091 RunTransactionTest(cache.http_cache(), transaction); 8091 RunTransactionTest(cache.http_cache(), transaction);
8092 } 8092 }
8093 8093
8094 // Tests that we can read metadata after validating the entry and with READ mode 8094 // Tests that we can read metadata after validating the entry and with READ mode
8095 // transactions. 8095 // transactions.
8096 TEST(HttpCache, ReadMetadata) { 8096 TEST(HttpCache, ReadMetadata) {
8097 MockHttpCache cache; 8097 MockHttpCache cache;
8098 8098
8099 // Write to the cache 8099 // Write to the cache
8100 HttpResponseInfo response; 8100 HttpResponseInfo response;
(...skipping 1258 matching lines...) Expand 10 before | Expand all | Expand 10 after
9359 ASSERT_TRUE(attrs->GetDictionary( 9359 ASSERT_TRUE(attrs->GetDictionary(
9360 base::trace_event::MemoryAllocatorDump::kNameSize, &size_attrs)); 9360 base::trace_event::MemoryAllocatorDump::kNameSize, &size_attrs));
9361 std::string size; 9361 std::string size;
9362 ASSERT_TRUE(size_attrs->GetString("value", &size)); 9362 ASSERT_TRUE(size_attrs->GetString("value", &size));
9363 int actual_size = 0; 9363 int actual_size = 0;
9364 ASSERT_TRUE(base::HexStringToInt(size, &actual_size)); 9364 ASSERT_TRUE(base::HexStringToInt(size, &actual_size));
9365 ASSERT_LT(0, actual_size); 9365 ASSERT_LT(0, actual_size);
9366 } 9366 }
9367 9367
9368 } // namespace net 9368 } // namespace net
OLDNEW
« no previous file with comments | « net/http/http_cache_transaction.cc ('k') | net/http/http_cache_writers.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698