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/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 99 bool CreateSetOfRandomEntries(std::set<std::string>* key_pool); | 99 bool CreateSetOfRandomEntries(std::set<std::string>* key_pool); |
| 100 bool EnumerateAndMatchKeys(int max_to_open, | 100 bool EnumerateAndMatchKeys(int max_to_open, |
| 101 TestIterator* iter, | 101 TestIterator* iter, |
| 102 std::set<std::string>* keys_to_match, | 102 std::set<std::string>* keys_to_match, |
| 103 size_t* count); | 103 size_t* count); |
| 104 | 104 |
| 105 // Computes the expected size of entry metadata, i.e. the total size without | 105 // Computes the expected size of entry metadata, i.e. the total size without |
| 106 // the actual data stored. This depends only on the entry's |key| size. | 106 // the actual data stored. This depends only on the entry's |key| size. |
| 107 int GetEntryMetadataSize(std::string key); | 107 int GetEntryMetadataSize(std::string key); |
| 108 | 108 |
| 109 // The simple backend only tracks of approximate size. This rounds the exact | |
|
pasko
2017/06/29 16:31:49
nit: "tracks size approximately"?
gavinp
2017/08/04 18:42:54
How about: "The Simple Backend only tracks the app
Maks Orlovich
2017/08/23 19:29:05
Done.
| |
| 110 // size appropriately. | |
| 111 int GetRoundedSize(int exact_size); | |
| 112 | |
| 109 // Actual tests: | 113 // Actual tests: |
| 110 void BackendBasics(); | 114 void BackendBasics(); |
| 111 void BackendKeying(); | 115 void BackendKeying(); |
| 112 void BackendShutdownWithPendingFileIO(bool fast); | 116 void BackendShutdownWithPendingFileIO(bool fast); |
| 113 void BackendShutdownWithPendingIO(bool fast); | 117 void BackendShutdownWithPendingIO(bool fast); |
| 114 void BackendShutdownWithPendingCreate(bool fast); | 118 void BackendShutdownWithPendingCreate(bool fast); |
| 115 void BackendShutdownWithPendingDoom(); | 119 void BackendShutdownWithPendingDoom(); |
| 116 void BackendSetSize(); | 120 void BackendSetSize(); |
| 117 void BackendLoad(); | 121 void BackendLoad(); |
| 118 void BackendChain(); | 122 void BackendChain(); |
| (...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 312 if (!simple_cache_mode_) | 316 if (!simple_cache_mode_) |
| 313 return key.size(); | 317 return key.size(); |
| 314 | 318 |
| 315 // For the simple cache, we must add the file header and EOF, and that for | 319 // For the simple cache, we must add the file header and EOF, and that for |
| 316 // every stream. | 320 // every stream. |
| 317 return disk_cache::kSimpleEntryStreamCount * | 321 return disk_cache::kSimpleEntryStreamCount * |
| 318 (sizeof(disk_cache::SimpleFileHeader) + | 322 (sizeof(disk_cache::SimpleFileHeader) + |
| 319 sizeof(disk_cache::SimpleFileEOF) + key.size()); | 323 sizeof(disk_cache::SimpleFileEOF) + key.size()); |
| 320 } | 324 } |
| 321 | 325 |
| 326 int DiskCacheBackendTest::GetRoundedSize(int exact_size) { | |
|
pasko
2017/06/29 16:31:49
should this be a method of cache backend? can be w
Maks Orlovich
2017/08/23 19:29:05
Hmm, maybe. Feels better wrt to abstraction bounda
| |
| 327 if (!simple_cache_mode_) | |
| 328 return exact_size; | |
| 329 | |
| 330 return (exact_size + 255) & 0xFFFFFF00; | |
| 331 } | |
| 332 | |
| 322 void DiskCacheBackendTest::BackendBasics() { | 333 void DiskCacheBackendTest::BackendBasics() { |
| 323 InitCache(); | 334 InitCache(); |
| 324 disk_cache::Entry *entry1 = NULL, *entry2 = NULL; | 335 disk_cache::Entry *entry1 = NULL, *entry2 = NULL; |
| 325 EXPECT_NE(net::OK, OpenEntry("the first key", &entry1)); | 336 EXPECT_NE(net::OK, OpenEntry("the first key", &entry1)); |
| 326 ASSERT_THAT(CreateEntry("the first key", &entry1), IsOk()); | 337 ASSERT_THAT(CreateEntry("the first key", &entry1), IsOk()); |
| 327 ASSERT_TRUE(NULL != entry1); | 338 ASSERT_TRUE(NULL != entry1); |
| 328 entry1->Close(); | 339 entry1->Close(); |
| 329 entry1 = NULL; | 340 entry1 = NULL; |
| 330 | 341 |
| 331 ASSERT_THAT(OpenEntry("the first key", &entry1), IsOk()); | 342 ASSERT_THAT(OpenEntry("the first key", &entry1), IsOk()); |
| (...skipping 1540 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1872 | 1883 |
| 1873 // The cache is initially empty. | 1884 // The cache is initially empty. |
| 1874 EXPECT_EQ(0, CalculateSizeOfAllEntries()); | 1885 EXPECT_EQ(0, CalculateSizeOfAllEntries()); |
| 1875 | 1886 |
| 1876 // Generate random entries and populate them with data of respective | 1887 // Generate random entries and populate them with data of respective |
| 1877 // sizes 0, 1, ..., count - 1 bytes. | 1888 // sizes 0, 1, ..., count - 1 bytes. |
| 1878 std::set<std::string> key_pool; | 1889 std::set<std::string> key_pool; |
| 1879 CreateSetOfRandomEntries(&key_pool); | 1890 CreateSetOfRandomEntries(&key_pool); |
| 1880 | 1891 |
| 1881 int count = 0; | 1892 int count = 0; |
| 1893 int total_size = 0; | |
| 1882 for (std::string key : key_pool) { | 1894 for (std::string key : key_pool) { |
| 1883 std::string data(count, ' '); | 1895 std::string data(count, ' '); |
| 1884 scoped_refptr<net::StringIOBuffer> buffer = new net::StringIOBuffer(data); | 1896 scoped_refptr<net::StringIOBuffer> buffer = new net::StringIOBuffer(data); |
| 1885 | 1897 |
| 1886 // Alternate between writing to first two streams to test that we do not | 1898 // Alternate between writing to first two streams to test that we do not |
| 1887 // take only one stream into account. | 1899 // take only one stream into account. |
| 1888 disk_cache::Entry* entry; | 1900 disk_cache::Entry* entry; |
| 1889 ASSERT_THAT(OpenEntry(key, &entry), IsOk()); | 1901 ASSERT_THAT(OpenEntry(key, &entry), IsOk()); |
| 1890 ASSERT_EQ(count, WriteData(entry, count % 2, 0, buffer.get(), count, true)); | 1902 ASSERT_EQ(count, WriteData(entry, count % 2, 0, buffer.get(), count, true)); |
| 1891 entry->Close(); | 1903 entry->Close(); |
| 1892 | 1904 |
| 1905 total_size += GetRoundedSize(count + GetEntryMetadataSize(key)); | |
| 1893 ++count; | 1906 ++count; |
| 1894 } | 1907 } |
| 1895 | 1908 |
| 1896 // The resulting size should be (0 + 1 + ... + count - 1) plus keys. | |
| 1897 int result = CalculateSizeOfAllEntries(); | 1909 int result = CalculateSizeOfAllEntries(); |
| 1898 int total_metadata_size = 0; | 1910 EXPECT_EQ(total_size, result); |
| 1899 for (std::string key : key_pool) | |
| 1900 total_metadata_size += GetEntryMetadataSize(key); | |
| 1901 EXPECT_EQ((count - 1) * count / 2 + total_metadata_size, result); | |
| 1902 | 1911 |
| 1903 // Add another entry and test if the size is updated. Then remove it and test | 1912 // Add another entry and test if the size is updated. Then remove it and test |
| 1904 // if the size is back to original value. | 1913 // if the size is back to original value. |
| 1905 { | 1914 { |
| 1906 const int last_entry_size = 47; | 1915 const int last_entry_size = 47; |
| 1907 std::string data(last_entry_size, ' '); | 1916 std::string data(last_entry_size, ' '); |
| 1908 scoped_refptr<net::StringIOBuffer> buffer = new net::StringIOBuffer(data); | 1917 scoped_refptr<net::StringIOBuffer> buffer = new net::StringIOBuffer(data); |
| 1909 | 1918 |
| 1910 disk_cache::Entry* entry; | 1919 disk_cache::Entry* entry; |
| 1911 std::string key = GenerateKey(true); | 1920 std::string key = GenerateKey(true); |
| 1912 ASSERT_THAT(CreateEntry(key, &entry), IsOk()); | 1921 ASSERT_THAT(CreateEntry(key, &entry), IsOk()); |
| 1913 ASSERT_EQ(last_entry_size, | 1922 ASSERT_EQ(last_entry_size, |
| 1914 WriteData(entry, 0, 0, buffer.get(), last_entry_size, true)); | 1923 WriteData(entry, 0, 0, buffer.get(), last_entry_size, true)); |
| 1915 entry->Close(); | 1924 entry->Close(); |
| 1916 | 1925 |
| 1917 int new_result = CalculateSizeOfAllEntries(); | 1926 int new_result = CalculateSizeOfAllEntries(); |
| 1918 EXPECT_EQ(result + last_entry_size + GetEntryMetadataSize(key), new_result); | 1927 EXPECT_EQ( |
| 1928 result + GetRoundedSize(last_entry_size + GetEntryMetadataSize(key)), | |
| 1929 new_result); | |
| 1919 | 1930 |
| 1920 DoomEntry(key); | 1931 DoomEntry(key); |
| 1921 new_result = CalculateSizeOfAllEntries(); | 1932 new_result = CalculateSizeOfAllEntries(); |
| 1922 EXPECT_EQ(result, new_result); | 1933 EXPECT_EQ(result, new_result); |
| 1923 } | 1934 } |
| 1924 | 1935 |
| 1925 // After dooming the entries, the size should be back to zero. | 1936 // After dooming the entries, the size should be back to zero. |
| 1926 ASSERT_THAT(DoomAllEntries(), IsOk()); | 1937 ASSERT_THAT(DoomAllEntries(), IsOk()); |
| 1927 EXPECT_EQ(0, CalculateSizeOfAllEntries()); | 1938 EXPECT_EQ(0, CalculateSizeOfAllEntries()); |
| 1928 } | 1939 } |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1962 | 1973 |
| 1963 ASSERT_THAT(CreateEntry("second", &entry), IsOk()); | 1974 ASSERT_THAT(CreateEntry("second", &entry), IsOk()); |
| 1964 entry->Close(); | 1975 entry->Close(); |
| 1965 ASSERT_THAT(CreateEntry("third_entry", &entry), IsOk()); | 1976 ASSERT_THAT(CreateEntry("third_entry", &entry), IsOk()); |
| 1966 entry->Close(); | 1977 entry->Close(); |
| 1967 FlushQueueForTest(); | 1978 FlushQueueForTest(); |
| 1968 | 1979 |
| 1969 AddDelay(); | 1980 AddDelay(); |
| 1970 Time end = Time::Now(); | 1981 Time end = Time::Now(); |
| 1971 | 1982 |
| 1972 int size_1 = GetEntryMetadataSize("first"); | 1983 int size_1 = GetRoundedSize(GetEntryMetadataSize("first")); |
| 1973 int size_2 = GetEntryMetadataSize("second"); | 1984 int size_2 = GetRoundedSize(GetEntryMetadataSize("second")); |
| 1974 int size_3 = GetEntryMetadataSize("third_entry"); | 1985 int size_3 = GetRoundedSize(GetEntryMetadataSize("third_entry")); |
| 1975 | 1986 |
| 1976 ASSERT_EQ(3, cache_->GetEntryCount()); | 1987 ASSERT_EQ(3, cache_->GetEntryCount()); |
| 1977 ASSERT_EQ(CalculateSizeOfAllEntries(), | 1988 ASSERT_EQ(CalculateSizeOfAllEntries(), |
| 1978 CalculateSizeOfEntriesBetween(base::Time(), base::Time::Max())); | 1989 CalculateSizeOfEntriesBetween(base::Time(), base::Time::Max())); |
| 1979 | 1990 |
| 1980 int start_end = CalculateSizeOfEntriesBetween(start, end); | 1991 int start_end = CalculateSizeOfEntriesBetween(start, end); |
| 1981 ASSERT_EQ(CalculateSizeOfAllEntries(), start_end); | 1992 ASSERT_EQ(CalculateSizeOfAllEntries(), start_end); |
| 1982 ASSERT_EQ(size_1 + size_2 + size_3, start_end); | 1993 ASSERT_EQ(size_1 + size_2 + size_3, start_end); |
| 1983 | 1994 |
| 1984 ASSERT_EQ(size_1, CalculateSizeOfEntriesBetween(start, middle)); | 1995 ASSERT_EQ(size_1, CalculateSizeOfEntriesBetween(start, middle)); |
| (...skipping 2100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 4085 disk_cache::SimpleBackendImpl::FlushWorkerPoolForTesting(); | 4096 disk_cache::SimpleBackendImpl::FlushWorkerPoolForTesting(); |
| 4086 base::RunLoop().RunUntilIdle(); | 4097 base::RunLoop().RunUntilIdle(); |
| 4087 | 4098 |
| 4088 disk_cache::Entry* reopen_entry1; | 4099 disk_cache::Entry* reopen_entry1; |
| 4089 ASSERT_THAT(OpenEntry(key1, &reopen_entry1), IsOk()); | 4100 ASSERT_THAT(OpenEntry(key1, &reopen_entry1), IsOk()); |
| 4090 | 4101 |
| 4091 // This shouldn't pick up entry2's write time incorrectly. | 4102 // This shouldn't pick up entry2's write time incorrectly. |
| 4092 EXPECT_LE(reopen_entry1->GetLastModified(), entry1_timestamp); | 4103 EXPECT_LE(reopen_entry1->GetLastModified(), entry1_timestamp); |
| 4093 reopen_entry1->Close(); | 4104 reopen_entry1->Close(); |
| 4094 } | 4105 } |
| OLD | NEW |