| 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/memory/ptr_util.h" | 9 #include "base/memory/ptr_util.h" |
| 10 #include "base/metrics/field_trial.h" | 10 #include "base/metrics/field_trial.h" |
| (...skipping 498 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 509 cache_path_, cache_thread.task_runner(), NULL)); | 509 cache_path_, cache_thread.task_runner(), NULL)); |
| 510 int rv = cache->Init(cb.callback()); | 510 int rv = cache->Init(cb.callback()); |
| 511 EXPECT_THAT(cb.GetResult(rv), IsError(net::ERR_FAILED)); | 511 EXPECT_THAT(cb.GetResult(rv), IsError(net::ERR_FAILED)); |
| 512 base::ThreadRestrictions::SetIOAllowed(prev); | 512 base::ThreadRestrictions::SetIOAllowed(prev); |
| 513 | 513 |
| 514 cache.reset(); | 514 cache.reset(); |
| 515 DisableIntegrityCheck(); | 515 DisableIntegrityCheck(); |
| 516 } | 516 } |
| 517 | 517 |
| 518 TEST_F(DiskCacheBackendTest, MemCacheMemoryDump) { | 518 TEST_F(DiskCacheBackendTest, MemCacheMemoryDump) { |
| 519 memory_only_ = true; | 519 SetMemoryOnlyMode(); |
| 520 BackendBasics(); | 520 BackendBasics(); |
| 521 base::trace_event::MemoryDumpArgs args = { | 521 base::trace_event::MemoryDumpArgs args = { |
| 522 base::trace_event::MemoryDumpLevelOfDetail::BACKGROUND}; | 522 base::trace_event::MemoryDumpLevelOfDetail::BACKGROUND}; |
| 523 base::trace_event::ProcessMemoryDump pmd(nullptr, args); | 523 base::trace_event::ProcessMemoryDump pmd(nullptr, args); |
| 524 base::trace_event::MemoryAllocatorDump* parent = | 524 base::trace_event::MemoryAllocatorDump* parent = |
| 525 pmd.CreateAllocatorDump("net/url_request_context/main/0x123/http_cache"); | 525 pmd.CreateAllocatorDump("net/url_request_context/main/0x123/http_cache"); |
| 526 | 526 |
| 527 ASSERT_LT(0u, cache_->DumpMemoryStats(&pmd, parent->absolute_name())); | 527 ASSERT_LT(0u, cache_->DumpMemoryStats(&pmd, parent->absolute_name())); |
| 528 EXPECT_EQ(2u, pmd.allocator_dumps().size()); | 528 EXPECT_EQ(2u, pmd.allocator_dumps().size()); |
| 529 const base::trace_event::MemoryAllocatorDump* sub_dump = | 529 const base::trace_event::MemoryAllocatorDump* sub_dump = |
| (...skipping 2643 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3173 ASSERT_THAT(CreateEntry("first parent", &entry), IsOk()); | 3173 ASSERT_THAT(CreateEntry("first parent", &entry), IsOk()); |
| 3174 disk_cache::ScopedEntryPtr first_parent(entry); | 3174 disk_cache::ScopedEntryPtr first_parent(entry); |
| 3175 | 3175 |
| 3176 // Create a ton of entries, and keep them open, to put the cache well above | 3176 // Create a ton of entries, and keep them open, to put the cache well above |
| 3177 // its eviction threshhold. | 3177 // its eviction threshhold. |
| 3178 const int kTooManyEntriesCount = kMaxEntryCount * 2; | 3178 const int kTooManyEntriesCount = kMaxEntryCount * 2; |
| 3179 std::list<disk_cache::ScopedEntryPtr> open_entries; | 3179 std::list<disk_cache::ScopedEntryPtr> open_entries; |
| 3180 std::string key_prefix("prefix"); | 3180 std::string key_prefix("prefix"); |
| 3181 for (int i = 0; i < kTooManyEntriesCount; ++i) { | 3181 for (int i = 0; i < kTooManyEntriesCount; ++i) { |
| 3182 ASSERT_THAT(CreateEntry(key_prefix + base::IntToString(i), &entry), IsOk()); | 3182 ASSERT_THAT(CreateEntry(key_prefix + base::IntToString(i), &entry), IsOk()); |
| 3183 EXPECT_EQ(kWriteSize, | 3183 // Not checking the result because it will start to fail once the max size |
| 3184 WriteData(entry, 1, 0, buffer.get(), kWriteSize, false)); | 3184 // is reached. |
| 3185 WriteData(entry, 1, 0, buffer.get(), kWriteSize, false); |
| 3185 open_entries.push_back(disk_cache::ScopedEntryPtr(entry)); | 3186 open_entries.push_back(disk_cache::ScopedEntryPtr(entry)); |
| 3186 } | 3187 } |
| 3187 EXPECT_LT(kMaxSize, CalculateSizeOfAllEntries()); | |
| 3188 | 3188 |
| 3189 // Writing this sparse data should not crash. | 3189 // Writing this sparse data should not crash. Ignoring the result because |
| 3190 EXPECT_EQ(1024, first_parent->WriteSparseData(32768, buffer.get(), 1024, | 3190 // we're only concerned with not crashing in this particular test. |
| 3191 net::CompletionCallback())); | 3191 first_parent->WriteSparseData(32768, buffer.get(), 1024, |
| 3192 net::CompletionCallback()); |
| 3193 } |
| 3194 |
| 3195 TEST_F(DiskCacheBackendTest, MemoryCapsWritesToMaxSize) { |
| 3196 // Verify that the memory backend won't grow beyond its max size if lots of |
| 3197 // open entries (each smaller than the max entry size) are trying to write |
| 3198 // beyond the max size. |
| 3199 SetMemoryOnlyMode(); |
| 3200 |
| 3201 const int kMaxSize = 100 * 1024; // 100KB cache |
| 3202 const int kNumEntries = 20; // 20 entries to write |
| 3203 const int kWriteSize = kMaxSize / 10; // Each entry writes 1/10th the max |
| 3204 |
| 3205 SetMaxSize(kMaxSize); |
| 3206 InitCache(); |
| 3207 |
| 3208 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kWriteSize)); |
| 3209 CacheTestFillBuffer(buffer->data(), kWriteSize, false); |
| 3210 |
| 3211 // Create an entry to be the final entry that gets written later. |
| 3212 disk_cache::Entry* entry; |
| 3213 ASSERT_THAT(CreateEntry("final", &entry), IsOk()); |
| 3214 disk_cache::ScopedEntryPtr final_entry(entry); |
| 3215 |
| 3216 // Create a ton of entries, write to the cache, and keep the entries open. |
| 3217 // They should start failing writes once the cache fills. |
| 3218 std::list<disk_cache::ScopedEntryPtr> open_entries; |
| 3219 std::string key_prefix("prefix"); |
| 3220 for (int i = 0; i < kNumEntries; ++i) { |
| 3221 ASSERT_THAT(CreateEntry(key_prefix + base::IntToString(i), &entry), IsOk()); |
| 3222 WriteData(entry, 1, 0, buffer.get(), kWriteSize, false); |
| 3223 open_entries.push_back(disk_cache::ScopedEntryPtr(entry)); |
| 3224 } |
| 3225 EXPECT_GE(kMaxSize, CalculateSizeOfAllEntries()); |
| 3226 |
| 3227 // Any more writing at this point should cause an error. |
| 3228 EXPECT_THAT( |
| 3229 WriteData(final_entry.get(), 1, 0, buffer.get(), kWriteSize, false), |
| 3230 IsError(net::ERR_INSUFFICIENT_RESOURCES)); |
| 3192 } | 3231 } |
| 3193 | 3232 |
| 3194 TEST_F(DiskCacheTest, Backend_UsageStatsTimer) { | 3233 TEST_F(DiskCacheTest, Backend_UsageStatsTimer) { |
| 3195 MessageLoopHelper helper; | 3234 MessageLoopHelper helper; |
| 3196 | 3235 |
| 3197 ASSERT_TRUE(CleanupCacheDir()); | 3236 ASSERT_TRUE(CleanupCacheDir()); |
| 3198 std::unique_ptr<disk_cache::BackendImpl> cache; | 3237 std::unique_ptr<disk_cache::BackendImpl> cache; |
| 3199 cache.reset(new disk_cache::BackendImpl( | 3238 cache.reset(new disk_cache::BackendImpl( |
| 3200 cache_path_, base::ThreadTaskRunnerHandle::Get(), NULL)); | 3239 cache_path_, base::ThreadTaskRunnerHandle::Get(), NULL)); |
| 3201 ASSERT_TRUE(NULL != cache.get()); | 3240 ASSERT_TRUE(NULL != cache.get()); |
| (...skipping 789 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3991 // because that would advance the cache directory mtime and invalidate the | 4030 // because that would advance the cache directory mtime and invalidate the |
| 3992 // index. | 4031 // index. |
| 3993 entry2->Doom(); | 4032 entry2->Doom(); |
| 3994 entry2->Close(); | 4033 entry2->Close(); |
| 3995 | 4034 |
| 3996 DisableFirstCleanup(); | 4035 DisableFirstCleanup(); |
| 3997 InitCache(); | 4036 InitCache(); |
| 3998 EXPECT_EQ(disk_cache::SimpleIndex::INITIALIZE_METHOD_LOADED, | 4037 EXPECT_EQ(disk_cache::SimpleIndex::INITIALIZE_METHOD_LOADED, |
| 3999 simple_cache_impl_->index()->init_method()); | 4038 simple_cache_impl_->index()->init_method()); |
| 4000 } | 4039 } |
| OLD | NEW |