| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "content/browser/cache_storage/cache_storage_cache.h" | 5 #include "content/browser/cache_storage/cache_storage_cache.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <memory> | 10 #include <memory> |
| (...skipping 510 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 521 | 521 |
| 522 return callback_error_ == CACHE_STORAGE_OK; | 522 return callback_error_ == CACHE_STORAGE_OK; |
| 523 } | 523 } |
| 524 | 524 |
| 525 int64_t Size() { | 525 int64_t Size() { |
| 526 // Storage notification happens after an operation completes. Let the any | 526 // Storage notification happens after an operation completes. Let the any |
| 527 // notifications complete before calling Size. | 527 // notifications complete before calling Size. |
| 528 base::RunLoop().RunUntilIdle(); | 528 base::RunLoop().RunUntilIdle(); |
| 529 | 529 |
| 530 base::RunLoop run_loop; | 530 base::RunLoop run_loop; |
| 531 bool callback_called = false; |
| 531 cache_->Size(base::Bind(&CacheStorageCacheTest::SizeCallback, | 532 cache_->Size(base::Bind(&CacheStorageCacheTest::SizeCallback, |
| 532 base::Unretained(this), &run_loop)); | 533 base::Unretained(this), &run_loop, |
| 534 &callback_called)); |
| 533 run_loop.Run(); | 535 run_loop.Run(); |
| 536 EXPECT_TRUE(callback_called); |
| 534 return callback_size_; | 537 return callback_size_; |
| 535 } | 538 } |
| 536 | 539 |
| 537 int64_t GetSizeThenClose() { | 540 int64_t GetSizeThenClose() { |
| 538 base::RunLoop run_loop; | 541 base::RunLoop run_loop; |
| 542 bool callback_called = false; |
| 539 cache_->GetSizeThenClose(base::Bind(&CacheStorageCacheTest::SizeCallback, | 543 cache_->GetSizeThenClose(base::Bind(&CacheStorageCacheTest::SizeCallback, |
| 540 base::Unretained(this), &run_loop)); | 544 base::Unretained(this), &run_loop, |
| 545 &callback_called)); |
| 541 run_loop.Run(); | 546 run_loop.Run(); |
| 547 EXPECT_TRUE(callback_called); |
| 542 return callback_size_; | 548 return callback_size_; |
| 543 } | 549 } |
| 544 | 550 |
| 545 void RequestsCallback(base::RunLoop* run_loop, | 551 void RequestsCallback(base::RunLoop* run_loop, |
| 546 CacheStorageError error, | 552 CacheStorageError error, |
| 547 std::unique_ptr<CacheStorageCache::Requests> requests) { | 553 std::unique_ptr<CacheStorageCache::Requests> requests) { |
| 548 callback_error_ = error; | 554 callback_error_ = error; |
| 549 callback_strings_.clear(); | 555 callback_strings_.clear(); |
| 550 if (requests) { | 556 if (requests) { |
| 551 for (size_t i = 0u; i < requests->size(); ++i) | 557 for (size_t i = 0u; i < requests->size(); ++i) |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 599 quit_closure.Run(); | 605 quit_closure.Run(); |
| 600 } | 606 } |
| 601 | 607 |
| 602 void CloseCallback(base::RunLoop* run_loop) { | 608 void CloseCallback(base::RunLoop* run_loop) { |
| 603 EXPECT_FALSE(callback_closed_); | 609 EXPECT_FALSE(callback_closed_); |
| 604 callback_closed_ = true; | 610 callback_closed_ = true; |
| 605 if (run_loop) | 611 if (run_loop) |
| 606 run_loop->Quit(); | 612 run_loop->Quit(); |
| 607 } | 613 } |
| 608 | 614 |
| 609 void SizeCallback(base::RunLoop* run_loop, int64_t size) { | 615 void SizeCallback(base::RunLoop* run_loop, |
| 616 bool* callback_called, |
| 617 int64_t size) { |
| 618 *callback_called = true; |
| 610 callback_size_ = size; | 619 callback_size_ = size; |
| 611 if (run_loop) | 620 if (run_loop) |
| 612 run_loop->Quit(); | 621 run_loop->Quit(); |
| 613 } | 622 } |
| 614 | 623 |
| 615 bool VerifyKeys(const std::vector<std::string>& expected_keys) { | 624 bool VerifyKeys(const std::vector<std::string>& expected_keys) { |
| 616 if (expected_keys.size() != callback_strings_.size()) | 625 if (expected_keys.size() != callback_strings_.size()) |
| 617 return false; | 626 return false; |
| 618 | 627 |
| 619 std::set<std::string> found_set; | 628 std::set<std::string> found_set; |
| (...skipping 663 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1283 EXPECT_TRUE(Delete(no_body_request_)); | 1292 EXPECT_TRUE(Delete(no_body_request_)); |
| 1284 EXPECT_EQ(0, Size()); | 1293 EXPECT_EQ(0, Size()); |
| 1285 | 1294 |
| 1286 EXPECT_TRUE(Put(body_request_, body_response_)); | 1295 EXPECT_TRUE(Put(body_request_, body_response_)); |
| 1287 EXPECT_LT(no_body_size, Size()); | 1296 EXPECT_LT(no_body_size, Size()); |
| 1288 | 1297 |
| 1289 EXPECT_TRUE(Delete(body_request_)); | 1298 EXPECT_TRUE(Delete(body_request_)); |
| 1290 EXPECT_EQ(0, Size()); | 1299 EXPECT_EQ(0, Size()); |
| 1291 } | 1300 } |
| 1292 | 1301 |
| 1302 TEST_P(CacheStorageCacheTestP, SizeOperationsArePrioritized) { |
| 1303 // Test that pending size operations (those waiting for initialization) run |
| 1304 // before other scheduler operations. |
| 1305 cache_->set_delay_backend_creation(true); // Delay cache initialization |
| 1306 |
| 1307 CacheStorageBatchOperation operation; |
| 1308 operation.operation_type = CACHE_STORAGE_CACHE_OPERATION_TYPE_PUT; |
| 1309 operation.request = body_request_; |
| 1310 operation.response = body_response_; |
| 1311 |
| 1312 callback_error_ = CACHE_STORAGE_ERROR_NOT_FOUND; |
| 1313 base::RunLoop run_loop; |
| 1314 // Start a put operation that blocks on initialization. |
| 1315 cache_->BatchOperation(std::vector<CacheStorageBatchOperation>(1, operation), |
| 1316 base::Bind(&CacheStorageCacheTest::ErrorTypeCallback, |
| 1317 base::Unretained(this), &run_loop)); |
| 1318 |
| 1319 // Next start a size operation that also blocks on initialization. |
| 1320 bool size_callback_called = false; |
| 1321 cache_->Size(base::Bind(&CacheStorageCacheTest::SizeCallback, |
| 1322 base::Unretained(this), nullptr, |
| 1323 &size_callback_called)); |
| 1324 |
| 1325 base::RunLoop().RunUntilIdle(); |
| 1326 EXPECT_FALSE(size_callback_called); |
| 1327 EXPECT_EQ(CACHE_STORAGE_ERROR_NOT_FOUND, callback_error_); |
| 1328 |
| 1329 // Finish initialization. The Size operation should complete before Put gets |
| 1330 // to run as Size has priority. See crbug.com/605663. |
| 1331 cache_->ContinueCreateBackend(); |
| 1332 run_loop.Run(); |
| 1333 EXPECT_TRUE(size_callback_called); |
| 1334 EXPECT_EQ(CACHE_STORAGE_OK, callback_error_); |
| 1335 } |
| 1336 |
| 1293 TEST_P(CacheStorageCacheTestP, GetSizeThenClose) { | 1337 TEST_P(CacheStorageCacheTestP, GetSizeThenClose) { |
| 1294 EXPECT_TRUE(Put(body_request_, body_response_)); | 1338 EXPECT_TRUE(Put(body_request_, body_response_)); |
| 1295 int64_t cache_size = Size(); | 1339 int64_t cache_size = Size(); |
| 1296 EXPECT_EQ(cache_size, GetSizeThenClose()); | 1340 EXPECT_EQ(cache_size, GetSizeThenClose()); |
| 1297 VerifyAllOpsFail(); | 1341 VerifyAllOpsFail(); |
| 1298 } | 1342 } |
| 1299 | 1343 |
| 1300 TEST_P(CacheStorageCacheTestP, OpsFailOnClosedBackendNeverCreated) { | 1344 TEST_P(CacheStorageCacheTestP, OpsFailOnClosedBackendNeverCreated) { |
| 1301 cache_->set_delay_backend_creation( | 1345 cache_->set_delay_backend_creation( |
| 1302 true); // Will hang the test if a backend is created. | 1346 true); // Will hang the test if a backend is created. |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1355 EXPECT_EQ(1, sequence_out); | 1399 EXPECT_EQ(1, sequence_out); |
| 1356 close_loop2->Run(); | 1400 close_loop2->Run(); |
| 1357 EXPECT_EQ(2, sequence_out); | 1401 EXPECT_EQ(2, sequence_out); |
| 1358 } | 1402 } |
| 1359 | 1403 |
| 1360 INSTANTIATE_TEST_CASE_P(CacheStorageCacheTest, | 1404 INSTANTIATE_TEST_CASE_P(CacheStorageCacheTest, |
| 1361 CacheStorageCacheTestP, | 1405 CacheStorageCacheTestP, |
| 1362 ::testing::Values(false, true)); | 1406 ::testing::Values(false, true)); |
| 1363 | 1407 |
| 1364 } // namespace content | 1408 } // namespace content |
| OLD | NEW |