Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(855)

Side by Side Diff: net/disk_cache/backend_unittest.cc

Issue 15203004: Disk cache: Reference CL for the implementation of file format version 3. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: IndexTable review Created 7 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « net/disk_cache/backend_impl.cc ('k') | net/disk_cache/block_files.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 "base/basictypes.h" 5 #include "base/basictypes.h"
6 #include "base/file_util.h" 6 #include "base/file_util.h"
7 #include "base/port.h" 7 #include "base/port.h"
8 #include "base/string_util.h" 8 #include "base/string_util.h"
9 #include "base/stringprintf.h" 9 #include "base/stringprintf.h"
10 #include "base/third_party/dynamic_annotations/dynamic_annotations.h" 10 #include "base/third_party/dynamic_annotations/dynamic_annotations.h"
(...skipping 10 matching lines...) Expand all
21 #include "net/disk_cache/entry_impl.h" 21 #include "net/disk_cache/entry_impl.h"
22 #include "net/disk_cache/histogram_macros.h" 22 #include "net/disk_cache/histogram_macros.h"
23 #include "net/disk_cache/mapped_file.h" 23 #include "net/disk_cache/mapped_file.h"
24 #include "net/disk_cache/mem_backend_impl.h" 24 #include "net/disk_cache/mem_backend_impl.h"
25 #include "net/disk_cache/simple/simple_backend_impl.h" 25 #include "net/disk_cache/simple/simple_backend_impl.h"
26 #include "net/disk_cache/simple/simple_entry_format.h" 26 #include "net/disk_cache/simple/simple_entry_format.h"
27 #include "net/disk_cache/simple/simple_util.h" 27 #include "net/disk_cache/simple/simple_util.h"
28 #include "net/disk_cache/tracing_cache_backend.h" 28 #include "net/disk_cache/tracing_cache_backend.h"
29 #include "testing/gtest/include/gtest/gtest.h" 29 #include "testing/gtest/include/gtest/gtest.h"
30 30
31 #if defined(OS_WIN) 31 using base::Time;
32 #include "base/win/scoped_handle.h"
33 #endif
34 32
35 using base::Time; 33 namespace {
34 const int kDelayToNextTimestamp = 60;
35 }
36 36
37 // Tests that can run with different types of caches. 37 // Tests that can run with different types of caches.
38 class DiskCacheBackendTest : public DiskCacheTestWithCache { 38 class DiskCacheBackendTest : public DiskCacheTestWithCache {
39 protected: 39 protected:
40 // Some utility methods:
41
42 // Perform IO operations on the cache until there is pending IO.
43 int GeneratePendingIO(net::TestCompletionCallback* cb);
44
45 // Adds 5 sparse entries. |doomed_start| and |doomed_end| if not NULL,
46 // will be filled with times, used by DoomEntriesSince and DoomEntriesBetween.
47 // There are 4 entries after doomed_start and 2 after doomed_end.
48 void InitSparseCache(base::Time* doomed_start, base::Time* doomed_end);
49
50 bool CreateSetOfRandomEntries(std::set<std::string>* key_pool);
51 bool EnumerateAndMatchKeys(int max_to_open,
52 void** iter,
53 std::set<std::string>* keys_to_match,
54 size_t* count);
55
56 // Actual tests:
40 void BackendBasics(); 57 void BackendBasics();
41 void BackendKeying(); 58 void BackendKeying();
42 void BackendShutdownWithPendingFileIO(bool fast); 59 void BackendShutdownWithPendingFileIO(bool fast);
43 void BackendShutdownWithPendingIO(bool fast); 60 void BackendShutdownWithPendingIO(bool fast);
44 void BackendShutdownWithPendingCreate(bool fast); 61 void BackendShutdownWithPendingCreate(bool fast);
45 void BackendSetSize(); 62 void BackendSetSize();
46 void BackendLoad(); 63 void BackendLoad();
47 void BackendChain(); 64 void BackendChain();
65 void BucketUse();
66 void BackendNewEvictionTrim();
48 void BackendValidEntry(); 67 void BackendValidEntry();
49 void BackendInvalidEntry(); 68 void BackendInvalidEntry();
50 void BackendInvalidEntryRead(); 69 void BackendInvalidEntryRead();
51 void BackendInvalidEntryWithLoad(); 70 void BackendInvalidEntryWithLoad();
52 void BackendTrimInvalidEntry(); 71 void BackendTrimInvalidEntry();
53 void BackendTrimInvalidEntry2(); 72 void BackendTrimInvalidEntry2();
54 void BackendEnumerations(); 73 void BackendEnumerations();
55 void BackendEnumerations2(); 74 void BackendEnumerations2();
56 void BackendInvalidEntryEnumeration(); 75 void BackendInvalidEntryEnumeration();
57 void BackendFixEnumerators(); 76 void BackendFixEnumerators();
58 void BackendDoomRecent(); 77 void BackendDoomRecent();
59
60 // Adds 5 sparse entries. |doomed_start| and |doomed_end| if not NULL,
61 // will be filled with times, used by DoomEntriesSince and DoomEntriesBetween.
62 // There are 4 entries after doomed_start and 2 after doomed_end.
63 void InitSparseCache(base::Time* doomed_start, base::Time* doomed_end);
64
65 void BackendDoomBetween(); 78 void BackendDoomBetween();
66 void BackendTransaction(const std::string& name, int num_entries, bool load); 79 void BackendTransaction(const std::string& name, int num_entries, bool load);
67 void BackendRecoverInsert(); 80 void BackendRecoverInsert();
68 void BackendRecoverRemove(); 81 void BackendRecoverRemove();
69 void BackendRecoverWithEviction(); 82 void BackendRecoverWithEviction();
70 void BackendInvalidEntry2(); 83 void BackendInvalidEntry2();
71 void BackendInvalidEntry3(); 84 void BackendInvalidEntry3();
72 void BackendInvalidEntry7(); 85 void BackendInvalidEntry7();
73 void BackendInvalidEntry8(); 86 void BackendInvalidEntry8();
74 void BackendInvalidEntry9(bool eviction); 87 void BackendInvalidEntry9(bool eviction);
75 void BackendInvalidEntry10(bool eviction); 88 void BackendInvalidEntry10(bool eviction);
76 void BackendInvalidEntry11(bool eviction); 89 void BackendInvalidEntry11(bool eviction);
77 void BackendTrimInvalidEntry12(); 90 void BackendTrimInvalidEntry12();
78 void BackendDoomAll(); 91 void BackendDoomAll();
79 void BackendDoomAll2(); 92 void BackendDoomAll2();
80 void BackendInvalidRankings(); 93 void BackendInvalidRankings();
81 void BackendInvalidRankings2(); 94 void BackendInvalidRankings2();
82 void BackendDisable(); 95 void BackendDisable();
83 void BackendDisable2(); 96 void BackendDisable2();
84 void BackendDisable3(); 97 void BackendDisable3();
85 void BackendDisable4(); 98 void BackendDisable4();
99 void BackendTotalBuffersSize1();
100 void BackendTotalBuffersSize2();
101 void BackendUpdateRankForExternalCacheHit();
86 void TracingBackendBasics(); 102 void TracingBackendBasics();
87 }; 103 };
88 104
105 int DiskCacheBackendTest::GeneratePendingIO(net::TestCompletionCallback* cb) {
106 if (!use_current_thread_) {
107 ADD_FAILURE();
108 return net::ERR_FAILED;
109 }
110
111 disk_cache::Entry* entry;
112 int rv = cache_->CreateEntry("some key", &entry, cb->callback());
113 if (cb->GetResult(rv) != net::OK)
114 return net::ERR_CACHE_CREATE_FAILURE;
115
116 const int kSize = 25000;
117 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kSize));
118 CacheTestFillBuffer(buffer->data(), kSize, false);
119
120 for (int i = 0; i < 10 * 1024 * 1024; i += 64 * 1024) {
121 // We are using the current thread as the cache thread because we want to
122 // be able to call directly this method to make sure that the OS (instead
123 // of us switching thread) is returning IO pending.
124 if (!simple_cache_mode_) {
125 rv = static_cast<disk_cache::EntryImpl*>(entry)->WriteDataImpl(
126 0, i, buffer.get(), kSize, cb->callback(), false);
127 } else {
128 rv = entry->WriteData(0, i, buffer.get(), kSize, cb->callback(), false);
129 }
130
131 if (rv == net::ERR_IO_PENDING)
132 break;
133 if (rv != kSize)
134 rv = net::ERR_FAILED;
135 }
136
137 // Don't call Close() to avoid going through the queue or we'll deadlock
138 // waiting for the operation to finish.
139 if (!simple_cache_mode_)
140 static_cast<disk_cache::EntryImpl*>(entry)->Release();
141 else
142 entry->Close();
143
144 return rv;
145 }
146
147 void DiskCacheBackendTest::InitSparseCache(base::Time* doomed_start,
148 base::Time* doomed_end) {
149 InitCache();
150
151 const int kSize = 50;
152 // This must be greater then MemEntryImpl::kMaxSparseEntrySize.
153 const int kOffset = 10 + 1024 * 1024;
154
155 disk_cache::Entry* entry0 = NULL;
156 disk_cache::Entry* entry1 = NULL;
157 disk_cache::Entry* entry2 = NULL;
158
159 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kSize));
160 CacheTestFillBuffer(buffer->data(), kSize, false);
161
162 ASSERT_EQ(net::OK, CreateEntry("zeroth", &entry0));
163 ASSERT_EQ(kSize, WriteSparseData(entry0, 0, buffer.get(), kSize));
164 ASSERT_EQ(kSize,
165 WriteSparseData(entry0, kOffset + kSize, buffer.get(), kSize));
166 entry0->Close();
167
168 FlushQueueForTest();
169 AddDelay();
170 if (doomed_start)
171 *doomed_start = base::Time::Now();
172
173 // Order in rankings list:
174 // first_part1, first_part2, second_part1, second_part2
175 ASSERT_EQ(net::OK, CreateEntry("first", &entry1));
176 ASSERT_EQ(kSize, WriteSparseData(entry1, 0, buffer.get(), kSize));
177 ASSERT_EQ(kSize,
178 WriteSparseData(entry1, kOffset + kSize, buffer.get(), kSize));
179 entry1->Close();
180
181 ASSERT_EQ(net::OK, CreateEntry("second", &entry2));
182 ASSERT_EQ(kSize, WriteSparseData(entry2, 0, buffer.get(), kSize));
183 ASSERT_EQ(kSize,
184 WriteSparseData(entry2, kOffset + kSize, buffer.get(), kSize));
185 entry2->Close();
186
187 FlushQueueForTest();
188 AddDelay();
189 if (doomed_end)
190 *doomed_end = base::Time::Now();
191
192 // Order in rankings list:
193 // third_part1, fourth_part1, third_part2, fourth_part2
194 disk_cache::Entry* entry3 = NULL;
195 disk_cache::Entry* entry4 = NULL;
196 ASSERT_EQ(net::OK, CreateEntry("third", &entry3));
197 ASSERT_EQ(kSize, WriteSparseData(entry3, 0, buffer.get(), kSize));
198 ASSERT_EQ(net::OK, CreateEntry("fourth", &entry4));
199 ASSERT_EQ(kSize, WriteSparseData(entry4, 0, buffer.get(), kSize));
200 ASSERT_EQ(kSize,
201 WriteSparseData(entry3, kOffset + kSize, buffer.get(), kSize));
202 ASSERT_EQ(kSize,
203 WriteSparseData(entry4, kOffset + kSize, buffer.get(), kSize));
204 entry3->Close();
205 entry4->Close();
206
207 FlushQueueForTest();
208 AddDelay();
209 }
210
211 // Creates entries based on random keys. Stores these keys in |key_pool|.
212 bool DiskCacheBackendTest::CreateSetOfRandomEntries(
213 std::set<std::string>* key_pool) {
214 const int kNumEntries = 10;
215
216 for (int i = 0; i < kNumEntries; ++i) {
217 std::string key = GenerateKey(true);
218 disk_cache::Entry* entry;
219 if (CreateEntry(key, &entry) != net::OK)
220 return false;
221 key_pool->insert(key);
222 entry->Close();
223 }
224 return key_pool->size() == implicit_cast<size_t>(cache_->GetEntryCount());
225 }
226
227 // Performs iteration over the backend and checks that the keys of entries
228 // opened are in |keys_to_match|, then erases them. Up to |max_to_open| entries
229 // will be opened, if it is positive. Otherwise, iteration will continue until
230 // OpenNextEntry stops returning net::OK.
231 bool DiskCacheBackendTest::EnumerateAndMatchKeys(
232 int max_to_open,
233 void** iter,
234 std::set<std::string>* keys_to_match,
235 size_t* count) {
236 disk_cache::Entry* entry;
237
238 while (OpenNextEntry(iter, &entry) == net::OK) {
239 if (!entry)
240 return false;
241 EXPECT_EQ(1U, keys_to_match->erase(entry->GetKey()));
242 entry->Close();
243 ++(*count);
244 if (max_to_open >= 0 && implicit_cast<int>(*count) >= max_to_open)
245 break;
246 };
247
248 return true;
249 }
250
89 void DiskCacheBackendTest::BackendBasics() { 251 void DiskCacheBackendTest::BackendBasics() {
90 InitCache(); 252 InitCache();
91 disk_cache::Entry *entry1 = NULL, *entry2 = NULL; 253 disk_cache::Entry *entry1 = NULL, *entry2 = NULL;
92 EXPECT_NE(net::OK, OpenEntry("the first key", &entry1)); 254 EXPECT_NE(net::OK, OpenEntry("the first key", &entry1));
93 ASSERT_EQ(net::OK, CreateEntry("the first key", &entry1)); 255 ASSERT_EQ(net::OK, CreateEntry("the first key", &entry1));
94 ASSERT_TRUE(NULL != entry1); 256 ASSERT_TRUE(NULL != entry1);
95 entry1->Close(); 257 entry1->Close();
96 entry1 = NULL; 258 entry1 = NULL;
97 259
98 ASSERT_EQ(net::OK, OpenEntry("the first key", &entry1)); 260 ASSERT_EQ(net::OK, OpenEntry("the first key", &entry1));
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
149 TEST_F(DiskCacheBackendTest, AppCacheBasics) { 311 TEST_F(DiskCacheBackendTest, AppCacheBasics) {
150 SetCacheType(net::APP_CACHE); 312 SetCacheType(net::APP_CACHE);
151 BackendBasics(); 313 BackendBasics();
152 } 314 }
153 315
154 TEST_F(DiskCacheBackendTest, ShaderCacheBasics) { 316 TEST_F(DiskCacheBackendTest, ShaderCacheBasics) {
155 SetCacheType(net::SHADER_CACHE); 317 SetCacheType(net::SHADER_CACHE);
156 BackendBasics(); 318 BackendBasics();
157 } 319 }
158 320
321 TEST_F(DiskCacheBackendTest, V3Basics) {
322 UseVersion3();
323 BackendBasics();
324 }
325
159 void DiskCacheBackendTest::BackendKeying() { 326 void DiskCacheBackendTest::BackendKeying() {
160 InitCache(); 327 InitCache();
161 const char* kName1 = "the first key"; 328 const char* kName1 = "the first key";
162 const char* kName2 = "the first Key"; 329 const char* kName2 = "the first Key";
163 disk_cache::Entry *entry1, *entry2; 330 disk_cache::Entry *entry1, *entry2;
164 ASSERT_EQ(net::OK, CreateEntry(kName1, &entry1)); 331 ASSERT_EQ(net::OK, CreateEntry(kName1, &entry1));
165 332
166 ASSERT_EQ(net::OK, CreateEntry(kName2, &entry2)); 333 ASSERT_EQ(net::OK, CreateEntry(kName2, &entry2));
167 EXPECT_TRUE(entry1 != entry2) << "Case sensitive"; 334 EXPECT_TRUE(entry1 != entry2) << "Case sensitive";
168 entry2->Close(); 335 entry2->Close();
(...skipping 25 matching lines...) Expand all
194 buffer2[19999] = '\0'; 361 buffer2[19999] = '\0';
195 ASSERT_EQ(net::OK, CreateEntry(buffer2, &entry2)) << "key on external file"; 362 ASSERT_EQ(net::OK, CreateEntry(buffer2, &entry2)) << "key on external file";
196 entry2->Close(); 363 entry2->Close();
197 entry1->Close(); 364 entry1->Close();
198 } 365 }
199 366
200 TEST_F(DiskCacheBackendTest, Keying) { 367 TEST_F(DiskCacheBackendTest, Keying) {
201 BackendKeying(); 368 BackendKeying();
202 } 369 }
203 370
371 TEST_F(DiskCacheBackendTest, V3Keying) {
372 UseVersion3();
373 BackendKeying();
374 }
375
204 TEST_F(DiskCacheBackendTest, NewEvictionKeying) { 376 TEST_F(DiskCacheBackendTest, NewEvictionKeying) {
205 SetNewEviction(); 377 SetNewEviction();
206 BackendKeying(); 378 BackendKeying();
207 } 379 }
208 380
381 TEST_F(DiskCacheBackendTest, V3NewEvictionKeying) {
382 UseVersion3();
383 SetNewEviction();
384 BackendKeying();
385 }
386
209 TEST_F(DiskCacheBackendTest, MemoryOnlyKeying) { 387 TEST_F(DiskCacheBackendTest, MemoryOnlyKeying) {
210 SetMemoryOnlyMode(); 388 SetMemoryOnlyMode();
211 BackendKeying(); 389 BackendKeying();
212 } 390 }
213 391
214 TEST_F(DiskCacheBackendTest, AppCacheKeying) { 392 TEST_F(DiskCacheBackendTest, AppCacheKeying) {
215 SetCacheType(net::APP_CACHE); 393 SetCacheType(net::APP_CACHE);
216 BackendKeying(); 394 BackendKeying();
217 } 395 }
218 396
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
288 base::FilePath filename = cache_path_.AppendASCII("f_000001"); 466 base::FilePath filename = cache_path_.AppendASCII("f_000001");
289 467
290 const int kSize = 50; 468 const int kSize = 50;
291 scoped_refptr<net::IOBuffer> buffer1(new net::IOBuffer(kSize)); 469 scoped_refptr<net::IOBuffer> buffer1(new net::IOBuffer(kSize));
292 CacheTestFillBuffer(buffer1->data(), kSize, false); 470 CacheTestFillBuffer(buffer1->data(), kSize, false);
293 ASSERT_EQ(kSize, file_util::WriteFile(filename, buffer1->data(), kSize)); 471 ASSERT_EQ(kSize, file_util::WriteFile(filename, buffer1->data(), kSize));
294 472
295 // Now let's create a file with the cache. 473 // Now let's create a file with the cache.
296 disk_cache::Entry* entry; 474 disk_cache::Entry* entry;
297 ASSERT_EQ(net::OK, CreateEntry("key", &entry)); 475 ASSERT_EQ(net::OK, CreateEntry("key", &entry));
298 ASSERT_EQ(0, WriteData(entry, 0, 20000, buffer1, 0, false)); 476 ASSERT_EQ(0, WriteData(entry, 0, 20000, buffer1.get(), 0, false));
299 entry->Close(); 477 entry->Close();
300 478
301 // And verify that the first file is still there. 479 // And verify that the first file is still there.
302 scoped_refptr<net::IOBuffer> buffer2(new net::IOBuffer(kSize)); 480 scoped_refptr<net::IOBuffer> buffer2(new net::IOBuffer(kSize));
303 ASSERT_EQ(kSize, file_util::ReadFile(filename, buffer2->data(), kSize)); 481 ASSERT_EQ(kSize, file_util::ReadFile(filename, buffer2->data(), kSize));
304 EXPECT_EQ(0, memcmp(buffer1->data(), buffer2->data(), kSize)); 482 EXPECT_EQ(0, memcmp(buffer1->data(), buffer2->data(), kSize));
305 } 483 }
306 484
307 // Tests that we deal with file-level pending operations at destruction time. 485 // Tests that we deal with file-level pending operations at destruction time.
308 void DiskCacheBackendTest::BackendShutdownWithPendingFileIO(bool fast) { 486 void DiskCacheBackendTest::BackendShutdownWithPendingFileIO(bool fast) {
487 ASSERT_TRUE(CleanupCacheDir());
488 if (fast)
489 AvoidTestFlag();
490
491 UseCurrentThread();
492 CreateBackend(NULL);
493 SetNoBuffering();
494
309 net::TestCompletionCallback cb; 495 net::TestCompletionCallback cb;
310 int rv; 496 int rv = GeneratePendingIO(&cb);
311 497
312 { 498 // The cache destructor will see one pending operation here.
313 ASSERT_TRUE(CleanupCacheDir()); 499 delete cache_;
314 base::Thread cache_thread("CacheThread"); 500 // Prevent the TearDown() to delete the backend again.
315 ASSERT_TRUE(cache_thread.StartWithOptions( 501 cache_ = NULL;
316 base::Thread::Options(MessageLoop::TYPE_IO, 0)));
317 502
318 uint32 flags = disk_cache::kNoBuffering; 503 if (rv == net::ERR_IO_PENDING) {
319 if (!fast) 504 if (fast)
320 flags |= disk_cache::kNoRandom; 505 EXPECT_FALSE(cb.have_result());
321 506 else
322 UseCurrentThread(); 507 EXPECT_TRUE(cb.have_result());
323 CreateBackend(flags, NULL);
324
325 disk_cache::EntryImpl* entry;
326 rv = cache_->CreateEntry(
327 "some key", reinterpret_cast<disk_cache::Entry**>(&entry),
328 cb.callback());
329 ASSERT_EQ(net::OK, cb.GetResult(rv));
330
331 const int kSize = 25000;
332 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kSize));
333 CacheTestFillBuffer(buffer->data(), kSize, false);
334
335 for (int i = 0; i < 10 * 1024 * 1024; i += 64 * 1024) {
336 // We are using the current thread as the cache thread because we want to
337 // be able to call directly this method to make sure that the OS (instead
338 // of us switching thread) is returning IO pending.
339 rv = entry->WriteDataImpl(0, i, buffer, kSize, cb.callback(), false);
340 if (rv == net::ERR_IO_PENDING)
341 break;
342 EXPECT_EQ(kSize, rv);
343 }
344
345 // Don't call Close() to avoid going through the queue or we'll deadlock
346 // waiting for the operation to finish.
347 entry->Release();
348
349 // The cache destructor will see one pending operation here.
350 delete cache_;
351 // Prevent the TearDown() to delete the backend again.
352 cache_ = NULL;
353
354 if (rv == net::ERR_IO_PENDING) {
355 if (fast)
356 EXPECT_FALSE(cb.have_result());
357 else
358 EXPECT_TRUE(cb.have_result());
359 }
360 } 508 }
361 509
362 MessageLoop::current()->RunUntilIdle(); 510 MessageLoop::current()->RunUntilIdle();
363 511
364 #if defined(OS_WIN)
365 // Wait for the actual operation to complete, or we'll keep a file handle that 512 // Wait for the actual operation to complete, or we'll keep a file handle that
366 // may cause issues later. Note that on Posix systems even though this test 513 // may cause issues later.
367 // uses a single thread, the actual IO is posted to a worker thread and the
368 // cache destructor breaks the link to reach cb when the operation completes.
369 rv = cb.GetResult(rv); 514 rv = cb.GetResult(rv);
370 #endif
371 } 515 }
372 516
373 TEST_F(DiskCacheBackendTest, ShutdownWithPendingFileIO) { 517 TEST_F(DiskCacheBackendTest, ShutdownWithPendingFileIO) {
374 BackendShutdownWithPendingFileIO(false); 518 BackendShutdownWithPendingFileIO(false);
375 } 519 }
376 520
377 // We'll be leaking from this test. 521 // We'll be leaking from this test.
378 TEST_F(DiskCacheBackendTest, ShutdownWithPendingFileIO_Fast) { 522 TEST_F(DiskCacheBackendTest, ShutdownWithPendingFileIO_Fast) {
379 // The integrity test sets kNoRandom so there's a version mismatch if we don't 523 // The integrity test sets kNoRandom so there's a version mismatch if we don't
380 // force new eviction. 524 // force new eviction.
381 SetNewEviction(); 525 SetNewEviction();
382 BackendShutdownWithPendingFileIO(true); 526 BackendShutdownWithPendingFileIO(true);
383 } 527 }
384 528
385 // Tests that we deal with background-thread pending operations. 529 // Tests that we deal with background-thread pending operations.
386 void DiskCacheBackendTest::BackendShutdownWithPendingIO(bool fast) { 530 void DiskCacheBackendTest::BackendShutdownWithPendingIO(bool fast) {
387 net::TestCompletionCallback cb; 531 net::TestCompletionCallback cb;
388 532
389 { 533 {
390 ASSERT_TRUE(CleanupCacheDir()); 534 ASSERT_TRUE(CleanupCacheDir());
391 base::Thread cache_thread("CacheThread"); 535 base::Thread cache_thread("CacheThread");
392 ASSERT_TRUE(cache_thread.StartWithOptions( 536 ASSERT_TRUE(cache_thread.StartWithOptions(
393 base::Thread::Options(MessageLoop::TYPE_IO, 0))); 537 base::Thread::Options(MessageLoop::TYPE_IO, 0)));
394 538
395 uint32 flags = disk_cache::kNoBuffering; 539 if (fast)
396 if (!fast) 540 AvoidTestFlag();
397 flags |= disk_cache::kNoRandom;
398 541
399 CreateBackend(flags, &cache_thread); 542 CreateBackend(&cache_thread);
543 SetNoBuffering();
400 544
401 disk_cache::Entry* entry; 545 disk_cache::Entry* entry;
402 int rv = cache_->CreateEntry("some key", &entry, cb.callback()); 546 int rv = cache_->CreateEntry("some key", &entry, cb.callback());
403 ASSERT_EQ(net::OK, cb.GetResult(rv)); 547 ASSERT_EQ(net::OK, cb.GetResult(rv));
404 548
405 entry->Close(); 549 entry->Close();
406 550
407 // The cache destructor will see one pending operation here. 551 // The cache destructor will see one pending operation here.
408 delete cache_; 552 delete cache_;
409 // Prevent the TearDown() to delete the backend again. 553 // Prevent the TearDown() to delete the backend again.
(...skipping 18 matching lines...) Expand all
428 // Tests that we deal with create-type pending operations. 572 // Tests that we deal with create-type pending operations.
429 void DiskCacheBackendTest::BackendShutdownWithPendingCreate(bool fast) { 573 void DiskCacheBackendTest::BackendShutdownWithPendingCreate(bool fast) {
430 net::TestCompletionCallback cb; 574 net::TestCompletionCallback cb;
431 575
432 { 576 {
433 ASSERT_TRUE(CleanupCacheDir()); 577 ASSERT_TRUE(CleanupCacheDir());
434 base::Thread cache_thread("CacheThread"); 578 base::Thread cache_thread("CacheThread");
435 ASSERT_TRUE(cache_thread.StartWithOptions( 579 ASSERT_TRUE(cache_thread.StartWithOptions(
436 base::Thread::Options(MessageLoop::TYPE_IO, 0))); 580 base::Thread::Options(MessageLoop::TYPE_IO, 0)));
437 581
438 disk_cache::BackendFlags flags = 582 if (fast)
439 fast ? disk_cache::kNone : disk_cache::kNoRandom; 583 AvoidTestFlag();
440 CreateBackend(flags, &cache_thread); 584 CreateBackend(&cache_thread);
441 585
442 disk_cache::Entry* entry; 586 disk_cache::Entry* entry;
443 int rv = cache_->CreateEntry("some key", &entry, cb.callback()); 587 int rv = cache_->CreateEntry("some key", &entry, cb.callback());
444 ASSERT_EQ(net::ERR_IO_PENDING, rv); 588 ASSERT_EQ(net::ERR_IO_PENDING, rv);
445 589
446 delete cache_; 590 delete cache_;
447 // Prevent the TearDown() to delete the backend again. 591 // Prevent the TearDown() to delete the backend again.
448 cache_ = NULL; 592 cache_ = NULL;
449 EXPECT_FALSE(cb.have_result()); 593 EXPECT_FALSE(cb.have_result());
450 } 594 }
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
489 SetMaxSize(cache_size); 633 SetMaxSize(cache_size);
490 InitCache(); 634 InitCache();
491 635
492 std::string first("some key"); 636 std::string first("some key");
493 std::string second("something else"); 637 std::string second("something else");
494 disk_cache::Entry* entry; 638 disk_cache::Entry* entry;
495 ASSERT_EQ(net::OK, CreateEntry(first, &entry)); 639 ASSERT_EQ(net::OK, CreateEntry(first, &entry));
496 640
497 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(cache_size)); 641 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(cache_size));
498 memset(buffer->data(), 0, cache_size); 642 memset(buffer->data(), 0, cache_size);
499 EXPECT_EQ(cache_size / 10, WriteData(entry, 0, 0, buffer, cache_size / 10, 643 EXPECT_EQ(cache_size / 10,
500 false)) << "normal file"; 644 WriteData(entry, 0, 0, buffer.get(), cache_size / 10, false)) <<
645 "normal file";
501 646
502 EXPECT_EQ(net::ERR_FAILED, WriteData(entry, 1, 0, buffer, cache_size / 5, 647 EXPECT_EQ(net::ERR_FAILED,
503 false)) << "file size above the limit"; 648 WriteData(entry, 1, 0, buffer.get(), cache_size / 5, false)) <<
649 "file size above the limit";
504 650
505 // By doubling the total size, we make this file cacheable. 651 // By doubling the total size, we make this file cacheable.
506 SetMaxSize(cache_size * 2); 652 SetMaxSize(cache_size * 2);
507 EXPECT_EQ(cache_size / 5, WriteData(entry, 1, 0, buffer, cache_size / 5, 653 EXPECT_EQ(cache_size / 5, WriteData(entry, 1, 0, buffer.get(), cache_size / 5,
508 false)); 654 false));
509 655
510 // Let's fill up the cache!. 656 // Let's fill up the cache!.
511 SetMaxSize(cache_size * 10); 657 SetMaxSize(cache_size * 10);
512 EXPECT_EQ(cache_size * 3 / 4, WriteData(entry, 0, 0, buffer, 658 EXPECT_EQ(cache_size * 3 / 4, WriteData(entry, 0, 0, buffer.get(),
513 cache_size * 3 / 4, false)); 659 cache_size * 3 / 4, false));
514 entry->Close(); 660 entry->Close();
515 FlushQueueForTest(); 661 FlushQueueForTest();
516 662
517 SetMaxSize(cache_size); 663 SetMaxSize(cache_size);
518 664
519 // The cache is 95% full. 665 // The cache is 95% full.
520 666
521 ASSERT_EQ(net::OK, CreateEntry(second, &entry)); 667 ASSERT_EQ(net::OK, CreateEntry(second, &entry));
522 EXPECT_EQ(cache_size / 10, WriteData(entry, 0, 0, buffer, cache_size / 10, 668 EXPECT_EQ(cache_size / 10,
523 false)); 669 WriteData(entry, 0, 0, buffer.get(), cache_size / 10, false));
524 670
525 disk_cache::Entry* entry2; 671 disk_cache::Entry* entry2;
526 ASSERT_EQ(net::OK, CreateEntry("an extra key", &entry2)); 672 std::string extra_key("an extra key");
527 EXPECT_EQ(cache_size / 10, WriteData(entry2, 0, 0, buffer, cache_size / 10, 673 ASSERT_EQ(net::OK, CreateEntry(extra_key, &entry2));
528 false)); 674 EXPECT_EQ(cache_size / 10, WriteData(entry2, 0, 0, buffer.get(),
675 cache_size / 10, false));
529 entry2->Close(); // This will trigger the cache trim. 676 entry2->Close(); // This will trigger the cache trim.
530 677 WaitForEntryToClose(extra_key);
531 EXPECT_NE(net::OK, OpenEntry(first, &entry2));
532 678
533 FlushQueueForTest(); // Make sure that we are done trimming the cache. 679 FlushQueueForTest(); // Make sure that we are done trimming the cache.
534 FlushQueueForTest(); // We may have posted two tasks to evict stuff. 680 FlushQueueForTest(); // We may have posted two tasks to evict stuff.
535 681
536 entry->Close(); 682 entry->Close();
683 WaitForEntryToClose(second);
684
685 EXPECT_NE(net::OK, OpenEntry(first, &entry2));
537 ASSERT_EQ(net::OK, OpenEntry(second, &entry)); 686 ASSERT_EQ(net::OK, OpenEntry(second, &entry));
538 EXPECT_EQ(cache_size / 10, entry->GetDataSize(0)); 687 EXPECT_EQ(cache_size / 10, entry->GetDataSize(0));
539 entry->Close(); 688 entry->Close();
540 } 689 }
541 690
542 TEST_F(DiskCacheBackendTest, SetSize) { 691 TEST_F(DiskCacheBackendTest, SetSize) {
543 BackendSetSize(); 692 BackendSetSize();
544 } 693 }
545 694
695 TEST_F(DiskCacheBackendTest, V3SetSize) {
696 UseVersion3();
697 UseVersion3();
698 SetNewEviction();
699 BackendSetSize();
700 }
701
546 TEST_F(DiskCacheBackendTest, NewEvictionSetSize) { 702 TEST_F(DiskCacheBackendTest, NewEvictionSetSize) {
547 SetNewEviction(); 703 SetNewEviction();
548 BackendSetSize(); 704 BackendSetSize();
549 } 705 }
550 706
707 TEST_F(DiskCacheBackendTest, V3NewEvictionSetSize) {
708 UseVersion3();
709 SetNewEviction();
710 BackendSetSize();
711 }
712
551 TEST_F(DiskCacheBackendTest, MemoryOnlySetSize) { 713 TEST_F(DiskCacheBackendTest, MemoryOnlySetSize) {
552 SetMemoryOnlyMode(); 714 SetMemoryOnlyMode();
553 BackendSetSize(); 715 BackendSetSize();
554 } 716 }
555 717
556 void DiskCacheBackendTest::BackendLoad() { 718 void DiskCacheBackendTest::BackendLoad() {
557 InitCache(); 719 InitCache();
558 int seed = static_cast<int>(Time::Now().ToInternalValue()); 720 int seed = static_cast<int>(Time::Now().ToInternalValue());
559 srand(seed); 721 srand(seed);
560 722
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after
647 TEST_F(DiskCacheBackendTest, AppCacheChain) { 809 TEST_F(DiskCacheBackendTest, AppCacheChain) {
648 SetCacheType(net::APP_CACHE); 810 SetCacheType(net::APP_CACHE);
649 BackendChain(); 811 BackendChain();
650 } 812 }
651 813
652 TEST_F(DiskCacheBackendTest, ShaderCacheChain) { 814 TEST_F(DiskCacheBackendTest, ShaderCacheChain) {
653 SetCacheType(net::SHADER_CACHE); 815 SetCacheType(net::SHADER_CACHE);
654 BackendChain(); 816 BackendChain();
655 } 817 }
656 818
657 TEST_F(DiskCacheBackendTest, NewEvictionTrim) { 819 // Tests the proper use of cell buckets while the index grow.
820 void DiskCacheBackendTest::BucketUse() {
821 UseVersion3();
822 InitCache();
823 const int kNumEntries = 20;
824 disk_cache::Entry* entries[kNumEntries];
825 std::string key("The first key");
826
827 // This generates kNumEntries collisions so the extra table has to be used.
828 // There are only 2 extra buckets on the test setup, so the index has to grow.
829 for (int i = 0; i < kNumEntries; i++) {
830 ASSERT_EQ(net::OK, CreateEntry(key, &entries[i]));
831 entries[i]->Doom();
832 FlushQueueForTest();
833 }
834
835 for (int i = 0; i < kNumEntries / 2; i++)
836 entries[i]->Close();
837
838 for (int i = 0; i < kNumEntries / 2; i++) {
839 ASSERT_EQ(net::OK, CreateEntry(key, &entries[i]));
840 entries[i]->Doom();
841 }
842
843 for (int i = 0; i < kNumEntries; i++)
844 entries[i]->Close();
845 }
846
847 TEST_F(DiskCacheBackendTest, V3BucketUse) {
848 BucketUse();
849 }
850
851 TEST_F(DiskCacheBackendTest, V3DoubleIndexBucketUse) {
852 PresetTestMode(); // Doubles the index instead of a slow growth.
853 BucketUse();
854 }
855
856 TEST_F(DiskCacheBackendTest, V3DoubleIndex) {
857 UseVersion3();
858 PresetTestMode();
859 InitCache();
860
861 disk_cache::Entry* entry;
862 const int kNumEntries = 200;
863 for (int i = 0; i < kNumEntries; i++) {
864 std::string name(base::StringPrintf("Key %d", i));
865 ASSERT_EQ(net::OK, CreateEntry(name, &entry));
866 entry->Close();
867 FlushQueueForTest();
868 }
869
870 // Generate enough collisions to force growing the extra table (and with it,
871 // the index).
872 const int kNumSavedEntries = 5;
873 disk_cache::Entry* entries[kNumSavedEntries];
874 std::string key("The first key");
875 for (int i = 0; i < kNumSavedEntries; i++) {
876 ASSERT_EQ(net::OK, CreateEntry(key, &entries[i]));
877 entries[i]->Doom();
878 FlushQueueForTest();
879 }
880
881 for (int i = 0; i < kNumSavedEntries; i++)
882 entries[i]->Close();
883
884 // Verify that all entries are there.
885 for (int i = 0; i < kNumEntries; i++) {
886 std::string name(base::StringPrintf("Key %d", i));
887 ASSERT_EQ(net::OK, OpenEntry(name, &entry));
888 entry->Close();
889 }
890 }
891
892 // This test leaks memory because it simulates a crash.
893 TEST_F(DiskCacheBackendTest, DISABLED_V3Backup) {
894 UseVersion3();
895 PresetTestMode();
896 InitCache();
897
898 disk_cache::Entry* entry;
899 std::string name1("First entry");
900 ASSERT_EQ(net::OK, CreateEntry(name1, &entry));
901 entry->Close();
902 WaitForEntryToClose(name1);
903
904 std::string name2("Another entry");
905 ASSERT_EQ(net::OK, CreateEntry(name2, &entry));
906
907 SimulateCrash();
908 EXPECT_EQ(net::OK, OpenEntry(name1, &entry));
909 entry->Close();
910 EXPECT_NE(net::OK, OpenEntry(name2, &entry));
911 }
912
913 void DiskCacheBackendTest::BackendNewEvictionTrim() {
658 SetNewEviction(); 914 SetNewEviction();
659 InitCache(); 915 InitCache();
660 916
661 disk_cache::Entry* entry; 917 disk_cache::Entry* entry;
662 for (int i = 0; i < 100; i++) { 918 for (int i = 0; i < 100; i++) {
663 std::string name(base::StringPrintf("Key %d", i)); 919 std::string name(base::StringPrintf("Key %d", i));
664 ASSERT_EQ(net::OK, CreateEntry(name, &entry)); 920 ASSERT_EQ(net::OK, CreateEntry(name, &entry));
665 entry->Close(); 921 entry->Close();
922 AddDelayForTest(kDelayToNextTimestamp);
923
666 if (i < 90) { 924 if (i < 90) {
667 // Entries 0 to 89 are in list 1; 90 to 99 are in list 0. 925 // Entries 0 to 89 are in list 1; 90 to 99 are in list 0.
668 ASSERT_EQ(net::OK, OpenEntry(name, &entry)); 926 ASSERT_EQ(net::OK, OpenEntry(name, &entry));
669 entry->Close(); 927 entry->Close();
670 } 928 }
929 if (!(i % 10))
930 FlushQueueForTest();
931
932 if (i == 0 || i == 90)
933 WaitForEntryToClose(name);
671 } 934 }
672 935
673 // The first eviction must come from list 1 (10% limit), the second must come 936 // The first eviction must come from list 1 (10% limit), the second must come
674 // from list 0. 937 // from list 0.
675 TrimForTest(false); 938 TrimForTest(false);
676 EXPECT_NE(net::OK, OpenEntry("Key 0", &entry)); 939 EXPECT_NE(net::OK, OpenEntry("Key 0", &entry));
677 TrimForTest(false); 940 TrimForTest(false);
678 EXPECT_NE(net::OK, OpenEntry("Key 90", &entry)); 941 EXPECT_NE(net::OK, OpenEntry("Key 90", &entry));
679 942
680 // Double check that we still have the list tails. 943 // Double check that we still have the list tails.
681 ASSERT_EQ(net::OK, OpenEntry("Key 1", &entry)); 944 ASSERT_EQ(net::OK, OpenEntry("Key 1", &entry));
682 entry->Close(); 945 entry->Close();
683 ASSERT_EQ(net::OK, OpenEntry("Key 91", &entry)); 946 ASSERT_EQ(net::OK, OpenEntry("Key 91", &entry));
684 entry->Close(); 947 entry->Close();
685 } 948 }
686 949
950 TEST_F(DiskCacheBackendTest, NewEvictionTrim) {
951 BackendNewEvictionTrim();
952 }
953
954 TEST_F(DiskCacheBackendTest, V3NewEvictionTrim) {
955 UseVersion3();
956 BackendNewEvictionTrim();
957 }
958
687 // Before looking for invalid entries, let's check a valid entry. 959 // Before looking for invalid entries, let's check a valid entry.
688 void DiskCacheBackendTest::BackendValidEntry() { 960 void DiskCacheBackendTest::BackendValidEntry() {
689 InitCache(); 961 InitCache();
690 962
691 std::string key("Some key"); 963 std::string key("Some key");
692 disk_cache::Entry* entry; 964 disk_cache::Entry* entry;
693 ASSERT_EQ(net::OK, CreateEntry(key, &entry)); 965 ASSERT_EQ(net::OK, CreateEntry(key, &entry));
694 966
695 const int kSize = 50; 967 const int kSize = 50;
696 scoped_refptr<net::IOBuffer> buffer1(new net::IOBuffer(kSize)); 968 scoped_refptr<net::IOBuffer> buffer1(new net::IOBuffer(kSize));
697 memset(buffer1->data(), 0, kSize); 969 memset(buffer1->data(), 0, kSize);
698 base::strlcpy(buffer1->data(), "And the data to save", kSize); 970 base::strlcpy(buffer1->data(), "And the data to save", kSize);
699 EXPECT_EQ(kSize, WriteData(entry, 0, 0, buffer1, kSize, false)); 971 EXPECT_EQ(kSize, WriteData(entry, 0, 0, buffer1.get(), kSize, false));
700 entry->Close(); 972 entry->Close();
701 SimulateCrash(); 973 SimulateCrash();
702 974
703 ASSERT_EQ(net::OK, OpenEntry(key, &entry)); 975 ASSERT_EQ(net::OK, OpenEntry(key, &entry));
704 976
705 scoped_refptr<net::IOBuffer> buffer2(new net::IOBuffer(kSize)); 977 scoped_refptr<net::IOBuffer> buffer2(new net::IOBuffer(kSize));
706 memset(buffer2->data(), 0, kSize); 978 memset(buffer2->data(), 0, kSize);
707 EXPECT_EQ(kSize, ReadData(entry, 0, 0, buffer2, kSize)); 979 EXPECT_EQ(kSize, ReadData(entry, 0, 0, buffer2.get(), kSize));
708 entry->Close(); 980 entry->Close();
709 EXPECT_STREQ(buffer1->data(), buffer2->data()); 981 EXPECT_STREQ(buffer1->data(), buffer2->data());
710 } 982 }
711 983
712 TEST_F(DiskCacheBackendTest, ValidEntry) { 984 TEST_F(DiskCacheBackendTest, ValidEntry) {
713 BackendValidEntry(); 985 BackendValidEntry();
714 } 986 }
715 987
716 TEST_F(DiskCacheBackendTest, NewEvictionValidEntry) { 988 TEST_F(DiskCacheBackendTest, NewEvictionValidEntry) {
717 SetNewEviction(); 989 SetNewEviction();
718 BackendValidEntry(); 990 BackendValidEntry();
719 } 991 }
720 992
721 // The same logic of the previous test (ValidEntry), but this time force the 993 // The same logic of the previous test (ValidEntry), but this time force the
722 // entry to be invalid, simulating a crash in the middle. 994 // entry to be invalid, simulating a crash in the middle.
723 // We'll be leaking memory from this test. 995 // We'll be leaking memory from this test.
724 void DiskCacheBackendTest::BackendInvalidEntry() { 996 void DiskCacheBackendTest::BackendInvalidEntry() {
725 InitCache(); 997 InitCache();
726 998
727 std::string key("Some key"); 999 std::string key("Some key");
728 disk_cache::Entry* entry; 1000 disk_cache::Entry* entry;
729 ASSERT_EQ(net::OK, CreateEntry(key, &entry)); 1001 ASSERT_EQ(net::OK, CreateEntry(key, &entry));
730 1002
731 const int kSize = 50; 1003 const int kSize = 50;
732 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kSize)); 1004 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kSize));
733 memset(buffer->data(), 0, kSize); 1005 memset(buffer->data(), 0, kSize);
734 base::strlcpy(buffer->data(), "And the data to save", kSize); 1006 base::strlcpy(buffer->data(), "And the data to save", kSize);
735 EXPECT_EQ(kSize, WriteData(entry, 0, 0, buffer, kSize, false)); 1007 EXPECT_EQ(kSize, WriteData(entry, 0, 0, buffer.get(), kSize, false));
736 SimulateCrash(); 1008 SimulateCrash();
737 1009
738 EXPECT_NE(net::OK, OpenEntry(key, &entry)); 1010 EXPECT_NE(net::OK, OpenEntry(key, &entry));
739 EXPECT_EQ(0, cache_->GetEntryCount()); 1011 EXPECT_EQ(0, cache_->GetEntryCount());
740 } 1012 }
741 1013
742 // This and the other intentionally leaky tests below are excluded from 1014 #if !defined(LEAK_SANITIZER)
743 // valgrind runs by naming them in the files 1015 // We'll be leaking memory from this test.
744 // net/data/valgrind/net_unittests.gtest.txt
745 // The scripts tools/valgrind/chrome_tests.sh
746 // read those files and pass the appropriate --gtest_filter to net_unittests.
747 TEST_F(DiskCacheBackendTest, InvalidEntry) { 1016 TEST_F(DiskCacheBackendTest, InvalidEntry) {
748 BackendInvalidEntry(); 1017 BackendInvalidEntry();
749 } 1018 }
750 1019
751 // We'll be leaking memory from this test. 1020 // We'll be leaking memory from this test.
752 TEST_F(DiskCacheBackendTest, NewEvictionInvalidEntry) { 1021 TEST_F(DiskCacheBackendTest, NewEvictionInvalidEntry) {
753 SetNewEviction(); 1022 SetNewEviction();
754 BackendInvalidEntry(); 1023 BackendInvalidEntry();
755 } 1024 }
756 1025
(...skipping 15 matching lines...) Expand all
772 InitCache(); 1041 InitCache();
773 1042
774 std::string key("Some key"); 1043 std::string key("Some key");
775 disk_cache::Entry* entry; 1044 disk_cache::Entry* entry;
776 ASSERT_EQ(net::OK, CreateEntry(key, &entry)); 1045 ASSERT_EQ(net::OK, CreateEntry(key, &entry));
777 1046
778 const int kSize = 50; 1047 const int kSize = 50;
779 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kSize)); 1048 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kSize));
780 memset(buffer->data(), 0, kSize); 1049 memset(buffer->data(), 0, kSize);
781 base::strlcpy(buffer->data(), "And the data to save", kSize); 1050 base::strlcpy(buffer->data(), "And the data to save", kSize);
782 EXPECT_EQ(kSize, WriteData(entry, 0, 0, buffer, kSize, false)); 1051 EXPECT_EQ(kSize, WriteData(entry, 0, 0, buffer.get(), kSize, false));
783 entry->Close(); 1052 entry->Close();
784 ASSERT_EQ(net::OK, OpenEntry(key, &entry)); 1053 ASSERT_EQ(net::OK, OpenEntry(key, &entry));
785 EXPECT_EQ(kSize, ReadData(entry, 0, 0, buffer, kSize)); 1054 EXPECT_EQ(kSize, ReadData(entry, 0, 0, buffer.get(), kSize));
786 1055
787 SimulateCrash(); 1056 SimulateCrash();
788 1057
789 if (type_ == net::APP_CACHE) { 1058 if (type_ == net::APP_CACHE) {
790 // Reading an entry and crashing should not make it dirty. 1059 // Reading an entry and crashing should not make it dirty.
791 ASSERT_EQ(net::OK, OpenEntry(key, &entry)); 1060 ASSERT_EQ(net::OK, OpenEntry(key, &entry));
792 EXPECT_EQ(1, cache_->GetEntryCount()); 1061 EXPECT_EQ(1, cache_->GetEntryCount());
793 entry->Close(); 1062 entry->Close();
794 } else { 1063 } else {
795 EXPECT_NE(net::OK, OpenEntry(key, &entry)); 1064 EXPECT_NE(net::OK, OpenEntry(key, &entry));
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after
898 SetMaxSize(kSize * 10); 1167 SetMaxSize(kSize * 10);
899 InitCache(); 1168 InitCache();
900 1169
901 std::string first("some key"); 1170 std::string first("some key");
902 std::string second("something else"); 1171 std::string second("something else");
903 disk_cache::Entry* entry; 1172 disk_cache::Entry* entry;
904 ASSERT_EQ(net::OK, CreateEntry(first, &entry)); 1173 ASSERT_EQ(net::OK, CreateEntry(first, &entry));
905 1174
906 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kSize)); 1175 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kSize));
907 memset(buffer->data(), 0, kSize); 1176 memset(buffer->data(), 0, kSize);
908 EXPECT_EQ(kSize, WriteData(entry, 0, 0, buffer, kSize, false)); 1177 EXPECT_EQ(kSize, WriteData(entry, 0, 0, buffer.get(), kSize, false));
909 1178
910 // Simulate a crash. 1179 // Simulate a crash.
911 SimulateCrash(); 1180 SimulateCrash();
912 1181
913 ASSERT_EQ(net::OK, CreateEntry(second, &entry)); 1182 ASSERT_EQ(net::OK, CreateEntry(second, &entry));
914 EXPECT_EQ(kSize, WriteData(entry, 0, 0, buffer, kSize, false)); 1183 EXPECT_EQ(kSize, WriteData(entry, 0, 0, buffer.get(), kSize, false));
915 1184
916 EXPECT_EQ(2, cache_->GetEntryCount()); 1185 EXPECT_EQ(2, cache_->GetEntryCount());
917 SetMaxSize(kSize); 1186 SetMaxSize(kSize);
918 entry->Close(); // Trim the cache. 1187 entry->Close(); // Trim the cache.
919 FlushQueueForTest(); 1188 FlushQueueForTest();
920 1189
921 // If we evicted the entry in less than 20mS, we have one entry in the cache; 1190 // If we evicted the entry in less than 20mS, we have one entry in the cache;
922 // if it took more than that, we posted a task and we'll delete the second 1191 // if it took more than that, we posted a task and we'll delete the second
923 // entry too. 1192 // entry too.
924 MessageLoop::current()->RunUntilIdle(); 1193 MessageLoop::current()->RunUntilIdle();
(...skipping 28 matching lines...) Expand all
953 InitCache(); 1222 InitCache();
954 1223
955 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kSize)); 1224 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kSize));
956 memset(buffer->data(), 0, kSize); 1225 memset(buffer->data(), 0, kSize);
957 disk_cache::Entry* entry; 1226 disk_cache::Entry* entry;
958 1227
959 // Writing 32 entries to this cache chains most of them. 1228 // Writing 32 entries to this cache chains most of them.
960 for (int i = 0; i < 32; i++) { 1229 for (int i = 0; i < 32; i++) {
961 std::string key(base::StringPrintf("some key %d", i)); 1230 std::string key(base::StringPrintf("some key %d", i));
962 ASSERT_EQ(net::OK, CreateEntry(key, &entry)); 1231 ASSERT_EQ(net::OK, CreateEntry(key, &entry));
963 EXPECT_EQ(kSize, WriteData(entry, 0, 0, buffer, kSize, false)); 1232 EXPECT_EQ(kSize, WriteData(entry, 0, 0, buffer.get(), kSize, false));
964 entry->Close(); 1233 entry->Close();
965 ASSERT_EQ(net::OK, OpenEntry(key, &entry)); 1234 ASSERT_EQ(net::OK, OpenEntry(key, &entry));
966 // Note that we are not closing the entries. 1235 // Note that we are not closing the entries.
967 } 1236 }
968 1237
969 // Simulate a crash. 1238 // Simulate a crash.
970 SimulateCrash(); 1239 SimulateCrash();
971 1240
972 ASSERT_EQ(net::OK, CreateEntry("Something else", &entry)); 1241 ASSERT_EQ(net::OK, CreateEntry("Something else", &entry));
973 EXPECT_EQ(kSize, WriteData(entry, 0, 0, buffer, kSize, false)); 1242 EXPECT_EQ(kSize, WriteData(entry, 0, 0, buffer.get(), kSize, false));
974 1243
975 FlushQueueForTest(); 1244 FlushQueueForTest();
976 EXPECT_EQ(33, cache_->GetEntryCount()); 1245 EXPECT_EQ(33, cache_->GetEntryCount());
977 SetMaxSize(kSize); 1246 SetMaxSize(kSize);
978 1247
979 // For the new eviction code, all corrupt entries are on the second list so 1248 // For the new eviction code, all corrupt entries are on the second list so
980 // they are not going away that easy. 1249 // they are not going away that easy.
981 if (new_eviction_) { 1250 if (new_eviction_) {
982 EXPECT_EQ(net::OK, DoomAllEntries()); 1251 EXPECT_EQ(net::OK, DoomAllEntries());
983 } 1252 }
(...skipping 14 matching lines...) Expand all
998 // We'll be leaking memory from this test. 1267 // We'll be leaking memory from this test.
999 TEST_F(DiskCacheBackendTest, TrimInvalidEntry2) { 1268 TEST_F(DiskCacheBackendTest, TrimInvalidEntry2) {
1000 BackendTrimInvalidEntry2(); 1269 BackendTrimInvalidEntry2();
1001 } 1270 }
1002 1271
1003 // We'll be leaking memory from this test. 1272 // We'll be leaking memory from this test.
1004 TEST_F(DiskCacheBackendTest, NewEvictionTrimInvalidEntry2) { 1273 TEST_F(DiskCacheBackendTest, NewEvictionTrimInvalidEntry2) {
1005 SetNewEviction(); 1274 SetNewEviction();
1006 BackendTrimInvalidEntry2(); 1275 BackendTrimInvalidEntry2();
1007 } 1276 }
1277 #endif // !defined(LEAK_SANITIZER)
1008 1278
1009 void DiskCacheBackendTest::BackendEnumerations() { 1279 void DiskCacheBackendTest::BackendEnumerations() {
1010 InitCache(); 1280 InitCache();
1011 Time initial = Time::Now(); 1281 Time initial = Time::Now();
1012 int seed = static_cast<int>(initial.ToInternalValue()); 1282 int seed = static_cast<int>(initial.ToInternalValue());
1013 srand(seed); 1283 srand(seed);
1014 1284
1015 const int kNumEntries = 100; 1285 const int kNumEntries = 100;
1016 for (int i = 0; i < kNumEntries; i++) { 1286 for (int i = 0; i < kNumEntries; i++) {
1017 std::string key = GenerateKey(true); 1287 std::string key = GenerateKey(true);
1018 disk_cache::Entry* entry; 1288 disk_cache::Entry* entry;
1019 ASSERT_EQ(net::OK, CreateEntry(key, &entry)); 1289 ASSERT_EQ(net::OK, CreateEntry(key, &entry));
1020 entry->Close(); 1290 entry->Close();
1291 if (!(i % 10))
1292 FlushQueueForTest();
1021 } 1293 }
1022 EXPECT_EQ(kNumEntries, cache_->GetEntryCount()); 1294 EXPECT_EQ(kNumEntries, cache_->GetEntryCount());
1023 Time final = Time::Now(); 1295 Time final = Time::Now();
1024 1296
1025 disk_cache::Entry* entry; 1297 disk_cache::Entry* entry;
1026 void* iter = NULL; 1298 void* iter = NULL;
1027 int count = 0; 1299 int count = 0;
1028 Time last_modified[kNumEntries]; 1300 Time last_modified[kNumEntries];
1029 Time last_used[kNumEntries]; 1301 Time last_used[kNumEntries];
1030 while (OpenNextEntry(&iter, &entry) == net::OK) { 1302 while (OpenNextEntry(&iter, &entry) == net::OK) {
(...skipping 22 matching lines...) Expand all
1053 entry->Close(); 1325 entry->Close();
1054 count++; 1326 count++;
1055 }; 1327 };
1056 EXPECT_EQ(kNumEntries, count); 1328 EXPECT_EQ(kNumEntries, count);
1057 } 1329 }
1058 1330
1059 TEST_F(DiskCacheBackendTest, Enumerations) { 1331 TEST_F(DiskCacheBackendTest, Enumerations) {
1060 BackendEnumerations(); 1332 BackendEnumerations();
1061 } 1333 }
1062 1334
1335 TEST_F(DiskCacheBackendTest, V3Enumerations) {
1336 UseVersion3();
1337 BackendEnumerations();
1338 }
1339
1063 TEST_F(DiskCacheBackendTest, NewEvictionEnumerations) { 1340 TEST_F(DiskCacheBackendTest, NewEvictionEnumerations) {
1064 SetNewEviction(); 1341 SetNewEviction();
1065 BackendEnumerations(); 1342 BackendEnumerations();
1066 } 1343 }
1067 1344
1345 TEST_F(DiskCacheBackendTest, V3NewEvictionEnumerations) {
1346 UseVersion3();
1347 SetNewEviction();
1348 BackendEnumerations();
1349 }
1350
1068 TEST_F(DiskCacheBackendTest, MemoryOnlyEnumerations) { 1351 TEST_F(DiskCacheBackendTest, MemoryOnlyEnumerations) {
1069 SetMemoryOnlyMode(); 1352 SetMemoryOnlyMode();
1070 BackendEnumerations(); 1353 BackendEnumerations();
1071 } 1354 }
1072 1355
1073 TEST_F(DiskCacheBackendTest, ShaderCacheEnumerations) { 1356 TEST_F(DiskCacheBackendTest, ShaderCacheEnumerations) {
1074 SetCacheType(net::SHADER_CACHE); 1357 SetCacheType(net::SHADER_CACHE);
1075 BackendEnumerations(); 1358 BackendEnumerations();
1076 } 1359 }
1077 1360
1078 TEST_F(DiskCacheBackendTest, AppCacheEnumerations) { 1361 TEST_F(DiskCacheBackendTest, AppCacheEnumerations) {
1079 SetCacheType(net::APP_CACHE); 1362 SetCacheType(net::APP_CACHE);
1080 BackendEnumerations(); 1363 BackendEnumerations();
1081 } 1364 }
1082 1365
1083 // Verifies enumerations while entries are open. 1366 // Verifies enumerations while entries are open.
1084 void DiskCacheBackendTest::BackendEnumerations2() { 1367 void DiskCacheBackendTest::BackendEnumerations2() {
1085 InitCache(); 1368 InitCache();
1086 const std::string first("first"); 1369 const std::string first("first");
1087 const std::string second("second"); 1370 const std::string second("second");
1088 disk_cache::Entry *entry1, *entry2; 1371 disk_cache::Entry *entry1, *entry2;
1089 ASSERT_EQ(net::OK, CreateEntry(first, &entry1)); 1372 ASSERT_EQ(net::OK, CreateEntry(first, &entry1));
1090 entry1->Close(); 1373 entry1->Close();
1091 ASSERT_EQ(net::OK, CreateEntry(second, &entry2)); 1374 ASSERT_EQ(net::OK, CreateEntry(second, &entry2));
1092 entry2->Close(); 1375 entry2->Close();
1093 FlushQueueForTest(); 1376 FlushQueueForTest();
1094 1377
1095 // Make sure that the timestamp is not the same. 1378 // Make sure that the timestamp is not the same.
1096 AddDelay(); 1379 AddDelayForTest(kDelayToNextTimestamp);
1097 ASSERT_EQ(net::OK, OpenEntry(second, &entry1)); 1380 ASSERT_EQ(net::OK, OpenEntry(second, &entry1));
1098 void* iter = NULL; 1381 void* iter = NULL;
1099 ASSERT_EQ(net::OK, OpenNextEntry(&iter, &entry2)); 1382 ASSERT_EQ(net::OK, OpenNextEntry(&iter, &entry2));
1100 EXPECT_EQ(entry2->GetKey(), second); 1383 EXPECT_EQ(entry2->GetKey(), second);
1101 1384
1102 // Two entries and the iterator pointing at "first". 1385 // Two entries and the iterator pointing at "first".
1103 entry1->Close(); 1386 entry1->Close();
1104 entry2->Close(); 1387 entry2->Close();
1105 1388
1106 // The iterator should still be valid, so we should not crash. 1389 // The iterator should still be valid, so we should not crash.
(...skipping 15 matching lines...) Expand all
1122 1405
1123 entry1->Close(); 1406 entry1->Close();
1124 entry2->Close(); 1407 entry2->Close();
1125 cache_->EndEnumeration(&iter); 1408 cache_->EndEnumeration(&iter);
1126 } 1409 }
1127 1410
1128 TEST_F(DiskCacheBackendTest, Enumerations2) { 1411 TEST_F(DiskCacheBackendTest, Enumerations2) {
1129 BackendEnumerations2(); 1412 BackendEnumerations2();
1130 } 1413 }
1131 1414
1415 TEST_F(DiskCacheBackendTest, V3Enumerations2) {
1416 UseVersion3();
1417 BackendEnumerations2();
1418 }
1419
1132 TEST_F(DiskCacheBackendTest, NewEvictionEnumerations2) { 1420 TEST_F(DiskCacheBackendTest, NewEvictionEnumerations2) {
1133 SetNewEviction(); 1421 SetNewEviction();
1134 BackendEnumerations2(); 1422 BackendEnumerations2();
1135 } 1423 }
1136 1424
1425 TEST_F(DiskCacheBackendTest, V3NewEvictionEnumerations2) {
1426 UseVersion3();
1427 SetNewEviction();
1428 BackendEnumerations2();
1429 }
1430
1137 TEST_F(DiskCacheBackendTest, MemoryOnlyEnumerations2) { 1431 TEST_F(DiskCacheBackendTest, MemoryOnlyEnumerations2) {
1138 SetMemoryOnlyMode(); 1432 SetMemoryOnlyMode();
1139 BackendEnumerations2(); 1433 BackendEnumerations2();
1140 } 1434 }
1141 1435
1142 TEST_F(DiskCacheBackendTest, AppCacheEnumerations2) { 1436 TEST_F(DiskCacheBackendTest, AppCacheEnumerations2) {
1143 SetCacheType(net::APP_CACHE); 1437 SetCacheType(net::APP_CACHE);
1144 BackendEnumerations2(); 1438 BackendEnumerations2();
1145 } 1439 }
1146 1440
1147 TEST_F(DiskCacheBackendTest, ShaderCacheEnumerations2) { 1441 TEST_F(DiskCacheBackendTest, ShaderCacheEnumerations2) {
1148 SetCacheType(net::SHADER_CACHE); 1442 SetCacheType(net::SHADER_CACHE);
1149 BackendEnumerations2(); 1443 BackendEnumerations2();
1150 } 1444 }
1151 1445
1152 // Verify that ReadData calls do not update the LRU cache 1446 // Verify that ReadData calls do not update the LRU cache
1153 // when using the SHADER_CACHE type. 1447 // when using the SHADER_CACHE type.
1154 TEST_F(DiskCacheBackendTest, ShaderCacheEnumerationReadData) { 1448 TEST_F(DiskCacheBackendTest, ShaderCacheEnumerationReadData) {
1155 SetCacheType(net::SHADER_CACHE); 1449 SetCacheType(net::SHADER_CACHE);
1156 InitCache(); 1450 InitCache();
1157 const std::string first("first"); 1451 const std::string first("first");
1158 const std::string second("second"); 1452 const std::string second("second");
1159 disk_cache::Entry *entry1, *entry2; 1453 disk_cache::Entry *entry1, *entry2;
1160 const int kSize = 50; 1454 const int kSize = 50;
1161 scoped_refptr<net::IOBuffer> buffer1(new net::IOBuffer(kSize)); 1455 scoped_refptr<net::IOBuffer> buffer1(new net::IOBuffer(kSize));
1162 1456
1163 ASSERT_EQ(net::OK, CreateEntry(first, &entry1)); 1457 ASSERT_EQ(net::OK, CreateEntry(first, &entry1));
1164 memset(buffer1->data(), 0, kSize); 1458 memset(buffer1->data(), 0, kSize);
1165 base::strlcpy(buffer1->data(), "And the data to save", kSize); 1459 base::strlcpy(buffer1->data(), "And the data to save", kSize);
1166 EXPECT_EQ(kSize, WriteData(entry1, 0, 0, buffer1, kSize, false)); 1460 EXPECT_EQ(kSize, WriteData(entry1, 0, 0, buffer1.get(), kSize, false));
1167 1461
1168 ASSERT_EQ(net::OK, CreateEntry(second, &entry2)); 1462 ASSERT_EQ(net::OK, CreateEntry(second, &entry2));
1169 entry2->Close(); 1463 entry2->Close();
1170 1464
1171 FlushQueueForTest(); 1465 FlushQueueForTest();
1172 1466
1173 // Make sure that the timestamp is not the same. 1467 // Make sure that the timestamp is not the same.
1174 AddDelay(); 1468 AddDelay();
1175 1469
1176 // Read from the last item in the LRU. 1470 // Read from the last item in the LRU.
1177 EXPECT_EQ(kSize, ReadData(entry1, 0, 0, buffer1, kSize)); 1471 EXPECT_EQ(kSize, ReadData(entry1, 0, 0, buffer1.get(), kSize));
1178 entry1->Close(); 1472 entry1->Close();
1179 1473
1180 void* iter = NULL; 1474 void* iter = NULL;
1181 ASSERT_EQ(net::OK, OpenNextEntry(&iter, &entry2)); 1475 ASSERT_EQ(net::OK, OpenNextEntry(&iter, &entry2));
1182 EXPECT_EQ(entry2->GetKey(), second); 1476 EXPECT_EQ(entry2->GetKey(), second);
1183 entry2->Close(); 1477 entry2->Close();
1184 cache_->EndEnumeration(&iter); 1478 cache_->EndEnumeration(&iter);
1185 } 1479 }
1186 1480
1481 #if !defined(LEAK_SANITIZER)
1187 // Verify handling of invalid entries while doing enumerations. 1482 // Verify handling of invalid entries while doing enumerations.
1188 // We'll be leaking memory from this test. 1483 // We'll be leaking memory from this test.
1189 void DiskCacheBackendTest::BackendInvalidEntryEnumeration() { 1484 void DiskCacheBackendTest::BackendInvalidEntryEnumeration() {
1190 InitCache(); 1485 InitCache();
1191 1486
1192 std::string key("Some key"); 1487 std::string key("Some key");
1193 disk_cache::Entry *entry, *entry1, *entry2; 1488 disk_cache::Entry *entry, *entry1, *entry2;
1194 ASSERT_EQ(net::OK, CreateEntry(key, &entry1)); 1489 ASSERT_EQ(net::OK, CreateEntry(key, &entry1));
1195 1490
1196 const int kSize = 50; 1491 const int kSize = 50;
1197 scoped_refptr<net::IOBuffer> buffer1(new net::IOBuffer(kSize)); 1492 scoped_refptr<net::IOBuffer> buffer1(new net::IOBuffer(kSize));
1198 memset(buffer1->data(), 0, kSize); 1493 memset(buffer1->data(), 0, kSize);
1199 base::strlcpy(buffer1->data(), "And the data to save", kSize); 1494 base::strlcpy(buffer1->data(), "And the data to save", kSize);
1200 EXPECT_EQ(kSize, WriteData(entry1, 0, 0, buffer1, kSize, false)); 1495 EXPECT_EQ(kSize, WriteData(entry1, 0, 0, buffer1.get(), kSize, false));
1201 entry1->Close(); 1496 entry1->Close();
1202 ASSERT_EQ(net::OK, OpenEntry(key, &entry1)); 1497 ASSERT_EQ(net::OK, OpenEntry(key, &entry1));
1203 EXPECT_EQ(kSize, ReadData(entry1, 0, 0, buffer1, kSize)); 1498 EXPECT_EQ(kSize, ReadData(entry1, 0, 0, buffer1.get(), kSize));
1204 1499
1205 std::string key2("Another key"); 1500 std::string key2("Another key");
1206 ASSERT_EQ(net::OK, CreateEntry(key2, &entry2)); 1501 ASSERT_EQ(net::OK, CreateEntry(key2, &entry2));
1207 entry2->Close(); 1502 entry2->Close();
1208 ASSERT_EQ(2, cache_->GetEntryCount()); 1503 ASSERT_EQ(2, cache_->GetEntryCount());
1209 1504
1210 SimulateCrash(); 1505 SimulateCrash();
1211 1506
1212 void* iter = NULL; 1507 void* iter = NULL;
1213 int count = 0; 1508 int count = 0;
(...skipping 10 matching lines...) Expand all
1224 // We'll be leaking memory from this test. 1519 // We'll be leaking memory from this test.
1225 TEST_F(DiskCacheBackendTest, InvalidEntryEnumeration) { 1520 TEST_F(DiskCacheBackendTest, InvalidEntryEnumeration) {
1226 BackendInvalidEntryEnumeration(); 1521 BackendInvalidEntryEnumeration();
1227 } 1522 }
1228 1523
1229 // We'll be leaking memory from this test. 1524 // We'll be leaking memory from this test.
1230 TEST_F(DiskCacheBackendTest, NewEvictionInvalidEntryEnumeration) { 1525 TEST_F(DiskCacheBackendTest, NewEvictionInvalidEntryEnumeration) {
1231 SetNewEviction(); 1526 SetNewEviction();
1232 BackendInvalidEntryEnumeration(); 1527 BackendInvalidEntryEnumeration();
1233 } 1528 }
1529 #endif // !defined(LEAK_SANITIZER)
1234 1530
1235 // Tests that if for some reason entries are modified close to existing cache 1531 // Tests that if for some reason entries are modified close to existing cache
1236 // iterators, we don't generate fatal errors or reset the cache. 1532 // iterators, we don't generate fatal errors or reset the cache.
1237 void DiskCacheBackendTest::BackendFixEnumerators() { 1533 void DiskCacheBackendTest::BackendFixEnumerators() {
1238 InitCache(); 1534 InitCache();
1239 1535
1240 int seed = static_cast<int>(Time::Now().ToInternalValue()); 1536 int seed = static_cast<int>(Time::Now().ToInternalValue());
1241 srand(seed); 1537 srand(seed);
1242 1538
1243 const int kNumEntries = 10; 1539 const int kNumEntries = 10;
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
1300 void DiskCacheBackendTest::BackendDoomRecent() { 1596 void DiskCacheBackendTest::BackendDoomRecent() {
1301 InitCache(); 1597 InitCache();
1302 1598
1303 disk_cache::Entry *entry; 1599 disk_cache::Entry *entry;
1304 ASSERT_EQ(net::OK, CreateEntry("first", &entry)); 1600 ASSERT_EQ(net::OK, CreateEntry("first", &entry));
1305 entry->Close(); 1601 entry->Close();
1306 ASSERT_EQ(net::OK, CreateEntry("second", &entry)); 1602 ASSERT_EQ(net::OK, CreateEntry("second", &entry));
1307 entry->Close(); 1603 entry->Close();
1308 FlushQueueForTest(); 1604 FlushQueueForTest();
1309 1605
1310 AddDelay(); 1606 AddDelayForTest(kDelayToNextTimestamp);
1311 Time middle = Time::Now(); 1607 Time middle = GetTime();
1312 1608
1313 ASSERT_EQ(net::OK, CreateEntry("third", &entry)); 1609 ASSERT_EQ(net::OK, CreateEntry("third", &entry));
1314 entry->Close(); 1610 entry->Close();
1315 ASSERT_EQ(net::OK, CreateEntry("fourth", &entry)); 1611 ASSERT_EQ(net::OK, CreateEntry("fourth", &entry));
1316 entry->Close(); 1612 entry->Close();
1317 FlushQueueForTest(); 1613 FlushQueueForTest();
1318 1614
1319 AddDelay(); 1615 AddDelayForTest(kDelayToNextTimestamp);
1320 Time final = Time::Now(); 1616 Time final = GetTime();
1321 1617
1322 ASSERT_EQ(4, cache_->GetEntryCount()); 1618 ASSERT_EQ(4, cache_->GetEntryCount());
1323 EXPECT_EQ(net::OK, DoomEntriesSince(final)); 1619 EXPECT_EQ(net::OK, DoomEntriesSince(final));
1324 ASSERT_EQ(4, cache_->GetEntryCount()); 1620 ASSERT_EQ(4, cache_->GetEntryCount());
1325 1621
1326 EXPECT_EQ(net::OK, DoomEntriesSince(middle)); 1622 EXPECT_EQ(net::OK, DoomEntriesSince(middle));
1327 ASSERT_EQ(2, cache_->GetEntryCount()); 1623 ASSERT_EQ(2, cache_->GetEntryCount());
1328 1624
1329 ASSERT_EQ(net::OK, OpenEntry("second", &entry)); 1625 ASSERT_EQ(net::OK, OpenEntry("second", &entry));
1330 entry->Close(); 1626 entry->Close();
1331 } 1627 }
1332 1628
1333 TEST_F(DiskCacheBackendTest, DoomRecent) { 1629 TEST_F(DiskCacheBackendTest, DoomRecent) {
1334 BackendDoomRecent(); 1630 BackendDoomRecent();
1335 } 1631 }
1336 1632
1633 TEST_F(DiskCacheBackendTest, V3DoomRecent) {
1634 UseVersion3();
1635 BackendDoomRecent();
1636 }
1637
1337 TEST_F(DiskCacheBackendTest, NewEvictionDoomRecent) { 1638 TEST_F(DiskCacheBackendTest, NewEvictionDoomRecent) {
1338 SetNewEviction(); 1639 SetNewEviction();
1339 BackendDoomRecent(); 1640 BackendDoomRecent();
1340 } 1641 }
1341 1642
1643 TEST_F(DiskCacheBackendTest, V3NewEvictionDoomRecent) {
1644 UseVersion3();
1645 SetNewEviction();
1646 BackendDoomRecent();
1647 }
1648
1342 TEST_F(DiskCacheBackendTest, MemoryOnlyDoomRecent) { 1649 TEST_F(DiskCacheBackendTest, MemoryOnlyDoomRecent) {
1343 SetMemoryOnlyMode(); 1650 SetMemoryOnlyMode();
1344 BackendDoomRecent(); 1651 BackendDoomRecent();
1345 } 1652 }
1346 1653
1347 void DiskCacheBackendTest::InitSparseCache(base::Time* doomed_start,
1348 base::Time* doomed_end) {
1349 InitCache();
1350
1351 const int kSize = 50;
1352 // This must be greater then MemEntryImpl::kMaxSparseEntrySize.
1353 const int kOffset = 10 + 1024 * 1024;
1354
1355 disk_cache::Entry* entry0 = NULL;
1356 disk_cache::Entry* entry1 = NULL;
1357 disk_cache::Entry* entry2 = NULL;
1358
1359 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kSize));
1360 CacheTestFillBuffer(buffer->data(), kSize, false);
1361
1362 ASSERT_EQ(net::OK, CreateEntry("zeroth", &entry0));
1363 ASSERT_EQ(kSize, WriteSparseData(entry0, 0, buffer.get(), kSize));
1364 ASSERT_EQ(kSize,
1365 WriteSparseData(entry0, kOffset + kSize, buffer.get(), kSize));
1366 entry0->Close();
1367
1368 FlushQueueForTest();
1369 AddDelay();
1370 if (doomed_start)
1371 *doomed_start = base::Time::Now();
1372
1373 // Order in rankings list:
1374 // first_part1, first_part2, second_part1, second_part2
1375 ASSERT_EQ(net::OK, CreateEntry("first", &entry1));
1376 ASSERT_EQ(kSize, WriteSparseData(entry1, 0, buffer.get(), kSize));
1377 ASSERT_EQ(kSize,
1378 WriteSparseData(entry1, kOffset + kSize, buffer.get(), kSize));
1379 entry1->Close();
1380
1381 ASSERT_EQ(net::OK, CreateEntry("second", &entry2));
1382 ASSERT_EQ(kSize, WriteSparseData(entry2, 0, buffer.get(), kSize));
1383 ASSERT_EQ(kSize,
1384 WriteSparseData(entry2, kOffset + kSize, buffer.get(), kSize));
1385 entry2->Close();
1386
1387 FlushQueueForTest();
1388 AddDelay();
1389 if (doomed_end)
1390 *doomed_end = base::Time::Now();
1391
1392 // Order in rankings list:
1393 // third_part1, fourth_part1, third_part2, fourth_part2
1394 disk_cache::Entry* entry3 = NULL;
1395 disk_cache::Entry* entry4 = NULL;
1396 ASSERT_EQ(net::OK, CreateEntry("third", &entry3));
1397 ASSERT_EQ(kSize, WriteSparseData(entry3, 0, buffer.get(), kSize));
1398 ASSERT_EQ(net::OK, CreateEntry("fourth", &entry4));
1399 ASSERT_EQ(kSize, WriteSparseData(entry4, 0, buffer.get(), kSize));
1400 ASSERT_EQ(kSize,
1401 WriteSparseData(entry3, kOffset + kSize, buffer.get(), kSize));
1402 ASSERT_EQ(kSize,
1403 WriteSparseData(entry4, kOffset + kSize, buffer.get(), kSize));
1404 entry3->Close();
1405 entry4->Close();
1406
1407 FlushQueueForTest();
1408 AddDelay();
1409 }
1410
1411 TEST_F(DiskCacheBackendTest, MemoryOnlyDoomEntriesSinceSparse) { 1654 TEST_F(DiskCacheBackendTest, MemoryOnlyDoomEntriesSinceSparse) {
1412 SetMemoryOnlyMode(); 1655 SetMemoryOnlyMode();
1413 base::Time start; 1656 base::Time start;
1414 InitSparseCache(&start, NULL); 1657 InitSparseCache(&start, NULL);
1415 DoomEntriesSince(start); 1658 DoomEntriesSince(start);
1416 EXPECT_EQ(1, cache_->GetEntryCount()); 1659 EXPECT_EQ(1, cache_->GetEntryCount());
1417 } 1660 }
1418 1661
1419 TEST_F(DiskCacheBackendTest, DoomEntriesSinceSparse) { 1662 TEST_F(DiskCacheBackendTest, DoomEntriesSinceSparse) {
1420 base::Time start; 1663 base::Time start;
1421 InitSparseCache(&start, NULL); 1664 InitSparseCache(&start, NULL);
1422 DoomEntriesSince(start); 1665 DoomEntriesSince(start);
1423 // NOTE: BackendImpl counts child entries in its GetEntryCount(), while 1666 // NOTE: BackendImpl counts child entries in its GetEntryCount(), while
1424 // MemBackendImpl does not. Thats why expected value differs here from 1667 // MemBackendImpl does not. Thats why expected value differs here from
1425 // MemoryOnlyDoomEntriesSinceSparse. 1668 // MemoryOnlyDoomEntriesSinceSparse.
1426 EXPECT_EQ(3, cache_->GetEntryCount()); 1669 EXPECT_EQ(3, cache_->GetEntryCount());
1427 } 1670 }
1428 1671
1672 TEST_F(DiskCacheBackendTest, V3DoomEntriesSinceSparse) {
1673 base::Time start;
1674 UseVersion3();
1675 InitSparseCache(&start, NULL);
1676 DoomEntriesSince(start);
1677 // NOTE: BackendImpl counts child entries in its GetEntryCount(), while
1678 // MemBackendImpl does not. Thats why expected value differs here from
1679 // MemoryOnlyDoomEntriesSinceSparse.
1680 EXPECT_EQ(3, cache_->GetEntryCount());
1681 }
1682
1429 TEST_F(DiskCacheBackendTest, MemoryOnlyDoomAllSparse) { 1683 TEST_F(DiskCacheBackendTest, MemoryOnlyDoomAllSparse) {
1430 SetMemoryOnlyMode(); 1684 SetMemoryOnlyMode();
1431 InitSparseCache(NULL, NULL); 1685 InitSparseCache(NULL, NULL);
1432 EXPECT_EQ(net::OK, DoomAllEntries()); 1686 EXPECT_EQ(net::OK, DoomAllEntries());
1433 EXPECT_EQ(0, cache_->GetEntryCount()); 1687 EXPECT_EQ(0, cache_->GetEntryCount());
1434 } 1688 }
1435 1689
1436 TEST_F(DiskCacheBackendTest, DoomAllSparse) { 1690 TEST_F(DiskCacheBackendTest, DoomAllSparse) {
1437 InitSparseCache(NULL, NULL); 1691 InitSparseCache(NULL, NULL);
1438 EXPECT_EQ(net::OK, DoomAllEntries()); 1692 EXPECT_EQ(net::OK, DoomAllEntries());
1439 EXPECT_EQ(0, cache_->GetEntryCount()); 1693 EXPECT_EQ(0, cache_->GetEntryCount());
1440 } 1694 }
1441 1695
1696 TEST_F(DiskCacheBackendTest, V3DoomAllSparse) {
1697 UseVersion3();
1698 InitSparseCache(NULL, NULL);
1699 EXPECT_EQ(net::OK, DoomAllEntries());
1700 EXPECT_EQ(0, cache_->GetEntryCount());
1701 }
1702
1442 void DiskCacheBackendTest::BackendDoomBetween() { 1703 void DiskCacheBackendTest::BackendDoomBetween() {
1443 InitCache(); 1704 InitCache();
1444 1705
1445 disk_cache::Entry *entry; 1706 disk_cache::Entry *entry;
1446 ASSERT_EQ(net::OK, CreateEntry("first", &entry)); 1707 ASSERT_EQ(net::OK, CreateEntry("first", &entry));
1447 entry->Close(); 1708 entry->Close();
1448 FlushQueueForTest(); 1709 FlushQueueForTest();
1449 1710
1450 AddDelay(); 1711 AddDelay();
1451 Time middle_start = Time::Now(); 1712 Time middle_start = Time::Now();
(...skipping 27 matching lines...) Expand all
1479 ASSERT_EQ(1, cache_->GetEntryCount()); 1740 ASSERT_EQ(1, cache_->GetEntryCount());
1480 1741
1481 ASSERT_EQ(net::OK, OpenEntry("first", &entry)); 1742 ASSERT_EQ(net::OK, OpenEntry("first", &entry));
1482 entry->Close(); 1743 entry->Close();
1483 } 1744 }
1484 1745
1485 TEST_F(DiskCacheBackendTest, DoomBetween) { 1746 TEST_F(DiskCacheBackendTest, DoomBetween) {
1486 BackendDoomBetween(); 1747 BackendDoomBetween();
1487 } 1748 }
1488 1749
1750 TEST_F(DiskCacheBackendTest, V3DoomBetween) {
1751 UseVersion3();
1752 BackendDoomBetween();
1753 }
1754
1489 TEST_F(DiskCacheBackendTest, NewEvictionDoomBetween) { 1755 TEST_F(DiskCacheBackendTest, NewEvictionDoomBetween) {
1490 SetNewEviction(); 1756 SetNewEviction();
1491 BackendDoomBetween(); 1757 BackendDoomBetween();
1492 } 1758 }
1493 1759
1760 TEST_F(DiskCacheBackendTest, V3NewEvictionDoomBetween) {
1761 UseVersion3();
1762 SetNewEviction();
1763 BackendDoomBetween();
1764 }
1765
1494 TEST_F(DiskCacheBackendTest, MemoryOnlyDoomBetween) { 1766 TEST_F(DiskCacheBackendTest, MemoryOnlyDoomBetween) {
1495 SetMemoryOnlyMode(); 1767 SetMemoryOnlyMode();
1496 BackendDoomBetween(); 1768 BackendDoomBetween();
1497 } 1769 }
1498 1770
1499 TEST_F(DiskCacheBackendTest, MemoryOnlyDoomEntriesBetweenSparse) { 1771 TEST_F(DiskCacheBackendTest, MemoryOnlyDoomEntriesBetweenSparse) {
1500 SetMemoryOnlyMode(); 1772 SetMemoryOnlyMode();
1501 base::Time start, end; 1773 base::Time start, end;
1502 InitSparseCache(&start, &end); 1774 InitSparseCache(&start, &end);
1503 DoomEntriesBetween(start, end); 1775 DoomEntriesBetween(start, end);
(...skipping 10 matching lines...) Expand all
1514 InitSparseCache(&start, &end); 1786 InitSparseCache(&start, &end);
1515 DoomEntriesBetween(start, end); 1787 DoomEntriesBetween(start, end);
1516 EXPECT_EQ(9, cache_->GetEntryCount()); 1788 EXPECT_EQ(9, cache_->GetEntryCount());
1517 1789
1518 start = end; 1790 start = end;
1519 end = base::Time::Now(); 1791 end = base::Time::Now();
1520 DoomEntriesBetween(start, end); 1792 DoomEntriesBetween(start, end);
1521 EXPECT_EQ(3, cache_->GetEntryCount()); 1793 EXPECT_EQ(3, cache_->GetEntryCount());
1522 } 1794 }
1523 1795
1796 TEST_F(DiskCacheBackendTest, V3DoomEntriesBetweenSparse) {
1797 base::Time start, end;
1798 UseVersion3();
1799 InitSparseCache(&start, &end);
1800 DoomEntriesBetween(start, end);
1801 EXPECT_EQ(9, cache_->GetEntryCount());
1802
1803 start = end;
1804 end = base::Time::Now();
1805 DoomEntriesBetween(start, end);
1806 EXPECT_EQ(3, cache_->GetEntryCount());
1807 }
1808
1524 void DiskCacheBackendTest::BackendTransaction(const std::string& name, 1809 void DiskCacheBackendTest::BackendTransaction(const std::string& name,
1525 int num_entries, bool load) { 1810 int num_entries, bool load) {
1526 success_ = false; 1811 success_ = false;
1527 ASSERT_TRUE(CopyTestCache(name)); 1812 ASSERT_TRUE(CopyTestCache(name));
1528 DisableFirstCleanup(); 1813 DisableFirstCleanup();
1529 1814
1530 uint32 mask; 1815 uint32 mask;
1531 if (load) { 1816 if (load) {
1532 mask = 0xf; 1817 mask = 0xf;
1533 SetMaxSize(0x100000); 1818 SetMaxSize(0x100000);
(...skipping 860 matching lines...) Expand 10 before | Expand all | Expand 10 after
2394 CacheTestFillBuffer(key2, sizeof(key2), true); 2679 CacheTestFillBuffer(key2, sizeof(key2), true);
2395 CacheTestFillBuffer(key3, sizeof(key3), true); 2680 CacheTestFillBuffer(key3, sizeof(key3), true);
2396 key2[sizeof(key2) - 1] = '\0'; 2681 key2[sizeof(key2) - 1] = '\0';
2397 key3[sizeof(key3) - 1] = '\0'; 2682 key3[sizeof(key3) - 1] = '\0';
2398 ASSERT_EQ(net::OK, CreateEntry(key2, &entry2)); 2683 ASSERT_EQ(net::OK, CreateEntry(key2, &entry2));
2399 ASSERT_EQ(net::OK, CreateEntry(key3, &entry3)); 2684 ASSERT_EQ(net::OK, CreateEntry(key3, &entry3));
2400 2685
2401 const int kBufSize = 20000; 2686 const int kBufSize = 20000;
2402 scoped_refptr<net::IOBuffer> buf(new net::IOBuffer(kBufSize)); 2687 scoped_refptr<net::IOBuffer> buf(new net::IOBuffer(kBufSize));
2403 memset(buf->data(), 0, kBufSize); 2688 memset(buf->data(), 0, kBufSize);
2404 EXPECT_EQ(100, WriteData(entry2, 0, 0, buf, 100, false)); 2689 EXPECT_EQ(100, WriteData(entry2, 0, 0, buf.get(), 100, false));
2405 EXPECT_EQ(kBufSize, WriteData(entry3, 0, 0, buf, kBufSize, false)); 2690 EXPECT_EQ(kBufSize, WriteData(entry3, 0, 0, buf.get(), kBufSize, false));
2406 2691
2407 // This line should disable the cache but not delete it. 2692 // This line should disable the cache but not delete it.
2408 EXPECT_NE(net::OK, OpenNextEntry(&iter, &entry4)); 2693 EXPECT_NE(net::OK, OpenNextEntry(&iter, &entry4));
2409 EXPECT_EQ(0, cache_->GetEntryCount()); 2694 EXPECT_EQ(0, cache_->GetEntryCount());
2410 2695
2411 EXPECT_NE(net::OK, CreateEntry("cache is disabled", &entry4)); 2696 EXPECT_NE(net::OK, CreateEntry("cache is disabled", &entry4));
2412 2697
2413 EXPECT_EQ(100, ReadData(entry2, 0, 0, buf, 100)); 2698 EXPECT_EQ(100, ReadData(entry2, 0, 0, buf.get(), 100));
2414 EXPECT_EQ(100, WriteData(entry2, 0, 0, buf, 100, false)); 2699 EXPECT_EQ(100, WriteData(entry2, 0, 0, buf.get(), 100, false));
2415 EXPECT_EQ(100, WriteData(entry2, 1, 0, buf, 100, false)); 2700 EXPECT_EQ(100, WriteData(entry2, 1, 0, buf.get(), 100, false));
2416 2701
2417 EXPECT_EQ(kBufSize, ReadData(entry3, 0, 0, buf, kBufSize)); 2702 EXPECT_EQ(kBufSize, ReadData(entry3, 0, 0, buf.get(), kBufSize));
2418 EXPECT_EQ(kBufSize, WriteData(entry3, 0, 0, buf, kBufSize, false)); 2703 EXPECT_EQ(kBufSize, WriteData(entry3, 0, 0, buf.get(), kBufSize, false));
2419 EXPECT_EQ(kBufSize, WriteData(entry3, 1, 0, buf, kBufSize, false)); 2704 EXPECT_EQ(kBufSize, WriteData(entry3, 1, 0, buf.get(), kBufSize, false));
2420 2705
2421 std::string key = entry2->GetKey(); 2706 std::string key = entry2->GetKey();
2422 EXPECT_EQ(sizeof(key2) - 1, key.size()); 2707 EXPECT_EQ(sizeof(key2) - 1, key.size());
2423 key = entry3->GetKey(); 2708 key = entry3->GetKey();
2424 EXPECT_EQ(sizeof(key3) - 1, key.size()); 2709 EXPECT_EQ(sizeof(key3) - 1, key.size());
2425 2710
2426 entry1->Close(); 2711 entry1->Close();
2427 entry2->Close(); 2712 entry2->Close();
2428 entry3->Close(); 2713 entry3->Close();
2429 FlushQueueForTest(); // Flushing the Close posts a task to restart the cache. 2714 FlushQueueForTest(); // Flushing the Close posts a task to restart the cache.
(...skipping 10 matching lines...) Expand all
2440 } 2725 }
2441 2726
2442 TEST_F(DiskCacheBackendTest, NewEvictionDisableSuccess4) { 2727 TEST_F(DiskCacheBackendTest, NewEvictionDisableSuccess4) {
2443 ASSERT_TRUE(CopyTestCache("bad_rankings")); 2728 ASSERT_TRUE(CopyTestCache("bad_rankings"));
2444 DisableFirstCleanup(); 2729 DisableFirstCleanup();
2445 SetNewEviction(); 2730 SetNewEviction();
2446 InitCache(); 2731 InitCache();
2447 BackendDisable4(); 2732 BackendDisable4();
2448 } 2733 }
2449 2734
2450 TEST_F(DiskCacheTest, Backend_UsageStats) { 2735 TEST_F(DiskCacheTest, Backend_UsageStatsTimer) {
2451 MessageLoopHelper helper; 2736 MessageLoopHelper helper;
2452 2737
2453 ASSERT_TRUE(CleanupCacheDir()); 2738 ASSERT_TRUE(CleanupCacheDir());
2454 scoped_ptr<disk_cache::BackendImpl> cache; 2739 scoped_ptr<disk_cache::BackendImpl> cache;
2455 cache.reset(new disk_cache::BackendImpl( 2740 cache.reset(new disk_cache::BackendImpl(
2456 cache_path_, base::MessageLoopProxy::current(), 2741 cache_path_, base::MessageLoopProxy::current(),
2457 NULL)); 2742 NULL));
2458 ASSERT_TRUE(NULL != cache.get()); 2743 ASSERT_TRUE(NULL != cache.get());
2459 cache->SetUnitTestMode(); 2744 cache->SetUnitTestMode();
2460 ASSERT_EQ(net::OK, cache->SyncInit()); 2745 ASSERT_EQ(net::OK, cache->SyncInit());
2461 2746
2462 // Wait for a callback that never comes... about 2 secs :). The message loop 2747 // Wait for a callback that never comes... about 2 secs :). The message loop
2463 // has to run to allow invocation of the usage timer. 2748 // has to run to allow invocation of the usage timer.
2464 helper.WaitUntilCacheIoFinished(1); 2749 helper.WaitUntilCacheIoFinished(1);
2465 } 2750 }
2466 2751
2752 TEST_F(DiskCacheBackendTest, Backend_UsageStats) {
2753 InitCache();
2754 disk_cache::Entry* entry;
2755 ASSERT_EQ(net::OK, CreateEntry("key", &entry));
2756 entry->Close();
2757 FlushQueueForTest();
2758
2759 disk_cache::StatsItems stats;
2760 cache_->GetStats(&stats);
2761 EXPECT_FALSE(stats.empty());
2762
2763 disk_cache::StatsItems::value_type hits("Create hit", "0x1");
2764 EXPECT_EQ(1, std::count(stats.begin(), stats.end(), hits));
2765
2766 delete cache_;
2767 cache_ = NULL;
2768
2769 // Now open the cache and verify that the stats are still there.
2770 DisableFirstCleanup();
2771 InitCache();
2772 EXPECT_EQ(1, cache_->GetEntryCount());
2773
2774 stats.clear();
2775 cache_->GetStats(&stats);
2776 EXPECT_FALSE(stats.empty());
2777
2778 EXPECT_EQ(1, std::count(stats.begin(), stats.end(), hits));
2779 }
2780
2467 void DiskCacheBackendTest::BackendDoomAll() { 2781 void DiskCacheBackendTest::BackendDoomAll() {
2468 InitCache(); 2782 InitCache();
2469 2783
2470 disk_cache::Entry *entry1, *entry2; 2784 disk_cache::Entry *entry1, *entry2;
2471 ASSERT_EQ(net::OK, CreateEntry("first", &entry1)); 2785 ASSERT_EQ(net::OK, CreateEntry("first", &entry1));
2472 ASSERT_EQ(net::OK, CreateEntry("second", &entry2)); 2786 ASSERT_EQ(net::OK, CreateEntry("second", &entry2));
2473 entry1->Close(); 2787 entry1->Close();
2474 entry2->Close(); 2788 entry2->Close();
2475 2789
2476 ASSERT_EQ(net::OK, CreateEntry("third", &entry1)); 2790 ASSERT_EQ(net::OK, CreateEntry("third", &entry1));
(...skipping 30 matching lines...) Expand all
2507 EXPECT_EQ(net::OK, DoomAllEntries()); 2821 EXPECT_EQ(net::OK, DoomAllEntries());
2508 ASSERT_EQ(0, cache_->GetEntryCount()); 2822 ASSERT_EQ(0, cache_->GetEntryCount());
2509 2823
2510 EXPECT_EQ(net::OK, DoomAllEntries()); 2824 EXPECT_EQ(net::OK, DoomAllEntries());
2511 } 2825 }
2512 2826
2513 TEST_F(DiskCacheBackendTest, DoomAll) { 2827 TEST_F(DiskCacheBackendTest, DoomAll) {
2514 BackendDoomAll(); 2828 BackendDoomAll();
2515 } 2829 }
2516 2830
2831 TEST_F(DiskCacheBackendTest, V3DoomAll) {
2832 UseVersion3();
2833 BackendDoomAll();
2834 }
2835
2517 TEST_F(DiskCacheBackendTest, NewEvictionDoomAll) { 2836 TEST_F(DiskCacheBackendTest, NewEvictionDoomAll) {
2518 SetNewEviction(); 2837 SetNewEviction();
2519 BackendDoomAll(); 2838 BackendDoomAll();
2520 } 2839 }
2521 2840
2841 TEST_F(DiskCacheBackendTest, V3NewEvictionDoomAll) {
2842 UseVersion3();
2843 SetNewEviction();
2844 BackendDoomAll();
2845 }
2846
2522 TEST_F(DiskCacheBackendTest, MemoryOnlyDoomAll) { 2847 TEST_F(DiskCacheBackendTest, MemoryOnlyDoomAll) {
2523 SetMemoryOnlyMode(); 2848 SetMemoryOnlyMode();
2524 BackendDoomAll(); 2849 BackendDoomAll();
2525 } 2850 }
2526 2851
2527 TEST_F(DiskCacheBackendTest, AppCacheOnlyDoomAll) { 2852 TEST_F(DiskCacheBackendTest, AppCacheOnlyDoomAll) {
2528 SetCacheType(net::APP_CACHE); 2853 SetCacheType(net::APP_CACHE);
2529 BackendDoomAll(); 2854 BackendDoomAll();
2530 } 2855 }
2531 2856
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after
2655 InitCache(); 2980 InitCache();
2656 disk_cache::BackendImpl* backend_ = cache_impl_; // Needed be the macro. 2981 disk_cache::BackendImpl* backend_ = cache_impl_; // Needed be the macro.
2657 2982
2658 for (int i = 1; i < 3; i++) { 2983 for (int i = 1; i < 3; i++) {
2659 CACHE_UMA(HOURS, "FillupTime", i, 28); 2984 CACHE_UMA(HOURS, "FillupTime", i, 28);
2660 } 2985 }
2661 } 2986 }
2662 2987
2663 // Make sure that we keep the total memory used by the internal buffers under 2988 // Make sure that we keep the total memory used by the internal buffers under
2664 // control. 2989 // control.
2665 TEST_F(DiskCacheBackendTest, TotalBuffersSize1) { 2990 void DiskCacheBackendTest::BackendTotalBuffersSize1() {
2666 InitCache(); 2991 InitCache();
2667 std::string key("the first key"); 2992 std::string key("the first key");
2668 disk_cache::Entry* entry; 2993 disk_cache::Entry* entry;
2669 ASSERT_EQ(net::OK, CreateEntry(key, &entry)); 2994 ASSERT_EQ(net::OK, CreateEntry(key, &entry));
2670 2995
2671 const int kSize = 200; 2996 const int kSize = 200;
2672 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kSize)); 2997 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kSize));
2673 CacheTestFillBuffer(buffer->data(), kSize, true); 2998 CacheTestFillBuffer(buffer->data(), kSize, true);
2674 2999
2675 for (int i = 0; i < 10; i++) { 3000 for (int i = 0; i < 10; i++) {
2676 SCOPED_TRACE(i); 3001 SCOPED_TRACE(i);
2677 // Allocate 2MB for this entry. 3002 // Allocate 2MB for this entry.
2678 EXPECT_EQ(kSize, WriteData(entry, 0, 0, buffer, kSize, true)); 3003 EXPECT_EQ(kSize, WriteData(entry, 0, 0, buffer.get(), kSize, true));
2679 EXPECT_EQ(kSize, WriteData(entry, 1, 0, buffer, kSize, true)); 3004 EXPECT_EQ(kSize, WriteData(entry, 1, 0, buffer.get(), kSize, true));
2680 EXPECT_EQ(kSize, WriteData(entry, 0, 1024 * 1024, buffer, kSize, false)); 3005 EXPECT_EQ(kSize,
2681 EXPECT_EQ(kSize, WriteData(entry, 1, 1024 * 1024, buffer, kSize, false)); 3006 WriteData(entry, 0, 1024 * 1024, buffer.get(), kSize, false));
3007 EXPECT_EQ(kSize,
3008 WriteData(entry, 1, 1024 * 1024, buffer.get(), kSize, false));
2682 3009
2683 // Delete one of the buffers and truncate the other. 3010 // Delete one of the buffers and truncate the other.
2684 EXPECT_EQ(0, WriteData(entry, 0, 0, buffer, 0, true)); 3011 EXPECT_EQ(0, WriteData(entry, 0, 0, buffer.get(), 0, true));
2685 EXPECT_EQ(0, WriteData(entry, 1, 10, buffer, 0, true)); 3012 EXPECT_EQ(0, WriteData(entry, 1, 10, buffer.get(), 0, true));
2686 3013
2687 // Delete the second buffer, writing 10 bytes to disk. 3014 // Delete the second buffer, writing 10 bytes to disk.
2688 entry->Close(); 3015 entry->Close();
2689 ASSERT_EQ(net::OK, OpenEntry(key, &entry)); 3016 ASSERT_EQ(net::OK, OpenEntry(key, &entry));
2690 } 3017 }
2691 3018
2692 entry->Close(); 3019 entry->Close();
2693 EXPECT_EQ(0, cache_impl_->GetTotalBuffersSize()); 3020 EXPECT_EQ(0, GetTotalBuffersSize());
3021 }
3022
3023 TEST_F(DiskCacheBackendTest, TotalBuffersSize1) {
3024 BackendTotalBuffersSize1();
3025 }
3026
3027 TEST_F(DiskCacheBackendTest, V3TotalBuffersSize1) {
3028 UseVersion3();
3029 BackendTotalBuffersSize1();
2694 } 3030 }
2695 3031
2696 // This test assumes at least 150MB of system memory. 3032 // This test assumes at least 150MB of system memory.
2697 TEST_F(DiskCacheBackendTest, TotalBuffersSize2) { 3033 void DiskCacheBackendTest::BackendTotalBuffersSize2() {
2698 InitCache(); 3034 InitCache();
2699 3035
2700 const int kOneMB = 1024 * 1024; 3036 const int kOneMB = 1024 * 1024;
2701 EXPECT_TRUE(cache_impl_->IsAllocAllowed(0, kOneMB)); 3037 EXPECT_TRUE(IsAllocAllowed(0, kOneMB));
2702 EXPECT_EQ(kOneMB, cache_impl_->GetTotalBuffersSize()); 3038 EXPECT_EQ(kOneMB, GetTotalBuffersSize());
2703 3039
2704 EXPECT_TRUE(cache_impl_->IsAllocAllowed(0, kOneMB)); 3040 EXPECT_TRUE(IsAllocAllowed(0, kOneMB));
2705 EXPECT_EQ(kOneMB * 2, cache_impl_->GetTotalBuffersSize()); 3041 EXPECT_EQ(kOneMB * 2, GetTotalBuffersSize());
2706 3042
2707 EXPECT_TRUE(cache_impl_->IsAllocAllowed(0, kOneMB)); 3043 EXPECT_TRUE(IsAllocAllowed(0, kOneMB));
2708 EXPECT_EQ(kOneMB * 3, cache_impl_->GetTotalBuffersSize()); 3044 EXPECT_EQ(kOneMB * 3, GetTotalBuffersSize());
2709 3045
2710 cache_impl_->BufferDeleted(kOneMB); 3046 BufferDeleted(kOneMB);
2711 EXPECT_EQ(kOneMB * 2, cache_impl_->GetTotalBuffersSize()); 3047 EXPECT_EQ(kOneMB * 2, GetTotalBuffersSize());
2712 3048
2713 // Check the upper limit. 3049 // Check the upper limit.
2714 EXPECT_FALSE(cache_impl_->IsAllocAllowed(0, 30 * kOneMB)); 3050 EXPECT_FALSE(IsAllocAllowed(0, 30 * kOneMB));
2715 3051
2716 for (int i = 0; i < 30; i++) 3052 for (int i = 0; i < 30; i++)
2717 cache_impl_->IsAllocAllowed(0, kOneMB); // Ignore the result. 3053 IsAllocAllowed(0, kOneMB); // Ignore the result.
2718 3054
2719 EXPECT_FALSE(cache_impl_->IsAllocAllowed(0, kOneMB)); 3055 EXPECT_FALSE(IsAllocAllowed(0, kOneMB));
3056 }
3057
3058 TEST_F(DiskCacheBackendTest, TotalBuffersSize2) {
3059 BackendTotalBuffersSize2();
3060 }
3061
3062 TEST_F(DiskCacheBackendTest, V3TotalBuffersSize2) {
3063 UseVersion3();
3064 BackendTotalBuffersSize2();
2720 } 3065 }
2721 3066
2722 // Tests that sharing of external files works and we are able to delete the 3067 // Tests that sharing of external files works and we are able to delete the
2723 // files when we need to. 3068 // files when we need to.
2724 TEST_F(DiskCacheBackendTest, FileSharing) { 3069 TEST_F(DiskCacheBackendTest, FileSharing) {
2725 InitCache(); 3070 InitCache();
2726 3071
2727 disk_cache::Addr address(0x80000001); 3072 disk_cache::Addr address(0x80000001);
2728 ASSERT_TRUE(cache_impl_->CreateExternalFile(&address)); 3073 ASSERT_TRUE(cache_impl_->CreateExternalFile(&address));
2729 base::FilePath name = cache_impl_->GetFileName(address); 3074 base::FilePath name = cache_impl_->GetFileName(address);
(...skipping 22 matching lines...) Expand all
2752 char buffer2[kSize]; 3097 char buffer2[kSize];
2753 memset(buffer1, 't', kSize); 3098 memset(buffer1, 't', kSize);
2754 memset(buffer2, 0, kSize); 3099 memset(buffer2, 0, kSize);
2755 EXPECT_TRUE(file->Write(buffer1, kSize, 0)); 3100 EXPECT_TRUE(file->Write(buffer1, kSize, 0));
2756 EXPECT_TRUE(file->Read(buffer2, kSize, 0)); 3101 EXPECT_TRUE(file->Read(buffer2, kSize, 0));
2757 EXPECT_EQ(0, memcmp(buffer1, buffer2, kSize)); 3102 EXPECT_EQ(0, memcmp(buffer1, buffer2, kSize));
2758 3103
2759 EXPECT_TRUE(disk_cache::DeleteCacheFile(name)); 3104 EXPECT_TRUE(disk_cache::DeleteCacheFile(name));
2760 } 3105 }
2761 3106
2762 TEST_F(DiskCacheBackendTest, UpdateRankForExternalCacheHit) { 3107 void DiskCacheBackendTest::BackendUpdateRankForExternalCacheHit() {
2763 InitCache(); 3108 InitCache();
2764 3109
2765 disk_cache::Entry* entry; 3110 disk_cache::Entry* entry;
2766 3111
2767 for (int i = 0; i < 2; ++i) { 3112 for (int i = 0; i < 2; ++i) {
2768 std::string key = base::StringPrintf("key%d", i); 3113 std::string key = base::StringPrintf("key%d", i);
2769 ASSERT_EQ(net::OK, CreateEntry(key, &entry)); 3114 ASSERT_EQ(net::OK, CreateEntry(key, &entry));
2770 entry->Close(); 3115 entry->Close();
3116 AddDelayForTest(kDelayToNextTimestamp);
2771 } 3117 }
3118 FlushQueueForTest();
2772 3119
2773 // Ping the oldest entry. 3120 // Ping the oldest entry.
3121 SetTestMode();
2774 cache_->OnExternalCacheHit("key0"); 3122 cache_->OnExternalCacheHit("key0");
3123 FlushQueueForTest();
2775 3124
2776 TrimForTest(false); 3125 TrimForTest(false);
2777 3126
2778 // Make sure the older key remains. 3127 // Make sure the older key remains.
2779 EXPECT_EQ(1, cache_->GetEntryCount()); 3128 EXPECT_NE(net::OK, OpenEntry("key1", &entry));
2780 ASSERT_EQ(net::OK, OpenEntry("key0", &entry)); 3129 ASSERT_EQ(net::OK, OpenEntry("key0", &entry));
2781 entry->Close(); 3130 entry->Close();
2782 } 3131 }
2783 3132
3133 TEST_F(DiskCacheBackendTest, UpdateRankForExternalCacheHit) {
3134 BackendUpdateRankForExternalCacheHit();
3135 }
3136
3137 TEST_F(DiskCacheBackendTest, V3UpdateRankForExternalCacheHit) {
3138 UseVersion3();
3139 BackendUpdateRankForExternalCacheHit();
3140 }
3141
2784 TEST_F(DiskCacheBackendTest, ShaderCacheUpdateRankForExternalCacheHit) { 3142 TEST_F(DiskCacheBackendTest, ShaderCacheUpdateRankForExternalCacheHit) {
2785 SetCacheType(net::SHADER_CACHE); 3143 SetCacheType(net::SHADER_CACHE);
2786 InitCache(); 3144 BackendUpdateRankForExternalCacheHit();
3145 }
2787 3146
2788 disk_cache::Entry* entry; 3147 TEST_F(DiskCacheBackendTest, V3ShaderCacheUpdateRankForExternalCacheHit) {
2789 3148 UseVersion3();
2790 for (int i = 0; i < 2; ++i) { 3149 SetCacheType(net::SHADER_CACHE);
2791 std::string key = base::StringPrintf("key%d", i); 3150 BackendUpdateRankForExternalCacheHit();
2792 ASSERT_EQ(net::OK, CreateEntry(key, &entry));
2793 entry->Close();
2794 }
2795
2796 // Ping the oldest entry.
2797 cache_->OnExternalCacheHit("key0");
2798
2799 TrimForTest(false);
2800
2801 // Make sure the older key remains.
2802 EXPECT_EQ(1, cache_->GetEntryCount());
2803 ASSERT_EQ(net::OK, OpenEntry("key0", &entry));
2804 entry->Close();
2805 } 3151 }
2806 3152
2807 void DiskCacheBackendTest::TracingBackendBasics() { 3153 void DiskCacheBackendTest::TracingBackendBasics() {
2808 InitCache(); 3154 InitCache();
2809 cache_ = new disk_cache::TracingCacheBackend(cache_); 3155 cache_ = new disk_cache::TracingCacheBackend(cache_);
2810 cache_impl_ = NULL; 3156 cache_impl_ = NULL;
2811 EXPECT_EQ(net::DISK_CACHE, cache_->GetCacheType()); 3157 EXPECT_EQ(net::DISK_CACHE, cache_->GetCacheType());
2812 if (!simple_cache_mode_) { 3158 if (!simple_cache_mode_) {
2813 EXPECT_EQ(0, cache_->GetEntryCount()); 3159 EXPECT_EQ(0, cache_->GetEntryCount());
2814 } 3160 }
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after
2967 // Tests that the Simple Cache Backend fails to initialize with non-matching 3313 // Tests that the Simple Cache Backend fails to initialize with non-matching
2968 // file structure on disk. 3314 // file structure on disk.
2969 TEST_F(DiskCacheBackendTest, SimpleCacheOverBlockfileCache) { 3315 TEST_F(DiskCacheBackendTest, SimpleCacheOverBlockfileCache) {
2970 // Create a cache structure with the |BackendImpl|. 3316 // Create a cache structure with the |BackendImpl|.
2971 InitCache(); 3317 InitCache();
2972 disk_cache::Entry* entry; 3318 disk_cache::Entry* entry;
2973 const int kSize = 50; 3319 const int kSize = 50;
2974 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kSize)); 3320 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kSize));
2975 CacheTestFillBuffer(buffer->data(), kSize, false); 3321 CacheTestFillBuffer(buffer->data(), kSize, false);
2976 ASSERT_EQ(net::OK, CreateEntry("key", &entry)); 3322 ASSERT_EQ(net::OK, CreateEntry("key", &entry));
2977 ASSERT_EQ(0, WriteData(entry, 0, 0, buffer, 0, false)); 3323 ASSERT_EQ(0, WriteData(entry, 0, 0, buffer.get(), 0, false));
2978 entry->Close(); 3324 entry->Close();
2979 delete cache_; 3325 delete cache_;
2980 cache_ = NULL; 3326 cache_ = NULL;
2981 3327
2982 // Check that the |SimpleBackendImpl| does not favor this structure. 3328 // Check that the |SimpleBackendImpl| does not favor this structure.
2983 base::Thread cache_thread("CacheThread"); 3329 base::Thread cache_thread("CacheThread");
2984 ASSERT_TRUE(cache_thread.StartWithOptions( 3330 ASSERT_TRUE(cache_thread.StartWithOptions(
2985 base::Thread::Options(MessageLoop::TYPE_IO, 0))); 3331 base::Thread::Options(MessageLoop::TYPE_IO, 0)));
2986 disk_cache::SimpleBackendImpl* simple_cache = 3332 disk_cache::SimpleBackendImpl* simple_cache =
2987 new disk_cache::SimpleBackendImpl(cache_path_, 0, net::DISK_CACHE, 3333 new disk_cache::SimpleBackendImpl(cache_path_,
2988 cache_thread.message_loop_proxy(), 3334 0,
3335 net::DISK_CACHE,
3336 cache_thread.message_loop_proxy().get(),
2989 NULL); 3337 NULL);
2990 net::TestCompletionCallback cb; 3338 net::TestCompletionCallback cb;
2991 int rv = simple_cache->Init(cb.callback()); 3339 int rv = simple_cache->Init(cb.callback());
2992 EXPECT_NE(net::OK, cb.GetResult(rv)); 3340 EXPECT_NE(net::OK, cb.GetResult(rv));
2993 delete simple_cache; 3341 delete simple_cache;
2994 DisableIntegrityCheck(); 3342 DisableIntegrityCheck();
2995 } 3343 }
2996 3344
2997 // Tests that the |BackendImpl| refuses to initialize on top of the files 3345 // Tests that the |BackendImpl| refuses to initialize on top of the files
2998 // generated by the Simple Cache Backend. 3346 // generated by the Simple Cache Backend.
2999 TEST_F(DiskCacheBackendTest, BlockfileCacheOverSimpleCache) { 3347 TEST_F(DiskCacheBackendTest, BlockfileCacheOverSimpleCache) {
3000 // Create a cache structure with the |SimpleBackendImpl|. 3348 // Create a cache structure with the |SimpleBackendImpl|.
3001 SetSimpleCacheMode(); 3349 SetSimpleCacheMode();
3002 InitCache(); 3350 InitCache();
3003 disk_cache::Entry* entry; 3351 disk_cache::Entry* entry;
3004 const int kSize = 50; 3352 const int kSize = 50;
3005 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kSize)); 3353 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kSize));
3006 CacheTestFillBuffer(buffer->data(), kSize, false); 3354 CacheTestFillBuffer(buffer->data(), kSize, false);
3007 ASSERT_EQ(net::OK, CreateEntry("key", &entry)); 3355 ASSERT_EQ(net::OK, CreateEntry("key", &entry));
3008 ASSERT_EQ(0, WriteData(entry, 0, 0, buffer, 0, false)); 3356 ASSERT_EQ(0, WriteData(entry, 0, 0, buffer.get(), 0, false));
3009 entry->Close(); 3357 entry->Close();
3010 delete cache_; 3358 delete cache_;
3011 cache_ = NULL; 3359 cache_ = NULL;
3012 3360
3013 // Check that the |BackendImpl| does not favor this structure. 3361 // Check that the |BackendImpl| does not favor this structure.
3014 base::Thread cache_thread("CacheThread"); 3362 base::Thread cache_thread("CacheThread");
3015 ASSERT_TRUE(cache_thread.StartWithOptions( 3363 ASSERT_TRUE(cache_thread.StartWithOptions(
3016 base::Thread::Options(MessageLoop::TYPE_IO, 0))); 3364 base::Thread::Options(MessageLoop::TYPE_IO, 0)));
3017 disk_cache::BackendImpl* cache = 3365 disk_cache::BackendImpl* cache =
3018 new disk_cache::BackendImpl(cache_path_, 3366 new disk_cache::BackendImpl(cache_path_,
3019 base::MessageLoopProxy::current(), 3367 base::MessageLoopProxy::current(),
3020 NULL); 3368 NULL);
3021 cache->SetUnitTestMode(); 3369 cache->SetUnitTestMode();
3022 net::TestCompletionCallback cb; 3370 net::TestCompletionCallback cb;
3023 int rv = cache->Init(cb.callback()); 3371 int rv = cache->Init(cb.callback());
3024 EXPECT_NE(net::OK, cb.GetResult(rv)); 3372 EXPECT_NE(net::OK, cb.GetResult(rv));
3025 delete cache; 3373 delete cache;
3026 DisableIntegrityCheck(); 3374 DisableIntegrityCheck();
3027 } 3375 }
3028 3376
3029 #endif // !defined(OS_WIN) 3377 #endif // !defined(OS_WIN)
OLDNEW
« no previous file with comments | « net/disk_cache/backend_impl.cc ('k') | net/disk_cache/block_files.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698