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 1358 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1369 // All requests are waiting for the active entry. | 1369 // All requests are waiting for the active entry. |
1370 for (int i = 0; i < kNumTransactions; ++i) { | 1370 for (int i = 0; i < kNumTransactions; ++i) { |
1371 Context* c = context_list[i]; | 1371 Context* c = context_list[i]; |
1372 EXPECT_EQ(LOAD_STATE_WAITING_FOR_CACHE, c->trans->GetLoadState()); | 1372 EXPECT_EQ(LOAD_STATE_WAITING_FOR_CACHE, c->trans->GetLoadState()); |
1373 } | 1373 } |
1374 | 1374 |
1375 // Allow all requests to move from the Create queue to the active entry. | 1375 // Allow all requests to move from the Create queue to the active entry. |
1376 base::RunLoop().RunUntilIdle(); | 1376 base::RunLoop().RunUntilIdle(); |
1377 | 1377 |
1378 // The first request should be a writer at this point, and the subsequent | 1378 // The first request should be a writer at this point, and the subsequent |
1379 // requests should be pending. | 1379 // requests should have passed the validation phase and waiting for the |
1380 // response to be written to the cache before they can read. | |
1380 | 1381 |
1381 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 1382 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
1382 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 1383 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
1383 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 1384 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
1384 | 1385 |
1385 // All requests depend on the writer, and the writer is between Start and | 1386 // All requests depend on the writer, and the writer is between Start and |
1386 // Read, i.e. idle. | 1387 // Read, i.e. idle. |
1387 for (int i = 0; i < kNumTransactions; ++i) { | 1388 for (int i = 0; i < kNumTransactions; ++i) { |
1388 Context* c = context_list[i]; | 1389 Context* c = context_list[i]; |
1389 EXPECT_EQ(LOAD_STATE_IDLE, c->trans->GetLoadState()); | 1390 EXPECT_EQ(LOAD_STATE_IDLE, c->trans->GetLoadState()); |
(...skipping 11 matching lines...) Expand all Loading... | |
1401 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 1402 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
1402 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 1403 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
1403 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 1404 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
1404 | 1405 |
1405 for (int i = 0; i < kNumTransactions; ++i) { | 1406 for (int i = 0; i < kNumTransactions; ++i) { |
1406 Context* c = context_list[i]; | 1407 Context* c = context_list[i]; |
1407 delete c; | 1408 delete c; |
1408 } | 1409 } |
1409 } | 1410 } |
1410 | 1411 |
1412 // Tests parallel validation similar to the above test, except here validation | |
1413 // request is sent out to the network and results in 304. | |
1414 TEST(HttpCache, SimpleGET_ParallelValidation) { | |
1415 MockHttpCache cache; | |
1416 | |
1417 std::vector<MockHttpRequest> request; | |
1418 ScopedMockTransaction transaction(kTypicalGET_Transaction); | |
1419 transaction.response_headers = | |
1420 "Etag: \"foopy\"\n" | |
1421 "Cache-Control: max-age=0\n"; | |
1422 MockHttpRequest request0(transaction); | |
1423 request.push_back(request0); | |
1424 | |
1425 ScopedMockTransaction transaction1(kTypicalGET_Transaction); | |
1426 transaction1.status = "HTTP/1.1 304 Not Modified"; | |
1427 transaction1.load_flags |= LOAD_VALIDATE_CACHE; | |
1428 transaction1.response_headers = | |
1429 "Etag: \"foopy\"\n" | |
1430 "Cache-Control: max-age=3600\n"; | |
1431 MockHttpRequest request1(transaction1); | |
1432 request.push_back(request1); | |
1433 | |
1434 std::vector<Context*> context_list; | |
1435 const int kNumTransactions = 2; | |
1436 | |
1437 for (int i = 0; i < kNumTransactions; ++i) { | |
1438 context_list.push_back(new Context()); | |
1439 Context* c = context_list[i]; | |
1440 | |
1441 c->result = cache.CreateTransaction(&c->trans); | |
1442 ASSERT_THAT(c->result, IsOk()); | |
1443 EXPECT_EQ(LOAD_STATE_IDLE, c->trans->GetLoadState()); | |
1444 | |
1445 c->result = c->trans->Start(&request[i], c->callback.callback(), | |
1446 NetLogWithSource()); | |
1447 } | |
1448 | |
1449 // All requests are waiting for the active entry. | |
1450 for (int i = 0; i < kNumTransactions; ++i) { | |
1451 Context* c = context_list[i]; | |
1452 EXPECT_EQ(LOAD_STATE_WAITING_FOR_CACHE, c->trans->GetLoadState()); | |
1453 } | |
1454 | |
1455 // Allow all requests to move from the Create queue to the active entry. | |
1456 base::RunLoop().RunUntilIdle(); | |
1457 | |
1458 // The first request should be a writer at this point, and the subsequent | |
1459 // requests should have passed the validation phase and waiting for the | |
1460 // response to be written to the cache before they can read. | |
1461 | |
1462 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | |
1463 EXPECT_EQ(0, cache.disk_cache()->open_count()); | |
1464 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
1465 | |
1466 // All requests depend on the writer, and the writer is between Start and | |
1467 // Read, i.e. idle. | |
1468 for (int i = 0; i < kNumTransactions; ++i) { | |
1469 Context* c = context_list[i]; | |
1470 EXPECT_EQ(LOAD_STATE_IDLE, c->trans->GetLoadState()); | |
1471 } | |
1472 | |
1473 for (int i = 0; i < kNumTransactions; ++i) { | |
1474 Context* c = context_list[i]; | |
1475 if (c->result == ERR_IO_PENDING) | |
1476 c->result = c->callback.WaitForResult(); | |
1477 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction); | |
1478 } | |
1479 | |
1480 // We should not have had to re-open the disk entry | |
1481 | |
1482 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | |
1483 EXPECT_EQ(0, cache.disk_cache()->open_count()); | |
1484 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
1485 | |
1486 for (int i = 0; i < kNumTransactions; ++i) { | |
1487 Context* c = context_list[i]; | |
1488 delete c; | |
1489 } | |
1490 } | |
1491 | |
1411 // This is a test for http://code.google.com/p/chromium/issues/detail?id=4769. | 1492 // This is a test for http://code.google.com/p/chromium/issues/detail?id=4769. |
1412 // If cancelling a request is racing with another request for the same resource | 1493 // If cancelling a request is racing with another request for the same resource |
1413 // finishing, we have to make sure that we remove both transactions from the | 1494 // finishing, we have to make sure that we remove both transactions from the |
1414 // entry. | 1495 // entry. |
1415 TEST(HttpCache, SimpleGET_RacingReaders) { | 1496 TEST(HttpCache, SimpleGET_RacingReaders) { |
1416 MockHttpCache cache; | 1497 MockHttpCache cache; |
1417 | 1498 |
1418 MockHttpRequest request(kSimpleGET_Transaction); | 1499 MockHttpRequest request(kSimpleGET_Transaction); |
1419 MockHttpRequest reader_request(kSimpleGET_Transaction); | 1500 MockHttpRequest reader_request(kSimpleGET_Transaction); |
1420 reader_request.load_flags = LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION; | 1501 reader_request.load_flags = LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION; |
(...skipping 25 matching lines...) Expand all Loading... | |
1446 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 1527 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
1447 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 1528 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
1448 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 1529 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
1449 | 1530 |
1450 Context* c = context_list[0]; | 1531 Context* c = context_list[0]; |
1451 ASSERT_THAT(c->result, IsError(ERR_IO_PENDING)); | 1532 ASSERT_THAT(c->result, IsError(ERR_IO_PENDING)); |
1452 c->result = c->callback.WaitForResult(); | 1533 c->result = c->callback.WaitForResult(); |
1453 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction); | 1534 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction); |
1454 | 1535 |
1455 // Now we have 2 active readers and two queued transactions. | 1536 // Now we have 2 active readers and two queued transactions. |
1456 | |
1457 EXPECT_EQ(LOAD_STATE_IDLE, context_list[2]->trans->GetLoadState()); | 1537 EXPECT_EQ(LOAD_STATE_IDLE, context_list[2]->trans->GetLoadState()); |
1458 EXPECT_EQ(LOAD_STATE_WAITING_FOR_CACHE, | 1538 EXPECT_EQ(LOAD_STATE_WAITING_FOR_CACHE, |
1459 context_list[3]->trans->GetLoadState()); | 1539 context_list[3]->trans->GetLoadState()); |
1460 | 1540 |
1461 c = context_list[1]; | 1541 c = context_list[1]; |
1462 ASSERT_THAT(c->result, IsError(ERR_IO_PENDING)); | 1542 ASSERT_THAT(c->result, IsError(ERR_IO_PENDING)); |
1463 c->result = c->callback.WaitForResult(); | 1543 c->result = c->callback.WaitForResult(); |
1464 if (c->result == OK) | 1544 if (c->result == OK) |
1465 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction); | 1545 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction); |
1466 | 1546 |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1561 ASSERT_THAT(c->result, IsOk()); | 1641 ASSERT_THAT(c->result, IsOk()); |
1562 | 1642 |
1563 c->result = | 1643 c->result = |
1564 c->trans->Start(&request, c->callback.callback(), NetLogWithSource()); | 1644 c->trans->Start(&request, c->callback.callback(), NetLogWithSource()); |
1565 } | 1645 } |
1566 | 1646 |
1567 // Allow all requests to move from the Create queue to the active entry. | 1647 // Allow all requests to move from the Create queue to the active entry. |
1568 base::RunLoop().RunUntilIdle(); | 1648 base::RunLoop().RunUntilIdle(); |
1569 | 1649 |
1570 // The first request should be a writer at this point, and the subsequent | 1650 // The first request should be a writer at this point, and the subsequent |
1571 // requests should be pending. | 1651 // requests should have completed validation. Since the validation does not |
1652 // result in a match, a new entry would be created. | |
1572 | 1653 |
1573 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 1654 EXPECT_EQ(3, cache.network_layer()->transaction_count()); |
1574 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 1655 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
1575 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 1656 EXPECT_EQ(2, cache.disk_cache()->create_count()); |
1576 | 1657 |
1577 // Now, make sure that the second request asks for the entry not to be stored. | 1658 // Now, make sure that the second request asks for the entry not to be stored. |
1578 request_handler.set_no_store(true); | 1659 request_handler.set_no_store(true); |
1579 | 1660 |
1580 for (int i = 0; i < kNumTransactions; ++i) { | 1661 for (int i = 0; i < kNumTransactions; ++i) { |
1581 Context* c = context_list[i]; | 1662 Context* c = context_list[i]; |
1582 if (c->result == ERR_IO_PENDING) | 1663 if (c->result == ERR_IO_PENDING) |
1583 c->result = c->callback.WaitForResult(); | 1664 c->result = c->callback.WaitForResult(); |
1584 ReadAndVerifyTransaction(c->trans.get(), kFastNoStoreGET_Transaction); | 1665 ReadAndVerifyTransaction(c->trans.get(), kFastNoStoreGET_Transaction); |
1585 delete c; | 1666 delete c; |
(...skipping 948 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2534 } | 2615 } |
2535 | 2616 |
2536 // Helper that does 4 requests using HttpCache: | 2617 // Helper that does 4 requests using HttpCache: |
2537 // | 2618 // |
2538 // (1) loads |kUrl| -- expects |net_response_1| to be returned. | 2619 // (1) loads |kUrl| -- expects |net_response_1| to be returned. |
2539 // (2) loads |kUrl| from cache only -- expects |net_response_1| to be returned. | 2620 // (2) loads |kUrl| from cache only -- expects |net_response_1| to be returned. |
2540 // (3) loads |kUrl| using |extra_request_headers| -- expects |net_response_2| to | 2621 // (3) loads |kUrl| using |extra_request_headers| -- expects |net_response_2| to |
2541 // be returned. | 2622 // be returned. |
2542 // (4) loads |kUrl| from cache only -- expects |cached_response_2| to be | 2623 // (4) loads |kUrl| from cache only -- expects |cached_response_2| to be |
2543 // returned. | 2624 // returned. |
2625 // The entry will be created once and will be opened for the 3 subsequent | |
2626 // requests. | |
2544 static void ConditionalizedRequestUpdatesCacheHelper( | 2627 static void ConditionalizedRequestUpdatesCacheHelper( |
2545 const Response& net_response_1, | 2628 const Response& net_response_1, |
2546 const Response& net_response_2, | 2629 const Response& net_response_2, |
2547 const Response& cached_response_2, | 2630 const Response& cached_response_2, |
2548 const char* extra_request_headers) { | 2631 const char* extra_request_headers) { |
2549 MockHttpCache cache; | 2632 MockHttpCache cache; |
2550 | 2633 |
2551 // The URL we will be requesting. | 2634 // The URL we will be requesting. |
2552 const char kUrl[] = "http://foobar.com/main.css"; | 2635 const char kUrl[] = "http://foobar.com/main.css"; |
2553 | 2636 |
(...skipping 4285 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
6839 | 6922 |
6840 RunTransactionTestWithResponseInfo(cache.http_cache(), kSimpleGET_Transaction, | 6923 RunTransactionTestWithResponseInfo(cache.http_cache(), kSimpleGET_Transaction, |
6841 &response); | 6924 &response); |
6842 EXPECT_TRUE(response.metadata.get() == NULL); | 6925 EXPECT_TRUE(response.metadata.get() == NULL); |
6843 | 6926 |
6844 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 6927 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
6845 EXPECT_EQ(2, cache.disk_cache()->open_count()); | 6928 EXPECT_EQ(2, cache.disk_cache()->open_count()); |
6846 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 6929 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
6847 } | 6930 } |
6848 | 6931 |
6849 // Tests that if a metadata writer transaction hits cache lock timeout, it will | |
shivanisha
2017/03/07 12:51:32
This is a test from an unrelated CL https://codere
| |
6850 // error out. | |
6851 TEST(HttpCache, WriteMetadata_CacheLockTimeout) { | |
6852 MockHttpCache cache; | |
6853 | |
6854 // Write to the cache | |
6855 HttpResponseInfo response; | |
6856 RunTransactionTestWithResponseInfo(cache.http_cache(), kSimpleGET_Transaction, | |
6857 &response); | |
6858 EXPECT_FALSE(response.metadata.get()); | |
6859 | |
6860 MockHttpRequest request(kSimpleGET_Transaction); | |
6861 Context c1; | |
6862 ASSERT_THAT(cache.CreateTransaction(&c1.trans), IsOk()); | |
6863 ASSERT_EQ(ERR_IO_PENDING, c1.trans->Start(&request, c1.callback.callback(), | |
6864 NetLogWithSource())); | |
6865 | |
6866 cache.SimulateCacheLockTimeout(); | |
6867 | |
6868 // Write meta data to the same entry. | |
6869 scoped_refptr<IOBufferWithSize> buf(new IOBufferWithSize(50)); | |
6870 memset(buf->data(), 0, buf->size()); | |
6871 base::strlcpy(buf->data(), "Hi there", buf->size()); | |
6872 cache.http_cache()->WriteMetadata(GURL(kSimpleGET_Transaction.url), | |
6873 DEFAULT_PRIORITY, response.response_time, | |
6874 buf.get(), buf->size()); | |
6875 | |
6876 // Release the buffer before the operation takes place. | |
6877 buf = NULL; | |
6878 | |
6879 // Makes sure we finish pending operations. | |
6880 base::RunLoop().RunUntilIdle(); | |
6881 | |
6882 RunTransactionTestWithResponseInfo(cache.http_cache(), kSimpleGET_Transaction, | |
6883 &response); | |
6884 | |
6885 // The writer transaction should fail due to cache lock timeout. | |
6886 ASSERT_FALSE(response.metadata.get()); | |
6887 } | |
6888 | |
6889 // Tests that we ignore VARY checks when writing metadata since the request | 6932 // Tests that we ignore VARY checks when writing metadata since the request |
6890 // headers for the WriteMetadata transaction are made up. | 6933 // headers for the WriteMetadata transaction are made up. |
6891 TEST(HttpCache, WriteMetadata_IgnoreVary) { | 6934 TEST(HttpCache, WriteMetadata_IgnoreVary) { |
6892 MockHttpCache cache; | 6935 MockHttpCache cache; |
6893 | 6936 |
6894 // Write to the cache | 6937 // Write to the cache |
6895 HttpResponseInfo response; | 6938 HttpResponseInfo response; |
6896 ScopedMockTransaction transaction(kSimpleGET_Transaction); | 6939 ScopedMockTransaction transaction(kSimpleGET_Transaction); |
6897 transaction.request_headers = "accept-encoding: gzip\r\n"; | 6940 transaction.request_headers = "accept-encoding: gzip\r\n"; |
6898 transaction.response_headers = | 6941 transaction.response_headers = |
(...skipping 1504 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
8403 ASSERT_TRUE(attrs->GetDictionary( | 8446 ASSERT_TRUE(attrs->GetDictionary( |
8404 base::trace_event::MemoryAllocatorDump::kNameSize, &size_attrs)); | 8447 base::trace_event::MemoryAllocatorDump::kNameSize, &size_attrs)); |
8405 std::string size; | 8448 std::string size; |
8406 ASSERT_TRUE(size_attrs->GetString("value", &size)); | 8449 ASSERT_TRUE(size_attrs->GetString("value", &size)); |
8407 int actual_size = 0; | 8450 int actual_size = 0; |
8408 ASSERT_TRUE(base::HexStringToInt(size, &actual_size)); | 8451 ASSERT_TRUE(base::HexStringToInt(size, &actual_size)); |
8409 ASSERT_LT(0, actual_size); | 8452 ASSERT_LT(0, actual_size); |
8410 } | 8453 } |
8411 | 8454 |
8412 } // namespace net | 8455 } // namespace net |
OLD | NEW |