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

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

Issue 2959183002: Start work on splitting off the usage piece of in-memory index. This revision is just from the prev…
Patch Set: Rebase Created 3 years, 2 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') | no next file » | 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 1045 matching lines...) Expand 10 before | Expand all | Expand 10 after
1056 EXPECT_EQ(2, cache.network_layer()->transaction_count()); 1056 EXPECT_EQ(2, cache.network_layer()->transaction_count());
1057 EXPECT_EQ(1, cache.disk_cache()->open_count()); 1057 EXPECT_EQ(1, cache.disk_cache()->open_count());
1058 EXPECT_EQ(1, cache.disk_cache()->create_count()); 1058 EXPECT_EQ(1, cache.disk_cache()->create_count());
1059 TestLoadTimingNetworkRequest(load_timing_info); 1059 TestLoadTimingNetworkRequest(load_timing_info);
1060 RemoveMockTransaction(&transaction); 1060 RemoveMockTransaction(&transaction);
1061 } 1061 }
1062 1062
1063 // Tests that was_cached was set properly on a failure, even if the cached 1063 // Tests that was_cached was set properly on a failure, even if the cached
1064 // response wasn't returned. 1064 // response wasn't returned.
1065 TEST(HttpCache, SimpleGET_CacheSignal_Failure) { 1065 TEST(HttpCache, SimpleGET_CacheSignal_Failure) {
1066 MockHttpCache cache; 1066 for (bool use_memory_entry_data : {false, true}) {
1067 MockHttpCache cache;
1068 cache.disk_cache()->set_support_in_memory_entry_data(use_memory_entry_data);
1067 1069
1068 // Prime cache. 1070 // Prime cache.
1069 MockTransaction transaction(kSimpleGET_Transaction); 1071 MockTransaction transaction(kSimpleGET_Transaction);
1070 transaction.response_headers = "Cache-Control: no-cache\n"; 1072 transaction.response_headers = "Cache-Control: no-cache\n";
1071 1073
1072 AddMockTransaction(&transaction); 1074 AddMockTransaction(&transaction);
1073 RunTransactionTest(cache.http_cache(), transaction); 1075 RunTransactionTest(cache.http_cache(), transaction);
1074 EXPECT_EQ(1, cache.network_layer()->transaction_count()); 1076 EXPECT_EQ(1, cache.network_layer()->transaction_count());
1075 EXPECT_EQ(1, cache.disk_cache()->create_count()); 1077 EXPECT_EQ(1, cache.disk_cache()->create_count());
1076 RemoveMockTransaction(&transaction); 1078 RemoveMockTransaction(&transaction);
1077 1079
1078 // Network failure with error; should fail but have was_cached set. 1080 // Network failure with error; should fail but have was_cached set.
1079 transaction.start_return_code = ERR_FAILED; 1081 transaction.start_return_code = ERR_FAILED;
1080 AddMockTransaction(&transaction); 1082 AddMockTransaction(&transaction);
1081 1083
1082 MockHttpRequest request(transaction); 1084 MockHttpRequest request(transaction);
1083 TestCompletionCallback callback; 1085 TestCompletionCallback callback;
1084 std::unique_ptr<HttpTransaction> trans; 1086 std::unique_ptr<HttpTransaction> trans;
1085 int rv = cache.http_cache()->CreateTransaction(DEFAULT_PRIORITY, &trans); 1087 int rv = cache.http_cache()->CreateTransaction(DEFAULT_PRIORITY, &trans);
1086 EXPECT_THAT(rv, IsOk()); 1088 EXPECT_THAT(rv, IsOk());
1087 ASSERT_TRUE(trans.get()); 1089 ASSERT_TRUE(trans.get());
1088 rv = trans->Start(&request, callback.callback(), NetLogWithSource()); 1090 rv = trans->Start(&request, callback.callback(), NetLogWithSource());
1089 EXPECT_THAT(callback.GetResult(rv), IsError(ERR_FAILED)); 1091 EXPECT_THAT(callback.GetResult(rv), IsError(ERR_FAILED));
1090 1092
1091 const HttpResponseInfo* response_info = trans->GetResponseInfo(); 1093 const HttpResponseInfo* response_info = trans->GetResponseInfo();
1092 ASSERT_TRUE(response_info); 1094 ASSERT_TRUE(response_info);
1093 EXPECT_TRUE(response_info->was_cached); 1095 // If use_memory_entry_data is true, we will not bother opening the entry,
1094 EXPECT_EQ(2, cache.network_layer()->transaction_count()); 1096 // and just kick it out, so was_cached will end up false.
1097 EXPECT_EQ(!use_memory_entry_data, response_info->was_cached);
1098 EXPECT_EQ(2, cache.network_layer()->transaction_count());
1095 1099
1096 RemoveMockTransaction(&transaction); 1100 RemoveMockTransaction(&transaction);
1101 }
1097 } 1102 }
1098 1103
1099 // Confirm if we have an empty cache, a read is marked as network verified. 1104 // Confirm if we have an empty cache, a read is marked as network verified.
1100 TEST(HttpCache, SimpleGET_NetworkAccessed_Network) { 1105 TEST(HttpCache, SimpleGET_NetworkAccessed_Network) {
1101 MockHttpCache cache; 1106 MockHttpCache cache;
1102 1107
1103 // write to the cache 1108 // write to the cache
1104 HttpResponseInfo response_info; 1109 HttpResponseInfo response_info;
1105 RunTransactionTestWithResponseInfo(cache.http_cache(), kSimpleGET_Transaction, 1110 RunTransactionTestWithResponseInfo(cache.http_cache(), kSimpleGET_Transaction,
1106 &response_info); 1111 &response_info);
(...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after
1309 1314
1310 static void PreserveRequestHeaders_Handler(const HttpRequestInfo* request, 1315 static void PreserveRequestHeaders_Handler(const HttpRequestInfo* request,
1311 std::string* response_status, 1316 std::string* response_status,
1312 std::string* response_headers, 1317 std::string* response_headers,
1313 std::string* response_data) { 1318 std::string* response_data) {
1314 EXPECT_TRUE(request->extra_headers.HasHeader(kExtraHeaderKey)); 1319 EXPECT_TRUE(request->extra_headers.HasHeader(kExtraHeaderKey));
1315 } 1320 }
1316 1321
1317 // Tests that we don't remove extra headers for simple requests. 1322 // Tests that we don't remove extra headers for simple requests.
1318 TEST(HttpCache, SimpleGET_PreserveRequestHeaders) { 1323 TEST(HttpCache, SimpleGET_PreserveRequestHeaders) {
1319 MockHttpCache cache; 1324 for (bool use_memory_entry_data : {false, true}) {
1325 MockHttpCache cache;
1326 cache.disk_cache()->set_support_in_memory_entry_data(use_memory_entry_data);
1320 1327
1321 MockTransaction transaction(kSimpleGET_Transaction); 1328 MockTransaction transaction(kSimpleGET_Transaction);
1322 transaction.handler = PreserveRequestHeaders_Handler; 1329 transaction.handler = PreserveRequestHeaders_Handler;
1323 transaction.request_headers = EXTRA_HEADER; 1330 transaction.request_headers = EXTRA_HEADER;
1324 transaction.response_headers = "Cache-Control: max-age=0\n"; 1331 transaction.response_headers = "Cache-Control: max-age=0\n";
1325 AddMockTransaction(&transaction); 1332 AddMockTransaction(&transaction);
1326 1333
1327 // Write, then revalidate the entry. 1334 // Write, then revalidate the entry.
1328 RunTransactionTest(cache.http_cache(), transaction); 1335 RunTransactionTest(cache.http_cache(), transaction);
1329 RunTransactionTest(cache.http_cache(), transaction); 1336 RunTransactionTest(cache.http_cache(), transaction);
1330 1337
1331 EXPECT_EQ(2, cache.network_layer()->transaction_count()); 1338 EXPECT_EQ(2, cache.network_layer()->transaction_count());
1332 EXPECT_EQ(1, cache.disk_cache()->open_count()); 1339
1333 EXPECT_EQ(1, cache.disk_cache()->create_count()); 1340 // If the backend supports memory entry data, we can figure out that the
1334 RemoveMockTransaction(&transaction); 1341 // entry has caching-hostile headers w/o opening it.
1342 if (use_memory_entry_data) {
1343 EXPECT_EQ(0, cache.disk_cache()->open_count());
1344 EXPECT_EQ(2, cache.disk_cache()->create_count());
1345 } else {
1346 EXPECT_EQ(1, cache.disk_cache()->open_count());
1347 EXPECT_EQ(1, cache.disk_cache()->create_count());
1348 }
1349 RemoveMockTransaction(&transaction);
1350 }
1335 } 1351 }
1336 1352
1337 // Tests that we don't remove extra headers for conditionalized requests. 1353 // Tests that we don't remove extra headers for conditionalized requests.
1338 TEST(HttpCache, ConditionalizedGET_PreserveRequestHeaders) { 1354 TEST(HttpCache, ConditionalizedGET_PreserveRequestHeaders) {
1339 MockHttpCache cache; 1355 for (bool use_memory_entry_data : {false, true}) {
1356 MockHttpCache cache;
1357 // Unlike in SimpleGET_PreserveRequestHeaders, this entry can be
1358 // conditionalized, so memory hints don't affect behavior.
1359 cache.disk_cache()->set_support_in_memory_entry_data(use_memory_entry_data);
1340 1360
1341 // Write to the cache. 1361 // Write to the cache.
1342 RunTransactionTest(cache.http_cache(), kETagGET_Transaction); 1362 RunTransactionTest(cache.http_cache(), kETagGET_Transaction);
1343 1363
1344 MockTransaction transaction(kETagGET_Transaction); 1364 MockTransaction transaction(kETagGET_Transaction);
1345 transaction.handler = PreserveRequestHeaders_Handler; 1365 transaction.handler = PreserveRequestHeaders_Handler;
1346 transaction.request_headers = "If-None-Match: \"foopy\"\r\n" EXTRA_HEADER; 1366 transaction.request_headers = "If-None-Match: \"foopy\"\r\n" EXTRA_HEADER;
1347 AddMockTransaction(&transaction); 1367 AddMockTransaction(&transaction);
1348 1368
1349 RunTransactionTest(cache.http_cache(), transaction); 1369 RunTransactionTest(cache.http_cache(), transaction);
1350 1370
1351 EXPECT_EQ(2, cache.network_layer()->transaction_count()); 1371 EXPECT_EQ(2, cache.network_layer()->transaction_count());
1352 EXPECT_EQ(1, cache.disk_cache()->open_count()); 1372 EXPECT_EQ(1, cache.disk_cache()->open_count());
1353 EXPECT_EQ(1, cache.disk_cache()->create_count()); 1373 EXPECT_EQ(1, cache.disk_cache()->create_count());
1354 RemoveMockTransaction(&transaction); 1374 RemoveMockTransaction(&transaction);
1375 }
1355 } 1376 }
1356 1377
1357 TEST(HttpCache, SimpleGET_ManyReaders) { 1378 TEST(HttpCache, SimpleGET_ManyReaders) {
1358 MockHttpCache cache; 1379 MockHttpCache cache;
1359 1380
1360 MockHttpRequest request(kSimpleGET_Transaction); 1381 MockHttpRequest request(kSimpleGET_Transaction);
1361 1382
1362 std::vector<std::unique_ptr<Context>> context_list; 1383 std::vector<std::unique_ptr<Context>> context_list;
1363 const int kNumTransactions = 5; 1384 const int kNumTransactions = 5;
1364 1385
(...skipping 4010 matching lines...) Expand 10 before | Expand all | Expand 10 after
5375 RunTransactionTestWithLog(cache.http_cache(), kRangeGET_TransactionOK, 5396 RunTransactionTestWithLog(cache.http_cache(), kRangeGET_TransactionOK,
5376 log.bound()); 5397 log.bound());
5377 5398
5378 EXPECT_TRUE(LogContainsEventType( 5399 EXPECT_TRUE(LogContainsEventType(
5379 log, NetLogEventType::HTTP_CACHE_RESTART_PARTIAL_REQUEST)); 5400 log, NetLogEventType::HTTP_CACHE_RESTART_PARTIAL_REQUEST));
5380 } 5401 }
5381 5402
5382 // Tests that a failure to conditionalize a regular request (no range) with a 5403 // Tests that a failure to conditionalize a regular request (no range) with a
5383 // sparse entry results in a full response. 5404 // sparse entry results in a full response.
5384 TEST(HttpCache, GET_NoConditionalization) { 5405 TEST(HttpCache, GET_NoConditionalization) {
5385 MockHttpCache cache; 5406 for (bool use_memory_entry_data : {false, true}) {
5386 cache.FailConditionalizations(); 5407 MockHttpCache cache;
5387 std::string headers; 5408 cache.disk_cache()->set_support_in_memory_entry_data(use_memory_entry_data);
5409 cache.FailConditionalizations();
5410 std::string headers;
5388 5411
5389 // Write to the cache (40-49). 5412 // Write to the cache (40-49).
5390 ScopedMockTransaction transaction(kRangeGET_TransactionOK); 5413 ScopedMockTransaction transaction(kRangeGET_TransactionOK);
5391 transaction.response_headers = "Content-Length: 10\n" 5414 transaction.response_headers =
5392 "ETag: \"foo\"\n"; 5415 "Content-Length: 10\n"
5393 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); 5416 "ETag: \"foo\"\n";
5417 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
5394 5418
5395 Verify206Response(headers, 40, 49); 5419 Verify206Response(headers, 40, 49);
5396 EXPECT_EQ(1, cache.network_layer()->transaction_count()); 5420 EXPECT_EQ(1, cache.network_layer()->transaction_count());
5397 EXPECT_EQ(0, cache.disk_cache()->open_count()); 5421 EXPECT_EQ(0, cache.disk_cache()->open_count());
5398 EXPECT_EQ(1, cache.disk_cache()->create_count()); 5422 EXPECT_EQ(1, cache.disk_cache()->create_count());
5399 5423
5400 // Now verify that the cached data is not used. 5424 // Now verify that the cached data is not used.
5401 // Don't ask for a range. The cache will attempt to use the cached data but 5425 // Don't ask for a range. The cache will attempt to use the cached data but
5402 // should discard it as it cannot be validated. A regular request should go 5426 // should discard it as it cannot be validated. A regular request should go
5403 // to the server and a new entry should be created. 5427 // to the server and a new entry should be created.
5404 transaction.request_headers = EXTRA_HEADER; 5428 transaction.request_headers = EXTRA_HEADER;
5405 transaction.data = "Not a range"; 5429 transaction.data = "Not a range";
5406 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); 5430 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
5407 5431
5408 EXPECT_EQ(0U, headers.find("HTTP/1.1 200 OK\n")); 5432 EXPECT_EQ(0U, headers.find("HTTP/1.1 200 OK\n"));
5409 EXPECT_EQ(2, cache.network_layer()->transaction_count()); 5433 EXPECT_EQ(2, cache.network_layer()->transaction_count());
5410 EXPECT_EQ(1, cache.disk_cache()->open_count()); 5434 EXPECT_EQ(1, cache.disk_cache()->open_count());
5411 EXPECT_EQ(2, cache.disk_cache()->create_count()); 5435 EXPECT_EQ(2, cache.disk_cache()->create_count());
5412 5436
5413 // The last response was saved. 5437 // The last response was saved.
5414 RunTransactionTest(cache.http_cache(), transaction); 5438 // ### before commit: figure out the way to actually test that.
5415 EXPECT_EQ(3, cache.network_layer()->transaction_count()); 5439 // The open results in a useless entry since
5416 EXPECT_EQ(2, cache.disk_cache()->open_count()); 5440 // &RangeTransactionServer::RangeHandler returns headers without an ETag
5417 EXPECT_EQ(2, cache.disk_cache()->create_count()); 5441 // or a non-range request (not the things we set above).
5442 RunTransactionTest(cache.http_cache(), transaction);
5443 EXPECT_EQ(3, cache.network_layer()->transaction_count());
5444 if (use_memory_entry_data) {
5445 EXPECT_EQ(1, cache.disk_cache()->open_count());
5446 EXPECT_EQ(3, cache.disk_cache()->create_count());
5447 } else {
5448 EXPECT_EQ(2, cache.disk_cache()->open_count());
5449 EXPECT_EQ(2, cache.disk_cache()->create_count());
5450 }
5451 }
5418 } 5452 }
5419 5453
5420 // Verifies that conditionalization failures when asking for a range that would 5454 // Verifies that conditionalization failures when asking for a range that would
5421 // require the cache to modify the range to ask, result in a network request 5455 // require the cache to modify the range to ask, result in a network request
5422 // that matches the user's one. 5456 // that matches the user's one.
5423 TEST(HttpCache, RangeGET_NoConditionalization2) { 5457 TEST(HttpCache, RangeGET_NoConditionalization2) {
5424 MockHttpCache cache; 5458 MockHttpCache cache;
5425 cache.FailConditionalizations(); 5459 cache.FailConditionalizations();
5426 std::string headers; 5460 std::string headers;
5427 5461
(...skipping 2451 matching lines...) Expand 10 before | Expand all | Expand 10 after
7879 // read the response body -- want to test that it is still getting cached. 7913 // read the response body -- want to test that it is still getting cached.
7880 } 7914 }
7881 EXPECT_EQ(1, cache.network_layer()->transaction_count()); 7915 EXPECT_EQ(1, cache.network_layer()->transaction_count());
7882 EXPECT_EQ(1, cache.disk_cache()->open_count()); 7916 EXPECT_EQ(1, cache.disk_cache()->open_count());
7883 EXPECT_EQ(1, cache.disk_cache()->create_count()); 7917 EXPECT_EQ(1, cache.disk_cache()->create_count());
7884 } 7918 }
7885 7919
7886 // Verify that no-cache resources are stored in cache, but are not fetched from 7920 // Verify that no-cache resources are stored in cache, but are not fetched from
7887 // cache during normal loads. 7921 // cache during normal loads.
7888 TEST(HttpCache, CacheControlNoCacheNormalLoad) { 7922 TEST(HttpCache, CacheControlNoCacheNormalLoad) {
7889 MockHttpCache cache; 7923 for (bool use_memory_entry_data : {false, true}) {
7924 MockHttpCache cache;
7925 cache.disk_cache()->set_support_in_memory_entry_data(use_memory_entry_data);
7890 7926
7891 ScopedMockTransaction transaction(kSimpleGET_Transaction); 7927 ScopedMockTransaction transaction(kSimpleGET_Transaction);
7892 transaction.response_headers = "cache-control: no-cache\n"; 7928 transaction.response_headers = "cache-control: no-cache\n";
7893 7929
7894 // Initial load. 7930 // Initial load.
7895 RunTransactionTest(cache.http_cache(), transaction); 7931 RunTransactionTest(cache.http_cache(), transaction);
7896 7932
7897 EXPECT_EQ(1, cache.network_layer()->transaction_count()); 7933 EXPECT_EQ(1, cache.network_layer()->transaction_count());
7898 EXPECT_EQ(0, cache.disk_cache()->open_count()); 7934 EXPECT_EQ(0, cache.disk_cache()->open_count());
7899 EXPECT_EQ(1, cache.disk_cache()->create_count()); 7935 EXPECT_EQ(1, cache.disk_cache()->create_count());
7900 7936
7901 // Try loading again; it should result in a network fetch. 7937 // Try loading again; it should result in a network fetch.
7902 RunTransactionTest(cache.http_cache(), transaction); 7938 RunTransactionTest(cache.http_cache(), transaction);
7903 7939
7904 EXPECT_EQ(2, cache.network_layer()->transaction_count()); 7940 EXPECT_EQ(2, cache.network_layer()->transaction_count());
7905 EXPECT_EQ(1, cache.disk_cache()->open_count()); 7941 if (use_memory_entry_data) {
7906 EXPECT_EQ(1, cache.disk_cache()->create_count()); 7942 EXPECT_EQ(0, cache.disk_cache()->open_count());
7943 EXPECT_EQ(2, cache.disk_cache()->create_count());
7944 } else {
7945 EXPECT_EQ(1, cache.disk_cache()->open_count());
7946 EXPECT_EQ(1, cache.disk_cache()->create_count());
7947 }
7907 7948
7908 disk_cache::Entry* entry; 7949 disk_cache::Entry* entry;
7909 EXPECT_TRUE(cache.OpenBackendEntry(transaction.url, &entry)); 7950 EXPECT_TRUE(cache.OpenBackendEntry(transaction.url, &entry));
7910 entry->Close(); 7951 entry->Close();
7952 }
7911 } 7953 }
7912 7954
7913 // Verify that no-cache resources are stored in cache and fetched from cache 7955 // Verify that no-cache resources are stored in cache and fetched from cache
7914 // when the LOAD_SKIP_CACHE_VALIDATION flag is set. 7956 // when the LOAD_SKIP_CACHE_VALIDATION flag is set.
7915 TEST(HttpCache, CacheControlNoCacheHistoryLoad) { 7957 TEST(HttpCache, CacheControlNoCacheHistoryLoad) {
7916 MockHttpCache cache; 7958 MockHttpCache cache;
7917 7959
7918 ScopedMockTransaction transaction(kSimpleGET_Transaction); 7960 ScopedMockTransaction transaction(kSimpleGET_Transaction);
7919 transaction.response_headers = "cache-control: no-cache\n"; 7961 transaction.response_headers = "cache-control: no-cache\n";
7920 7962
(...skipping 1671 matching lines...) Expand 10 before | Expand all | Expand 10 after
9592 ASSERT_TRUE(attrs->GetDictionary( 9634 ASSERT_TRUE(attrs->GetDictionary(
9593 base::trace_event::MemoryAllocatorDump::kNameSize, &size_attrs)); 9635 base::trace_event::MemoryAllocatorDump::kNameSize, &size_attrs));
9594 std::string size; 9636 std::string size;
9595 ASSERT_TRUE(size_attrs->GetString("value", &size)); 9637 ASSERT_TRUE(size_attrs->GetString("value", &size));
9596 int actual_size = 0; 9638 int actual_size = 0;
9597 ASSERT_TRUE(base::HexStringToInt(size, &actual_size)); 9639 ASSERT_TRUE(base::HexStringToInt(size, &actual_size));
9598 ASSERT_LT(0, actual_size); 9640 ASSERT_LT(0, actual_size);
9599 } 9641 }
9600 9642
9601 } // namespace net 9643 } // namespace net
OLDNEW
« no previous file with comments | « net/http/http_cache_transaction.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698