| 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/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 102 bool CreateSetOfRandomEntries(std::set<std::string>* key_pool); | 102 bool CreateSetOfRandomEntries(std::set<std::string>* key_pool); |
| 103 bool EnumerateAndMatchKeys(int max_to_open, | 103 bool EnumerateAndMatchKeys(int max_to_open, |
| 104 TestIterator* iter, | 104 TestIterator* iter, |
| 105 std::set<std::string>* keys_to_match, | 105 std::set<std::string>* keys_to_match, |
| 106 size_t* count); | 106 size_t* count); |
| 107 | 107 |
| 108 // Computes the expected size of entry metadata, i.e. the total size without | 108 // Computes the expected size of entry metadata, i.e. the total size without |
| 109 // the actual data stored. This depends only on the entry's |key| size. | 109 // the actual data stored. This depends only on the entry's |key| size. |
| 110 int GetEntryMetadataSize(std::string key); | 110 int GetEntryMetadataSize(std::string key); |
| 111 | 111 |
| 112 // The Simple Backend only tracks the approximate sizes of entries. This |
| 113 // rounds the exact size appropriately. |
| 114 int GetRoundedSize(int exact_size); |
| 115 |
| 112 // Actual tests: | 116 // Actual tests: |
| 113 void BackendBasics(); | 117 void BackendBasics(); |
| 114 void BackendKeying(); | 118 void BackendKeying(); |
| 115 void BackendShutdownWithPendingFileIO(bool fast); | 119 void BackendShutdownWithPendingFileIO(bool fast); |
| 116 void BackendShutdownWithPendingIO(bool fast); | 120 void BackendShutdownWithPendingIO(bool fast); |
| 117 void BackendShutdownWithPendingCreate(bool fast); | 121 void BackendShutdownWithPendingCreate(bool fast); |
| 118 void BackendShutdownWithPendingDoom(); | 122 void BackendShutdownWithPendingDoom(); |
| 119 void BackendSetSize(); | 123 void BackendSetSize(); |
| 120 void BackendLoad(); | 124 void BackendLoad(); |
| 121 void BackendChain(); | 125 void BackendChain(); |
| (...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 315 if (!simple_cache_mode_) | 319 if (!simple_cache_mode_) |
| 316 return key.size(); | 320 return key.size(); |
| 317 | 321 |
| 318 // For the simple cache, we must add the file header and EOF, and that for | 322 // For the simple cache, we must add the file header and EOF, and that for |
| 319 // every stream. | 323 // every stream. |
| 320 return disk_cache::kSimpleEntryStreamCount * | 324 return disk_cache::kSimpleEntryStreamCount * |
| 321 (sizeof(disk_cache::SimpleFileHeader) + | 325 (sizeof(disk_cache::SimpleFileHeader) + |
| 322 sizeof(disk_cache::SimpleFileEOF) + key.size()); | 326 sizeof(disk_cache::SimpleFileEOF) + key.size()); |
| 323 } | 327 } |
| 324 | 328 |
| 329 int DiskCacheBackendTest::GetRoundedSize(int exact_size) { |
| 330 if (!simple_cache_mode_) |
| 331 return exact_size; |
| 332 |
| 333 return (exact_size + 255) & 0xFFFFFF00; |
| 334 } |
| 335 |
| 325 void DiskCacheBackendTest::BackendBasics() { | 336 void DiskCacheBackendTest::BackendBasics() { |
| 326 InitCache(); | 337 InitCache(); |
| 327 disk_cache::Entry *entry1 = NULL, *entry2 = NULL; | 338 disk_cache::Entry *entry1 = NULL, *entry2 = NULL; |
| 328 EXPECT_NE(net::OK, OpenEntry("the first key", &entry1)); | 339 EXPECT_NE(net::OK, OpenEntry("the first key", &entry1)); |
| 329 ASSERT_THAT(CreateEntry("the first key", &entry1), IsOk()); | 340 ASSERT_THAT(CreateEntry("the first key", &entry1), IsOk()); |
| 330 ASSERT_TRUE(NULL != entry1); | 341 ASSERT_TRUE(NULL != entry1); |
| 331 entry1->Close(); | 342 entry1->Close(); |
| 332 entry1 = NULL; | 343 entry1 = NULL; |
| 333 | 344 |
| 334 ASSERT_THAT(OpenEntry("the first key", &entry1), IsOk()); | 345 ASSERT_THAT(OpenEntry("the first key", &entry1), IsOk()); |
| (...skipping 1667 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2002 | 2013 |
| 2003 // The cache is initially empty. | 2014 // The cache is initially empty. |
| 2004 EXPECT_EQ(0, CalculateSizeOfAllEntries()); | 2015 EXPECT_EQ(0, CalculateSizeOfAllEntries()); |
| 2005 | 2016 |
| 2006 // Generate random entries and populate them with data of respective | 2017 // Generate random entries and populate them with data of respective |
| 2007 // sizes 0, 1, ..., count - 1 bytes. | 2018 // sizes 0, 1, ..., count - 1 bytes. |
| 2008 std::set<std::string> key_pool; | 2019 std::set<std::string> key_pool; |
| 2009 CreateSetOfRandomEntries(&key_pool); | 2020 CreateSetOfRandomEntries(&key_pool); |
| 2010 | 2021 |
| 2011 int count = 0; | 2022 int count = 0; |
| 2023 int total_size = 0; |
| 2012 for (std::string key : key_pool) { | 2024 for (std::string key : key_pool) { |
| 2013 std::string data(count, ' '); | 2025 std::string data(count, ' '); |
| 2014 scoped_refptr<net::StringIOBuffer> buffer = new net::StringIOBuffer(data); | 2026 scoped_refptr<net::StringIOBuffer> buffer = new net::StringIOBuffer(data); |
| 2015 | 2027 |
| 2016 // Alternate between writing to first two streams to test that we do not | 2028 // Alternate between writing to first two streams to test that we do not |
| 2017 // take only one stream into account. | 2029 // take only one stream into account. |
| 2018 disk_cache::Entry* entry; | 2030 disk_cache::Entry* entry; |
| 2019 ASSERT_THAT(OpenEntry(key, &entry), IsOk()); | 2031 ASSERT_THAT(OpenEntry(key, &entry), IsOk()); |
| 2020 ASSERT_EQ(count, WriteData(entry, count % 2, 0, buffer.get(), count, true)); | 2032 ASSERT_EQ(count, WriteData(entry, count % 2, 0, buffer.get(), count, true)); |
| 2021 entry->Close(); | 2033 entry->Close(); |
| 2022 | 2034 |
| 2035 total_size += GetRoundedSize(count + GetEntryMetadataSize(key)); |
| 2023 ++count; | 2036 ++count; |
| 2024 } | 2037 } |
| 2025 | 2038 |
| 2026 // The resulting size should be (0 + 1 + ... + count - 1) plus keys. | |
| 2027 int result = CalculateSizeOfAllEntries(); | 2039 int result = CalculateSizeOfAllEntries(); |
| 2028 int total_metadata_size = 0; | 2040 EXPECT_EQ(total_size, result); |
| 2029 for (std::string key : key_pool) | |
| 2030 total_metadata_size += GetEntryMetadataSize(key); | |
| 2031 EXPECT_EQ((count - 1) * count / 2 + total_metadata_size, result); | |
| 2032 | 2041 |
| 2033 // Add another entry and test if the size is updated. Then remove it and test | 2042 // Add another entry and test if the size is updated. Then remove it and test |
| 2034 // if the size is back to original value. | 2043 // if the size is back to original value. |
| 2035 { | 2044 { |
| 2036 const int last_entry_size = 47; | 2045 const int last_entry_size = 47; |
| 2037 std::string data(last_entry_size, ' '); | 2046 std::string data(last_entry_size, ' '); |
| 2038 scoped_refptr<net::StringIOBuffer> buffer = new net::StringIOBuffer(data); | 2047 scoped_refptr<net::StringIOBuffer> buffer = new net::StringIOBuffer(data); |
| 2039 | 2048 |
| 2040 disk_cache::Entry* entry; | 2049 disk_cache::Entry* entry; |
| 2041 std::string key = GenerateKey(true); | 2050 std::string key = GenerateKey(true); |
| 2042 ASSERT_THAT(CreateEntry(key, &entry), IsOk()); | 2051 ASSERT_THAT(CreateEntry(key, &entry), IsOk()); |
| 2043 ASSERT_EQ(last_entry_size, | 2052 ASSERT_EQ(last_entry_size, |
| 2044 WriteData(entry, 0, 0, buffer.get(), last_entry_size, true)); | 2053 WriteData(entry, 0, 0, buffer.get(), last_entry_size, true)); |
| 2045 entry->Close(); | 2054 entry->Close(); |
| 2046 | 2055 |
| 2047 int new_result = CalculateSizeOfAllEntries(); | 2056 int new_result = CalculateSizeOfAllEntries(); |
| 2048 EXPECT_EQ(result + last_entry_size + GetEntryMetadataSize(key), new_result); | 2057 EXPECT_EQ( |
| 2058 result + GetRoundedSize(last_entry_size + GetEntryMetadataSize(key)), |
| 2059 new_result); |
| 2049 | 2060 |
| 2050 DoomEntry(key); | 2061 DoomEntry(key); |
| 2051 new_result = CalculateSizeOfAllEntries(); | 2062 new_result = CalculateSizeOfAllEntries(); |
| 2052 EXPECT_EQ(result, new_result); | 2063 EXPECT_EQ(result, new_result); |
| 2053 } | 2064 } |
| 2054 | 2065 |
| 2055 // After dooming the entries, the size should be back to zero. | 2066 // After dooming the entries, the size should be back to zero. |
| 2056 ASSERT_THAT(DoomAllEntries(), IsOk()); | 2067 ASSERT_THAT(DoomAllEntries(), IsOk()); |
| 2057 EXPECT_EQ(0, CalculateSizeOfAllEntries()); | 2068 EXPECT_EQ(0, CalculateSizeOfAllEntries()); |
| 2058 } | 2069 } |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2092 | 2103 |
| 2093 ASSERT_THAT(CreateEntry("second", &entry), IsOk()); | 2104 ASSERT_THAT(CreateEntry("second", &entry), IsOk()); |
| 2094 entry->Close(); | 2105 entry->Close(); |
| 2095 ASSERT_THAT(CreateEntry("third_entry", &entry), IsOk()); | 2106 ASSERT_THAT(CreateEntry("third_entry", &entry), IsOk()); |
| 2096 entry->Close(); | 2107 entry->Close(); |
| 2097 FlushQueueForTest(); | 2108 FlushQueueForTest(); |
| 2098 | 2109 |
| 2099 AddDelay(); | 2110 AddDelay(); |
| 2100 Time end = Time::Now(); | 2111 Time end = Time::Now(); |
| 2101 | 2112 |
| 2102 int size_1 = GetEntryMetadataSize("first"); | 2113 int size_1 = GetRoundedSize(GetEntryMetadataSize("first")); |
| 2103 int size_2 = GetEntryMetadataSize("second"); | 2114 int size_2 = GetRoundedSize(GetEntryMetadataSize("second")); |
| 2104 int size_3 = GetEntryMetadataSize("third_entry"); | 2115 int size_3 = GetRoundedSize(GetEntryMetadataSize("third_entry")); |
| 2105 | 2116 |
| 2106 ASSERT_EQ(3, cache_->GetEntryCount()); | 2117 ASSERT_EQ(3, cache_->GetEntryCount()); |
| 2107 ASSERT_EQ(CalculateSizeOfAllEntries(), | 2118 ASSERT_EQ(CalculateSizeOfAllEntries(), |
| 2108 CalculateSizeOfEntriesBetween(base::Time(), base::Time::Max())); | 2119 CalculateSizeOfEntriesBetween(base::Time(), base::Time::Max())); |
| 2109 | 2120 |
| 2110 int start_end = CalculateSizeOfEntriesBetween(start, end); | 2121 int start_end = CalculateSizeOfEntriesBetween(start, end); |
| 2111 ASSERT_EQ(CalculateSizeOfAllEntries(), start_end); | 2122 ASSERT_EQ(CalculateSizeOfAllEntries(), start_end); |
| 2112 ASSERT_EQ(size_1 + size_2 + size_3, start_end); | 2123 ASSERT_EQ(size_1 + size_2 + size_3, start_end); |
| 2113 | 2124 |
| 2114 ASSERT_EQ(size_1, CalculateSizeOfEntriesBetween(start, middle)); | 2125 ASSERT_EQ(size_1, CalculateSizeOfEntriesBetween(start, middle)); |
| (...skipping 2093 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 4208 disk_cache::SimpleBackendImpl::FlushWorkerPoolForTesting(); | 4219 disk_cache::SimpleBackendImpl::FlushWorkerPoolForTesting(); |
| 4209 base::RunLoop().RunUntilIdle(); | 4220 base::RunLoop().RunUntilIdle(); |
| 4210 | 4221 |
| 4211 disk_cache::Entry* reopen_entry1; | 4222 disk_cache::Entry* reopen_entry1; |
| 4212 ASSERT_THAT(OpenEntry(key1, &reopen_entry1), IsOk()); | 4223 ASSERT_THAT(OpenEntry(key1, &reopen_entry1), IsOk()); |
| 4213 | 4224 |
| 4214 // This shouldn't pick up entry2's write time incorrectly. | 4225 // This shouldn't pick up entry2's write time incorrectly. |
| 4215 EXPECT_LE(reopen_entry1->GetLastModified(), entry1_timestamp); | 4226 EXPECT_LE(reopen_entry1->GetLastModified(), entry1_timestamp); |
| 4216 reopen_entry1->Close(); | 4227 reopen_entry1->Close(); |
| 4217 } | 4228 } |
| OLD | NEW |