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

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

Issue 2841034: Disk cache: Switch the disk cache to use the cache_thread.... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Fix tsan issues Created 10 years, 5 months 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
Property Changes:
Deleted: svn:mergeinfo
OLDNEW
1 // Copyright (c) 2006-2010 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2006-2010 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/path_service.h" 7 #include "base/path_service.h"
8 #include "base/platform_thread.h" 8 #include "base/platform_thread.h"
9 #include "base/string_util.h" 9 #include "base/string_util.h"
10 #include "base/thread.h"
11 #include "net/base/io_buffer.h" 10 #include "net/base/io_buffer.h"
12 #include "net/base/net_errors.h" 11 #include "net/base/net_errors.h"
13 #include "net/base/test_completion_callback.h" 12 #include "net/base/test_completion_callback.h"
14 #include "net/disk_cache/backend_impl.h" 13 #include "net/disk_cache/backend_impl.h"
15 #include "net/disk_cache/disk_cache_test_base.h" 14 #include "net/disk_cache/disk_cache_test_base.h"
16 #include "net/disk_cache/disk_cache_test_util.h" 15 #include "net/disk_cache/disk_cache_test_util.h"
17 #include "net/disk_cache/histogram_macros.h" 16 #include "net/disk_cache/histogram_macros.h"
18 #include "net/disk_cache/mapped_file.h" 17 #include "net/disk_cache/mapped_file.h"
19 #include "net/disk_cache/mem_backend_impl.h" 18 #include "net/disk_cache/mem_backend_impl.h"
20 #include "testing/gtest/include/gtest/gtest.h" 19 #include "testing/gtest/include/gtest/gtest.h"
(...skipping 226 matching lines...) Expand 10 before | Expand all | Expand 10 after
247 ASSERT_EQ(net::OK, CreateEntry("key", &entry)); 246 ASSERT_EQ(net::OK, CreateEntry("key", &entry));
248 ASSERT_EQ(0, entry->WriteData(0, 20000, buffer1, 0, NULL, false)); 247 ASSERT_EQ(0, entry->WriteData(0, 20000, buffer1, 0, NULL, false));
249 entry->Close(); 248 entry->Close();
250 249
251 // And verify that the first file is still there. 250 // And verify that the first file is still there.
252 scoped_refptr<net::IOBuffer> buffer2 = new net::IOBuffer(kSize); 251 scoped_refptr<net::IOBuffer> buffer2 = new net::IOBuffer(kSize);
253 ASSERT_EQ(kSize, file_util::ReadFile(filename, buffer2->data(), kSize)); 252 ASSERT_EQ(kSize, file_util::ReadFile(filename, buffer2->data(), kSize));
254 EXPECT_EQ(0, memcmp(buffer1->data(), buffer2->data(), kSize)); 253 EXPECT_EQ(0, memcmp(buffer1->data(), buffer2->data(), kSize));
255 } 254 }
256 255
256 // Tests that we deal with file-level pending operations at destruction time.
257 TEST_F(DiskCacheTest, ShutdownWithPendingIO) { 257 TEST_F(DiskCacheTest, ShutdownWithPendingIO) {
258 TestCompletionCallback callback; 258 TestCompletionCallback cb;
259 259
260 { 260 {
261 FilePath path = GetCacheFilePath(); 261 FilePath path = GetCacheFilePath();
262 ASSERT_TRUE(DeleteCache(path)); 262 ASSERT_TRUE(DeleteCache(path));
263 base::Thread cache_thread("CacheThread"); 263 base::Thread cache_thread("CacheThread");
264 ASSERT_TRUE(cache_thread.StartWithOptions( 264 ASSERT_TRUE(cache_thread.StartWithOptions(
265 base::Thread::Options(MessageLoop::TYPE_IO, 0))); 265 base::Thread::Options(MessageLoop::TYPE_IO, 0)));
266 266
267 disk_cache::Backend* cache; 267 disk_cache::Backend* cache;
268 int rv = disk_cache::BackendImpl::CreateBackend( 268 int rv = disk_cache::BackendImpl::CreateBackend(
269 path, false, 0, net::DISK_CACHE, disk_cache::kNoRandom, 269 path, false, 0, net::DISK_CACHE, disk_cache::kNoRandom,
270 cache_thread.message_loop_proxy(), &cache, &callback); 270 base::MessageLoopProxy::CreateForCurrentThread(), &cache, &cb);
271 ASSERT_EQ(net::OK, callback.GetResult(rv)); 271 ASSERT_EQ(net::OK, cb.GetResult(rv));
272 272
273 disk_cache::Entry* entry; 273 disk_cache::EntryImpl* entry;
274 rv = cache->CreateEntry("some key", &entry, &callback); 274 rv = cache->CreateEntry("some key",
275 ASSERT_EQ(net::OK, callback.GetResult(rv)); 275 reinterpret_cast<disk_cache::Entry**>(&entry), &cb);
276 ASSERT_EQ(net::OK, cb.GetResult(rv));
276 277
277 const int kSize = 25000; 278 const int kSize = 25000;
278 scoped_refptr<net::IOBuffer> buffer = new net::IOBuffer(kSize); 279 scoped_refptr<net::IOBuffer> buffer = new net::IOBuffer(kSize);
279 CacheTestFillBuffer(buffer->data(), kSize, false); 280 CacheTestFillBuffer(buffer->data(), kSize, false);
280 281
281 for (int i = 0; i < 10 * 1024 * 1024; i += 64 * 1024) { 282 for (int i = 0; i < 10 * 1024 * 1024; i += 64 * 1024) {
282 int rv = entry->WriteData(0, i, buffer, kSize, &callback, false); 283 // We are using the current thread as the cache thread because we want to
284 // be able to call directly this method to make sure that the OS (instead
285 // of us switching thread) is returning IO pending.
286 rv = entry->WriteDataImpl(0, i, buffer, kSize, &cb, false);
283 if (rv == net::ERR_IO_PENDING) 287 if (rv == net::ERR_IO_PENDING)
284 break; 288 break;
285 EXPECT_EQ(kSize, rv); 289 EXPECT_EQ(kSize, rv);
286 } 290 }
287 291
292 // Don't call Close() to avoid going through the queue or we'll deadlock
293 // waiting for the operation to finish.
294 entry->Release();
295
296 // The cache destructor will see one pending operation here.
297 delete cache;
298
299 if (rv == net::ERR_IO_PENDING) {
300 EXPECT_TRUE(cb.have_result());
301 }
302 }
303
304 MessageLoop::current()->RunAllPending();
305 }
306
307 // Tests that we deal with background-thread pending operations.
308 TEST_F(DiskCacheTest, ShutdownWithPendingIO2) {
309 TestCompletionCallback cb;
310
311 {
312 FilePath path = GetCacheFilePath();
313 ASSERT_TRUE(DeleteCache(path));
314 base::Thread cache_thread("CacheThread");
315 ASSERT_TRUE(cache_thread.StartWithOptions(
316 base::Thread::Options(MessageLoop::TYPE_IO, 0)));
317
318 disk_cache::Backend* cache;
319 int rv = disk_cache::BackendImpl::CreateBackend(
320 path, false, 0, net::DISK_CACHE, disk_cache::kNoRandom,
321 cache_thread.message_loop_proxy(), &cache, &cb);
322 ASSERT_EQ(net::OK, cb.GetResult(rv));
323
324 disk_cache::Entry* entry;
325 rv = cache->CreateEntry("some key", &entry, &cb);
326 ASSERT_EQ(net::OK, cb.GetResult(rv));
327
328 const int kSize = 25000;
329 scoped_refptr<net::IOBuffer> buffer = new net::IOBuffer(kSize);
330 CacheTestFillBuffer(buffer->data(), kSize, false);
331
332 rv = entry->WriteData(0, 0, buffer, kSize, &cb, false);
333 EXPECT_EQ(net::ERR_IO_PENDING, rv);
334
288 entry->Close(); 335 entry->Close();
289 336
290 // The cache destructor will see one pending operation here. 337 // The cache destructor will see two pending operations here.
291 delete cache; 338 delete cache;
292 } 339 }
293 340
294 MessageLoop::current()->RunAllPending(); 341 MessageLoop::current()->RunAllPending();
295 } 342 }
296 343
297 TEST_F(DiskCacheTest, TruncatedIndex) { 344 TEST_F(DiskCacheTest, TruncatedIndex) {
298 FilePath path = GetCacheFilePath(); 345 FilePath path = GetCacheFilePath();
299 ASSERT_TRUE(DeleteCache(path)); 346 ASSERT_TRUE(DeleteCache(path));
300 FilePath index = path.AppendASCII("index"); 347 FilePath index = path.AppendASCII("index");
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after
395 } 442 }
396 443
397 for (int i = 0; i < 100; i++) { 444 for (int i = 0; i < 100; i++) {
398 disk_cache::Entry* entry; 445 disk_cache::Entry* entry;
399 ASSERT_EQ(net::OK, OpenEntry(entries[i]->GetKey(), &entry)); 446 ASSERT_EQ(net::OK, OpenEntry(entries[i]->GetKey(), &entry));
400 EXPECT_TRUE(entry == entries[i]); 447 EXPECT_TRUE(entry == entries[i]);
401 entry->Close(); 448 entry->Close();
402 entries[i]->Doom(); 449 entries[i]->Doom();
403 entries[i]->Close(); 450 entries[i]->Close();
404 } 451 }
452 FlushQueueForTest();
405 EXPECT_EQ(0, cache_->GetEntryCount()); 453 EXPECT_EQ(0, cache_->GetEntryCount());
406 } 454 }
407 455
408 TEST_F(DiskCacheBackendTest, Load) { 456 TEST_F(DiskCacheBackendTest, Load) {
409 // Work with a tiny index table (16 entries) 457 // Work with a tiny index table (16 entries)
410 SetMask(0xf); 458 SetMask(0xf);
411 SetMaxSize(0x100000); 459 SetMaxSize(0x100000);
412 BackendLoad(); 460 BackendLoad();
413 } 461 }
414 462
(...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after
618 666
619 // Simulate a crash. 667 // Simulate a crash.
620 SimulateCrash(); 668 SimulateCrash();
621 669
622 ASSERT_EQ(net::OK, CreateEntry(second, &entry)); 670 ASSERT_EQ(net::OK, CreateEntry(second, &entry));
623 EXPECT_EQ(kSize, entry->WriteData(0, 0, buffer, kSize, NULL, false)); 671 EXPECT_EQ(kSize, entry->WriteData(0, 0, buffer, kSize, NULL, false));
624 672
625 EXPECT_EQ(2, cache_->GetEntryCount()); 673 EXPECT_EQ(2, cache_->GetEntryCount());
626 SetMaxSize(kSize); 674 SetMaxSize(kSize);
627 entry->Close(); // Trim the cache. 675 entry->Close(); // Trim the cache.
676 FlushQueueForTest();
628 677
629 // If we evicted the entry in less than 20mS, we have one entry in the cache; 678 // If we evicted the entry in less than 20mS, we have one entry in the cache;
630 // if it took more than that, we posted a task and we'll delete the second 679 // if it took more than that, we posted a task and we'll delete the second
631 // entry too. 680 // entry too.
632 MessageLoop::current()->RunAllPending(); 681 MessageLoop::current()->RunAllPending();
633 EXPECT_GE(1, cache_->GetEntryCount()); 682 EXPECT_GE(1, cache_->GetEntryCount());
634 EXPECT_NE(net::OK, OpenEntry(first, &entry)); 683 EXPECT_NE(net::OK, OpenEntry(first, &entry));
635 } 684 }
636 685
637 // We'll be leaking memory from this test. 686 // We'll be leaking memory from this test.
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
678 EXPECT_EQ(33, cache_->GetEntryCount()); 727 EXPECT_EQ(33, cache_->GetEntryCount());
679 SetMaxSize(kSize); 728 SetMaxSize(kSize);
680 729
681 // For the new eviction code, all corrupt entries are on the second list so 730 // For the new eviction code, all corrupt entries are on the second list so
682 // they are not going away that easy. 731 // they are not going away that easy.
683 if (new_eviction_) { 732 if (new_eviction_) {
684 EXPECT_EQ(net::OK, DoomAllEntries()); 733 EXPECT_EQ(net::OK, DoomAllEntries());
685 } 734 }
686 735
687 entry->Close(); // Trim the cache. 736 entry->Close(); // Trim the cache.
737 FlushQueueForTest();
688 738
689 // We may abort the eviction before cleaning up everything. 739 // We may abort the eviction before cleaning up everything.
690 MessageLoop::current()->RunAllPending(); 740 MessageLoop::current()->RunAllPending();
691 EXPECT_GE(30, cache_->GetEntryCount()); 741 EXPECT_GE(30, cache_->GetEntryCount());
692 } 742 }
693 743
694 // We'll be leaking memory from this test. 744 // We'll be leaking memory from this test.
695 TEST_F(DiskCacheBackendTest, TrimInvalidEntry2) { 745 TEST_F(DiskCacheBackendTest, TrimInvalidEntry2) {
696 BackendTrimInvalidEntry2(); 746 BackendTrimInvalidEntry2();
697 } 747 }
(...skipping 554 matching lines...) Expand 10 before | Expand all | Expand 10 after
1252 1302
1253 // If the LRU is corrupt, we delete the cache. 1303 // If the LRU is corrupt, we delete the cache.
1254 void DiskCacheBackendTest::BackendInvalidRankings() { 1304 void DiskCacheBackendTest::BackendInvalidRankings() {
1255 disk_cache::Entry* entry; 1305 disk_cache::Entry* entry;
1256 void* iter = NULL; 1306 void* iter = NULL;
1257 ASSERT_EQ(net::OK, OpenNextEntry(&iter, &entry)); 1307 ASSERT_EQ(net::OK, OpenNextEntry(&iter, &entry));
1258 entry->Close(); 1308 entry->Close();
1259 EXPECT_EQ(2, cache_->GetEntryCount()); 1309 EXPECT_EQ(2, cache_->GetEntryCount());
1260 1310
1261 EXPECT_NE(net::OK, OpenNextEntry(&iter, &entry)); 1311 EXPECT_NE(net::OK, OpenNextEntry(&iter, &entry));
1262 MessageLoop::current()->RunAllPending(); 1312 FlushQueueForTest(); // Allow the restart to finish.
1263 EXPECT_EQ(0, cache_->GetEntryCount()); 1313 EXPECT_EQ(0, cache_->GetEntryCount());
1264 } 1314 }
1265 1315
1266 TEST_F(DiskCacheBackendTest, InvalidRankingsSuccess) { 1316 TEST_F(DiskCacheBackendTest, InvalidRankingsSuccess) {
1267 ASSERT_TRUE(CopyTestCache(L"bad_rankings")); 1317 ASSERT_TRUE(CopyTestCache(L"bad_rankings"));
1268 DisableFirstCleanup(); 1318 DisableFirstCleanup();
1269 SetDirectMode(); 1319 SetDirectMode();
1270 InitCache(); 1320 InitCache();
1271 BackendInvalidRankings(); 1321 BackendInvalidRankings();
1272 } 1322 }
(...skipping 30 matching lines...) Expand all
1303 void DiskCacheBackendTest::BackendDisable() { 1353 void DiskCacheBackendTest::BackendDisable() {
1304 disk_cache::Entry *entry1, *entry2; 1354 disk_cache::Entry *entry1, *entry2;
1305 void* iter = NULL; 1355 void* iter = NULL;
1306 ASSERT_EQ(net::OK, OpenNextEntry(&iter, &entry1)); 1356 ASSERT_EQ(net::OK, OpenNextEntry(&iter, &entry1));
1307 1357
1308 EXPECT_NE(net::OK, OpenNextEntry(&iter, &entry2)); 1358 EXPECT_NE(net::OK, OpenNextEntry(&iter, &entry2));
1309 EXPECT_EQ(2, cache_->GetEntryCount()); 1359 EXPECT_EQ(2, cache_->GetEntryCount());
1310 EXPECT_NE(net::OK, CreateEntry("Something new", &entry2)); 1360 EXPECT_NE(net::OK, CreateEntry("Something new", &entry2));
1311 1361
1312 entry1->Close(); 1362 entry1->Close();
1313 MessageLoop::current()->RunAllPending(); 1363 FlushQueueForTest(); // Flushing the Close posts a task to restart the cache.
1364 FlushQueueForTest(); // This one actually allows that task to complete.
1314 1365
1315 EXPECT_EQ(0, cache_->GetEntryCount()); 1366 EXPECT_EQ(0, cache_->GetEntryCount());
1316 } 1367 }
1317 1368
1318 TEST_F(DiskCacheBackendTest, DisableSuccess) { 1369 TEST_F(DiskCacheBackendTest, DisableSuccess) {
1319 ASSERT_TRUE(CopyTestCache(L"bad_rankings")); 1370 ASSERT_TRUE(CopyTestCache(L"bad_rankings"));
1320 DisableFirstCleanup(); 1371 DisableFirstCleanup();
1321 SetDirectMode(); 1372 SetDirectMode();
1322 InitCache(); 1373 InitCache();
1323 BackendDisable(); 1374 BackendDisable();
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
1358 disk_cache::Entry* entry; 1409 disk_cache::Entry* entry;
1359 void* iter = NULL; 1410 void* iter = NULL;
1360 int count = 0; 1411 int count = 0;
1361 while (OpenNextEntry(&iter, &entry) == net::OK) { 1412 while (OpenNextEntry(&iter, &entry) == net::OK) {
1362 ASSERT_TRUE(NULL != entry); 1413 ASSERT_TRUE(NULL != entry);
1363 entry->Close(); 1414 entry->Close();
1364 count++; 1415 count++;
1365 ASSERT_LT(count, 9); 1416 ASSERT_LT(count, 9);
1366 }; 1417 };
1367 1418
1368 MessageLoop::current()->RunAllPending(); 1419 FlushQueueForTest();
1369 EXPECT_EQ(0, cache_->GetEntryCount()); 1420 EXPECT_EQ(0, cache_->GetEntryCount());
1370 } 1421 }
1371 1422
1372 TEST_F(DiskCacheBackendTest, DisableSuccess2) { 1423 TEST_F(DiskCacheBackendTest, DisableSuccess2) {
1373 ASSERT_TRUE(CopyTestCache(L"list_loop")); 1424 ASSERT_TRUE(CopyTestCache(L"list_loop"));
1374 DisableFirstCleanup(); 1425 DisableFirstCleanup();
1375 SetDirectMode(); 1426 SetDirectMode();
1376 InitCache(); 1427 InitCache();
1377 BackendDisable2(); 1428 BackendDisable2();
1378 } 1429 }
(...skipping 28 matching lines...) Expand all
1407 1458
1408 // If the index size changes when we disable the cache, we should not crash. 1459 // If the index size changes when we disable the cache, we should not crash.
1409 void DiskCacheBackendTest::BackendDisable3() { 1460 void DiskCacheBackendTest::BackendDisable3() {
1410 disk_cache::Entry *entry1, *entry2; 1461 disk_cache::Entry *entry1, *entry2;
1411 void* iter = NULL; 1462 void* iter = NULL;
1412 EXPECT_EQ(2, cache_->GetEntryCount()); 1463 EXPECT_EQ(2, cache_->GetEntryCount());
1413 ASSERT_EQ(net::OK, OpenNextEntry(&iter, &entry1)); 1464 ASSERT_EQ(net::OK, OpenNextEntry(&iter, &entry1));
1414 entry1->Close(); 1465 entry1->Close();
1415 1466
1416 EXPECT_NE(net::OK, OpenNextEntry(&iter, &entry2)); 1467 EXPECT_NE(net::OK, OpenNextEntry(&iter, &entry2));
1417 MessageLoop::current()->RunAllPending(); 1468 FlushQueueForTest();
1418 1469
1419 ASSERT_EQ(net::OK, CreateEntry("Something new", &entry2)); 1470 ASSERT_EQ(net::OK, CreateEntry("Something new", &entry2));
1420 entry2->Close(); 1471 entry2->Close();
1421 1472
1422 EXPECT_EQ(1, cache_->GetEntryCount()); 1473 EXPECT_EQ(1, cache_->GetEntryCount());
1423 } 1474 }
1424 1475
1425 TEST_F(DiskCacheBackendTest, DisableSuccess3) { 1476 TEST_F(DiskCacheBackendTest, DisableSuccess3) {
1426 ASSERT_TRUE(CopyTestCache(L"bad_rankings2")); 1477 ASSERT_TRUE(CopyTestCache(L"bad_rankings2"));
1427 DisableFirstCleanup(); 1478 DisableFirstCleanup();
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
1475 EXPECT_EQ(kBufSize, entry3->WriteData(1, 0, buf, kBufSize, NULL, false)); 1526 EXPECT_EQ(kBufSize, entry3->WriteData(1, 0, buf, kBufSize, NULL, false));
1476 1527
1477 std::string key = entry2->GetKey(); 1528 std::string key = entry2->GetKey();
1478 EXPECT_EQ(sizeof(key2) - 1, key.size()); 1529 EXPECT_EQ(sizeof(key2) - 1, key.size());
1479 key = entry3->GetKey(); 1530 key = entry3->GetKey();
1480 EXPECT_EQ(sizeof(key3) - 1, key.size()); 1531 EXPECT_EQ(sizeof(key3) - 1, key.size());
1481 1532
1482 entry1->Close(); 1533 entry1->Close();
1483 entry2->Close(); 1534 entry2->Close();
1484 entry3->Close(); 1535 entry3->Close();
1485 MessageLoop::current()->RunAllPending(); 1536 FlushQueueForTest(); // Flushing the Close posts a task to restart the cache.
1537 FlushQueueForTest(); // This one actually allows that task to complete.
1486 1538
1487 EXPECT_EQ(0, cache_->GetEntryCount()); 1539 EXPECT_EQ(0, cache_->GetEntryCount());
1488 } 1540 }
1489 1541
1490 TEST_F(DiskCacheBackendTest, DisableSuccess4) { 1542 TEST_F(DiskCacheBackendTest, DisableSuccess4) {
1491 ASSERT_TRUE(CopyTestCache(L"bad_rankings")); 1543 ASSERT_TRUE(CopyTestCache(L"bad_rankings"));
1492 DisableFirstCleanup(); 1544 DisableFirstCleanup();
1493 SetDirectMode(); 1545 SetDirectMode();
1494 InitCache(); 1546 InitCache();
1495 BackendDisable4(); 1547 BackendDisable4();
(...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after
1697 // another. 1749 // another.
1698 TEST_F(DiskCacheBackendTest, Histograms) { 1750 TEST_F(DiskCacheBackendTest, Histograms) {
1699 SetDirectMode(); 1751 SetDirectMode();
1700 InitCache(); 1752 InitCache();
1701 disk_cache::BackendImpl* backend_ = cache_impl_; // Needed be the macro. 1753 disk_cache::BackendImpl* backend_ = cache_impl_; // Needed be the macro.
1702 1754
1703 for (int i = 1; i < 3; i++) { 1755 for (int i = 1; i < 3; i++) {
1704 CACHE_UMA(HOURS, "FillupTime", i, 28); 1756 CACHE_UMA(HOURS, "FillupTime", i, 28);
1705 } 1757 }
1706 } 1758 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698