Chromium Code Reviews| 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 WriteData(entry, 1, 0, buffer.get(), kWriteSize, false); |
|
Randy Smith (Not in Mondays)
2017/03/30 16:38:11
Comments about why you're not checking the result?
jkarlin
2017/03/30 17:18:27
Done.
| |
| 3184 WriteData(entry, 1, 0, buffer.get(), kWriteSize, false)); | |
| 3185 open_entries.push_back(disk_cache::ScopedEntryPtr(entry)); | 3184 open_entries.push_back(disk_cache::ScopedEntryPtr(entry)); |
| 3186 } | 3185 } |
| 3187 EXPECT_LT(kMaxSize, CalculateSizeOfAllEntries()); | |
| 3188 | 3186 |
| 3189 // Writing this sparse data should not crash. | 3187 // Writing this sparse data should not crash. |
| 3190 EXPECT_EQ(1024, first_parent->WriteSparseData(32768, buffer.get(), 1024, | 3188 first_parent->WriteSparseData(32768, buffer.get(), 1024, |
| 3191 net::CompletionCallback())); | 3189 net::CompletionCallback()); |
| 3190 } | |
| 3191 | |
| 3192 TEST_F(DiskCacheBackendTest, MemoryCapsWritesToMaxSize) { | |
| 3193 // Verify that the memory backend won't grow beyond its max size if lots of | |
| 3194 // open entries (each smaller than the max entry size) are trying to write | |
| 3195 // beyond the max size. | |
| 3196 SetMemoryOnlyMode(); | |
| 3197 | |
| 3198 const int kMaxSize = 100 * 1024; // 100KB cache | |
| 3199 const int kNumEntries = 20; // 20 entries to write | |
| 3200 const int kWriteSize = kMaxSize / 10; // Each entry writes 1/10th the max | |
| 3201 | |
| 3202 SetMaxSize(kMaxSize); | |
| 3203 InitCache(); | |
| 3204 | |
| 3205 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kWriteSize)); | |
| 3206 CacheTestFillBuffer(buffer->data(), kWriteSize, false); | |
| 3207 | |
| 3208 // Create an entry to be the final entry that gets written later. | |
| 3209 disk_cache::Entry* entry; | |
| 3210 ASSERT_THAT(CreateEntry("final", &entry), IsOk()); | |
| 3211 disk_cache::ScopedEntryPtr final_entry(entry); | |
| 3212 | |
| 3213 // Create a ton of entries, write to the cache, and keep the entries open. | |
| 3214 // They should start failing writes once the cache fills. | |
| 3215 std::list<disk_cache::ScopedEntryPtr> open_entries; | |
| 3216 std::string key_prefix("prefix"); | |
| 3217 for (int i = 0; i < kNumEntries; ++i) { | |
| 3218 ASSERT_THAT(CreateEntry(key_prefix + base::IntToString(i), &entry), IsOk()); | |
| 3219 WriteData(entry, 1, 0, buffer.get(), kWriteSize, false); | |
| 3220 open_entries.push_back(disk_cache::ScopedEntryPtr(entry)); | |
| 3221 } | |
| 3222 EXPECT_GE(kMaxSize, CalculateSizeOfAllEntries()); | |
| 3223 | |
| 3224 // Any more writing at this point should cause an error. | |
| 3225 EXPECT_THAT( | |
| 3226 WriteData(final_entry.get(), 1, 0, buffer.get(), kWriteSize, false), | |
| 3227 IsError(net::ERR_INSUFFICIENT_RESOURCES)); | |
| 3192 } | 3228 } |
| 3193 | 3229 |
| 3194 TEST_F(DiskCacheTest, Backend_UsageStatsTimer) { | 3230 TEST_F(DiskCacheTest, Backend_UsageStatsTimer) { |
| 3195 MessageLoopHelper helper; | 3231 MessageLoopHelper helper; |
| 3196 | 3232 |
| 3197 ASSERT_TRUE(CleanupCacheDir()); | 3233 ASSERT_TRUE(CleanupCacheDir()); |
| 3198 std::unique_ptr<disk_cache::BackendImpl> cache; | 3234 std::unique_ptr<disk_cache::BackendImpl> cache; |
| 3199 cache.reset(new disk_cache::BackendImpl( | 3235 cache.reset(new disk_cache::BackendImpl( |
| 3200 cache_path_, base::ThreadTaskRunnerHandle::Get(), NULL)); | 3236 cache_path_, base::ThreadTaskRunnerHandle::Get(), NULL)); |
| 3201 ASSERT_TRUE(NULL != cache.get()); | 3237 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 | 4027 // because that would advance the cache directory mtime and invalidate the |
| 3992 // index. | 4028 // index. |
| 3993 entry2->Doom(); | 4029 entry2->Doom(); |
| 3994 entry2->Close(); | 4030 entry2->Close(); |
| 3995 | 4031 |
| 3996 DisableFirstCleanup(); | 4032 DisableFirstCleanup(); |
| 3997 InitCache(); | 4033 InitCache(); |
| 3998 EXPECT_EQ(disk_cache::SimpleIndex::INITIALIZE_METHOD_LOADED, | 4034 EXPECT_EQ(disk_cache::SimpleIndex::INITIALIZE_METHOD_LOADED, |
| 3999 simple_cache_impl_->index()->init_method()); | 4035 simple_cache_impl_->index()->init_method()); |
| 4000 } | 4036 } |
| OLD | NEW |