| 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 720 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 731 NetLogEventType::HTTP_CACHE_CREATE_ENTRY)); | 731 NetLogEventType::HTTP_CACHE_CREATE_ENTRY)); |
| 732 | 732 |
| 733 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 733 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 734 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 734 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 735 EXPECT_EQ(0, cache.disk_cache()->create_count()); | 735 EXPECT_EQ(0, cache.disk_cache()->create_count()); |
| 736 TestLoadTimingNetworkRequest(load_timing_info); | 736 TestLoadTimingNetworkRequest(load_timing_info); |
| 737 } | 737 } |
| 738 | 738 |
| 739 TEST(HttpCache, SimpleGETNoDiskCache2) { | 739 TEST(HttpCache, SimpleGETNoDiskCache2) { |
| 740 // This will initialize a cache object with NULL backend. | 740 // This will initialize a cache object with NULL backend. |
| 741 std::unique_ptr<MockBlockingBackendFactory> factory( | 741 auto factory = base::MakeUnique<MockBlockingBackendFactory>(); |
| 742 new MockBlockingBackendFactory()); | |
| 743 factory->set_fail(true); | 742 factory->set_fail(true); |
| 744 factory->FinishCreation(); // We'll complete synchronously. | 743 factory->FinishCreation(); // We'll complete synchronously. |
| 745 MockHttpCache cache(std::move(factory)); | 744 MockHttpCache cache(std::move(factory)); |
| 746 | 745 |
| 747 // Read from the network, and don't use the cache. | 746 // Read from the network, and don't use the cache. |
| 748 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); | 747 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); |
| 749 | 748 |
| 750 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 749 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 751 EXPECT_FALSE(cache.http_cache()->GetCurrentBackend()); | 750 EXPECT_FALSE(cache.http_cache()->GetCurrentBackend()); |
| 752 } | 751 } |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 793 EXPECT_EQ(2, cache.disk_cache()->create_count()); | 792 EXPECT_EQ(2, cache.disk_cache()->create_count()); |
| 794 } | 793 } |
| 795 | 794 |
| 796 // Tests that disk failures after the transaction has started don't cause the | 795 // Tests that disk failures after the transaction has started don't cause the |
| 797 // request to fail. | 796 // request to fail. |
| 798 TEST(HttpCache, SimpleGETWithDiskFailures2) { | 797 TEST(HttpCache, SimpleGETWithDiskFailures2) { |
| 799 MockHttpCache cache; | 798 MockHttpCache cache; |
| 800 | 799 |
| 801 MockHttpRequest request(kSimpleGET_Transaction); | 800 MockHttpRequest request(kSimpleGET_Transaction); |
| 802 | 801 |
| 803 std::unique_ptr<Context> c(new Context()); | 802 auto c = base::MakeUnique<Context>(); |
| 804 int rv = cache.CreateTransaction(&c->trans); | 803 int rv = cache.CreateTransaction(&c->trans); |
| 805 ASSERT_THAT(rv, IsOk()); | 804 ASSERT_THAT(rv, IsOk()); |
| 806 | 805 |
| 807 rv = c->trans->Start(&request, c->callback.callback(), NetLogWithSource()); | 806 rv = c->trans->Start(&request, c->callback.callback(), NetLogWithSource()); |
| 808 EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); | 807 EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 809 rv = c->callback.WaitForResult(); | 808 rv = c->callback.WaitForResult(); |
| 810 | 809 |
| 811 // Start failing request now. | 810 // Start failing request now. |
| 812 cache.disk_cache()->set_soft_failures(true); | 811 cache.disk_cache()->set_soft_failures(true); |
| 813 | 812 |
| (...skipping 24 matching lines...) Expand all Loading... |
| 838 // Read from the network, and write to the cache. | 837 // Read from the network, and write to the cache. |
| 839 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); | 838 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); |
| 840 | 839 |
| 841 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 840 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 842 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 841 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 843 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 842 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 844 | 843 |
| 845 cache.disk_cache()->set_soft_failures(true); | 844 cache.disk_cache()->set_soft_failures(true); |
| 846 | 845 |
| 847 // Now fail to read from the cache. | 846 // Now fail to read from the cache. |
| 848 std::unique_ptr<Context> c(new Context()); | 847 auto c = base::MakeUnique<Context>(); |
| 849 int rv = cache.CreateTransaction(&c->trans); | 848 int rv = cache.CreateTransaction(&c->trans); |
| 850 ASSERT_THAT(rv, IsOk()); | 849 ASSERT_THAT(rv, IsOk()); |
| 851 | 850 |
| 852 MockHttpRequest request(kSimpleGET_Transaction); | 851 MockHttpRequest request(kSimpleGET_Transaction); |
| 853 rv = c->trans->Start(&request, c->callback.callback(), NetLogWithSource()); | 852 rv = c->trans->Start(&request, c->callback.callback(), NetLogWithSource()); |
| 854 EXPECT_THAT(c->callback.GetResult(rv), IsOk()); | 853 EXPECT_THAT(c->callback.GetResult(rv), IsOk()); |
| 855 | 854 |
| 856 // Now verify that the entry was removed from the cache. | 855 // Now verify that the entry was removed from the cache. |
| 857 cache.disk_cache()->set_soft_failures(false); | 856 cache.disk_cache()->set_soft_failures(false); |
| 858 | 857 |
| (...skipping 479 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1338 EXPECT_EQ(1, cache.disk_cache()->open_count()); | 1337 EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 1339 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 1338 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 1340 RemoveMockTransaction(&transaction); | 1339 RemoveMockTransaction(&transaction); |
| 1341 } | 1340 } |
| 1342 | 1341 |
| 1343 TEST(HttpCache, SimpleGET_ManyReaders) { | 1342 TEST(HttpCache, SimpleGET_ManyReaders) { |
| 1344 MockHttpCache cache; | 1343 MockHttpCache cache; |
| 1345 | 1344 |
| 1346 MockHttpRequest request(kSimpleGET_Transaction); | 1345 MockHttpRequest request(kSimpleGET_Transaction); |
| 1347 | 1346 |
| 1348 std::vector<Context*> context_list; | 1347 std::vector<std::unique_ptr<Context>> context_list; |
| 1349 const int kNumTransactions = 5; | 1348 const int kNumTransactions = 5; |
| 1350 | 1349 |
| 1351 for (int i = 0; i < kNumTransactions; ++i) { | 1350 for (int i = 0; i < kNumTransactions; ++i) { |
| 1352 context_list.push_back(new Context()); | 1351 context_list.push_back(base::MakeUnique<Context>()); |
| 1353 Context* c = context_list[i]; | 1352 Context* c = context_list[i].get(); |
| 1354 | 1353 |
| 1355 c->result = cache.CreateTransaction(&c->trans); | 1354 c->result = cache.CreateTransaction(&c->trans); |
| 1356 ASSERT_THAT(c->result, IsOk()); | 1355 ASSERT_THAT(c->result, IsOk()); |
| 1357 EXPECT_EQ(LOAD_STATE_IDLE, c->trans->GetLoadState()); | 1356 EXPECT_EQ(LOAD_STATE_IDLE, c->trans->GetLoadState()); |
| 1358 | 1357 |
| 1359 c->result = | 1358 c->result = |
| 1360 c->trans->Start(&request, c->callback.callback(), NetLogWithSource()); | 1359 c->trans->Start(&request, c->callback.callback(), NetLogWithSource()); |
| 1361 } | 1360 } |
| 1362 | 1361 |
| 1363 // All requests are waiting for the active entry. | 1362 // All requests are waiting for the active entry. |
| 1364 for (int i = 0; i < kNumTransactions; ++i) { | 1363 for (int i = 0; i < kNumTransactions; ++i) { |
| 1365 Context* c = context_list[i]; | 1364 Context* c = context_list[i].get(); |
| 1366 EXPECT_EQ(LOAD_STATE_WAITING_FOR_CACHE, c->trans->GetLoadState()); | 1365 EXPECT_EQ(LOAD_STATE_WAITING_FOR_CACHE, c->trans->GetLoadState()); |
| 1367 } | 1366 } |
| 1368 | 1367 |
| 1369 // Allow all requests to move from the Create queue to the active entry. | 1368 // Allow all requests to move from the Create queue to the active entry. |
| 1370 base::RunLoop().RunUntilIdle(); | 1369 base::RunLoop().RunUntilIdle(); |
| 1371 | 1370 |
| 1372 // The first request should be a writer at this point, and the subsequent | 1371 // The first request should be a writer at this point, and the subsequent |
| 1373 // requests should be pending. | 1372 // requests should be pending. |
| 1374 | 1373 |
| 1375 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 1374 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 1376 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 1375 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 1377 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 1376 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 1378 | 1377 |
| 1379 // All requests depend on the writer, and the writer is between Start and | 1378 // All requests depend on the writer, and the writer is between Start and |
| 1380 // Read, i.e. idle. | 1379 // Read, i.e. idle. |
| 1381 for (int i = 0; i < kNumTransactions; ++i) { | 1380 for (int i = 0; i < kNumTransactions; ++i) { |
| 1382 Context* c = context_list[i]; | 1381 Context* c = context_list[i].get(); |
| 1383 EXPECT_EQ(LOAD_STATE_IDLE, c->trans->GetLoadState()); | 1382 EXPECT_EQ(LOAD_STATE_IDLE, c->trans->GetLoadState()); |
| 1384 } | 1383 } |
| 1385 | 1384 |
| 1386 for (int i = 0; i < kNumTransactions; ++i) { | 1385 for (int i = 0; i < kNumTransactions; ++i) { |
| 1387 Context* c = context_list[i]; | 1386 Context* c = context_list[i].get(); |
| 1388 if (c->result == ERR_IO_PENDING) | 1387 if (c->result == ERR_IO_PENDING) |
| 1389 c->result = c->callback.WaitForResult(); | 1388 c->result = c->callback.WaitForResult(); |
| 1390 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction); | 1389 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction); |
| 1391 } | 1390 } |
| 1392 | 1391 |
| 1393 // We should not have had to re-open the disk entry | 1392 // We should not have had to re-open the disk entry |
| 1394 | 1393 |
| 1395 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 1394 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 1396 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 1395 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 1397 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 1396 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 1398 | |
| 1399 for (int i = 0; i < kNumTransactions; ++i) { | |
| 1400 Context* c = context_list[i]; | |
| 1401 delete c; | |
| 1402 } | |
| 1403 } | 1397 } |
| 1404 | 1398 |
| 1405 // This is a test for http://code.google.com/p/chromium/issues/detail?id=4769. | 1399 // This is a test for http://code.google.com/p/chromium/issues/detail?id=4769. |
| 1406 // If cancelling a request is racing with another request for the same resource | 1400 // If cancelling a request is racing with another request for the same resource |
| 1407 // finishing, we have to make sure that we remove both transactions from the | 1401 // finishing, we have to make sure that we remove both transactions from the |
| 1408 // entry. | 1402 // entry. |
| 1409 TEST(HttpCache, SimpleGET_RacingReaders) { | 1403 TEST(HttpCache, SimpleGET_RacingReaders) { |
| 1410 MockHttpCache cache; | 1404 MockHttpCache cache; |
| 1411 | 1405 |
| 1412 MockHttpRequest request(kSimpleGET_Transaction); | 1406 MockHttpRequest request(kSimpleGET_Transaction); |
| 1413 MockHttpRequest reader_request(kSimpleGET_Transaction); | 1407 MockHttpRequest reader_request(kSimpleGET_Transaction); |
| 1414 reader_request.load_flags = LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION; | 1408 reader_request.load_flags = LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION; |
| 1415 | 1409 |
| 1416 std::vector<Context*> context_list; | 1410 std::vector<std::unique_ptr<Context>> context_list; |
| 1417 const int kNumTransactions = 5; | 1411 const int kNumTransactions = 5; |
| 1418 | 1412 |
| 1419 for (int i = 0; i < kNumTransactions; ++i) { | 1413 for (int i = 0; i < kNumTransactions; ++i) { |
| 1420 context_list.push_back(new Context()); | 1414 context_list.push_back(base::MakeUnique<Context>()); |
| 1421 Context* c = context_list[i]; | 1415 Context* c = context_list[i].get(); |
| 1422 | 1416 |
| 1423 c->result = cache.CreateTransaction(&c->trans); | 1417 c->result = cache.CreateTransaction(&c->trans); |
| 1424 ASSERT_THAT(c->result, IsOk()); | 1418 ASSERT_THAT(c->result, IsOk()); |
| 1425 | 1419 |
| 1426 MockHttpRequest* this_request = &request; | 1420 MockHttpRequest* this_request = &request; |
| 1427 if (i == 1 || i == 2) | 1421 if (i == 1 || i == 2) |
| 1428 this_request = &reader_request; | 1422 this_request = &reader_request; |
| 1429 | 1423 |
| 1430 c->result = c->trans->Start(this_request, c->callback.callback(), | 1424 c->result = c->trans->Start(this_request, c->callback.callback(), |
| 1431 NetLogWithSource()); | 1425 NetLogWithSource()); |
| 1432 } | 1426 } |
| 1433 | 1427 |
| 1434 // Allow all requests to move from the Create queue to the active entry. | 1428 // Allow all requests to move from the Create queue to the active entry. |
| 1435 base::RunLoop().RunUntilIdle(); | 1429 base::RunLoop().RunUntilIdle(); |
| 1436 | 1430 |
| 1437 // The first request should be a writer at this point, and the subsequent | 1431 // The first request should be a writer at this point, and the subsequent |
| 1438 // requests should be pending. | 1432 // requests should be pending. |
| 1439 | 1433 |
| 1440 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 1434 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 1441 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 1435 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 1442 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 1436 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 1443 | 1437 |
| 1444 Context* c = context_list[0]; | 1438 Context* c = context_list[0].get(); |
| 1445 ASSERT_THAT(c->result, IsError(ERR_IO_PENDING)); | 1439 ASSERT_THAT(c->result, IsError(ERR_IO_PENDING)); |
| 1446 c->result = c->callback.WaitForResult(); | 1440 c->result = c->callback.WaitForResult(); |
| 1447 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction); | 1441 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction); |
| 1448 | 1442 |
| 1449 // Now we have 2 active readers and two queued transactions. | 1443 // Now we have 2 active readers and two queued transactions. |
| 1450 | 1444 |
| 1451 EXPECT_EQ(LOAD_STATE_IDLE, context_list[2]->trans->GetLoadState()); | 1445 EXPECT_EQ(LOAD_STATE_IDLE, context_list[2]->trans->GetLoadState()); |
| 1452 EXPECT_EQ(LOAD_STATE_WAITING_FOR_CACHE, | 1446 EXPECT_EQ(LOAD_STATE_WAITING_FOR_CACHE, |
| 1453 context_list[3]->trans->GetLoadState()); | 1447 context_list[3]->trans->GetLoadState()); |
| 1454 | 1448 |
| 1455 c = context_list[1]; | 1449 c = context_list[1].get(); |
| 1456 ASSERT_THAT(c->result, IsError(ERR_IO_PENDING)); | 1450 ASSERT_THAT(c->result, IsError(ERR_IO_PENDING)); |
| 1457 c->result = c->callback.WaitForResult(); | 1451 c->result = c->callback.WaitForResult(); |
| 1458 if (c->result == OK) | 1452 if (c->result == OK) |
| 1459 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction); | 1453 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction); |
| 1460 | 1454 |
| 1461 // At this point we have one reader, two pending transactions and a task on | 1455 // At this point we have one reader, two pending transactions and a task on |
| 1462 // the queue to move to the next transaction. Now we cancel the request that | 1456 // the queue to move to the next transaction. Now we cancel the request that |
| 1463 // is the current reader, and expect the queued task to be able to start the | 1457 // is the current reader, and expect the queued task to be able to start the |
| 1464 // next request. | 1458 // next request. |
| 1465 | 1459 |
| 1466 c = context_list[2]; | 1460 c = context_list[2].get(); |
| 1467 c->trans.reset(); | 1461 c->trans.reset(); |
| 1468 | 1462 |
| 1469 for (int i = 3; i < kNumTransactions; ++i) { | 1463 for (int i = 3; i < kNumTransactions; ++i) { |
| 1470 Context* c = context_list[i]; | 1464 Context* c = context_list[i].get(); |
| 1471 if (c->result == ERR_IO_PENDING) | 1465 if (c->result == ERR_IO_PENDING) |
| 1472 c->result = c->callback.WaitForResult(); | 1466 c->result = c->callback.WaitForResult(); |
| 1473 if (c->result == OK) | 1467 if (c->result == OK) |
| 1474 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction); | 1468 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction); |
| 1475 } | 1469 } |
| 1476 | 1470 |
| 1477 // We should not have had to re-open the disk entry. | 1471 // We should not have had to re-open the disk entry. |
| 1478 | 1472 |
| 1479 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 1473 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 1480 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 1474 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 1481 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 1475 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 1482 | |
| 1483 for (int i = 0; i < kNumTransactions; ++i) { | |
| 1484 Context* c = context_list[i]; | |
| 1485 delete c; | |
| 1486 } | |
| 1487 } | 1476 } |
| 1488 | 1477 |
| 1489 // Tests that we can doom an entry with pending transactions and delete one of | 1478 // Tests that we can doom an entry with pending transactions and delete one of |
| 1490 // the pending transactions before the first one completes. | 1479 // the pending transactions before the first one completes. |
| 1491 // See http://code.google.com/p/chromium/issues/detail?id=25588 | 1480 // See http://code.google.com/p/chromium/issues/detail?id=25588 |
| 1492 TEST(HttpCache, SimpleGET_DoomWithPending) { | 1481 TEST(HttpCache, SimpleGET_DoomWithPending) { |
| 1493 // We need simultaneous doomed / not_doomed entries so let's use a real cache. | 1482 // We need simultaneous doomed / not_doomed entries so let's use a real cache. |
| 1494 MockHttpCache cache(HttpCache::DefaultBackend::InMemory(1024 * 1024)); | 1483 MockHttpCache cache(HttpCache::DefaultBackend::InMemory(1024 * 1024)); |
| 1495 | 1484 |
| 1496 MockHttpRequest request(kSimpleGET_Transaction); | 1485 MockHttpRequest request(kSimpleGET_Transaction); |
| 1497 MockHttpRequest writer_request(kSimpleGET_Transaction); | 1486 MockHttpRequest writer_request(kSimpleGET_Transaction); |
| 1498 writer_request.load_flags = LOAD_BYPASS_CACHE; | 1487 writer_request.load_flags = LOAD_BYPASS_CACHE; |
| 1499 | 1488 |
| 1500 std::vector<std::unique_ptr<Context>> context_list; | 1489 std::vector<std::unique_ptr<Context>> context_list; |
| 1501 const int kNumTransactions = 4; | 1490 const int kNumTransactions = 4; |
| 1502 | 1491 |
| 1503 for (int i = 0; i < kNumTransactions; ++i) { | 1492 for (int i = 0; i < kNumTransactions; ++i) { |
| 1504 context_list.push_back(base::WrapUnique(new Context())); | 1493 context_list.push_back(base::MakeUnique<Context>()); |
| 1505 Context* c = context_list[i].get(); | 1494 Context* c = context_list[i].get(); |
| 1506 | 1495 |
| 1507 c->result = cache.CreateTransaction(&c->trans); | 1496 c->result = cache.CreateTransaction(&c->trans); |
| 1508 ASSERT_THAT(c->result, IsOk()); | 1497 ASSERT_THAT(c->result, IsOk()); |
| 1509 | 1498 |
| 1510 MockHttpRequest* this_request = &request; | 1499 MockHttpRequest* this_request = &request; |
| 1511 if (i == 3) | 1500 if (i == 3) |
| 1512 this_request = &writer_request; | 1501 this_request = &writer_request; |
| 1513 | 1502 |
| 1514 c->result = c->trans->Start(this_request, c->callback.callback(), | 1503 c->result = c->trans->Start(this_request, c->callback.callback(), |
| (...skipping 22 matching lines...) Expand all Loading... |
| 1537 // We may attempt to delete an entry synchronously with the act of adding a new | 1526 // We may attempt to delete an entry synchronously with the act of adding a new |
| 1538 // transaction to said entry. | 1527 // transaction to said entry. |
| 1539 TEST(HttpCache, FastNoStoreGET_DoneWithPending) { | 1528 TEST(HttpCache, FastNoStoreGET_DoneWithPending) { |
| 1540 MockHttpCache cache; | 1529 MockHttpCache cache; |
| 1541 | 1530 |
| 1542 // The headers will be served right from the call to Start() the request. | 1531 // The headers will be served right from the call to Start() the request. |
| 1543 MockHttpRequest request(kFastNoStoreGET_Transaction); | 1532 MockHttpRequest request(kFastNoStoreGET_Transaction); |
| 1544 FastTransactionServer request_handler; | 1533 FastTransactionServer request_handler; |
| 1545 AddMockTransaction(&kFastNoStoreGET_Transaction); | 1534 AddMockTransaction(&kFastNoStoreGET_Transaction); |
| 1546 | 1535 |
| 1547 std::vector<Context*> context_list; | 1536 std::vector<std::unique_ptr<Context>> context_list; |
| 1548 const int kNumTransactions = 3; | 1537 const int kNumTransactions = 3; |
| 1549 | 1538 |
| 1550 for (int i = 0; i < kNumTransactions; ++i) { | 1539 for (int i = 0; i < kNumTransactions; ++i) { |
| 1551 context_list.push_back(new Context()); | 1540 context_list.push_back(base::MakeUnique<Context>()); |
| 1552 Context* c = context_list[i]; | 1541 Context* c = context_list[i].get(); |
| 1553 | 1542 |
| 1554 c->result = cache.CreateTransaction(&c->trans); | 1543 c->result = cache.CreateTransaction(&c->trans); |
| 1555 ASSERT_THAT(c->result, IsOk()); | 1544 ASSERT_THAT(c->result, IsOk()); |
| 1556 | 1545 |
| 1557 c->result = | 1546 c->result = |
| 1558 c->trans->Start(&request, c->callback.callback(), NetLogWithSource()); | 1547 c->trans->Start(&request, c->callback.callback(), NetLogWithSource()); |
| 1559 } | 1548 } |
| 1560 | 1549 |
| 1561 // Allow all requests to move from the Create queue to the active entry. | 1550 // Allow all requests to move from the Create queue to the active entry. |
| 1562 base::RunLoop().RunUntilIdle(); | 1551 base::RunLoop().RunUntilIdle(); |
| 1563 | 1552 |
| 1564 // The first request should be a writer at this point, and the subsequent | 1553 // The first request should be a writer at this point, and the subsequent |
| 1565 // requests should be pending. | 1554 // requests should be pending. |
| 1566 | 1555 |
| 1567 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 1556 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 1568 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 1557 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 1569 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 1558 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 1570 | 1559 |
| 1571 // Now, make sure that the second request asks for the entry not to be stored. | 1560 // Now, make sure that the second request asks for the entry not to be stored. |
| 1572 request_handler.set_no_store(true); | 1561 request_handler.set_no_store(true); |
| 1573 | 1562 |
| 1574 for (int i = 0; i < kNumTransactions; ++i) { | 1563 for (int i = 0; i < kNumTransactions; ++i) { |
| 1575 Context* c = context_list[i]; | 1564 Context* c = context_list[i].get(); |
| 1576 if (c->result == ERR_IO_PENDING) | 1565 if (c->result == ERR_IO_PENDING) |
| 1577 c->result = c->callback.WaitForResult(); | 1566 c->result = c->callback.WaitForResult(); |
| 1578 ReadAndVerifyTransaction(c->trans.get(), kFastNoStoreGET_Transaction); | 1567 ReadAndVerifyTransaction(c->trans.get(), kFastNoStoreGET_Transaction); |
| 1579 delete c; | 1568 context_list[i].reset(); |
| 1580 } | 1569 } |
| 1581 | 1570 |
| 1582 EXPECT_EQ(3, cache.network_layer()->transaction_count()); | 1571 EXPECT_EQ(3, cache.network_layer()->transaction_count()); |
| 1583 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 1572 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 1584 EXPECT_EQ(2, cache.disk_cache()->create_count()); | 1573 EXPECT_EQ(2, cache.disk_cache()->create_count()); |
| 1585 | 1574 |
| 1586 RemoveMockTransaction(&kFastNoStoreGET_Transaction); | 1575 RemoveMockTransaction(&kFastNoStoreGET_Transaction); |
| 1587 } | 1576 } |
| 1588 | 1577 |
| 1589 TEST(HttpCache, SimpleGET_ManyWriters_CancelFirst) { | 1578 TEST(HttpCache, SimpleGET_ManyWriters_CancelFirst) { |
| 1590 MockHttpCache cache; | 1579 MockHttpCache cache; |
| 1591 | 1580 |
| 1592 MockHttpRequest request(kSimpleGET_Transaction); | 1581 MockHttpRequest request(kSimpleGET_Transaction); |
| 1593 | 1582 |
| 1594 std::vector<Context*> context_list; | 1583 std::vector<std::unique_ptr<Context>> context_list; |
| 1595 const int kNumTransactions = 2; | 1584 const int kNumTransactions = 2; |
| 1596 | 1585 |
| 1597 for (int i = 0; i < kNumTransactions; ++i) { | 1586 for (int i = 0; i < kNumTransactions; ++i) { |
| 1598 context_list.push_back(new Context()); | 1587 context_list.push_back(base::MakeUnique<Context>()); |
| 1599 Context* c = context_list[i]; | 1588 Context* c = context_list[i].get(); |
| 1600 | 1589 |
| 1601 c->result = cache.CreateTransaction(&c->trans); | 1590 c->result = cache.CreateTransaction(&c->trans); |
| 1602 ASSERT_THAT(c->result, IsOk()); | 1591 ASSERT_THAT(c->result, IsOk()); |
| 1603 | 1592 |
| 1604 c->result = | 1593 c->result = |
| 1605 c->trans->Start(&request, c->callback.callback(), NetLogWithSource()); | 1594 c->trans->Start(&request, c->callback.callback(), NetLogWithSource()); |
| 1606 } | 1595 } |
| 1607 | 1596 |
| 1608 // Allow all requests to move from the Create queue to the active entry. | 1597 // Allow all requests to move from the Create queue to the active entry. |
| 1609 base::RunLoop().RunUntilIdle(); | 1598 base::RunLoop().RunUntilIdle(); |
| 1610 | 1599 |
| 1611 // The first request should be a writer at this point, and the subsequent | 1600 // The first request should be a writer at this point, and the subsequent |
| 1612 // requests should be pending. | 1601 // requests should be pending. |
| 1613 | 1602 |
| 1614 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 1603 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 1615 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 1604 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 1616 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 1605 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 1617 | 1606 |
| 1618 for (int i = 0; i < kNumTransactions; ++i) { | 1607 for (int i = 0; i < kNumTransactions; ++i) { |
| 1619 Context* c = context_list[i]; | 1608 Context* c = context_list[i].get(); |
| 1620 if (c->result == ERR_IO_PENDING) | 1609 if (c->result == ERR_IO_PENDING) |
| 1621 c->result = c->callback.WaitForResult(); | 1610 c->result = c->callback.WaitForResult(); |
| 1622 // Destroy only the first transaction. | 1611 // Destroy only the first transaction. |
| 1623 if (i == 0) { | 1612 if (i == 0) { |
| 1624 delete c; | 1613 context_list[i].reset(); |
| 1625 context_list[i] = NULL; | |
| 1626 } | 1614 } |
| 1627 } | 1615 } |
| 1628 | 1616 |
| 1629 // Complete the rest of the transactions. | 1617 // Complete the rest of the transactions. |
| 1630 for (int i = 1; i < kNumTransactions; ++i) { | 1618 for (int i = 1; i < kNumTransactions; ++i) { |
| 1631 Context* c = context_list[i]; | 1619 Context* c = context_list[i].get(); |
| 1632 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction); | 1620 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction); |
| 1633 } | 1621 } |
| 1634 | 1622 |
| 1635 // We should have had to re-open the disk entry. | 1623 // We should have had to re-open the disk entry. |
| 1636 | 1624 |
| 1637 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | 1625 EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 1638 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 1626 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 1639 EXPECT_EQ(2, cache.disk_cache()->create_count()); | 1627 EXPECT_EQ(2, cache.disk_cache()->create_count()); |
| 1640 | |
| 1641 for (int i = 1; i < kNumTransactions; ++i) { | |
| 1642 Context* c = context_list[i]; | |
| 1643 delete c; | |
| 1644 } | |
| 1645 } | 1628 } |
| 1646 | 1629 |
| 1647 // Tests that we can cancel requests that are queued waiting to open the disk | 1630 // Tests that we can cancel requests that are queued waiting to open the disk |
| 1648 // cache entry. | 1631 // cache entry. |
| 1649 TEST(HttpCache, SimpleGET_ManyWriters_CancelCreate) { | 1632 TEST(HttpCache, SimpleGET_ManyWriters_CancelCreate) { |
| 1650 MockHttpCache cache; | 1633 MockHttpCache cache; |
| 1651 | 1634 |
| 1652 MockHttpRequest request(kSimpleGET_Transaction); | 1635 MockHttpRequest request(kSimpleGET_Transaction); |
| 1653 | 1636 |
| 1654 std::vector<Context*> context_list; | 1637 std::vector<std::unique_ptr<Context>> context_list; |
| 1655 const int kNumTransactions = 5; | 1638 const int kNumTransactions = 5; |
| 1656 | 1639 |
| 1657 for (int i = 0; i < kNumTransactions; i++) { | 1640 for (int i = 0; i < kNumTransactions; i++) { |
| 1658 context_list.push_back(new Context()); | 1641 context_list.push_back(base::MakeUnique<Context>()); |
| 1659 Context* c = context_list[i]; | 1642 Context* c = context_list[i].get(); |
| 1660 | 1643 |
| 1661 c->result = cache.CreateTransaction(&c->trans); | 1644 c->result = cache.CreateTransaction(&c->trans); |
| 1662 ASSERT_THAT(c->result, IsOk()); | 1645 ASSERT_THAT(c->result, IsOk()); |
| 1663 | 1646 |
| 1664 c->result = | 1647 c->result = |
| 1665 c->trans->Start(&request, c->callback.callback(), NetLogWithSource()); | 1648 c->trans->Start(&request, c->callback.callback(), NetLogWithSource()); |
| 1666 } | 1649 } |
| 1667 | 1650 |
| 1668 // The first request should be creating the disk cache entry and the others | 1651 // The first request should be creating the disk cache entry and the others |
| 1669 // should be pending. | 1652 // should be pending. |
| 1670 | 1653 |
| 1671 EXPECT_EQ(0, cache.network_layer()->transaction_count()); | 1654 EXPECT_EQ(0, cache.network_layer()->transaction_count()); |
| 1672 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 1655 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 1673 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 1656 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 1674 | 1657 |
| 1675 // Cancel a request from the pending queue. | 1658 // Cancel a request from the pending queue. |
| 1676 delete context_list[3]; | 1659 context_list[3].reset(); |
| 1677 context_list[3] = NULL; | |
| 1678 | 1660 |
| 1679 // Cancel the request that is creating the entry. This will force the pending | 1661 // Cancel the request that is creating the entry. This will force the pending |
| 1680 // operations to restart. | 1662 // operations to restart. |
| 1681 delete context_list[0]; | 1663 context_list[0].reset(); |
| 1682 context_list[0] = NULL; | |
| 1683 | 1664 |
| 1684 // Complete the rest of the transactions. | 1665 // Complete the rest of the transactions. |
| 1685 for (int i = 1; i < kNumTransactions; i++) { | 1666 for (int i = 1; i < kNumTransactions; i++) { |
| 1686 Context* c = context_list[i]; | 1667 Context* c = context_list[i].get(); |
| 1687 if (c) { | 1668 if (c) { |
| 1688 c->result = c->callback.GetResult(c->result); | 1669 c->result = c->callback.GetResult(c->result); |
| 1689 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction); | 1670 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction); |
| 1690 } | 1671 } |
| 1691 } | 1672 } |
| 1692 | 1673 |
| 1693 // We should have had to re-create the disk entry. | 1674 // We should have had to re-create the disk entry. |
| 1694 | 1675 |
| 1695 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 1676 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 1696 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 1677 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 1697 EXPECT_EQ(2, cache.disk_cache()->create_count()); | 1678 EXPECT_EQ(2, cache.disk_cache()->create_count()); |
| 1698 | |
| 1699 for (int i = 1; i < kNumTransactions; ++i) { | |
| 1700 delete context_list[i]; | |
| 1701 } | |
| 1702 } | 1679 } |
| 1703 | 1680 |
| 1704 // Tests that we can cancel a single request to open a disk cache entry. | 1681 // Tests that we can cancel a single request to open a disk cache entry. |
| 1705 TEST(HttpCache, SimpleGET_CancelCreate) { | 1682 TEST(HttpCache, SimpleGET_CancelCreate) { |
| 1706 MockHttpCache cache; | 1683 MockHttpCache cache; |
| 1707 | 1684 |
| 1708 MockHttpRequest request(kSimpleGET_Transaction); | 1685 MockHttpRequest request(kSimpleGET_Transaction); |
| 1709 | 1686 |
| 1710 Context* c = new Context(); | 1687 auto c = base::MakeUnique<Context>(); |
| 1711 | 1688 |
| 1712 c->result = cache.CreateTransaction(&c->trans); | 1689 c->result = cache.CreateTransaction(&c->trans); |
| 1713 ASSERT_THAT(c->result, IsOk()); | 1690 ASSERT_THAT(c->result, IsOk()); |
| 1714 | 1691 |
| 1715 c->result = | 1692 c->result = |
| 1716 c->trans->Start(&request, c->callback.callback(), NetLogWithSource()); | 1693 c->trans->Start(&request, c->callback.callback(), NetLogWithSource()); |
| 1717 EXPECT_THAT(c->result, IsError(ERR_IO_PENDING)); | 1694 EXPECT_THAT(c->result, IsError(ERR_IO_PENDING)); |
| 1718 | 1695 |
| 1719 // Release the reference that the mock disk cache keeps for this entry, so | 1696 // Release the reference that the mock disk cache keeps for this entry, so |
| 1720 // that we test that the http cache handles the cancellation correctly. | 1697 // that we test that the http cache handles the cancellation correctly. |
| 1721 cache.disk_cache()->ReleaseAll(); | 1698 cache.disk_cache()->ReleaseAll(); |
| 1722 delete c; | 1699 c.reset(); |
| 1723 | 1700 |
| 1724 base::RunLoop().RunUntilIdle(); | 1701 base::RunLoop().RunUntilIdle(); |
| 1725 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 1702 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 1726 } | 1703 } |
| 1727 | 1704 |
| 1728 // Tests that we delete/create entries even if multiple requests are queued. | 1705 // Tests that we delete/create entries even if multiple requests are queued. |
| 1729 TEST(HttpCache, SimpleGET_ManyWriters_BypassCache) { | 1706 TEST(HttpCache, SimpleGET_ManyWriters_BypassCache) { |
| 1730 MockHttpCache cache; | 1707 MockHttpCache cache; |
| 1731 | 1708 |
| 1732 MockHttpRequest request(kSimpleGET_Transaction); | 1709 MockHttpRequest request(kSimpleGET_Transaction); |
| 1733 request.load_flags = LOAD_BYPASS_CACHE; | 1710 request.load_flags = LOAD_BYPASS_CACHE; |
| 1734 | 1711 |
| 1735 std::vector<Context*> context_list; | 1712 std::vector<std::unique_ptr<Context>> context_list; |
| 1736 const int kNumTransactions = 5; | 1713 const int kNumTransactions = 5; |
| 1737 | 1714 |
| 1738 for (int i = 0; i < kNumTransactions; i++) { | 1715 for (int i = 0; i < kNumTransactions; i++) { |
| 1739 context_list.push_back(new Context()); | 1716 context_list.push_back(base::MakeUnique<Context>()); |
| 1740 Context* c = context_list[i]; | 1717 Context* c = context_list[i].get(); |
| 1741 | 1718 |
| 1742 c->result = cache.CreateTransaction(&c->trans); | 1719 c->result = cache.CreateTransaction(&c->trans); |
| 1743 ASSERT_THAT(c->result, IsOk()); | 1720 ASSERT_THAT(c->result, IsOk()); |
| 1744 | 1721 |
| 1745 c->result = | 1722 c->result = |
| 1746 c->trans->Start(&request, c->callback.callback(), NetLogWithSource()); | 1723 c->trans->Start(&request, c->callback.callback(), NetLogWithSource()); |
| 1747 } | 1724 } |
| 1748 | 1725 |
| 1749 // The first request should be deleting the disk cache entry and the others | 1726 // The first request should be deleting the disk cache entry and the others |
| 1750 // should be pending. | 1727 // should be pending. |
| 1751 | 1728 |
| 1752 EXPECT_EQ(0, cache.network_layer()->transaction_count()); | 1729 EXPECT_EQ(0, cache.network_layer()->transaction_count()); |
| 1753 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 1730 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 1754 EXPECT_EQ(0, cache.disk_cache()->create_count()); | 1731 EXPECT_EQ(0, cache.disk_cache()->create_count()); |
| 1755 | 1732 |
| 1756 // Complete the transactions. | 1733 // Complete the transactions. |
| 1757 for (int i = 0; i < kNumTransactions; i++) { | 1734 for (int i = 0; i < kNumTransactions; i++) { |
| 1758 Context* c = context_list[i]; | 1735 Context* c = context_list[i].get(); |
| 1759 c->result = c->callback.GetResult(c->result); | 1736 c->result = c->callback.GetResult(c->result); |
| 1760 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction); | 1737 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction); |
| 1761 } | 1738 } |
| 1762 | 1739 |
| 1763 // We should have had to re-create the disk entry multiple times. | 1740 // We should have had to re-create the disk entry multiple times. |
| 1764 | 1741 |
| 1765 EXPECT_EQ(5, cache.network_layer()->transaction_count()); | 1742 EXPECT_EQ(5, cache.network_layer()->transaction_count()); |
| 1766 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 1743 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 1767 EXPECT_EQ(5, cache.disk_cache()->create_count()); | 1744 EXPECT_EQ(5, cache.disk_cache()->create_count()); |
| 1768 | |
| 1769 for (int i = 0; i < kNumTransactions; ++i) { | |
| 1770 delete context_list[i]; | |
| 1771 } | |
| 1772 } | 1745 } |
| 1773 | 1746 |
| 1774 // Tests that a (simulated) timeout allows transactions waiting on the cache | 1747 // Tests that a (simulated) timeout allows transactions waiting on the cache |
| 1775 // lock to continue. | 1748 // lock to continue. |
| 1776 TEST(HttpCache, SimpleGET_WriterTimeout) { | 1749 TEST(HttpCache, SimpleGET_WriterTimeout) { |
| 1777 MockHttpCache cache; | 1750 MockHttpCache cache; |
| 1778 cache.SimulateCacheLockTimeout(); | 1751 cache.SimulateCacheLockTimeout(); |
| 1779 | 1752 |
| 1780 MockHttpRequest request(kSimpleGET_Transaction); | 1753 MockHttpRequest request(kSimpleGET_Transaction); |
| 1781 Context c1, c2; | 1754 Context c1, c2; |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1849 trans.reset(); | 1822 trans.reset(); |
| 1850 | 1823 |
| 1851 // Make sure we pump any pending events, which should include a call to | 1824 // Make sure we pump any pending events, which should include a call to |
| 1852 // HttpCache::Transaction::OnCacheReadCompleted. | 1825 // HttpCache::Transaction::OnCacheReadCompleted. |
| 1853 base::RunLoop().RunUntilIdle(); | 1826 base::RunLoop().RunUntilIdle(); |
| 1854 } | 1827 } |
| 1855 | 1828 |
| 1856 // Tests that we can delete the HttpCache and deal with queued transactions | 1829 // Tests that we can delete the HttpCache and deal with queued transactions |
| 1857 // ("waiting for the backend" as opposed to Active or Doomed entries). | 1830 // ("waiting for the backend" as opposed to Active or Doomed entries). |
| 1858 TEST(HttpCache, SimpleGET_ManyWriters_DeleteCache) { | 1831 TEST(HttpCache, SimpleGET_ManyWriters_DeleteCache) { |
| 1859 std::unique_ptr<MockHttpCache> cache( | 1832 auto cache = base::MakeUnique<MockHttpCache>( |
| 1860 new MockHttpCache(base::WrapUnique(new MockBackendNoCbFactory()))); | 1833 base::MakeUnique<MockBackendNoCbFactory>()); |
| 1861 | 1834 |
| 1862 MockHttpRequest request(kSimpleGET_Transaction); | 1835 MockHttpRequest request(kSimpleGET_Transaction); |
| 1863 | 1836 |
| 1864 std::vector<Context*> context_list; | 1837 std::vector<std::unique_ptr<Context>> context_list; |
| 1865 const int kNumTransactions = 5; | 1838 const int kNumTransactions = 5; |
| 1866 | 1839 |
| 1867 for (int i = 0; i < kNumTransactions; i++) { | 1840 for (int i = 0; i < kNumTransactions; i++) { |
| 1868 context_list.push_back(new Context()); | 1841 context_list.push_back(base::MakeUnique<Context>()); |
| 1869 Context* c = context_list[i]; | 1842 Context* c = context_list[i].get(); |
| 1870 | 1843 |
| 1871 c->result = cache->CreateTransaction(&c->trans); | 1844 c->result = cache->CreateTransaction(&c->trans); |
| 1872 ASSERT_THAT(c->result, IsOk()); | 1845 ASSERT_THAT(c->result, IsOk()); |
| 1873 | 1846 |
| 1874 c->result = | 1847 c->result = |
| 1875 c->trans->Start(&request, c->callback.callback(), NetLogWithSource()); | 1848 c->trans->Start(&request, c->callback.callback(), NetLogWithSource()); |
| 1876 } | 1849 } |
| 1877 | 1850 |
| 1878 // The first request should be creating the disk cache entry and the others | 1851 // The first request should be creating the disk cache entry and the others |
| 1879 // should be pending. | 1852 // should be pending. |
| 1880 | 1853 |
| 1881 EXPECT_EQ(0, cache->network_layer()->transaction_count()); | 1854 EXPECT_EQ(0, cache->network_layer()->transaction_count()); |
| 1882 EXPECT_EQ(0, cache->disk_cache()->open_count()); | 1855 EXPECT_EQ(0, cache->disk_cache()->open_count()); |
| 1883 EXPECT_EQ(0, cache->disk_cache()->create_count()); | 1856 EXPECT_EQ(0, cache->disk_cache()->create_count()); |
| 1884 | 1857 |
| 1885 cache.reset(); | 1858 cache.reset(); |
| 1886 | |
| 1887 // There is not much to do with the transactions at this point... they are | |
| 1888 // waiting for a callback that will not fire. | |
| 1889 for (int i = 0; i < kNumTransactions; ++i) { | |
| 1890 delete context_list[i]; | |
| 1891 } | |
| 1892 } | 1859 } |
| 1893 | 1860 |
| 1894 // Tests that we queue requests when initializing the backend. | 1861 // Tests that we queue requests when initializing the backend. |
| 1895 TEST(HttpCache, SimpleGET_WaitForBackend) { | 1862 TEST(HttpCache, SimpleGET_WaitForBackend) { |
| 1896 MockBlockingBackendFactory* factory = new MockBlockingBackendFactory(); | 1863 MockBlockingBackendFactory* factory = new MockBlockingBackendFactory(); |
| 1897 MockHttpCache cache(base::WrapUnique(factory)); | 1864 MockHttpCache cache(base::WrapUnique(factory)); |
| 1898 | 1865 |
| 1899 MockHttpRequest request0(kSimpleGET_Transaction); | 1866 MockHttpRequest request0(kSimpleGET_Transaction); |
| 1900 MockHttpRequest request1(kTypicalGET_Transaction); | 1867 MockHttpRequest request1(kTypicalGET_Transaction); |
| 1901 MockHttpRequest request2(kETagGET_Transaction); | 1868 MockHttpRequest request2(kETagGET_Transaction); |
| 1902 | 1869 |
| 1903 std::vector<Context*> context_list; | 1870 std::vector<std::unique_ptr<Context>> context_list; |
| 1904 const int kNumTransactions = 3; | 1871 const int kNumTransactions = 3; |
| 1905 | 1872 |
| 1906 for (int i = 0; i < kNumTransactions; i++) { | 1873 for (int i = 0; i < kNumTransactions; i++) { |
| 1907 context_list.push_back(new Context()); | 1874 context_list.push_back(base::MakeUnique<Context>()); |
| 1908 Context* c = context_list[i]; | 1875 Context* c = context_list[i].get(); |
| 1909 | 1876 |
| 1910 c->result = cache.CreateTransaction(&c->trans); | 1877 c->result = cache.CreateTransaction(&c->trans); |
| 1911 ASSERT_THAT(c->result, IsOk()); | 1878 ASSERT_THAT(c->result, IsOk()); |
| 1912 } | 1879 } |
| 1913 | 1880 |
| 1914 context_list[0]->result = context_list[0]->trans->Start( | 1881 context_list[0]->result = context_list[0]->trans->Start( |
| 1915 &request0, context_list[0]->callback.callback(), NetLogWithSource()); | 1882 &request0, context_list[0]->callback.callback(), NetLogWithSource()); |
| 1916 context_list[1]->result = context_list[1]->trans->Start( | 1883 context_list[1]->result = context_list[1]->trans->Start( |
| 1917 &request1, context_list[1]->callback.callback(), NetLogWithSource()); | 1884 &request1, context_list[1]->callback.callback(), NetLogWithSource()); |
| 1918 context_list[2]->result = context_list[2]->trans->Start( | 1885 context_list[2]->result = context_list[2]->trans->Start( |
| 1919 &request2, context_list[2]->callback.callback(), NetLogWithSource()); | 1886 &request2, context_list[2]->callback.callback(), NetLogWithSource()); |
| 1920 | 1887 |
| 1921 // Just to make sure that everything is still pending. | 1888 // Just to make sure that everything is still pending. |
| 1922 base::RunLoop().RunUntilIdle(); | 1889 base::RunLoop().RunUntilIdle(); |
| 1923 | 1890 |
| 1924 // The first request should be creating the disk cache. | 1891 // The first request should be creating the disk cache. |
| 1925 EXPECT_FALSE(context_list[0]->callback.have_result()); | 1892 EXPECT_FALSE(context_list[0]->callback.have_result()); |
| 1926 | 1893 |
| 1927 factory->FinishCreation(); | 1894 factory->FinishCreation(); |
| 1928 | 1895 |
| 1929 base::RunLoop().RunUntilIdle(); | 1896 base::RunLoop().RunUntilIdle(); |
| 1930 EXPECT_EQ(3, cache.network_layer()->transaction_count()); | 1897 EXPECT_EQ(3, cache.network_layer()->transaction_count()); |
| 1931 EXPECT_EQ(3, cache.disk_cache()->create_count()); | 1898 EXPECT_EQ(3, cache.disk_cache()->create_count()); |
| 1932 | 1899 |
| 1933 for (int i = 0; i < kNumTransactions; ++i) { | 1900 for (int i = 0; i < kNumTransactions; ++i) { |
| 1934 EXPECT_TRUE(context_list[i]->callback.have_result()); | 1901 EXPECT_TRUE(context_list[i]->callback.have_result()); |
| 1935 delete context_list[i]; | 1902 context_list[i].reset(); |
| 1936 } | 1903 } |
| 1937 } | 1904 } |
| 1938 | 1905 |
| 1939 // Tests that we can cancel requests that are queued waiting for the backend | 1906 // Tests that we can cancel requests that are queued waiting for the backend |
| 1940 // to be initialized. | 1907 // to be initialized. |
| 1941 TEST(HttpCache, SimpleGET_WaitForBackend_CancelCreate) { | 1908 TEST(HttpCache, SimpleGET_WaitForBackend_CancelCreate) { |
| 1942 MockBlockingBackendFactory* factory = new MockBlockingBackendFactory(); | 1909 MockBlockingBackendFactory* factory = new MockBlockingBackendFactory(); |
| 1943 MockHttpCache cache(base::WrapUnique(factory)); | 1910 MockHttpCache cache(base::WrapUnique(factory)); |
| 1944 | 1911 |
| 1945 MockHttpRequest request0(kSimpleGET_Transaction); | 1912 MockHttpRequest request0(kSimpleGET_Transaction); |
| 1946 MockHttpRequest request1(kTypicalGET_Transaction); | 1913 MockHttpRequest request1(kTypicalGET_Transaction); |
| 1947 MockHttpRequest request2(kETagGET_Transaction); | 1914 MockHttpRequest request2(kETagGET_Transaction); |
| 1948 | 1915 |
| 1949 std::vector<Context*> context_list; | 1916 std::vector<std::unique_ptr<Context>> context_list; |
| 1950 const int kNumTransactions = 3; | 1917 const int kNumTransactions = 3; |
| 1951 | 1918 |
| 1952 for (int i = 0; i < kNumTransactions; i++) { | 1919 for (int i = 0; i < kNumTransactions; i++) { |
| 1953 context_list.push_back(new Context()); | 1920 context_list.push_back(base::MakeUnique<Context>()); |
| 1954 Context* c = context_list[i]; | 1921 Context* c = context_list[i].get(); |
| 1955 | 1922 |
| 1956 c->result = cache.CreateTransaction(&c->trans); | 1923 c->result = cache.CreateTransaction(&c->trans); |
| 1957 ASSERT_THAT(c->result, IsOk()); | 1924 ASSERT_THAT(c->result, IsOk()); |
| 1958 } | 1925 } |
| 1959 | 1926 |
| 1960 context_list[0]->result = context_list[0]->trans->Start( | 1927 context_list[0]->result = context_list[0]->trans->Start( |
| 1961 &request0, context_list[0]->callback.callback(), NetLogWithSource()); | 1928 &request0, context_list[0]->callback.callback(), NetLogWithSource()); |
| 1962 context_list[1]->result = context_list[1]->trans->Start( | 1929 context_list[1]->result = context_list[1]->trans->Start( |
| 1963 &request1, context_list[1]->callback.callback(), NetLogWithSource()); | 1930 &request1, context_list[1]->callback.callback(), NetLogWithSource()); |
| 1964 context_list[2]->result = context_list[2]->trans->Start( | 1931 context_list[2]->result = context_list[2]->trans->Start( |
| 1965 &request2, context_list[2]->callback.callback(), NetLogWithSource()); | 1932 &request2, context_list[2]->callback.callback(), NetLogWithSource()); |
| 1966 | 1933 |
| 1967 // Just to make sure that everything is still pending. | 1934 // Just to make sure that everything is still pending. |
| 1968 base::RunLoop().RunUntilIdle(); | 1935 base::RunLoop().RunUntilIdle(); |
| 1969 | 1936 |
| 1970 // The first request should be creating the disk cache. | 1937 // The first request should be creating the disk cache. |
| 1971 EXPECT_FALSE(context_list[0]->callback.have_result()); | 1938 EXPECT_FALSE(context_list[0]->callback.have_result()); |
| 1972 | 1939 |
| 1973 // Cancel a request from the pending queue. | 1940 // Cancel a request from the pending queue. |
| 1974 delete context_list[1]; | 1941 context_list[1].reset(); |
| 1975 context_list[1] = NULL; | |
| 1976 | 1942 |
| 1977 // Cancel the request that is creating the entry. | 1943 // Cancel the request that is creating the entry. |
| 1978 delete context_list[0]; | 1944 context_list[0].reset(); |
| 1979 context_list[0] = NULL; | |
| 1980 | 1945 |
| 1981 // Complete the last transaction. | 1946 // Complete the last transaction. |
| 1982 factory->FinishCreation(); | 1947 factory->FinishCreation(); |
| 1983 | 1948 |
| 1984 context_list[2]->result = | 1949 context_list[2]->result = |
| 1985 context_list[2]->callback.GetResult(context_list[2]->result); | 1950 context_list[2]->callback.GetResult(context_list[2]->result); |
| 1986 ReadAndVerifyTransaction(context_list[2]->trans.get(), kETagGET_Transaction); | 1951 ReadAndVerifyTransaction(context_list[2]->trans.get(), kETagGET_Transaction); |
| 1987 | 1952 |
| 1988 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 1953 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 1989 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 1954 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 1990 | |
| 1991 delete context_list[2]; | |
| 1992 } | 1955 } |
| 1993 | 1956 |
| 1994 // Tests that we can delete the cache while creating the backend. | 1957 // Tests that we can delete the cache while creating the backend. |
| 1995 TEST(HttpCache, DeleteCacheWaitingForBackend) { | 1958 TEST(HttpCache, DeleteCacheWaitingForBackend) { |
| 1996 MockBlockingBackendFactory* factory = new MockBlockingBackendFactory(); | 1959 MockBlockingBackendFactory* factory = new MockBlockingBackendFactory(); |
| 1997 std::unique_ptr<MockHttpCache> cache( | 1960 auto cache = base::MakeUnique<MockHttpCache>(base::WrapUnique(factory)); |
| 1998 new MockHttpCache(base::WrapUnique(factory))); | |
| 1999 | 1961 |
| 2000 MockHttpRequest request(kSimpleGET_Transaction); | 1962 MockHttpRequest request(kSimpleGET_Transaction); |
| 2001 | 1963 |
| 2002 std::unique_ptr<Context> c(new Context()); | 1964 auto c = base::MakeUnique<Context>(); |
| 2003 c->result = cache->CreateTransaction(&c->trans); | 1965 c->result = cache->CreateTransaction(&c->trans); |
| 2004 ASSERT_THAT(c->result, IsOk()); | 1966 ASSERT_THAT(c->result, IsOk()); |
| 2005 | 1967 |
| 2006 c->trans->Start(&request, c->callback.callback(), NetLogWithSource()); | 1968 c->trans->Start(&request, c->callback.callback(), NetLogWithSource()); |
| 2007 | 1969 |
| 2008 // Just to make sure that everything is still pending. | 1970 // Just to make sure that everything is still pending. |
| 2009 base::RunLoop().RunUntilIdle(); | 1971 base::RunLoop().RunUntilIdle(); |
| 2010 | 1972 |
| 2011 // The request should be creating the disk cache. | 1973 // The request should be creating the disk cache. |
| 2012 EXPECT_FALSE(c->callback.have_result()); | 1974 EXPECT_FALSE(c->callback.have_result()); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 2030 MockHttpCache* cache = new MockHttpCache(base::WrapUnique(factory)); | 1992 MockHttpCache* cache = new MockHttpCache(base::WrapUnique(factory)); |
| 2031 | 1993 |
| 2032 DeleteCacheCompletionCallback cb(cache); | 1994 DeleteCacheCompletionCallback cb(cache); |
| 2033 disk_cache::Backend* backend; | 1995 disk_cache::Backend* backend; |
| 2034 int rv = cache->http_cache()->GetBackend(&backend, cb.callback()); | 1996 int rv = cache->http_cache()->GetBackend(&backend, cb.callback()); |
| 2035 EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); | 1997 EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 2036 | 1998 |
| 2037 // Now let's queue a regular transaction | 1999 // Now let's queue a regular transaction |
| 2038 MockHttpRequest request(kSimpleGET_Transaction); | 2000 MockHttpRequest request(kSimpleGET_Transaction); |
| 2039 | 2001 |
| 2040 std::unique_ptr<Context> c(new Context()); | 2002 auto c = base::MakeUnique<Context>(); |
| 2041 c->result = cache->CreateTransaction(&c->trans); | 2003 c->result = cache->CreateTransaction(&c->trans); |
| 2042 ASSERT_THAT(c->result, IsOk()); | 2004 ASSERT_THAT(c->result, IsOk()); |
| 2043 | 2005 |
| 2044 c->trans->Start(&request, c->callback.callback(), NetLogWithSource()); | 2006 c->trans->Start(&request, c->callback.callback(), NetLogWithSource()); |
| 2045 | 2007 |
| 2046 // And another direct backend request. | 2008 // And another direct backend request. |
| 2047 TestCompletionCallback cb2; | 2009 TestCompletionCallback cb2; |
| 2048 rv = cache->http_cache()->GetBackend(&backend, cb2.callback()); | 2010 rv = cache->http_cache()->GetBackend(&backend, cb2.callback()); |
| 2049 EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); | 2011 EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 2050 | 2012 |
| (...skipping 1015 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3066 TEST(HttpCache, SimplePOST_WithRanges) { | 3028 TEST(HttpCache, SimplePOST_WithRanges) { |
| 3067 MockHttpCache cache; | 3029 MockHttpCache cache; |
| 3068 | 3030 |
| 3069 MockTransaction transaction(kSimplePOST_Transaction); | 3031 MockTransaction transaction(kSimplePOST_Transaction); |
| 3070 transaction.request_headers = "Range: bytes = 0-4\r\n"; | 3032 transaction.request_headers = "Range: bytes = 0-4\r\n"; |
| 3071 | 3033 |
| 3072 const int64_t kUploadId = 1; // Just a dummy value. | 3034 const int64_t kUploadId = 1; // Just a dummy value. |
| 3073 | 3035 |
| 3074 std::vector<std::unique_ptr<UploadElementReader>> element_readers; | 3036 std::vector<std::unique_ptr<UploadElementReader>> element_readers; |
| 3075 element_readers.push_back( | 3037 element_readers.push_back( |
| 3076 base::WrapUnique(new UploadBytesElementReader("hello", 5))); | 3038 base::MakeUnique<UploadBytesElementReader>("hello", 5)); |
| 3077 ElementsUploadDataStream upload_data_stream(std::move(element_readers), | 3039 ElementsUploadDataStream upload_data_stream(std::move(element_readers), |
| 3078 kUploadId); | 3040 kUploadId); |
| 3079 | 3041 |
| 3080 MockHttpRequest request(transaction); | 3042 MockHttpRequest request(transaction); |
| 3081 request.upload_data_stream = &upload_data_stream; | 3043 request.upload_data_stream = &upload_data_stream; |
| 3082 | 3044 |
| 3083 // Attempt to populate the cache. | 3045 // Attempt to populate the cache. |
| 3084 RunTransactionTestWithRequest(cache.http_cache(), transaction, request, NULL); | 3046 RunTransactionTestWithRequest(cache.http_cache(), transaction, request, NULL); |
| 3085 | 3047 |
| 3086 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 3048 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 3087 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 3049 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 3088 EXPECT_EQ(0, cache.disk_cache()->create_count()); | 3050 EXPECT_EQ(0, cache.disk_cache()->create_count()); |
| 3089 } | 3051 } |
| 3090 | 3052 |
| 3091 // Tests that a POST is cached separately from a GET. | 3053 // Tests that a POST is cached separately from a GET. |
| 3092 TEST(HttpCache, SimplePOST_SeparateCache) { | 3054 TEST(HttpCache, SimplePOST_SeparateCache) { |
| 3093 MockHttpCache cache; | 3055 MockHttpCache cache; |
| 3094 | 3056 |
| 3095 std::vector<std::unique_ptr<UploadElementReader>> element_readers; | 3057 std::vector<std::unique_ptr<UploadElementReader>> element_readers; |
| 3096 element_readers.push_back( | 3058 element_readers.push_back( |
| 3097 base::WrapUnique(new UploadBytesElementReader("hello", 5))); | 3059 base::MakeUnique<UploadBytesElementReader>("hello", 5)); |
| 3098 ElementsUploadDataStream upload_data_stream(std::move(element_readers), 1); | 3060 ElementsUploadDataStream upload_data_stream(std::move(element_readers), 1); |
| 3099 | 3061 |
| 3100 MockTransaction transaction(kSimplePOST_Transaction); | 3062 MockTransaction transaction(kSimplePOST_Transaction); |
| 3101 MockHttpRequest req1(transaction); | 3063 MockHttpRequest req1(transaction); |
| 3102 req1.upload_data_stream = &upload_data_stream; | 3064 req1.upload_data_stream = &upload_data_stream; |
| 3103 | 3065 |
| 3104 RunTransactionTestWithRequest(cache.http_cache(), transaction, req1, NULL); | 3066 RunTransactionTestWithRequest(cache.http_cache(), transaction, req1, NULL); |
| 3105 | 3067 |
| 3106 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 3068 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 3107 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 3069 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 3127 | 3089 |
| 3128 // Attempt to populate the cache. | 3090 // Attempt to populate the cache. |
| 3129 RunTransactionTestWithRequest(cache.http_cache(), transaction, req1, NULL); | 3091 RunTransactionTestWithRequest(cache.http_cache(), transaction, req1, NULL); |
| 3130 | 3092 |
| 3131 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 3093 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 3132 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 3094 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 3133 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 3095 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 3134 | 3096 |
| 3135 std::vector<std::unique_ptr<UploadElementReader>> element_readers; | 3097 std::vector<std::unique_ptr<UploadElementReader>> element_readers; |
| 3136 element_readers.push_back( | 3098 element_readers.push_back( |
| 3137 base::WrapUnique(new UploadBytesElementReader("hello", 5))); | 3099 base::MakeUnique<UploadBytesElementReader>("hello", 5)); |
| 3138 ElementsUploadDataStream upload_data_stream(std::move(element_readers), 1); | 3100 ElementsUploadDataStream upload_data_stream(std::move(element_readers), 1); |
| 3139 | 3101 |
| 3140 transaction.method = "POST"; | 3102 transaction.method = "POST"; |
| 3141 transaction.status = "HTTP/1.1 205 No Content"; | 3103 transaction.status = "HTTP/1.1 205 No Content"; |
| 3142 MockHttpRequest req2(transaction); | 3104 MockHttpRequest req2(transaction); |
| 3143 req2.upload_data_stream = &upload_data_stream; | 3105 req2.upload_data_stream = &upload_data_stream; |
| 3144 | 3106 |
| 3145 RunTransactionTestWithRequest(cache.http_cache(), transaction, req2, NULL); | 3107 RunTransactionTestWithRequest(cache.http_cache(), transaction, req2, NULL); |
| 3146 | 3108 |
| 3147 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | 3109 EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 3167 | 3129 |
| 3168 // Attempt to populate the cache. | 3130 // Attempt to populate the cache. |
| 3169 RunTransactionTestWithRequest(cache.http_cache(), transaction, req1, NULL); | 3131 RunTransactionTestWithRequest(cache.http_cache(), transaction, req1, NULL); |
| 3170 | 3132 |
| 3171 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 3133 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 3172 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 3134 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 3173 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 3135 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 3174 | 3136 |
| 3175 std::vector<std::unique_ptr<UploadElementReader>> element_readers; | 3137 std::vector<std::unique_ptr<UploadElementReader>> element_readers; |
| 3176 element_readers.push_back( | 3138 element_readers.push_back( |
| 3177 base::WrapUnique(new UploadBytesElementReader("hello", 5))); | 3139 base::MakeUnique<UploadBytesElementReader>("hello", 5)); |
| 3178 ElementsUploadDataStream upload_data_stream(std::move(element_readers), 0); | 3140 ElementsUploadDataStream upload_data_stream(std::move(element_readers), 0); |
| 3179 | 3141 |
| 3180 transaction.method = "POST"; | 3142 transaction.method = "POST"; |
| 3181 transaction.status = "HTTP/1.1 205 No Content"; | 3143 transaction.status = "HTTP/1.1 205 No Content"; |
| 3182 MockHttpRequest req2(transaction); | 3144 MockHttpRequest req2(transaction); |
| 3183 req2.upload_data_stream = &upload_data_stream; | 3145 req2.upload_data_stream = &upload_data_stream; |
| 3184 | 3146 |
| 3185 RunTransactionTestWithRequest(cache.http_cache(), transaction, req2, NULL); | 3147 RunTransactionTestWithRequest(cache.http_cache(), transaction, req2, NULL); |
| 3186 | 3148 |
| 3187 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | 3149 EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 3188 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 3150 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 3189 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 3151 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 3190 | 3152 |
| 3191 RunTransactionTestWithRequest(cache.http_cache(), transaction, req1, NULL); | 3153 RunTransactionTestWithRequest(cache.http_cache(), transaction, req1, NULL); |
| 3192 | 3154 |
| 3193 EXPECT_EQ(3, cache.network_layer()->transaction_count()); | 3155 EXPECT_EQ(3, cache.network_layer()->transaction_count()); |
| 3194 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 3156 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 3195 EXPECT_EQ(2, cache.disk_cache()->create_count()); | 3157 EXPECT_EQ(2, cache.disk_cache()->create_count()); |
| 3196 RemoveMockTransaction(&transaction); | 3158 RemoveMockTransaction(&transaction); |
| 3197 } | 3159 } |
| 3198 | 3160 |
| 3199 // Tests that processing a POST before creating the backend doesn't crash. | 3161 // Tests that processing a POST before creating the backend doesn't crash. |
| 3200 TEST(HttpCache, SimplePOST_NoUploadId_NoBackend) { | 3162 TEST(HttpCache, SimplePOST_NoUploadId_NoBackend) { |
| 3201 // This will initialize a cache object with NULL backend. | 3163 // This will initialize a cache object with NULL backend. |
| 3202 std::unique_ptr<MockBlockingBackendFactory> factory( | 3164 auto factory = base::MakeUnique<MockBlockingBackendFactory>(); |
| 3203 new MockBlockingBackendFactory()); | |
| 3204 factory->set_fail(true); | 3165 factory->set_fail(true); |
| 3205 factory->FinishCreation(); | 3166 factory->FinishCreation(); |
| 3206 MockHttpCache cache(std::move(factory)); | 3167 MockHttpCache cache(std::move(factory)); |
| 3207 | 3168 |
| 3208 std::vector<std::unique_ptr<UploadElementReader>> element_readers; | 3169 std::vector<std::unique_ptr<UploadElementReader>> element_readers; |
| 3209 element_readers.push_back( | 3170 element_readers.push_back( |
| 3210 base::WrapUnique(new UploadBytesElementReader("hello", 5))); | 3171 base::MakeUnique<UploadBytesElementReader>("hello", 5)); |
| 3211 ElementsUploadDataStream upload_data_stream(std::move(element_readers), 0); | 3172 ElementsUploadDataStream upload_data_stream(std::move(element_readers), 0); |
| 3212 | 3173 |
| 3213 MockTransaction transaction(kSimplePOST_Transaction); | 3174 MockTransaction transaction(kSimplePOST_Transaction); |
| 3214 AddMockTransaction(&transaction); | 3175 AddMockTransaction(&transaction); |
| 3215 MockHttpRequest req(transaction); | 3176 MockHttpRequest req(transaction); |
| 3216 req.upload_data_stream = &upload_data_stream; | 3177 req.upload_data_stream = &upload_data_stream; |
| 3217 | 3178 |
| 3218 RunTransactionTestWithRequest(cache.http_cache(), transaction, req, NULL); | 3179 RunTransactionTestWithRequest(cache.http_cache(), transaction, req, NULL); |
| 3219 | 3180 |
| 3220 RemoveMockTransaction(&transaction); | 3181 RemoveMockTransaction(&transaction); |
| 3221 } | 3182 } |
| 3222 | 3183 |
| 3223 // Tests that we don't invalidate entries as a result of a failed POST. | 3184 // Tests that we don't invalidate entries as a result of a failed POST. |
| 3224 TEST(HttpCache, SimplePOST_DontInvalidate_100) { | 3185 TEST(HttpCache, SimplePOST_DontInvalidate_100) { |
| 3225 MockHttpCache cache; | 3186 MockHttpCache cache; |
| 3226 | 3187 |
| 3227 MockTransaction transaction(kSimpleGET_Transaction); | 3188 MockTransaction transaction(kSimpleGET_Transaction); |
| 3228 AddMockTransaction(&transaction); | 3189 AddMockTransaction(&transaction); |
| 3229 MockHttpRequest req1(transaction); | 3190 MockHttpRequest req1(transaction); |
| 3230 | 3191 |
| 3231 // Attempt to populate the cache. | 3192 // Attempt to populate the cache. |
| 3232 RunTransactionTestWithRequest(cache.http_cache(), transaction, req1, NULL); | 3193 RunTransactionTestWithRequest(cache.http_cache(), transaction, req1, NULL); |
| 3233 | 3194 |
| 3234 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 3195 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 3235 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 3196 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 3236 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 3197 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 3237 | 3198 |
| 3238 std::vector<std::unique_ptr<UploadElementReader>> element_readers; | 3199 std::vector<std::unique_ptr<UploadElementReader>> element_readers; |
| 3239 element_readers.push_back( | 3200 element_readers.push_back( |
| 3240 base::WrapUnique(new UploadBytesElementReader("hello", 5))); | 3201 base::MakeUnique<UploadBytesElementReader>("hello", 5)); |
| 3241 ElementsUploadDataStream upload_data_stream(std::move(element_readers), 1); | 3202 ElementsUploadDataStream upload_data_stream(std::move(element_readers), 1); |
| 3242 | 3203 |
| 3243 transaction.method = "POST"; | 3204 transaction.method = "POST"; |
| 3244 transaction.status = "HTTP/1.1 100 Continue"; | 3205 transaction.status = "HTTP/1.1 100 Continue"; |
| 3245 MockHttpRequest req2(transaction); | 3206 MockHttpRequest req2(transaction); |
| 3246 req2.upload_data_stream = &upload_data_stream; | 3207 req2.upload_data_stream = &upload_data_stream; |
| 3247 | 3208 |
| 3248 RunTransactionTestWithRequest(cache.http_cache(), transaction, req2, NULL); | 3209 RunTransactionTestWithRequest(cache.http_cache(), transaction, req2, NULL); |
| 3249 | 3210 |
| 3250 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | 3211 EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| (...skipping 291 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3542 | 3503 |
| 3543 // Tests that we do not cache the response of a PUT. | 3504 // Tests that we do not cache the response of a PUT. |
| 3544 TEST(HttpCache, SimplePUT_Miss) { | 3505 TEST(HttpCache, SimplePUT_Miss) { |
| 3545 MockHttpCache cache; | 3506 MockHttpCache cache; |
| 3546 | 3507 |
| 3547 MockTransaction transaction(kSimplePOST_Transaction); | 3508 MockTransaction transaction(kSimplePOST_Transaction); |
| 3548 transaction.method = "PUT"; | 3509 transaction.method = "PUT"; |
| 3549 | 3510 |
| 3550 std::vector<std::unique_ptr<UploadElementReader>> element_readers; | 3511 std::vector<std::unique_ptr<UploadElementReader>> element_readers; |
| 3551 element_readers.push_back( | 3512 element_readers.push_back( |
| 3552 base::WrapUnique(new UploadBytesElementReader("hello", 5))); | 3513 base::MakeUnique<UploadBytesElementReader>("hello", 5)); |
| 3553 ElementsUploadDataStream upload_data_stream(std::move(element_readers), 0); | 3514 ElementsUploadDataStream upload_data_stream(std::move(element_readers), 0); |
| 3554 | 3515 |
| 3555 MockHttpRequest request(transaction); | 3516 MockHttpRequest request(transaction); |
| 3556 request.upload_data_stream = &upload_data_stream; | 3517 request.upload_data_stream = &upload_data_stream; |
| 3557 | 3518 |
| 3558 // Attempt to populate the cache. | 3519 // Attempt to populate the cache. |
| 3559 RunTransactionTestWithRequest(cache.http_cache(), transaction, request, NULL); | 3520 RunTransactionTestWithRequest(cache.http_cache(), transaction, request, NULL); |
| 3560 | 3521 |
| 3561 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 3522 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 3562 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 3523 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 3563 EXPECT_EQ(0, cache.disk_cache()->create_count()); | 3524 EXPECT_EQ(0, cache.disk_cache()->create_count()); |
| 3564 } | 3525 } |
| 3565 | 3526 |
| 3566 // Tests that we invalidate entries as a result of a PUT. | 3527 // Tests that we invalidate entries as a result of a PUT. |
| 3567 TEST(HttpCache, SimplePUT_Invalidate) { | 3528 TEST(HttpCache, SimplePUT_Invalidate) { |
| 3568 MockHttpCache cache; | 3529 MockHttpCache cache; |
| 3569 | 3530 |
| 3570 MockTransaction transaction(kSimpleGET_Transaction); | 3531 MockTransaction transaction(kSimpleGET_Transaction); |
| 3571 MockHttpRequest req1(transaction); | 3532 MockHttpRequest req1(transaction); |
| 3572 | 3533 |
| 3573 // Attempt to populate the cache. | 3534 // Attempt to populate the cache. |
| 3574 RunTransactionTestWithRequest(cache.http_cache(), transaction, req1, NULL); | 3535 RunTransactionTestWithRequest(cache.http_cache(), transaction, req1, NULL); |
| 3575 | 3536 |
| 3576 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 3537 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 3577 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 3538 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 3578 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 3539 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 3579 | 3540 |
| 3580 std::vector<std::unique_ptr<UploadElementReader>> element_readers; | 3541 std::vector<std::unique_ptr<UploadElementReader>> element_readers; |
| 3581 element_readers.push_back( | 3542 element_readers.push_back( |
| 3582 base::WrapUnique(new UploadBytesElementReader("hello", 5))); | 3543 base::MakeUnique<UploadBytesElementReader>("hello", 5)); |
| 3583 ElementsUploadDataStream upload_data_stream(std::move(element_readers), 0); | 3544 ElementsUploadDataStream upload_data_stream(std::move(element_readers), 0); |
| 3584 | 3545 |
| 3585 transaction.method = "PUT"; | 3546 transaction.method = "PUT"; |
| 3586 MockHttpRequest req2(transaction); | 3547 MockHttpRequest req2(transaction); |
| 3587 req2.upload_data_stream = &upload_data_stream; | 3548 req2.upload_data_stream = &upload_data_stream; |
| 3588 | 3549 |
| 3589 RunTransactionTestWithRequest(cache.http_cache(), transaction, req2, NULL); | 3550 RunTransactionTestWithRequest(cache.http_cache(), transaction, req2, NULL); |
| 3590 | 3551 |
| 3591 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | 3552 EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 3592 EXPECT_EQ(1, cache.disk_cache()->open_count()); | 3553 EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 3609 | 3570 |
| 3610 // Attempt to populate the cache. | 3571 // Attempt to populate the cache. |
| 3611 RunTransactionTestWithRequest(cache.http_cache(), transaction, req1, NULL); | 3572 RunTransactionTestWithRequest(cache.http_cache(), transaction, req1, NULL); |
| 3612 | 3573 |
| 3613 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 3574 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 3614 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 3575 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 3615 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 3576 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 3616 | 3577 |
| 3617 std::vector<std::unique_ptr<UploadElementReader>> element_readers; | 3578 std::vector<std::unique_ptr<UploadElementReader>> element_readers; |
| 3618 element_readers.push_back( | 3579 element_readers.push_back( |
| 3619 base::WrapUnique(new UploadBytesElementReader("hello", 5))); | 3580 base::MakeUnique<UploadBytesElementReader>("hello", 5)); |
| 3620 ElementsUploadDataStream upload_data_stream(std::move(element_readers), 0); | 3581 ElementsUploadDataStream upload_data_stream(std::move(element_readers), 0); |
| 3621 | 3582 |
| 3622 transaction.method = "PUT"; | 3583 transaction.method = "PUT"; |
| 3623 transaction.status = "HTTP/1.1 305 Use Proxy"; | 3584 transaction.status = "HTTP/1.1 305 Use Proxy"; |
| 3624 MockHttpRequest req2(transaction); | 3585 MockHttpRequest req2(transaction); |
| 3625 req2.upload_data_stream = &upload_data_stream; | 3586 req2.upload_data_stream = &upload_data_stream; |
| 3626 | 3587 |
| 3627 RunTransactionTestWithRequest(cache.http_cache(), transaction, req2, NULL); | 3588 RunTransactionTestWithRequest(cache.http_cache(), transaction, req2, NULL); |
| 3628 | 3589 |
| 3629 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | 3590 EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 3648 | 3609 |
| 3649 // Attempt to populate the cache. | 3610 // Attempt to populate the cache. |
| 3650 RunTransactionTestWithRequest(cache.http_cache(), transaction, req1, NULL); | 3611 RunTransactionTestWithRequest(cache.http_cache(), transaction, req1, NULL); |
| 3651 | 3612 |
| 3652 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 3613 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 3653 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 3614 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 3654 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 3615 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 3655 | 3616 |
| 3656 std::vector<std::unique_ptr<UploadElementReader>> element_readers; | 3617 std::vector<std::unique_ptr<UploadElementReader>> element_readers; |
| 3657 element_readers.push_back( | 3618 element_readers.push_back( |
| 3658 base::WrapUnique(new UploadBytesElementReader("hello", 5))); | 3619 base::MakeUnique<UploadBytesElementReader>("hello", 5)); |
| 3659 ElementsUploadDataStream upload_data_stream(std::move(element_readers), 0); | 3620 ElementsUploadDataStream upload_data_stream(std::move(element_readers), 0); |
| 3660 | 3621 |
| 3661 transaction.method = "PUT"; | 3622 transaction.method = "PUT"; |
| 3662 transaction.status = "HTTP/1.1 404 Not Found"; | 3623 transaction.status = "HTTP/1.1 404 Not Found"; |
| 3663 MockHttpRequest req2(transaction); | 3624 MockHttpRequest req2(transaction); |
| 3664 req2.upload_data_stream = &upload_data_stream; | 3625 req2.upload_data_stream = &upload_data_stream; |
| 3665 | 3626 |
| 3666 RunTransactionTestWithRequest(cache.http_cache(), transaction, req2, NULL); | 3627 RunTransactionTestWithRequest(cache.http_cache(), transaction, req2, NULL); |
| 3667 | 3628 |
| 3668 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | 3629 EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 3679 | 3640 |
| 3680 // Tests that we do not cache the response of a DELETE. | 3641 // Tests that we do not cache the response of a DELETE. |
| 3681 TEST(HttpCache, SimpleDELETE_Miss) { | 3642 TEST(HttpCache, SimpleDELETE_Miss) { |
| 3682 MockHttpCache cache; | 3643 MockHttpCache cache; |
| 3683 | 3644 |
| 3684 MockTransaction transaction(kSimplePOST_Transaction); | 3645 MockTransaction transaction(kSimplePOST_Transaction); |
| 3685 transaction.method = "DELETE"; | 3646 transaction.method = "DELETE"; |
| 3686 | 3647 |
| 3687 std::vector<std::unique_ptr<UploadElementReader>> element_readers; | 3648 std::vector<std::unique_ptr<UploadElementReader>> element_readers; |
| 3688 element_readers.push_back( | 3649 element_readers.push_back( |
| 3689 base::WrapUnique(new UploadBytesElementReader("hello", 5))); | 3650 base::MakeUnique<UploadBytesElementReader>("hello", 5)); |
| 3690 ElementsUploadDataStream upload_data_stream(std::move(element_readers), 0); | 3651 ElementsUploadDataStream upload_data_stream(std::move(element_readers), 0); |
| 3691 | 3652 |
| 3692 MockHttpRequest request(transaction); | 3653 MockHttpRequest request(transaction); |
| 3693 request.upload_data_stream = &upload_data_stream; | 3654 request.upload_data_stream = &upload_data_stream; |
| 3694 | 3655 |
| 3695 // Attempt to populate the cache. | 3656 // Attempt to populate the cache. |
| 3696 RunTransactionTestWithRequest(cache.http_cache(), transaction, request, NULL); | 3657 RunTransactionTestWithRequest(cache.http_cache(), transaction, request, NULL); |
| 3697 | 3658 |
| 3698 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 3659 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 3699 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 3660 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 3700 EXPECT_EQ(0, cache.disk_cache()->create_count()); | 3661 EXPECT_EQ(0, cache.disk_cache()->create_count()); |
| 3701 } | 3662 } |
| 3702 | 3663 |
| 3703 // Tests that we invalidate entries as a result of a DELETE. | 3664 // Tests that we invalidate entries as a result of a DELETE. |
| 3704 TEST(HttpCache, SimpleDELETE_Invalidate) { | 3665 TEST(HttpCache, SimpleDELETE_Invalidate) { |
| 3705 MockHttpCache cache; | 3666 MockHttpCache cache; |
| 3706 | 3667 |
| 3707 MockTransaction transaction(kSimpleGET_Transaction); | 3668 MockTransaction transaction(kSimpleGET_Transaction); |
| 3708 MockHttpRequest req1(transaction); | 3669 MockHttpRequest req1(transaction); |
| 3709 | 3670 |
| 3710 // Attempt to populate the cache. | 3671 // Attempt to populate the cache. |
| 3711 RunTransactionTestWithRequest(cache.http_cache(), transaction, req1, NULL); | 3672 RunTransactionTestWithRequest(cache.http_cache(), transaction, req1, NULL); |
| 3712 | 3673 |
| 3713 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 3674 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 3714 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 3675 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 3715 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 3676 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 3716 | 3677 |
| 3717 std::vector<std::unique_ptr<UploadElementReader>> element_readers; | 3678 std::vector<std::unique_ptr<UploadElementReader>> element_readers; |
| 3718 element_readers.push_back( | 3679 element_readers.push_back( |
| 3719 base::WrapUnique(new UploadBytesElementReader("hello", 5))); | 3680 base::MakeUnique<UploadBytesElementReader>("hello", 5)); |
| 3720 ElementsUploadDataStream upload_data_stream(std::move(element_readers), 0); | 3681 ElementsUploadDataStream upload_data_stream(std::move(element_readers), 0); |
| 3721 | 3682 |
| 3722 transaction.method = "DELETE"; | 3683 transaction.method = "DELETE"; |
| 3723 MockHttpRequest req2(transaction); | 3684 MockHttpRequest req2(transaction); |
| 3724 req2.upload_data_stream = &upload_data_stream; | 3685 req2.upload_data_stream = &upload_data_stream; |
| 3725 | 3686 |
| 3726 RunTransactionTestWithRequest(cache.http_cache(), transaction, req2, NULL); | 3687 RunTransactionTestWithRequest(cache.http_cache(), transaction, req2, NULL); |
| 3727 | 3688 |
| 3728 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | 3689 EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 3729 EXPECT_EQ(1, cache.disk_cache()->open_count()); | 3690 EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| (...skipping 1536 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 5266 #else | 5227 #else |
| 5267 #define MAYBE_RangeGET_Cancel RangeGET_Cancel | 5228 #define MAYBE_RangeGET_Cancel RangeGET_Cancel |
| 5268 #endif | 5229 #endif |
| 5269 // Tests that we don't delete a sparse entry when we cancel a request. | 5230 // Tests that we don't delete a sparse entry when we cancel a request. |
| 5270 TEST(HttpCache, MAYBE_RangeGET_Cancel) { | 5231 TEST(HttpCache, MAYBE_RangeGET_Cancel) { |
| 5271 MockHttpCache cache; | 5232 MockHttpCache cache; |
| 5272 AddMockTransaction(&kRangeGET_TransactionOK); | 5233 AddMockTransaction(&kRangeGET_TransactionOK); |
| 5273 | 5234 |
| 5274 MockHttpRequest request(kRangeGET_TransactionOK); | 5235 MockHttpRequest request(kRangeGET_TransactionOK); |
| 5275 | 5236 |
| 5276 Context* c = new Context(); | 5237 auto c = base::MakeUnique<Context>(); |
| 5277 int rv = cache.CreateTransaction(&c->trans); | 5238 int rv = cache.CreateTransaction(&c->trans); |
| 5278 ASSERT_THAT(rv, IsOk()); | 5239 ASSERT_THAT(rv, IsOk()); |
| 5279 | 5240 |
| 5280 rv = c->trans->Start(&request, c->callback.callback(), NetLogWithSource()); | 5241 rv = c->trans->Start(&request, c->callback.callback(), NetLogWithSource()); |
| 5281 if (rv == ERR_IO_PENDING) | 5242 if (rv == ERR_IO_PENDING) |
| 5282 rv = c->callback.WaitForResult(); | 5243 rv = c->callback.WaitForResult(); |
| 5283 | 5244 |
| 5284 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 5245 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 5285 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 5246 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 5286 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 5247 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 5287 | 5248 |
| 5288 // Make sure that the entry has some data stored. | 5249 // Make sure that the entry has some data stored. |
| 5289 scoped_refptr<IOBufferWithSize> buf(new IOBufferWithSize(10)); | 5250 scoped_refptr<IOBufferWithSize> buf(new IOBufferWithSize(10)); |
| 5290 rv = c->trans->Read(buf.get(), buf->size(), c->callback.callback()); | 5251 rv = c->trans->Read(buf.get(), buf->size(), c->callback.callback()); |
| 5291 if (rv == ERR_IO_PENDING) | 5252 if (rv == ERR_IO_PENDING) |
| 5292 rv = c->callback.WaitForResult(); | 5253 rv = c->callback.WaitForResult(); |
| 5293 EXPECT_EQ(buf->size(), rv); | 5254 EXPECT_EQ(buf->size(), rv); |
| 5294 | 5255 |
| 5295 // Destroy the transaction. | 5256 // Destroy the transaction. |
| 5296 delete c; | 5257 c.reset(); |
| 5297 | 5258 |
| 5298 // Verify that the entry has not been deleted. | 5259 // Verify that the entry has not been deleted. |
| 5299 disk_cache::Entry* entry; | 5260 disk_cache::Entry* entry; |
| 5300 ASSERT_TRUE(cache.OpenBackendEntry(kRangeGET_TransactionOK.url, &entry)); | 5261 ASSERT_TRUE(cache.OpenBackendEntry(kRangeGET_TransactionOK.url, &entry)); |
| 5301 entry->Close(); | 5262 entry->Close(); |
| 5302 RemoveMockTransaction(&kRangeGET_TransactionOK); | 5263 RemoveMockTransaction(&kRangeGET_TransactionOK); |
| 5303 } | 5264 } |
| 5304 | 5265 |
| 5305 // Fails only on bots. crbug.com/533640 | 5266 // Fails only on bots. crbug.com/533640 |
| 5306 #if defined(OS_ANDROID) | 5267 #if defined(OS_ANDROID) |
| 5307 #define MAYBE_RangeGET_Cancel2 DISABLED_RangeGET_Cancel2 | 5268 #define MAYBE_RangeGET_Cancel2 DISABLED_RangeGET_Cancel2 |
| 5308 #else | 5269 #else |
| 5309 #define MAYBE_RangeGET_Cancel2 RangeGET_Cancel2 | 5270 #define MAYBE_RangeGET_Cancel2 RangeGET_Cancel2 |
| 5310 #endif | 5271 #endif |
| 5311 // Tests that we don't delete a sparse entry when we start a new request after | 5272 // Tests that we don't delete a sparse entry when we start a new request after |
| 5312 // cancelling the previous one. | 5273 // cancelling the previous one. |
| 5313 TEST(HttpCache, MAYBE_RangeGET_Cancel2) { | 5274 TEST(HttpCache, MAYBE_RangeGET_Cancel2) { |
| 5314 MockHttpCache cache; | 5275 MockHttpCache cache; |
| 5315 AddMockTransaction(&kRangeGET_TransactionOK); | 5276 AddMockTransaction(&kRangeGET_TransactionOK); |
| 5316 | 5277 |
| 5317 RunTransactionTest(cache.http_cache(), kRangeGET_TransactionOK); | 5278 RunTransactionTest(cache.http_cache(), kRangeGET_TransactionOK); |
| 5318 MockHttpRequest request(kRangeGET_TransactionOK); | 5279 MockHttpRequest request(kRangeGET_TransactionOK); |
| 5319 request.load_flags |= LOAD_VALIDATE_CACHE; | 5280 request.load_flags |= LOAD_VALIDATE_CACHE; |
| 5320 | 5281 |
| 5321 Context* c = new Context(); | 5282 auto c = base::MakeUnique<Context>(); |
| 5322 int rv = cache.CreateTransaction(&c->trans); | 5283 int rv = cache.CreateTransaction(&c->trans); |
| 5323 ASSERT_THAT(rv, IsOk()); | 5284 ASSERT_THAT(rv, IsOk()); |
| 5324 | 5285 |
| 5325 rv = c->trans->Start(&request, c->callback.callback(), NetLogWithSource()); | 5286 rv = c->trans->Start(&request, c->callback.callback(), NetLogWithSource()); |
| 5326 if (rv == ERR_IO_PENDING) | 5287 if (rv == ERR_IO_PENDING) |
| 5327 rv = c->callback.WaitForResult(); | 5288 rv = c->callback.WaitForResult(); |
| 5328 | 5289 |
| 5329 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | 5290 EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 5330 EXPECT_EQ(1, cache.disk_cache()->open_count()); | 5291 EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 5331 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 5292 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 5332 | 5293 |
| 5333 // Make sure that we revalidate the entry and read from the cache (a single | 5294 // Make sure that we revalidate the entry and read from the cache (a single |
| 5334 // read will return while waiting for the network). | 5295 // read will return while waiting for the network). |
| 5335 scoped_refptr<IOBufferWithSize> buf(new IOBufferWithSize(5)); | 5296 scoped_refptr<IOBufferWithSize> buf(new IOBufferWithSize(5)); |
| 5336 rv = c->trans->Read(buf.get(), buf->size(), c->callback.callback()); | 5297 rv = c->trans->Read(buf.get(), buf->size(), c->callback.callback()); |
| 5337 EXPECT_EQ(5, c->callback.GetResult(rv)); | 5298 EXPECT_EQ(5, c->callback.GetResult(rv)); |
| 5338 rv = c->trans->Read(buf.get(), buf->size(), c->callback.callback()); | 5299 rv = c->trans->Read(buf.get(), buf->size(), c->callback.callback()); |
| 5339 EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); | 5300 EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 5340 | 5301 |
| 5341 // Destroy the transaction before completing the read. | 5302 // Destroy the transaction before completing the read. |
| 5342 delete c; | 5303 c.reset(); |
| 5343 | 5304 |
| 5344 // We have the read and the delete (OnProcessPendingQueue) waiting on the | 5305 // We have the read and the delete (OnProcessPendingQueue) waiting on the |
| 5345 // message loop. This means that a new transaction will just reuse the same | 5306 // message loop. This means that a new transaction will just reuse the same |
| 5346 // active entry (no open or create). | 5307 // active entry (no open or create). |
| 5347 | 5308 |
| 5348 RunTransactionTest(cache.http_cache(), kRangeGET_TransactionOK); | 5309 RunTransactionTest(cache.http_cache(), kRangeGET_TransactionOK); |
| 5349 | 5310 |
| 5350 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | 5311 EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 5351 EXPECT_EQ(1, cache.disk_cache()->open_count()); | 5312 EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 5352 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 5313 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 5353 RemoveMockTransaction(&kRangeGET_TransactionOK); | 5314 RemoveMockTransaction(&kRangeGET_TransactionOK); |
| 5354 } | 5315 } |
| 5355 | 5316 |
| 5356 // A slight variation of the previous test, this time we cancel two requests in | 5317 // A slight variation of the previous test, this time we cancel two requests in |
| 5357 // a row, making sure that the second is waiting for the entry to be ready. | 5318 // a row, making sure that the second is waiting for the entry to be ready. |
| 5358 TEST(HttpCache, RangeGET_Cancel3) { | 5319 TEST(HttpCache, RangeGET_Cancel3) { |
| 5359 MockHttpCache cache; | 5320 MockHttpCache cache; |
| 5360 AddMockTransaction(&kRangeGET_TransactionOK); | 5321 AddMockTransaction(&kRangeGET_TransactionOK); |
| 5361 | 5322 |
| 5362 RunTransactionTest(cache.http_cache(), kRangeGET_TransactionOK); | 5323 RunTransactionTest(cache.http_cache(), kRangeGET_TransactionOK); |
| 5363 MockHttpRequest request(kRangeGET_TransactionOK); | 5324 MockHttpRequest request(kRangeGET_TransactionOK); |
| 5364 request.load_flags |= LOAD_VALIDATE_CACHE; | 5325 request.load_flags |= LOAD_VALIDATE_CACHE; |
| 5365 | 5326 |
| 5366 Context* c = new Context(); | 5327 auto c = base::MakeUnique<Context>(); |
| 5367 int rv = cache.CreateTransaction(&c->trans); | 5328 int rv = cache.CreateTransaction(&c->trans); |
| 5368 ASSERT_THAT(rv, IsOk()); | 5329 ASSERT_THAT(rv, IsOk()); |
| 5369 | 5330 |
| 5370 rv = c->trans->Start(&request, c->callback.callback(), NetLogWithSource()); | 5331 rv = c->trans->Start(&request, c->callback.callback(), NetLogWithSource()); |
| 5371 EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); | 5332 EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 5372 rv = c->callback.WaitForResult(); | 5333 rv = c->callback.WaitForResult(); |
| 5373 | 5334 |
| 5374 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | 5335 EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 5375 EXPECT_EQ(1, cache.disk_cache()->open_count()); | 5336 EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 5376 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 5337 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 5377 | 5338 |
| 5378 // Make sure that we revalidate the entry and read from the cache (a single | 5339 // Make sure that we revalidate the entry and read from the cache (a single |
| 5379 // read will return while waiting for the network). | 5340 // read will return while waiting for the network). |
| 5380 scoped_refptr<IOBufferWithSize> buf(new IOBufferWithSize(5)); | 5341 scoped_refptr<IOBufferWithSize> buf(new IOBufferWithSize(5)); |
| 5381 rv = c->trans->Read(buf.get(), buf->size(), c->callback.callback()); | 5342 rv = c->trans->Read(buf.get(), buf->size(), c->callback.callback()); |
| 5382 EXPECT_EQ(5, c->callback.GetResult(rv)); | 5343 EXPECT_EQ(5, c->callback.GetResult(rv)); |
| 5383 rv = c->trans->Read(buf.get(), buf->size(), c->callback.callback()); | 5344 rv = c->trans->Read(buf.get(), buf->size(), c->callback.callback()); |
| 5384 EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); | 5345 EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 5385 | 5346 |
| 5386 // Destroy the transaction before completing the read. | 5347 // Destroy the previous transaction before completing the read. |
| 5387 delete c; | 5348 c.reset(); |
| 5388 | 5349 |
| 5389 // We have the read and the delete (OnProcessPendingQueue) waiting on the | 5350 // We have the read and the delete (OnProcessPendingQueue) waiting on the |
| 5390 // message loop. This means that a new transaction will just reuse the same | 5351 // message loop. This means that a new transaction will just reuse the same |
| 5391 // active entry (no open or create). | 5352 // active entry (no open or create). |
| 5392 | 5353 |
| 5393 c = new Context(); | 5354 c = base::MakeUnique<Context>(); |
| 5394 rv = cache.CreateTransaction(&c->trans); | 5355 rv = cache.CreateTransaction(&c->trans); |
| 5395 ASSERT_THAT(rv, IsOk()); | 5356 ASSERT_THAT(rv, IsOk()); |
| 5396 | 5357 |
| 5397 rv = c->trans->Start(&request, c->callback.callback(), NetLogWithSource()); | 5358 rv = c->trans->Start(&request, c->callback.callback(), NetLogWithSource()); |
| 5398 EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); | 5359 EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 5399 | 5360 |
| 5400 MockDiskEntry::IgnoreCallbacks(true); | 5361 MockDiskEntry::IgnoreCallbacks(true); |
| 5401 base::RunLoop().RunUntilIdle(); | 5362 base::RunLoop().RunUntilIdle(); |
| 5402 MockDiskEntry::IgnoreCallbacks(false); | 5363 MockDiskEntry::IgnoreCallbacks(false); |
| 5403 | 5364 |
| 5404 // The new transaction is waiting for the query range callback. | 5365 // The new transaction is waiting for the query range callback. |
| 5405 delete c; | 5366 c.reset(); |
| 5406 | 5367 |
| 5407 // And we should not crash when the callback is delivered. | 5368 // And we should not crash when the callback is delivered. |
| 5408 base::RunLoop().RunUntilIdle(); | 5369 base::RunLoop().RunUntilIdle(); |
| 5409 | 5370 |
| 5410 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | 5371 EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 5411 EXPECT_EQ(1, cache.disk_cache()->open_count()); | 5372 EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 5412 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 5373 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 5413 RemoveMockTransaction(&kRangeGET_TransactionOK); | 5374 RemoveMockTransaction(&kRangeGET_TransactionOK); |
| 5414 } | 5375 } |
| 5415 | 5376 |
| (...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 5537 disk_cache::Entry* en; | 5498 disk_cache::Entry* en; |
| 5538 ASSERT_TRUE(cache.OpenBackendEntry(kRangeGET_TransactionOK.url, &en)); | 5499 ASSERT_TRUE(cache.OpenBackendEntry(kRangeGET_TransactionOK.url, &en)); |
| 5539 en->Close(); | 5500 en->Close(); |
| 5540 | 5501 |
| 5541 RemoveMockTransaction(&kRangeGET_TransactionOK); | 5502 RemoveMockTransaction(&kRangeGET_TransactionOK); |
| 5542 } | 5503 } |
| 5543 | 5504 |
| 5544 // Tests that we don't crash with a range request if the disk cache was not | 5505 // Tests that we don't crash with a range request if the disk cache was not |
| 5545 // initialized properly. | 5506 // initialized properly. |
| 5546 TEST(HttpCache, RangeGET_NoDiskCache) { | 5507 TEST(HttpCache, RangeGET_NoDiskCache) { |
| 5547 std::unique_ptr<MockBlockingBackendFactory> factory( | 5508 auto factory = base::MakeUnique<MockBlockingBackendFactory>(); |
| 5548 new MockBlockingBackendFactory()); | |
| 5549 factory->set_fail(true); | 5509 factory->set_fail(true); |
| 5550 factory->FinishCreation(); // We'll complete synchronously. | 5510 factory->FinishCreation(); // We'll complete synchronously. |
| 5551 MockHttpCache cache(std::move(factory)); | 5511 MockHttpCache cache(std::move(factory)); |
| 5552 | 5512 |
| 5553 AddMockTransaction(&kRangeGET_TransactionOK); | 5513 AddMockTransaction(&kRangeGET_TransactionOK); |
| 5554 | 5514 |
| 5555 RunTransactionTest(cache.http_cache(), kRangeGET_TransactionOK); | 5515 RunTransactionTest(cache.http_cache(), kRangeGET_TransactionOK); |
| 5556 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 5516 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 5557 | 5517 |
| 5558 RemoveMockTransaction(&kRangeGET_TransactionOK); | 5518 RemoveMockTransaction(&kRangeGET_TransactionOK); |
| (...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 5736 EXPECT_EQ("HTTP/1.1 200 OK", response2.headers->GetStatusLine()); | 5696 EXPECT_EQ("HTTP/1.1 200 OK", response2.headers->GetStatusLine()); |
| 5737 } | 5697 } |
| 5738 | 5698 |
| 5739 // Tests that we delete an entry when the request is cancelled before starting | 5699 // Tests that we delete an entry when the request is cancelled before starting |
| 5740 // to read from the network. | 5700 // to read from the network. |
| 5741 TEST(HttpCache, DoomOnDestruction) { | 5701 TEST(HttpCache, DoomOnDestruction) { |
| 5742 MockHttpCache cache; | 5702 MockHttpCache cache; |
| 5743 | 5703 |
| 5744 MockHttpRequest request(kSimpleGET_Transaction); | 5704 MockHttpRequest request(kSimpleGET_Transaction); |
| 5745 | 5705 |
| 5746 Context* c = new Context(); | 5706 auto c = base::MakeUnique<Context>(); |
| 5747 int rv = cache.CreateTransaction(&c->trans); | 5707 int rv = cache.CreateTransaction(&c->trans); |
| 5748 ASSERT_THAT(rv, IsOk()); | 5708 ASSERT_THAT(rv, IsOk()); |
| 5749 | 5709 |
| 5750 rv = c->trans->Start(&request, c->callback.callback(), NetLogWithSource()); | 5710 rv = c->trans->Start(&request, c->callback.callback(), NetLogWithSource()); |
| 5751 if (rv == ERR_IO_PENDING) | 5711 if (rv == ERR_IO_PENDING) |
| 5752 c->result = c->callback.WaitForResult(); | 5712 c->result = c->callback.WaitForResult(); |
| 5753 | 5713 |
| 5754 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 5714 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 5755 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 5715 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 5756 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 5716 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 5757 | 5717 |
| 5758 // Destroy the transaction. We only have the headers so we should delete this | 5718 // Destroy the transaction. We only have the headers so we should delete this |
| 5759 // entry. | 5719 // entry. |
| 5760 delete c; | 5720 c.reset(); |
| 5761 | 5721 |
| 5762 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); | 5722 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); |
| 5763 | 5723 |
| 5764 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | 5724 EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 5765 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 5725 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 5766 EXPECT_EQ(2, cache.disk_cache()->create_count()); | 5726 EXPECT_EQ(2, cache.disk_cache()->create_count()); |
| 5767 } | 5727 } |
| 5768 | 5728 |
| 5769 // Tests that we delete an entry when the request is cancelled if the response | 5729 // Tests that we delete an entry when the request is cancelled if the response |
| 5770 // does not have content-length and strong validators. | 5730 // does not have content-length and strong validators. |
| 5771 TEST(HttpCache, DoomOnDestruction2) { | 5731 TEST(HttpCache, DoomOnDestruction2) { |
| 5772 MockHttpCache cache; | 5732 MockHttpCache cache; |
| 5773 | 5733 |
| 5774 MockHttpRequest request(kSimpleGET_Transaction); | 5734 MockHttpRequest request(kSimpleGET_Transaction); |
| 5775 | 5735 |
| 5776 Context* c = new Context(); | 5736 auto c = base::MakeUnique<Context>(); |
| 5777 int rv = cache.CreateTransaction(&c->trans); | 5737 int rv = cache.CreateTransaction(&c->trans); |
| 5778 ASSERT_THAT(rv, IsOk()); | 5738 ASSERT_THAT(rv, IsOk()); |
| 5779 | 5739 |
| 5780 rv = c->trans->Start(&request, c->callback.callback(), NetLogWithSource()); | 5740 rv = c->trans->Start(&request, c->callback.callback(), NetLogWithSource()); |
| 5781 if (rv == ERR_IO_PENDING) | 5741 if (rv == ERR_IO_PENDING) |
| 5782 rv = c->callback.WaitForResult(); | 5742 rv = c->callback.WaitForResult(); |
| 5783 | 5743 |
| 5784 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 5744 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 5785 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 5745 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 5786 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 5746 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 5787 | 5747 |
| 5788 // Make sure that the entry has some data stored. | 5748 // Make sure that the entry has some data stored. |
| 5789 scoped_refptr<IOBufferWithSize> buf(new IOBufferWithSize(10)); | 5749 scoped_refptr<IOBufferWithSize> buf(new IOBufferWithSize(10)); |
| 5790 rv = c->trans->Read(buf.get(), buf->size(), c->callback.callback()); | 5750 rv = c->trans->Read(buf.get(), buf->size(), c->callback.callback()); |
| 5791 if (rv == ERR_IO_PENDING) | 5751 if (rv == ERR_IO_PENDING) |
| 5792 rv = c->callback.WaitForResult(); | 5752 rv = c->callback.WaitForResult(); |
| 5793 EXPECT_EQ(buf->size(), rv); | 5753 EXPECT_EQ(buf->size(), rv); |
| 5794 | 5754 |
| 5795 // Destroy the transaction. | 5755 // Destroy the transaction. |
| 5796 delete c; | 5756 c.reset(); |
| 5797 | 5757 |
| 5798 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); | 5758 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); |
| 5799 | 5759 |
| 5800 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | 5760 EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 5801 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 5761 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 5802 EXPECT_EQ(2, cache.disk_cache()->create_count()); | 5762 EXPECT_EQ(2, cache.disk_cache()->create_count()); |
| 5803 } | 5763 } |
| 5804 | 5764 |
| 5805 // Tests that we delete an entry when the request is cancelled if the response | 5765 // Tests that we delete an entry when the request is cancelled if the response |
| 5806 // has an "Accept-Ranges: none" header. | 5766 // has an "Accept-Ranges: none" header. |
| 5807 TEST(HttpCache, DoomOnDestruction3) { | 5767 TEST(HttpCache, DoomOnDestruction3) { |
| 5808 MockHttpCache cache; | 5768 MockHttpCache cache; |
| 5809 | 5769 |
| 5810 MockTransaction transaction(kSimpleGET_Transaction); | 5770 MockTransaction transaction(kSimpleGET_Transaction); |
| 5811 transaction.response_headers = | 5771 transaction.response_headers = |
| 5812 "Last-Modified: Wed, 28 Nov 2007 00:40:09 GMT\n" | 5772 "Last-Modified: Wed, 28 Nov 2007 00:40:09 GMT\n" |
| 5813 "Content-Length: 22\n" | 5773 "Content-Length: 22\n" |
| 5814 "Accept-Ranges: none\n" | 5774 "Accept-Ranges: none\n" |
| 5815 "Etag: \"foopy\"\n"; | 5775 "Etag: \"foopy\"\n"; |
| 5816 AddMockTransaction(&transaction); | 5776 AddMockTransaction(&transaction); |
| 5817 MockHttpRequest request(transaction); | 5777 MockHttpRequest request(transaction); |
| 5818 | 5778 |
| 5819 Context* c = new Context(); | 5779 auto c = base::MakeUnique<Context>(); |
| 5820 int rv = cache.CreateTransaction(&c->trans); | 5780 int rv = cache.CreateTransaction(&c->trans); |
| 5821 ASSERT_THAT(rv, IsOk()); | 5781 ASSERT_THAT(rv, IsOk()); |
| 5822 | 5782 |
| 5823 rv = c->trans->Start(&request, c->callback.callback(), NetLogWithSource()); | 5783 rv = c->trans->Start(&request, c->callback.callback(), NetLogWithSource()); |
| 5824 if (rv == ERR_IO_PENDING) | 5784 if (rv == ERR_IO_PENDING) |
| 5825 rv = c->callback.WaitForResult(); | 5785 rv = c->callback.WaitForResult(); |
| 5826 | 5786 |
| 5827 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 5787 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 5828 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 5788 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 5829 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 5789 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 5830 | 5790 |
| 5831 // Make sure that the entry has some data stored. | 5791 // Make sure that the entry has some data stored. |
| 5832 scoped_refptr<IOBufferWithSize> buf(new IOBufferWithSize(10)); | 5792 scoped_refptr<IOBufferWithSize> buf(new IOBufferWithSize(10)); |
| 5833 rv = c->trans->Read(buf.get(), buf->size(), c->callback.callback()); | 5793 rv = c->trans->Read(buf.get(), buf->size(), c->callback.callback()); |
| 5834 if (rv == ERR_IO_PENDING) | 5794 if (rv == ERR_IO_PENDING) |
| 5835 rv = c->callback.WaitForResult(); | 5795 rv = c->callback.WaitForResult(); |
| 5836 EXPECT_EQ(buf->size(), rv); | 5796 EXPECT_EQ(buf->size(), rv); |
| 5837 | 5797 |
| 5838 // Destroy the transaction. | 5798 // Destroy the transaction. |
| 5839 delete c; | 5799 c.reset(); |
| 5840 | 5800 |
| 5841 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); | 5801 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); |
| 5842 | 5802 |
| 5843 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | 5803 EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 5844 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 5804 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 5845 EXPECT_EQ(2, cache.disk_cache()->create_count()); | 5805 EXPECT_EQ(2, cache.disk_cache()->create_count()); |
| 5846 | 5806 |
| 5847 RemoveMockTransaction(&transaction); | 5807 RemoveMockTransaction(&transaction); |
| 5848 } | 5808 } |
| 5849 | 5809 |
| 5850 // Tests that we mark an entry as incomplete when the request is cancelled. | 5810 // Tests that we mark an entry as incomplete when the request is cancelled. |
| 5851 TEST(HttpCache, SetTruncatedFlag) { | 5811 TEST(HttpCache, SetTruncatedFlag) { |
| 5852 MockHttpCache cache; | 5812 MockHttpCache cache; |
| 5853 | 5813 |
| 5854 ScopedMockTransaction transaction(kSimpleGET_Transaction); | 5814 ScopedMockTransaction transaction(kSimpleGET_Transaction); |
| 5855 transaction.response_headers = | 5815 transaction.response_headers = |
| 5856 "Last-Modified: Wed, 28 Nov 2007 00:40:09 GMT\n" | 5816 "Last-Modified: Wed, 28 Nov 2007 00:40:09 GMT\n" |
| 5857 "Content-Length: 22\n" | 5817 "Content-Length: 22\n" |
| 5858 "Etag: \"foopy\"\n"; | 5818 "Etag: \"foopy\"\n"; |
| 5859 MockHttpRequest request(transaction); | 5819 MockHttpRequest request(transaction); |
| 5860 | 5820 |
| 5861 std::unique_ptr<Context> c(new Context()); | 5821 auto c = base::MakeUnique<Context>(); |
| 5862 | 5822 |
| 5863 int rv = cache.CreateTransaction(&c->trans); | 5823 int rv = cache.CreateTransaction(&c->trans); |
| 5864 ASSERT_THAT(rv, IsOk()); | 5824 ASSERT_THAT(rv, IsOk()); |
| 5865 | 5825 |
| 5866 rv = c->trans->Start(&request, c->callback.callback(), NetLogWithSource()); | 5826 rv = c->trans->Start(&request, c->callback.callback(), NetLogWithSource()); |
| 5867 if (rv == ERR_IO_PENDING) | 5827 if (rv == ERR_IO_PENDING) |
| 5868 rv = c->callback.WaitForResult(); | 5828 rv = c->callback.WaitForResult(); |
| 5869 | 5829 |
| 5870 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 5830 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 5871 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 5831 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 5904 TEST(HttpCache, DontSetTruncatedFlag) { | 5864 TEST(HttpCache, DontSetTruncatedFlag) { |
| 5905 MockHttpCache cache; | 5865 MockHttpCache cache; |
| 5906 | 5866 |
| 5907 ScopedMockTransaction transaction(kSimpleGET_Transaction); | 5867 ScopedMockTransaction transaction(kSimpleGET_Transaction); |
| 5908 transaction.response_headers = | 5868 transaction.response_headers = |
| 5909 "Last-Modified: Wed, 28 Nov 2007 00:40:09 GMT\n" | 5869 "Last-Modified: Wed, 28 Nov 2007 00:40:09 GMT\n" |
| 5910 "Content-Length: 22\n" | 5870 "Content-Length: 22\n" |
| 5911 "Etag: \"foopy\"\n"; | 5871 "Etag: \"foopy\"\n"; |
| 5912 MockHttpRequest request(transaction); | 5872 MockHttpRequest request(transaction); |
| 5913 | 5873 |
| 5914 std::unique_ptr<Context> c(new Context()); | 5874 auto c = base::MakeUnique<Context>(); |
| 5915 int rv = cache.CreateTransaction(&c->trans); | 5875 int rv = cache.CreateTransaction(&c->trans); |
| 5916 ASSERT_THAT(rv, IsOk()); | 5876 ASSERT_THAT(rv, IsOk()); |
| 5917 | 5877 |
| 5918 rv = c->trans->Start(&request, c->callback.callback(), NetLogWithSource()); | 5878 rv = c->trans->Start(&request, c->callback.callback(), NetLogWithSource()); |
| 5919 EXPECT_THAT(c->callback.GetResult(rv), IsOk()); | 5879 EXPECT_THAT(c->callback.GetResult(rv), IsOk()); |
| 5920 | 5880 |
| 5921 // Read everything. | 5881 // Read everything. |
| 5922 scoped_refptr<IOBufferWithSize> buf(new IOBufferWithSize(22)); | 5882 scoped_refptr<IOBufferWithSize> buf(new IOBufferWithSize(22)); |
| 5923 rv = c->trans->Read(buf.get(), buf->size(), c->callback.callback()); | 5883 rv = c->trans->Read(buf.get(), buf->size(), c->callback.callback()); |
| 5924 EXPECT_EQ(buf->size(), c->callback.GetResult(rv)); | 5884 EXPECT_EQ(buf->size(), c->callback.GetResult(rv)); |
| 5925 | 5885 |
| 5926 // Destroy the transaction. | 5886 // Destroy the transaction. |
| 5927 c->trans.reset(); | 5887 c->trans.reset(); |
| 5928 | 5888 |
| 5929 // Verify that the entry is not marked as truncated. | 5889 // Verify that the entry is not marked as truncated. |
| 5930 VerifyTruncatedFlag(&cache, kSimpleGET_Transaction.url, false, 0); | 5890 VerifyTruncatedFlag(&cache, kSimpleGET_Transaction.url, false, 0); |
| 5931 } | 5891 } |
| 5932 | 5892 |
| 5933 // Tests that sparse entries don't set the truncate flag. | 5893 // Tests that sparse entries don't set the truncate flag. |
| 5934 TEST(HttpCache, RangeGET_DontTruncate) { | 5894 TEST(HttpCache, RangeGET_DontTruncate) { |
| 5935 MockHttpCache cache; | 5895 MockHttpCache cache; |
| 5936 | 5896 |
| 5937 ScopedMockTransaction transaction(kRangeGET_TransactionOK); | 5897 ScopedMockTransaction transaction(kRangeGET_TransactionOK); |
| 5938 transaction.request_headers = "Range: bytes = 0-19\r\n" EXTRA_HEADER; | 5898 transaction.request_headers = "Range: bytes = 0-19\r\n" EXTRA_HEADER; |
| 5939 | 5899 |
| 5940 std::unique_ptr<MockHttpRequest> request(new MockHttpRequest(transaction)); | 5900 auto request = base::MakeUnique<MockHttpRequest>(transaction); |
| 5941 std::unique_ptr<HttpTransaction> trans; | 5901 std::unique_ptr<HttpTransaction> trans; |
| 5942 | 5902 |
| 5943 int rv = cache.http_cache()->CreateTransaction(DEFAULT_PRIORITY, &trans); | 5903 int rv = cache.http_cache()->CreateTransaction(DEFAULT_PRIORITY, &trans); |
| 5944 EXPECT_THAT(rv, IsOk()); | 5904 EXPECT_THAT(rv, IsOk()); |
| 5945 | 5905 |
| 5946 TestCompletionCallback cb; | 5906 TestCompletionCallback cb; |
| 5947 rv = trans->Start(request.get(), cb.callback(), NetLogWithSource()); | 5907 rv = trans->Start(request.get(), cb.callback(), NetLogWithSource()); |
| 5948 EXPECT_EQ(0, cb.GetResult(rv)); | 5908 EXPECT_EQ(0, cb.GetResult(rv)); |
| 5949 | 5909 |
| 5950 scoped_refptr<IOBuffer> buf(new IOBuffer(10)); | 5910 scoped_refptr<IOBuffer> buf(new IOBuffer(10)); |
| 5951 rv = trans->Read(buf.get(), 10, cb.callback()); | 5911 rv = trans->Read(buf.get(), 10, cb.callback()); |
| 5952 EXPECT_EQ(10, cb.GetResult(rv)); | 5912 EXPECT_EQ(10, cb.GetResult(rv)); |
| 5953 | 5913 |
| 5954 // Should not trigger any DCHECK. | 5914 // Should not trigger any DCHECK. |
| 5955 trans.reset(); | 5915 trans.reset(); |
| 5956 VerifyTruncatedFlag(&cache, kRangeGET_TransactionOK.url, false, 0); | 5916 VerifyTruncatedFlag(&cache, kRangeGET_TransactionOK.url, false, 0); |
| 5957 } | 5917 } |
| 5958 | 5918 |
| 5959 // Tests that sparse entries don't set the truncate flag (when the byte range | 5919 // Tests that sparse entries don't set the truncate flag (when the byte range |
| 5960 // starts after 0). | 5920 // starts after 0). |
| 5961 TEST(HttpCache, RangeGET_DontTruncate2) { | 5921 TEST(HttpCache, RangeGET_DontTruncate2) { |
| 5962 MockHttpCache cache; | 5922 MockHttpCache cache; |
| 5963 | 5923 |
| 5964 ScopedMockTransaction transaction(kRangeGET_TransactionOK); | 5924 ScopedMockTransaction transaction(kRangeGET_TransactionOK); |
| 5965 transaction.request_headers = "Range: bytes = 30-49\r\n" EXTRA_HEADER; | 5925 transaction.request_headers = "Range: bytes = 30-49\r\n" EXTRA_HEADER; |
| 5966 | 5926 |
| 5967 std::unique_ptr<MockHttpRequest> request(new MockHttpRequest(transaction)); | 5927 auto request = base::MakeUnique<MockHttpRequest>(transaction); |
| 5968 std::unique_ptr<HttpTransaction> trans; | 5928 std::unique_ptr<HttpTransaction> trans; |
| 5969 | 5929 |
| 5970 int rv = cache.http_cache()->CreateTransaction(DEFAULT_PRIORITY, &trans); | 5930 int rv = cache.http_cache()->CreateTransaction(DEFAULT_PRIORITY, &trans); |
| 5971 EXPECT_THAT(rv, IsOk()); | 5931 EXPECT_THAT(rv, IsOk()); |
| 5972 | 5932 |
| 5973 TestCompletionCallback cb; | 5933 TestCompletionCallback cb; |
| 5974 rv = trans->Start(request.get(), cb.callback(), NetLogWithSource()); | 5934 rv = trans->Start(request.get(), cb.callback(), NetLogWithSource()); |
| 5975 EXPECT_EQ(0, cb.GetResult(rv)); | 5935 EXPECT_EQ(0, cb.GetResult(rv)); |
| 5976 | 5936 |
| 5977 scoped_refptr<IOBuffer> buf(new IOBuffer(10)); | 5937 scoped_refptr<IOBuffer> buf(new IOBuffer(10)); |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 6079 // Now make a regular request. | 6039 // Now make a regular request. |
| 6080 MockTransaction transaction(kRangeGET_TransactionOK); | 6040 MockTransaction transaction(kRangeGET_TransactionOK); |
| 6081 transaction.request_headers = EXTRA_HEADER; | 6041 transaction.request_headers = EXTRA_HEADER; |
| 6082 std::string response_headers(transaction.response_headers); | 6042 std::string response_headers(transaction.response_headers); |
| 6083 response_headers += ("Cache-Control: no-store\n"); | 6043 response_headers += ("Cache-Control: no-store\n"); |
| 6084 transaction.response_headers = response_headers.c_str(); | 6044 transaction.response_headers = response_headers.c_str(); |
| 6085 transaction.data = kFullRangeData; | 6045 transaction.data = kFullRangeData; |
| 6086 AddMockTransaction(&transaction); | 6046 AddMockTransaction(&transaction); |
| 6087 | 6047 |
| 6088 MockHttpRequest request(transaction); | 6048 MockHttpRequest request(transaction); |
| 6089 Context* c = new Context(); | 6049 auto c = base::MakeUnique<Context>(); |
| 6090 | 6050 |
| 6091 int rv = cache.CreateTransaction(&c->trans); | 6051 int rv = cache.CreateTransaction(&c->trans); |
| 6092 ASSERT_THAT(rv, IsOk()); | 6052 ASSERT_THAT(rv, IsOk()); |
| 6093 | 6053 |
| 6094 // Queue another request to this transaction. We have to start this request | 6054 // Queue another request to this transaction. We have to start this request |
| 6095 // before the first one gets the response from the server and dooms the entry, | 6055 // before the first one gets the response from the server and dooms the entry, |
| 6096 // otherwise it will just create a new entry without being queued to the first | 6056 // otherwise it will just create a new entry without being queued to the first |
| 6097 // request. | 6057 // request. |
| 6098 Context* pending = new Context(); | 6058 auto pending = base::MakeUnique<Context>(); |
| 6099 ASSERT_THAT(cache.CreateTransaction(&pending->trans), IsOk()); | 6059 ASSERT_THAT(cache.CreateTransaction(&pending->trans), IsOk()); |
| 6100 | 6060 |
| 6101 rv = c->trans->Start(&request, c->callback.callback(), NetLogWithSource()); | 6061 rv = c->trans->Start(&request, c->callback.callback(), NetLogWithSource()); |
| 6102 EXPECT_EQ(ERR_IO_PENDING, | 6062 EXPECT_EQ(ERR_IO_PENDING, |
| 6103 pending->trans->Start(&request, pending->callback.callback(), | 6063 pending->trans->Start(&request, pending->callback.callback(), |
| 6104 NetLogWithSource())); | 6064 NetLogWithSource())); |
| 6105 EXPECT_THAT(c->callback.GetResult(rv), IsOk()); | 6065 EXPECT_THAT(c->callback.GetResult(rv), IsOk()); |
| 6106 | 6066 |
| 6107 // Make sure that the entry has some data stored. | 6067 // Make sure that the entry has some data stored. |
| 6108 scoped_refptr<IOBufferWithSize> buf(new IOBufferWithSize(5)); | 6068 scoped_refptr<IOBufferWithSize> buf(new IOBufferWithSize(5)); |
| 6109 rv = c->trans->Read(buf.get(), buf->size(), c->callback.callback()); | 6069 rv = c->trans->Read(buf.get(), buf->size(), c->callback.callback()); |
| 6110 EXPECT_EQ(5, c->callback.GetResult(rv)); | 6070 EXPECT_EQ(5, c->callback.GetResult(rv)); |
| 6111 | 6071 |
| 6112 // Cancel the requests. | 6072 // Cancel the requests. |
| 6113 delete c; | 6073 c.reset(); |
| 6114 delete pending; | 6074 pending.reset(); |
| 6115 | 6075 |
| 6116 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 6076 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 6117 EXPECT_EQ(1, cache.disk_cache()->open_count()); | 6077 EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 6118 EXPECT_EQ(2, cache.disk_cache()->create_count()); | 6078 EXPECT_EQ(2, cache.disk_cache()->create_count()); |
| 6119 | 6079 |
| 6120 base::RunLoop().RunUntilIdle(); | 6080 base::RunLoop().RunUntilIdle(); |
| 6121 RemoveMockTransaction(&transaction); | 6081 RemoveMockTransaction(&transaction); |
| 6122 } | 6082 } |
| 6123 | 6083 |
| 6124 // Tests that we delete truncated entries if the server changes its mind midway. | 6084 // Tests that we delete truncated entries if the server changes its mind midway. |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 6171 "Accept-Ranges: bytes\n" | 6131 "Accept-Ranges: bytes\n" |
| 6172 "Content-Length: 80\n"); | 6132 "Content-Length: 80\n"); |
| 6173 CreateTruncatedEntry(raw_headers, &cache); | 6133 CreateTruncatedEntry(raw_headers, &cache); |
| 6174 | 6134 |
| 6175 // Now make a regular request. | 6135 // Now make a regular request. |
| 6176 std::string headers; | 6136 std::string headers; |
| 6177 MockTransaction transaction(kRangeGET_TransactionOK); | 6137 MockTransaction transaction(kRangeGET_TransactionOK); |
| 6178 transaction.request_headers = EXTRA_HEADER; | 6138 transaction.request_headers = EXTRA_HEADER; |
| 6179 transaction.data = kFullRangeData; | 6139 transaction.data = kFullRangeData; |
| 6180 | 6140 |
| 6181 std::unique_ptr<Context> c(new Context); | 6141 auto c = base::MakeUnique<Context>(); |
| 6182 int rv = cache.CreateTransaction(&c->trans); | 6142 int rv = cache.CreateTransaction(&c->trans); |
| 6183 ASSERT_THAT(rv, IsOk()); | 6143 ASSERT_THAT(rv, IsOk()); |
| 6184 | 6144 |
| 6185 MockHttpRequest request(transaction); | 6145 MockHttpRequest request(transaction); |
| 6186 rv = c->trans->Start(&request, c->callback.callback(), NetLogWithSource()); | 6146 rv = c->trans->Start(&request, c->callback.callback(), NetLogWithSource()); |
| 6187 EXPECT_THAT(c->callback.GetResult(rv), IsOk()); | 6147 EXPECT_THAT(c->callback.GetResult(rv), IsOk()); |
| 6188 | 6148 |
| 6189 // We should have checked with the server before finishing Start(). | 6149 // We should have checked with the server before finishing Start(). |
| 6190 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 6150 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 6191 EXPECT_EQ(1, cache.disk_cache()->open_count()); | 6151 EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 6206 "Content-Length: 80\n"); | 6166 "Content-Length: 80\n"); |
| 6207 CreateTruncatedEntry(raw_headers, &cache); | 6167 CreateTruncatedEntry(raw_headers, &cache); |
| 6208 | 6168 |
| 6209 // Now make a regular request. | 6169 // Now make a regular request. |
| 6210 MockTransaction transaction(kRangeGET_TransactionOK); | 6170 MockTransaction transaction(kRangeGET_TransactionOK); |
| 6211 transaction.request_headers = "X-Require-Mock-Auth: dummy\r\n" | 6171 transaction.request_headers = "X-Require-Mock-Auth: dummy\r\n" |
| 6212 EXTRA_HEADER; | 6172 EXTRA_HEADER; |
| 6213 transaction.data = kFullRangeData; | 6173 transaction.data = kFullRangeData; |
| 6214 RangeTransactionServer handler; | 6174 RangeTransactionServer handler; |
| 6215 | 6175 |
| 6216 std::unique_ptr<Context> c(new Context); | 6176 auto c = base::MakeUnique<Context>(); |
| 6217 int rv = cache.CreateTransaction(&c->trans); | 6177 int rv = cache.CreateTransaction(&c->trans); |
| 6218 ASSERT_THAT(rv, IsOk()); | 6178 ASSERT_THAT(rv, IsOk()); |
| 6219 | 6179 |
| 6220 MockHttpRequest request(transaction); | 6180 MockHttpRequest request(transaction); |
| 6221 rv = c->trans->Start(&request, c->callback.callback(), NetLogWithSource()); | 6181 rv = c->trans->Start(&request, c->callback.callback(), NetLogWithSource()); |
| 6222 EXPECT_THAT(c->callback.GetResult(rv), IsOk()); | 6182 EXPECT_THAT(c->callback.GetResult(rv), IsOk()); |
| 6223 | 6183 |
| 6224 const HttpResponseInfo* response = c->trans->GetResponseInfo(); | 6184 const HttpResponseInfo* response = c->trans->GetResponseInfo(); |
| 6225 ASSERT_TRUE(response); | 6185 ASSERT_TRUE(response); |
| 6226 ASSERT_EQ(401, response->headers->response_code()); | 6186 ASSERT_EQ(401, response->headers->response_code()); |
| (...skipping 30 matching lines...) Expand all Loading... |
| 6257 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 6217 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 6258 | 6218 |
| 6259 // And now read from the cache and the network. 10-19 will get a | 6219 // And now read from the cache and the network. 10-19 will get a |
| 6260 // 401, but will have already returned 0-9. | 6220 // 401, but will have already returned 0-9. |
| 6261 // We do not set X-Require-Mock-Auth because that causes the mock | 6221 // We do not set X-Require-Mock-Auth because that causes the mock |
| 6262 // network transaction to become IsReadyToRestartForAuth(). | 6222 // network transaction to become IsReadyToRestartForAuth(). |
| 6263 transaction.request_headers = | 6223 transaction.request_headers = |
| 6264 "Range: bytes = 0-79\r\n" | 6224 "Range: bytes = 0-79\r\n" |
| 6265 "X-Require-Mock-Auth-Alt: dummy\r\n" EXTRA_HEADER; | 6225 "X-Require-Mock-Auth-Alt: dummy\r\n" EXTRA_HEADER; |
| 6266 | 6226 |
| 6267 std::unique_ptr<Context> c(new Context); | 6227 auto c = base::MakeUnique<Context>(); |
| 6268 int rv = cache.CreateTransaction(&c->trans); | 6228 int rv = cache.CreateTransaction(&c->trans); |
| 6269 ASSERT_THAT(rv, IsOk()); | 6229 ASSERT_THAT(rv, IsOk()); |
| 6270 | 6230 |
| 6271 MockHttpRequest request(transaction); | 6231 MockHttpRequest request(transaction); |
| 6272 | 6232 |
| 6273 rv = c->trans->Start(&request, c->callback.callback(), NetLogWithSource()); | 6233 rv = c->trans->Start(&request, c->callback.callback(), NetLogWithSource()); |
| 6274 if (rv == ERR_IO_PENDING) | 6234 if (rv == ERR_IO_PENDING) |
| 6275 rv = c->callback.WaitForResult(); | 6235 rv = c->callback.WaitForResult(); |
| 6276 std::string content; | 6236 std::string content; |
| 6277 rv = ReadTransaction(c->trans.get(), &content); | 6237 rv = ReadTransaction(c->trans.get(), &content); |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 6316 "Last-Modified: Sat, 18 Apr 2009 01:10:43 GMT\n" | 6276 "Last-Modified: Sat, 18 Apr 2009 01:10:43 GMT\n" |
| 6317 "ETag: \"foo\"\n" | 6277 "ETag: \"foo\"\n" |
| 6318 "Accept-Ranges: bytes\n" | 6278 "Accept-Ranges: bytes\n" |
| 6319 "Content-Length: 80\n"); | 6279 "Content-Length: 80\n"); |
| 6320 CreateTruncatedEntry(raw_headers, &cache); | 6280 CreateTruncatedEntry(raw_headers, &cache); |
| 6321 | 6281 |
| 6322 // Now make a regular request. | 6282 // Now make a regular request. |
| 6323 transaction.request_headers = EXTRA_HEADER; | 6283 transaction.request_headers = EXTRA_HEADER; |
| 6324 | 6284 |
| 6325 MockHttpRequest request(transaction); | 6285 MockHttpRequest request(transaction); |
| 6326 Context* c = new Context(); | 6286 auto c = base::MakeUnique<Context>(); |
| 6327 int rv = cache.CreateTransaction(&c->trans); | 6287 int rv = cache.CreateTransaction(&c->trans); |
| 6328 ASSERT_THAT(rv, IsOk()); | 6288 ASSERT_THAT(rv, IsOk()); |
| 6329 | 6289 |
| 6330 rv = c->trans->Start(&request, c->callback.callback(), NetLogWithSource()); | 6290 rv = c->trans->Start(&request, c->callback.callback(), NetLogWithSource()); |
| 6331 EXPECT_THAT(c->callback.GetResult(rv), IsOk()); | 6291 EXPECT_THAT(c->callback.GetResult(rv), IsOk()); |
| 6332 | 6292 |
| 6333 // Read 20 bytes from the cache, and 10 from the net. | 6293 // Read 20 bytes from the cache, and 10 from the net. |
| 6334 scoped_refptr<IOBuffer> buf(new IOBuffer(100)); | 6294 scoped_refptr<IOBuffer> buf(new IOBuffer(100)); |
| 6335 rv = c->trans->Read(buf.get(), 20, c->callback.callback()); | 6295 rv = c->trans->Read(buf.get(), 20, c->callback.callback()); |
| 6336 EXPECT_EQ(20, c->callback.GetResult(rv)); | 6296 EXPECT_EQ(20, c->callback.GetResult(rv)); |
| 6337 rv = c->trans->Read(buf.get(), 10, c->callback.callback()); | 6297 rv = c->trans->Read(buf.get(), 10, c->callback.callback()); |
| 6338 EXPECT_EQ(10, c->callback.GetResult(rv)); | 6298 EXPECT_EQ(10, c->callback.GetResult(rv)); |
| 6339 | 6299 |
| 6340 // At this point, we are already reading so canceling the request should leave | 6300 // At this point, we are already reading so canceling the request should leave |
| 6341 // a truncated one. | 6301 // a truncated one. |
| 6342 delete c; | 6302 c.reset(); |
| 6343 | 6303 |
| 6344 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | 6304 EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 6345 EXPECT_EQ(1, cache.disk_cache()->open_count()); | 6305 EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 6346 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 6306 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 6347 | 6307 |
| 6348 // Verify that the disk entry was updated: now we have 30 bytes. | 6308 // Verify that the disk entry was updated: now we have 30 bytes. |
| 6349 VerifyTruncatedFlag(&cache, kRangeGET_TransactionOK.url, true, 30); | 6309 VerifyTruncatedFlag(&cache, kRangeGET_TransactionOK.url, true, 30); |
| 6350 } | 6310 } |
| 6351 | 6311 |
| 6352 // Tests that we can handle range requests when we have a truncated entry. | 6312 // Tests that we can handle range requests when we have a truncated entry. |
| (...skipping 309 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 6662 ASSERT_THAT(cache.CreateTransaction(&trans), IsOk()); | 6622 ASSERT_THAT(cache.CreateTransaction(&trans), IsOk()); |
| 6663 | 6623 |
| 6664 int rv = trans->Start(&request, callback.callback(), NetLogWithSource()); | 6624 int rv = trans->Start(&request, callback.callback(), NetLogWithSource()); |
| 6665 if (rv == ERR_IO_PENDING) | 6625 if (rv == ERR_IO_PENDING) |
| 6666 rv = callback.WaitForResult(); | 6626 rv = callback.WaitForResult(); |
| 6667 ASSERT_THAT(rv, IsError(ERR_CACHE_MISS)); | 6627 ASSERT_THAT(rv, IsError(ERR_CACHE_MISS)); |
| 6668 } | 6628 } |
| 6669 | 6629 |
| 6670 // Ensure that we don't crash by if left-behind transactions. | 6630 // Ensure that we don't crash by if left-behind transactions. |
| 6671 TEST(HttpCache, OutlivedTransactions) { | 6631 TEST(HttpCache, OutlivedTransactions) { |
| 6672 MockHttpCache* cache = new MockHttpCache; | 6632 auto cache = base::MakeUnique<MockHttpCache>(); |
| 6673 | 6633 |
| 6674 std::unique_ptr<HttpTransaction> trans; | 6634 std::unique_ptr<HttpTransaction> trans; |
| 6675 EXPECT_THAT(cache->CreateTransaction(&trans), IsOk()); | 6635 EXPECT_THAT(cache->CreateTransaction(&trans), IsOk()); |
| 6676 | 6636 |
| 6677 delete cache; | 6637 cache.reset(); |
| 6678 trans.reset(); | 6638 trans.reset(); |
| 6679 } | 6639 } |
| 6680 | 6640 |
| 6681 // Test that the disabled mode works. | 6641 // Test that the disabled mode works. |
| 6682 TEST(HttpCache, CacheDisabledMode) { | 6642 TEST(HttpCache, CacheDisabledMode) { |
| 6683 MockHttpCache cache; | 6643 MockHttpCache cache; |
| 6684 | 6644 |
| 6685 // write to the cache | 6645 // write to the cache |
| 6686 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); | 6646 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); |
| 6687 | 6647 |
| (...skipping 1286 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 7974 // Makes sure that a request stops using the cache when the response headers | 7934 // Makes sure that a request stops using the cache when the response headers |
| 7975 // with "Cache-Control: no-store" arrives. That means that another request for | 7935 // with "Cache-Control: no-store" arrives. That means that another request for |
| 7976 // the same URL can be processed before the response body of the original | 7936 // the same URL can be processed before the response body of the original |
| 7977 // request arrives. | 7937 // request arrives. |
| 7978 TEST(HttpCache, NoStoreResponseShouldNotBlockFollowingRequests) { | 7938 TEST(HttpCache, NoStoreResponseShouldNotBlockFollowingRequests) { |
| 7979 MockHttpCache cache; | 7939 MockHttpCache cache; |
| 7980 ScopedMockTransaction mock_transaction(kSimpleGET_Transaction); | 7940 ScopedMockTransaction mock_transaction(kSimpleGET_Transaction); |
| 7981 mock_transaction.response_headers = "Cache-Control: no-store\n"; | 7941 mock_transaction.response_headers = "Cache-Control: no-store\n"; |
| 7982 MockHttpRequest request(mock_transaction); | 7942 MockHttpRequest request(mock_transaction); |
| 7983 | 7943 |
| 7984 std::unique_ptr<Context> first(new Context); | 7944 auto first = base::MakeUnique<Context>(); |
| 7985 first->result = cache.CreateTransaction(&first->trans); | 7945 first->result = cache.CreateTransaction(&first->trans); |
| 7986 ASSERT_THAT(first->result, IsOk()); | 7946 ASSERT_THAT(first->result, IsOk()); |
| 7987 EXPECT_EQ(LOAD_STATE_IDLE, first->trans->GetLoadState()); | 7947 EXPECT_EQ(LOAD_STATE_IDLE, first->trans->GetLoadState()); |
| 7988 first->result = first->trans->Start(&request, first->callback.callback(), | 7948 first->result = first->trans->Start(&request, first->callback.callback(), |
| 7989 NetLogWithSource()); | 7949 NetLogWithSource()); |
| 7990 EXPECT_EQ(LOAD_STATE_WAITING_FOR_CACHE, first->trans->GetLoadState()); | 7950 EXPECT_EQ(LOAD_STATE_WAITING_FOR_CACHE, first->trans->GetLoadState()); |
| 7991 | 7951 |
| 7992 base::RunLoop().RunUntilIdle(); | 7952 base::RunLoop().RunUntilIdle(); |
| 7993 EXPECT_EQ(LOAD_STATE_IDLE, first->trans->GetLoadState()); | 7953 EXPECT_EQ(LOAD_STATE_IDLE, first->trans->GetLoadState()); |
| 7994 ASSERT_TRUE(first->trans->GetResponseInfo()); | 7954 ASSERT_TRUE(first->trans->GetResponseInfo()); |
| 7995 EXPECT_TRUE(first->trans->GetResponseInfo()->headers->HasHeaderValue( | 7955 EXPECT_TRUE(first->trans->GetResponseInfo()->headers->HasHeaderValue( |
| 7996 "Cache-Control", "no-store")); | 7956 "Cache-Control", "no-store")); |
| 7997 // Here we have read the response header but not read the response body yet. | 7957 // Here we have read the response header but not read the response body yet. |
| 7998 | 7958 |
| 7999 // Let us create the second (read) transaction. | 7959 // Let us create the second (read) transaction. |
| 8000 std::unique_ptr<Context> second(new Context); | 7960 auto second = base::MakeUnique<Context>(); |
| 8001 second->result = cache.CreateTransaction(&second->trans); | 7961 second->result = cache.CreateTransaction(&second->trans); |
| 8002 ASSERT_THAT(second->result, IsOk()); | 7962 ASSERT_THAT(second->result, IsOk()); |
| 8003 EXPECT_EQ(LOAD_STATE_IDLE, second->trans->GetLoadState()); | 7963 EXPECT_EQ(LOAD_STATE_IDLE, second->trans->GetLoadState()); |
| 8004 second->result = second->trans->Start(&request, second->callback.callback(), | 7964 second->result = second->trans->Start(&request, second->callback.callback(), |
| 8005 NetLogWithSource()); | 7965 NetLogWithSource()); |
| 8006 | 7966 |
| 8007 // Here the second transaction proceeds without reading the first body. | 7967 // Here the second transaction proceeds without reading the first body. |
| 8008 EXPECT_EQ(LOAD_STATE_WAITING_FOR_CACHE, second->trans->GetLoadState()); | 7968 EXPECT_EQ(LOAD_STATE_WAITING_FOR_CACHE, second->trans->GetLoadState()); |
| 8009 base::RunLoop().RunUntilIdle(); | 7969 base::RunLoop().RunUntilIdle(); |
| 8010 EXPECT_EQ(LOAD_STATE_IDLE, second->trans->GetLoadState()); | 7970 EXPECT_EQ(LOAD_STATE_IDLE, second->trans->GetLoadState()); |
| (...skipping 217 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 8228 HttpResponseInfo response_info; | 8188 HttpResponseInfo response_info; |
| 8229 RunTransactionTestWithResponseInfo(cache.http_cache(), | 8189 RunTransactionTestWithResponseInfo(cache.http_cache(), |
| 8230 kTypicalGET_Transaction, &response_info); | 8190 kTypicalGET_Transaction, &response_info); |
| 8231 | 8191 |
| 8232 EXPECT_FALSE(response_info.was_cached); | 8192 EXPECT_FALSE(response_info.was_cached); |
| 8233 EXPECT_TRUE(response_info.network_accessed); | 8193 EXPECT_TRUE(response_info.network_accessed); |
| 8234 EXPECT_EQ(CacheEntryStatus::ENTRY_CANT_CONDITIONALIZE, | 8194 EXPECT_EQ(CacheEntryStatus::ENTRY_CANT_CONDITIONALIZE, |
| 8235 response_info.cache_entry_status); | 8195 response_info.cache_entry_status); |
| 8236 | 8196 |
| 8237 base::trace_event::MemoryDumpArgs dump_args = {GetParam()}; | 8197 base::trace_event::MemoryDumpArgs dump_args = {GetParam()}; |
| 8238 std::unique_ptr<base::trace_event::ProcessMemoryDump> process_memory_dump( | 8198 auto process_memory_dump = |
| 8239 new base::trace_event::ProcessMemoryDump(nullptr, dump_args)); | 8199 base::MakeUnique<base::trace_event::ProcessMemoryDump>(nullptr, |
| 8200 dump_args); |
| 8240 base::trace_event::MemoryAllocatorDump* parent_dump = | 8201 base::trace_event::MemoryAllocatorDump* parent_dump = |
| 8241 process_memory_dump->CreateAllocatorDump( | 8202 process_memory_dump->CreateAllocatorDump( |
| 8242 "net/url_request_context/main/0x123"); | 8203 "net/url_request_context/main/0x123"); |
| 8243 cache.http_cache()->DumpMemoryStats(process_memory_dump.get(), | 8204 cache.http_cache()->DumpMemoryStats(process_memory_dump.get(), |
| 8244 parent_dump->absolute_name()); | 8205 parent_dump->absolute_name()); |
| 8245 | 8206 |
| 8246 const base::trace_event::MemoryAllocatorDump* dump = | 8207 const base::trace_event::MemoryAllocatorDump* dump = |
| 8247 process_memory_dump->GetAllocatorDump( | 8208 process_memory_dump->GetAllocatorDump( |
| 8248 "net/url_request_context/main/0x123/http_cache"); | 8209 "net/url_request_context/main/0x123/http_cache"); |
| 8249 ASSERT_NE(nullptr, dump); | 8210 ASSERT_NE(nullptr, dump); |
| 8250 std::unique_ptr<base::Value> raw_attrs = | 8211 std::unique_ptr<base::Value> raw_attrs = |
| 8251 dump->attributes_for_testing()->ToBaseValue(); | 8212 dump->attributes_for_testing()->ToBaseValue(); |
| 8252 base::DictionaryValue* attrs; | 8213 base::DictionaryValue* attrs; |
| 8253 ASSERT_TRUE(raw_attrs->GetAsDictionary(&attrs)); | 8214 ASSERT_TRUE(raw_attrs->GetAsDictionary(&attrs)); |
| 8254 base::DictionaryValue* size_attrs; | 8215 base::DictionaryValue* size_attrs; |
| 8255 ASSERT_TRUE(attrs->GetDictionary( | 8216 ASSERT_TRUE(attrs->GetDictionary( |
| 8256 base::trace_event::MemoryAllocatorDump::kNameSize, &size_attrs)); | 8217 base::trace_event::MemoryAllocatorDump::kNameSize, &size_attrs)); |
| 8257 std::string size; | 8218 std::string size; |
| 8258 ASSERT_TRUE(size_attrs->GetString("value", &size)); | 8219 ASSERT_TRUE(size_attrs->GetString("value", &size)); |
| 8259 int actual_size = 0; | 8220 int actual_size = 0; |
| 8260 ASSERT_TRUE(base::HexStringToInt(size, &actual_size)); | 8221 ASSERT_TRUE(base::HexStringToInt(size, &actual_size)); |
| 8261 ASSERT_LT(0, actual_size); | 8222 ASSERT_LT(0, actual_size); |
| 8262 } | 8223 } |
| 8263 | 8224 |
| 8264 } // namespace net | 8225 } // namespace net |
| OLD | NEW |