| 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 <stdint.h> | 5 #include <stdint.h> |
| 6 | 6 |
| 7 #include "base/files/file.h" | 7 #include "base/files/file.h" |
| 8 #include "base/files/file_util.h" | 8 #include "base/files/file_util.h" |
| 9 #include "base/metrics/field_trial.h" | 9 #include "base/metrics/field_trial.h" |
| 10 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
| (...skipping 463 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 474 rv = disk_cache::CreateCacheBackend(net::MEMORY_CACHE, | 474 rv = disk_cache::CreateCacheBackend(net::MEMORY_CACHE, |
| 475 net::CACHE_BACKEND_DEFAULT, | 475 net::CACHE_BACKEND_DEFAULT, |
| 476 base::FilePath(), 0, | 476 base::FilePath(), 0, |
| 477 false, NULL, NULL, &cache, | 477 false, NULL, NULL, &cache, |
| 478 cb.callback()); | 478 cb.callback()); |
| 479 ASSERT_EQ(net::OK, cb.GetResult(rv)); | 479 ASSERT_EQ(net::OK, cb.GetResult(rv)); |
| 480 ASSERT_TRUE(cache.get()); | 480 ASSERT_TRUE(cache.get()); |
| 481 cache.reset(); | 481 cache.reset(); |
| 482 } | 482 } |
| 483 | 483 |
| 484 base::MessageLoop::current()->RunUntilIdle(); | 484 base::RunLoop().RunUntilIdle(); |
| 485 } | 485 } |
| 486 | 486 |
| 487 // Tests that |BackendImpl| fails to initialize with a missing file. | 487 // Tests that |BackendImpl| fails to initialize with a missing file. |
| 488 TEST_F(DiskCacheBackendTest, CreateBackend_MissingFile) { | 488 TEST_F(DiskCacheBackendTest, CreateBackend_MissingFile) { |
| 489 ASSERT_TRUE(CopyTestCache("bad_entry")); | 489 ASSERT_TRUE(CopyTestCache("bad_entry")); |
| 490 base::FilePath filename = cache_path_.AppendASCII("data_1"); | 490 base::FilePath filename = cache_path_.AppendASCII("data_1"); |
| 491 base::DeleteFile(filename, false); | 491 base::DeleteFile(filename, false); |
| 492 base::Thread cache_thread("CacheThread"); | 492 base::Thread cache_thread("CacheThread"); |
| 493 ASSERT_TRUE(cache_thread.StartWithOptions( | 493 ASSERT_TRUE(cache_thread.StartWithOptions( |
| 494 base::Thread::Options(base::MessageLoop::TYPE_IO, 0))); | 494 base::Thread::Options(base::MessageLoop::TYPE_IO, 0))); |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 543 // The cache destructor will see one pending operation here. | 543 // The cache destructor will see one pending operation here. |
| 544 cache_.reset(); | 544 cache_.reset(); |
| 545 | 545 |
| 546 if (rv == net::ERR_IO_PENDING) { | 546 if (rv == net::ERR_IO_PENDING) { |
| 547 if (fast || simple_cache_mode_) | 547 if (fast || simple_cache_mode_) |
| 548 EXPECT_FALSE(cb.have_result()); | 548 EXPECT_FALSE(cb.have_result()); |
| 549 else | 549 else |
| 550 EXPECT_TRUE(cb.have_result()); | 550 EXPECT_TRUE(cb.have_result()); |
| 551 } | 551 } |
| 552 | 552 |
| 553 base::MessageLoop::current()->RunUntilIdle(); | 553 base::RunLoop().RunUntilIdle(); |
| 554 | 554 |
| 555 #if !defined(OS_IOS) | 555 #if !defined(OS_IOS) |
| 556 // Wait for the actual operation to complete, or we'll keep a file handle that | 556 // Wait for the actual operation to complete, or we'll keep a file handle that |
| 557 // may cause issues later. Note that on iOS systems even though this test | 557 // may cause issues later. Note that on iOS systems even though this test |
| 558 // uses a single thread, the actual IO is posted to a worker thread and the | 558 // uses a single thread, the actual IO is posted to a worker thread and the |
| 559 // cache destructor breaks the link to reach cb when the operation completes. | 559 // cache destructor breaks the link to reach cb when the operation completes. |
| 560 rv = cb.GetResult(rv); | 560 rv = cb.GetResult(rv); |
| 561 #endif | 561 #endif |
| 562 } | 562 } |
| 563 | 563 |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 607 | 607 |
| 608 CreateBackend(disk_cache::kNoBuffering, NULL); | 608 CreateBackend(disk_cache::kNoBuffering, NULL); |
| 609 rv = GeneratePendingIO(&cb); | 609 rv = GeneratePendingIO(&cb); |
| 610 | 610 |
| 611 // cache_ has a pending operation, and extra_cache will go away. | 611 // cache_ has a pending operation, and extra_cache will go away. |
| 612 extra_cache.reset(); | 612 extra_cache.reset(); |
| 613 | 613 |
| 614 if (rv == net::ERR_IO_PENDING) | 614 if (rv == net::ERR_IO_PENDING) |
| 615 EXPECT_FALSE(cb.have_result()); | 615 EXPECT_FALSE(cb.have_result()); |
| 616 | 616 |
| 617 base::MessageLoop::current()->RunUntilIdle(); | 617 base::RunLoop().RunUntilIdle(); |
| 618 | 618 |
| 619 // Wait for the actual operation to complete, or we'll keep a file handle that | 619 // Wait for the actual operation to complete, or we'll keep a file handle that |
| 620 // may cause issues later. | 620 // may cause issues later. |
| 621 rv = cb.GetResult(rv); | 621 rv = cb.GetResult(rv); |
| 622 } | 622 } |
| 623 #endif | 623 #endif |
| 624 | 624 |
| 625 // Tests that we deal with background-thread pending operations. | 625 // Tests that we deal with background-thread pending operations. |
| 626 void DiskCacheBackendTest::BackendShutdownWithPendingIO(bool fast) { | 626 void DiskCacheBackendTest::BackendShutdownWithPendingIO(bool fast) { |
| 627 net::TestCompletionCallback cb; | 627 net::TestCompletionCallback cb; |
| (...skipping 13 matching lines...) Expand all Loading... |
| 641 disk_cache::Entry* entry; | 641 disk_cache::Entry* entry; |
| 642 int rv = cache_->CreateEntry("some key", &entry, cb.callback()); | 642 int rv = cache_->CreateEntry("some key", &entry, cb.callback()); |
| 643 ASSERT_EQ(net::OK, cb.GetResult(rv)); | 643 ASSERT_EQ(net::OK, cb.GetResult(rv)); |
| 644 | 644 |
| 645 entry->Close(); | 645 entry->Close(); |
| 646 | 646 |
| 647 // The cache destructor will see one pending operation here. | 647 // The cache destructor will see one pending operation here. |
| 648 cache_.reset(); | 648 cache_.reset(); |
| 649 } | 649 } |
| 650 | 650 |
| 651 base::MessageLoop::current()->RunUntilIdle(); | 651 base::RunLoop().RunUntilIdle(); |
| 652 EXPECT_FALSE(cb.have_result()); | 652 EXPECT_FALSE(cb.have_result()); |
| 653 } | 653 } |
| 654 | 654 |
| 655 TEST_F(DiskCacheBackendTest, ShutdownWithPendingIO) { | 655 TEST_F(DiskCacheBackendTest, ShutdownWithPendingIO) { |
| 656 BackendShutdownWithPendingIO(false); | 656 BackendShutdownWithPendingIO(false); |
| 657 } | 657 } |
| 658 | 658 |
| 659 #if !defined(LEAK_SANITIZER) | 659 #if !defined(LEAK_SANITIZER) |
| 660 // We'll be leaking from this test. | 660 // We'll be leaking from this test. |
| 661 TEST_F(DiskCacheBackendTest, ShutdownWithPendingIO_Fast) { | 661 TEST_F(DiskCacheBackendTest, ShutdownWithPendingIO_Fast) { |
| (...skipping 19 matching lines...) Expand all Loading... |
| 681 CreateBackend(flags, &cache_thread); | 681 CreateBackend(flags, &cache_thread); |
| 682 | 682 |
| 683 disk_cache::Entry* entry; | 683 disk_cache::Entry* entry; |
| 684 int rv = cache_->CreateEntry("some key", &entry, cb.callback()); | 684 int rv = cache_->CreateEntry("some key", &entry, cb.callback()); |
| 685 ASSERT_EQ(net::ERR_IO_PENDING, rv); | 685 ASSERT_EQ(net::ERR_IO_PENDING, rv); |
| 686 | 686 |
| 687 cache_.reset(); | 687 cache_.reset(); |
| 688 EXPECT_FALSE(cb.have_result()); | 688 EXPECT_FALSE(cb.have_result()); |
| 689 } | 689 } |
| 690 | 690 |
| 691 base::MessageLoop::current()->RunUntilIdle(); | 691 base::RunLoop().RunUntilIdle(); |
| 692 EXPECT_FALSE(cb.have_result()); | 692 EXPECT_FALSE(cb.have_result()); |
| 693 } | 693 } |
| 694 | 694 |
| 695 TEST_F(DiskCacheBackendTest, ShutdownWithPendingCreate) { | 695 TEST_F(DiskCacheBackendTest, ShutdownWithPendingCreate) { |
| 696 BackendShutdownWithPendingCreate(false); | 696 BackendShutdownWithPendingCreate(false); |
| 697 } | 697 } |
| 698 | 698 |
| 699 #if !defined(LEAK_SANITIZER) | 699 #if !defined(LEAK_SANITIZER) |
| 700 // We'll be leaking an entry from this test. | 700 // We'll be leaking an entry from this test. |
| 701 TEST_F(DiskCacheBackendTest, ShutdownWithPendingCreate_Fast) { | 701 TEST_F(DiskCacheBackendTest, ShutdownWithPendingCreate_Fast) { |
| (...skipping 21 matching lines...) Expand all Loading... |
| 723 entry->Close(); | 723 entry->Close(); |
| 724 entry = nullptr; | 724 entry = nullptr; |
| 725 | 725 |
| 726 rv = cache_->DoomEntry("some key", cb.callback()); | 726 rv = cache_->DoomEntry("some key", cb.callback()); |
| 727 ASSERT_EQ(net::ERR_IO_PENDING, rv); | 727 ASSERT_EQ(net::ERR_IO_PENDING, rv); |
| 728 | 728 |
| 729 cache_.reset(); | 729 cache_.reset(); |
| 730 EXPECT_FALSE(cb.have_result()); | 730 EXPECT_FALSE(cb.have_result()); |
| 731 } | 731 } |
| 732 | 732 |
| 733 base::MessageLoop::current()->RunUntilIdle(); | 733 base::RunLoop().RunUntilIdle(); |
| 734 EXPECT_FALSE(cb.have_result()); | 734 EXPECT_FALSE(cb.have_result()); |
| 735 } | 735 } |
| 736 | 736 |
| 737 TEST_F(DiskCacheBackendTest, ShutdownWithPendingDoom) { | 737 TEST_F(DiskCacheBackendTest, ShutdownWithPendingDoom) { |
| 738 BackendShutdownWithPendingDoom(); | 738 BackendShutdownWithPendingDoom(); |
| 739 } | 739 } |
| 740 | 740 |
| 741 // Disabled on android since this test requires cache creator to create | 741 // Disabled on android since this test requires cache creator to create |
| 742 // blockfile caches. | 742 // blockfile caches. |
| 743 #if !defined(OS_ANDROID) | 743 #if !defined(OS_ANDROID) |
| (...skipping 452 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1196 EXPECT_EQ(kSize, WriteData(entry, 0, 0, buffer.get(), kSize, false)); | 1196 EXPECT_EQ(kSize, WriteData(entry, 0, 0, buffer.get(), kSize, false)); |
| 1197 | 1197 |
| 1198 EXPECT_EQ(2, cache_->GetEntryCount()); | 1198 EXPECT_EQ(2, cache_->GetEntryCount()); |
| 1199 SetMaxSize(kSize); | 1199 SetMaxSize(kSize); |
| 1200 entry->Close(); // Trim the cache. | 1200 entry->Close(); // Trim the cache. |
| 1201 FlushQueueForTest(); | 1201 FlushQueueForTest(); |
| 1202 | 1202 |
| 1203 // If we evicted the entry in less than 20mS, we have one entry in the cache; | 1203 // If we evicted the entry in less than 20mS, we have one entry in the cache; |
| 1204 // if it took more than that, we posted a task and we'll delete the second | 1204 // if it took more than that, we posted a task and we'll delete the second |
| 1205 // entry too. | 1205 // entry too. |
| 1206 base::MessageLoop::current()->RunUntilIdle(); | 1206 base::RunLoop().RunUntilIdle(); |
| 1207 | 1207 |
| 1208 // This may be not thread-safe in general, but for now it's OK so add some | 1208 // This may be not thread-safe in general, but for now it's OK so add some |
| 1209 // ThreadSanitizer annotations to ignore data races on cache_. | 1209 // ThreadSanitizer annotations to ignore data races on cache_. |
| 1210 // See http://crbug.com/55970 | 1210 // See http://crbug.com/55970 |
| 1211 ANNOTATE_IGNORE_READS_BEGIN(); | 1211 ANNOTATE_IGNORE_READS_BEGIN(); |
| 1212 EXPECT_GE(1, cache_->GetEntryCount()); | 1212 EXPECT_GE(1, cache_->GetEntryCount()); |
| 1213 ANNOTATE_IGNORE_READS_END(); | 1213 ANNOTATE_IGNORE_READS_END(); |
| 1214 | 1214 |
| 1215 EXPECT_NE(net::OK, OpenEntry(first, &entry)); | 1215 EXPECT_NE(net::OK, OpenEntry(first, &entry)); |
| 1216 } | 1216 } |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1261 // For the new eviction code, all corrupt entries are on the second list so | 1261 // For the new eviction code, all corrupt entries are on the second list so |
| 1262 // they are not going away that easy. | 1262 // they are not going away that easy. |
| 1263 if (new_eviction_) { | 1263 if (new_eviction_) { |
| 1264 EXPECT_EQ(net::OK, DoomAllEntries()); | 1264 EXPECT_EQ(net::OK, DoomAllEntries()); |
| 1265 } | 1265 } |
| 1266 | 1266 |
| 1267 entry->Close(); // Trim the cache. | 1267 entry->Close(); // Trim the cache. |
| 1268 FlushQueueForTest(); | 1268 FlushQueueForTest(); |
| 1269 | 1269 |
| 1270 // We may abort the eviction before cleaning up everything. | 1270 // We may abort the eviction before cleaning up everything. |
| 1271 base::MessageLoop::current()->RunUntilIdle(); | 1271 base::RunLoop().RunUntilIdle(); |
| 1272 FlushQueueForTest(); | 1272 FlushQueueForTest(); |
| 1273 // If it's not clear enough: we may still have eviction tasks running at this | 1273 // If it's not clear enough: we may still have eviction tasks running at this |
| 1274 // time, so the number of entries is changing while we read it. | 1274 // time, so the number of entries is changing while we read it. |
| 1275 ANNOTATE_IGNORE_READS_AND_WRITES_BEGIN(); | 1275 ANNOTATE_IGNORE_READS_AND_WRITES_BEGIN(); |
| 1276 EXPECT_GE(30, cache_->GetEntryCount()); | 1276 EXPECT_GE(30, cache_->GetEntryCount()); |
| 1277 ANNOTATE_IGNORE_READS_AND_WRITES_END(); | 1277 ANNOTATE_IGNORE_READS_AND_WRITES_END(); |
| 1278 } | 1278 } |
| 1279 | 1279 |
| 1280 // We'll be leaking memory from this test. | 1280 // We'll be leaking memory from this test. |
| 1281 TEST_F(DiskCacheBackendTest, TrimInvalidEntry2) { | 1281 TEST_F(DiskCacheBackendTest, TrimInvalidEntry2) { |
| (...skipping 1779 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3061 entry2->Close(); | 3061 entry2->Close(); |
| 3062 | 3062 |
| 3063 ASSERT_EQ(net::OK, CreateEntry("third", &entry1)); | 3063 ASSERT_EQ(net::OK, CreateEntry("third", &entry1)); |
| 3064 ASSERT_EQ(net::OK, CreateEntry("fourth", &entry2)); | 3064 ASSERT_EQ(net::OK, CreateEntry("fourth", &entry2)); |
| 3065 | 3065 |
| 3066 ASSERT_EQ(4, cache_->GetEntryCount()); | 3066 ASSERT_EQ(4, cache_->GetEntryCount()); |
| 3067 EXPECT_EQ(net::OK, DoomAllEntries()); | 3067 EXPECT_EQ(net::OK, DoomAllEntries()); |
| 3068 ASSERT_EQ(0, cache_->GetEntryCount()); | 3068 ASSERT_EQ(0, cache_->GetEntryCount()); |
| 3069 | 3069 |
| 3070 // We should stop posting tasks at some point (if we post any). | 3070 // We should stop posting tasks at some point (if we post any). |
| 3071 base::MessageLoop::current()->RunUntilIdle(); | 3071 base::RunLoop().RunUntilIdle(); |
| 3072 | 3072 |
| 3073 disk_cache::Entry *entry3, *entry4; | 3073 disk_cache::Entry *entry3, *entry4; |
| 3074 EXPECT_NE(net::OK, OpenEntry("third", &entry3)); | 3074 EXPECT_NE(net::OK, OpenEntry("third", &entry3)); |
| 3075 ASSERT_EQ(net::OK, CreateEntry("third", &entry3)); | 3075 ASSERT_EQ(net::OK, CreateEntry("third", &entry3)); |
| 3076 ASSERT_EQ(net::OK, CreateEntry("fourth", &entry4)); | 3076 ASSERT_EQ(net::OK, CreateEntry("fourth", &entry4)); |
| 3077 | 3077 |
| 3078 EXPECT_EQ(net::OK, DoomAllEntries()); | 3078 EXPECT_EQ(net::OK, DoomAllEntries()); |
| 3079 ASSERT_EQ(0, cache_->GetEntryCount()); | 3079 ASSERT_EQ(0, cache_->GetEntryCount()); |
| 3080 | 3080 |
| 3081 entry1->Close(); | 3081 entry1->Close(); |
| (...skipping 719 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3801 // because that would advance the cache directory mtime and invalidate the | 3801 // because that would advance the cache directory mtime and invalidate the |
| 3802 // index. | 3802 // index. |
| 3803 entry2->Doom(); | 3803 entry2->Doom(); |
| 3804 entry2->Close(); | 3804 entry2->Close(); |
| 3805 | 3805 |
| 3806 DisableFirstCleanup(); | 3806 DisableFirstCleanup(); |
| 3807 InitCache(); | 3807 InitCache(); |
| 3808 EXPECT_EQ(disk_cache::SimpleIndex::INITIALIZE_METHOD_LOADED, | 3808 EXPECT_EQ(disk_cache::SimpleIndex::INITIALIZE_METHOD_LOADED, |
| 3809 simple_cache_impl_->index()->init_method()); | 3809 simple_cache_impl_->index()->init_method()); |
| 3810 } | 3810 } |
| OLD | NEW |