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

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

Issue 3167020: Disk cache: Extend the internal buffering performed by each entry... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 10 years, 4 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
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/platform_thread.h" 7 #include "base/platform_thread.h"
8 #include "base/string_util.h" 8 #include "base/string_util.h"
9 #include "net/base/io_buffer.h" 9 #include "net/base/io_buffer.h"
10 #include "net/base/net_errors.h" 10 #include "net/base/net_errors.h"
(...skipping 206 matching lines...) Expand 10 before | Expand all | Expand 10 after
217 FilePath filename = GetCacheFilePath().AppendASCII("f_000001"); 217 FilePath filename = GetCacheFilePath().AppendASCII("f_000001");
218 218
219 const int kSize = 50; 219 const int kSize = 50;
220 scoped_refptr<net::IOBuffer> buffer1 = new net::IOBuffer(kSize); 220 scoped_refptr<net::IOBuffer> buffer1 = new net::IOBuffer(kSize);
221 CacheTestFillBuffer(buffer1->data(), kSize, false); 221 CacheTestFillBuffer(buffer1->data(), kSize, false);
222 ASSERT_EQ(kSize, file_util::WriteFile(filename, buffer1->data(), kSize)); 222 ASSERT_EQ(kSize, file_util::WriteFile(filename, buffer1->data(), kSize));
223 223
224 // Now let's create a file with the cache. 224 // Now let's create a file with the cache.
225 disk_cache::Entry* entry; 225 disk_cache::Entry* entry;
226 ASSERT_EQ(net::OK, CreateEntry("key", &entry)); 226 ASSERT_EQ(net::OK, CreateEntry("key", &entry));
227 ASSERT_EQ(0, entry->WriteData(0, 20000, buffer1, 0, NULL, false)); 227 ASSERT_EQ(0, WriteData(entry, 0, 20000, buffer1, 0, false));
228 entry->Close(); 228 entry->Close();
229 229
230 // And verify that the first file is still there. 230 // And verify that the first file is still there.
231 scoped_refptr<net::IOBuffer> buffer2 = new net::IOBuffer(kSize); 231 scoped_refptr<net::IOBuffer> buffer2 = new net::IOBuffer(kSize);
232 ASSERT_EQ(kSize, file_util::ReadFile(filename, buffer2->data(), kSize)); 232 ASSERT_EQ(kSize, file_util::ReadFile(filename, buffer2->data(), kSize));
233 EXPECT_EQ(0, memcmp(buffer1->data(), buffer2->data(), kSize)); 233 EXPECT_EQ(0, memcmp(buffer1->data(), buffer2->data(), kSize));
234 } 234 }
235 235
236 // Tests that we deal with file-level pending operations at destruction time. 236 // Tests that we deal with file-level pending operations at destruction time.
237 TEST_F(DiskCacheTest, ShutdownWithPendingIO) { 237 TEST_F(DiskCacheTest, ShutdownWithPendingIO) {
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after
348 SetMaxSize(cache_size); 348 SetMaxSize(cache_size);
349 InitCache(); 349 InitCache();
350 350
351 std::string first("some key"); 351 std::string first("some key");
352 std::string second("something else"); 352 std::string second("something else");
353 disk_cache::Entry* entry; 353 disk_cache::Entry* entry;
354 ASSERT_EQ(net::OK, CreateEntry(first, &entry)); 354 ASSERT_EQ(net::OK, CreateEntry(first, &entry));
355 355
356 scoped_refptr<net::IOBuffer> buffer = new net::IOBuffer(cache_size); 356 scoped_refptr<net::IOBuffer> buffer = new net::IOBuffer(cache_size);
357 memset(buffer->data(), 0, cache_size); 357 memset(buffer->data(), 0, cache_size);
358 EXPECT_EQ(cache_size / 10, entry->WriteData(0, 0, buffer, cache_size / 10, 358 EXPECT_EQ(cache_size / 10, WriteData(entry, 0, 0, buffer, cache_size / 10,
359 NULL, false)) << "normal file"; 359 false)) << "normal file";
360 360
361 EXPECT_EQ(net::ERR_FAILED, entry->WriteData(1, 0, buffer, cache_size / 5, 361 EXPECT_EQ(net::ERR_FAILED, WriteData(entry, 1, 0, buffer, cache_size / 5,
362 NULL, false)) << "file size above the limit"; 362 false)) << "file size above the limit";
363 363
364 // By doubling the total size, we make this file cacheable. 364 // By doubling the total size, we make this file cacheable.
365 SetMaxSize(cache_size * 2); 365 SetMaxSize(cache_size * 2);
366 EXPECT_EQ(cache_size / 5, entry->WriteData(1, 0, buffer, cache_size / 5, 366 EXPECT_EQ(cache_size / 5, WriteData(entry, 1, 0, buffer, cache_size / 5,
367 NULL, false)); 367 false));
368 368
369 // Let's fill up the cache!. 369 // Let's fill up the cache!.
370 SetMaxSize(cache_size * 10); 370 SetMaxSize(cache_size * 10);
371 EXPECT_EQ(cache_size * 3 / 4, entry->WriteData(0, 0, buffer, 371 EXPECT_EQ(cache_size * 3 / 4, WriteData(entry, 0, 0, buffer,
372 cache_size * 3 / 4, NULL, false)); 372 cache_size * 3 / 4, false));
373 entry->Close(); 373 entry->Close();
374 FlushQueueForTest(); 374 FlushQueueForTest();
375 375
376 SetMaxSize(cache_size); 376 SetMaxSize(cache_size);
377 377
378 // The cache is 95% full. 378 // The cache is 95% full.
379 379
380 ASSERT_EQ(net::OK, CreateEntry(second, &entry)); 380 ASSERT_EQ(net::OK, CreateEntry(second, &entry));
381 EXPECT_EQ(cache_size / 10, entry->WriteData(0, 0, buffer, cache_size / 10, 381 EXPECT_EQ(cache_size / 10, WriteData(entry, 0, 0, buffer, cache_size / 10,
382 NULL, false)); 382 false));
383 383
384 disk_cache::Entry* entry2; 384 disk_cache::Entry* entry2;
385 ASSERT_EQ(net::OK, CreateEntry("an extra key", &entry2)); 385 ASSERT_EQ(net::OK, CreateEntry("an extra key", &entry2));
386 EXPECT_EQ(cache_size / 10, entry2->WriteData(0, 0, buffer, cache_size / 10, 386 EXPECT_EQ(cache_size / 10, WriteData(entry2, 0, 0, buffer, cache_size / 10,
387 NULL, false)); 387 false));
388 entry2->Close(); // This will trigger the cache trim. 388 entry2->Close(); // This will trigger the cache trim.
389 389
390 EXPECT_NE(net::OK, OpenEntry(first, &entry2)); 390 EXPECT_NE(net::OK, OpenEntry(first, &entry2));
391 391
392 FlushQueueForTest(); // Make sure that we are done trimming the cache. 392 FlushQueueForTest(); // Make sure that we are done trimming the cache.
393 FlushQueueForTest(); // We may have posted two tasks to evict stuff. 393 FlushQueueForTest(); // We may have posted two tasks to evict stuff.
394 394
395 entry->Close(); 395 entry->Close();
396 ASSERT_EQ(net::OK, OpenEntry(second, &entry)); 396 ASSERT_EQ(net::OK, OpenEntry(second, &entry));
397 EXPECT_EQ(cache_size / 10, entry->GetDataSize(0)); 397 EXPECT_EQ(cache_size / 10, entry->GetDataSize(0));
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
465 SetMemoryOnlyMode(); 465 SetMemoryOnlyMode();
466 BackendLoad(); 466 BackendLoad();
467 } 467 }
468 468
469 // Before looking for invalid entries, let's check a valid entry. 469 // Before looking for invalid entries, let's check a valid entry.
470 void DiskCacheBackendTest::BackendValidEntry() { 470 void DiskCacheBackendTest::BackendValidEntry() {
471 SetDirectMode(); 471 SetDirectMode();
472 InitCache(); 472 InitCache();
473 473
474 std::string key("Some key"); 474 std::string key("Some key");
475 disk_cache::Entry* entry1; 475 disk_cache::Entry* entry;
476 ASSERT_EQ(net::OK, CreateEntry(key, &entry1)); 476 ASSERT_EQ(net::OK, CreateEntry(key, &entry));
477 477
478 const int kSize = 50; 478 const int kSize = 50;
479 scoped_refptr<net::IOBuffer> buffer1 = new net::IOBuffer(kSize); 479 scoped_refptr<net::IOBuffer> buffer1 = new net::IOBuffer(kSize);
480 memset(buffer1->data(), 0, kSize); 480 memset(buffer1->data(), 0, kSize);
481 base::strlcpy(buffer1->data(), "And the data to save", kSize); 481 base::strlcpy(buffer1->data(), "And the data to save", kSize);
482 EXPECT_EQ(kSize, entry1->WriteData(0, 0, buffer1, kSize, NULL, false)); 482 EXPECT_EQ(kSize, WriteData(entry, 0, 0, buffer1, kSize, false));
483 entry1->Close(); 483 entry->Close();
484 SimulateCrash(); 484 SimulateCrash();
485 485
486 ASSERT_EQ(net::OK, OpenEntry(key, &entry1)); 486 ASSERT_EQ(net::OK, OpenEntry(key, &entry));
487 487
488 scoped_refptr<net::IOBuffer> buffer2 = new net::IOBuffer(kSize); 488 scoped_refptr<net::IOBuffer> buffer2 = new net::IOBuffer(kSize);
489 memset(buffer2->data(), 0, kSize); 489 memset(buffer2->data(), 0, kSize);
490 EXPECT_EQ(kSize, entry1->ReadData(0, 0, buffer2, kSize, NULL)); 490 EXPECT_EQ(kSize, ReadData(entry, 0, 0, buffer2, kSize));
491 entry1->Close(); 491 entry->Close();
492 EXPECT_STREQ(buffer1->data(), buffer2->data()); 492 EXPECT_STREQ(buffer1->data(), buffer2->data());
493 } 493 }
494 494
495 TEST_F(DiskCacheBackendTest, ValidEntry) { 495 TEST_F(DiskCacheBackendTest, ValidEntry) {
496 BackendValidEntry(); 496 BackendValidEntry();
497 } 497 }
498 498
499 TEST_F(DiskCacheBackendTest, NewEvictionValidEntry) { 499 TEST_F(DiskCacheBackendTest, NewEvictionValidEntry) {
500 SetNewEviction(); 500 SetNewEviction();
501 BackendValidEntry(); 501 BackendValidEntry();
502 } 502 }
503 503
504 // The same logic of the previous test (ValidEntry), but this time force the 504 // The same logic of the previous test (ValidEntry), but this time force the
505 // entry to be invalid, simulating a crash in the middle. 505 // entry to be invalid, simulating a crash in the middle.
506 // We'll be leaking memory from this test. 506 // We'll be leaking memory from this test.
507 void DiskCacheBackendTest::BackendInvalidEntry() { 507 void DiskCacheBackendTest::BackendInvalidEntry() {
508 // Use the implementation directly... we need to simulate a crash. 508 // Use the implementation directly... we need to simulate a crash.
509 SetDirectMode(); 509 SetDirectMode();
510 InitCache(); 510 InitCache();
511 511
512 std::string key("Some key"); 512 std::string key("Some key");
513 disk_cache::Entry* entry1; 513 disk_cache::Entry* entry;
514 ASSERT_EQ(net::OK, CreateEntry(key, &entry1)); 514 ASSERT_EQ(net::OK, CreateEntry(key, &entry));
515 515
516 const int kSize = 50; 516 const int kSize = 50;
517 scoped_refptr<net::IOBuffer> buffer1 = new net::IOBuffer(kSize); 517 scoped_refptr<net::IOBuffer> buffer = new net::IOBuffer(kSize);
518 memset(buffer1->data(), 0, kSize); 518 memset(buffer->data(), 0, kSize);
519 base::strlcpy(buffer1->data(), "And the data to save", kSize); 519 base::strlcpy(buffer->data(), "And the data to save", kSize);
520 EXPECT_EQ(kSize, entry1->WriteData(0, 0, buffer1, kSize, NULL, false)); 520 EXPECT_EQ(kSize, WriteData(entry, 0, 0, buffer, kSize, false));
521 SimulateCrash(); 521 SimulateCrash();
522 522
523 EXPECT_NE(net::OK, OpenEntry(key, &entry1)); 523 EXPECT_NE(net::OK, OpenEntry(key, &entry));
524 EXPECT_EQ(0, cache_->GetEntryCount()); 524 EXPECT_EQ(0, cache_->GetEntryCount());
525 } 525 }
526 526
527 // This and the other intentionally leaky tests below are excluded from 527 // This and the other intentionally leaky tests below are excluded from
528 // purify and valgrind runs by naming them in the files 528 // purify and valgrind runs by naming them in the files
529 // net/data/purify/net_unittests.exe.gtest.txt and 529 // net/data/purify/net_unittests.exe.gtest.txt and
530 // net/data/valgrind/net_unittests.gtest.txt 530 // net/data/valgrind/net_unittests.gtest.txt
531 // The scripts tools/{purify,valgrind}/chrome_tests.sh 531 // The scripts tools/{purify,valgrind}/chrome_tests.sh
532 // read those files and pass the appropriate --gtest_filter to net_unittests. 532 // read those files and pass the appropriate --gtest_filter to net_unittests.
533 TEST_F(DiskCacheBackendTest, InvalidEntry) { 533 TEST_F(DiskCacheBackendTest, InvalidEntry) {
534 BackendInvalidEntry(); 534 BackendInvalidEntry();
535 } 535 }
536 536
537 // We'll be leaking memory from this test. 537 // We'll be leaking memory from this test.
538 TEST_F(DiskCacheBackendTest, NewEvictionInvalidEntry) { 538 TEST_F(DiskCacheBackendTest, NewEvictionInvalidEntry) {
539 SetNewEviction(); 539 SetNewEviction();
540 BackendInvalidEntry(); 540 BackendInvalidEntry();
541 } 541 }
542 542
543 // Almost the same test, but this time crash the cache after reading an entry. 543 // Almost the same test, but this time crash the cache after reading an entry.
544 // We'll be leaking memory from this test. 544 // We'll be leaking memory from this test.
545 void DiskCacheBackendTest::BackendInvalidEntryRead() { 545 void DiskCacheBackendTest::BackendInvalidEntryRead() {
546 // Use the implementation directly... we need to simulate a crash. 546 // Use the implementation directly... we need to simulate a crash.
547 SetDirectMode(); 547 SetDirectMode();
548 InitCache(); 548 InitCache();
549 549
550 std::string key("Some key"); 550 std::string key("Some key");
551 disk_cache::Entry* entry1; 551 disk_cache::Entry* entry;
552 ASSERT_EQ(net::OK, CreateEntry(key, &entry1)); 552 ASSERT_EQ(net::OK, CreateEntry(key, &entry));
553 553
554 const int kSize = 50; 554 const int kSize = 50;
555 scoped_refptr<net::IOBuffer> buffer1 = new net::IOBuffer(kSize); 555 scoped_refptr<net::IOBuffer> buffer = new net::IOBuffer(kSize);
556 memset(buffer1->data(), 0, kSize); 556 memset(buffer->data(), 0, kSize);
557 base::strlcpy(buffer1->data(), "And the data to save", kSize); 557 base::strlcpy(buffer->data(), "And the data to save", kSize);
558 EXPECT_EQ(kSize, entry1->WriteData(0, 0, buffer1, kSize, NULL, false)); 558 EXPECT_EQ(kSize, WriteData(entry, 0, 0, buffer, kSize, false));
559 entry1->Close(); 559 entry->Close();
560 ASSERT_EQ(net::OK, OpenEntry(key, &entry1)); 560 ASSERT_EQ(net::OK, OpenEntry(key, &entry));
561 EXPECT_EQ(kSize, entry1->ReadData(0, 0, buffer1, kSize, NULL)); 561 EXPECT_EQ(kSize, ReadData(entry, 0, 0, buffer, kSize));
562 562
563 SimulateCrash(); 563 SimulateCrash();
564 564
565 EXPECT_NE(net::OK, OpenEntry(key, &entry1)); 565 EXPECT_NE(net::OK, OpenEntry(key, &entry));
566 EXPECT_EQ(0, cache_->GetEntryCount()); 566 EXPECT_EQ(0, cache_->GetEntryCount());
567 } 567 }
568 568
569 // We'll be leaking memory from this test. 569 // We'll be leaking memory from this test.
570 TEST_F(DiskCacheBackendTest, InvalidEntryRead) { 570 TEST_F(DiskCacheBackendTest, InvalidEntryRead) {
571 BackendInvalidEntryRead(); 571 BackendInvalidEntryRead();
572 } 572 }
573 573
574 // We'll be leaking memory from this test. 574 // We'll be leaking memory from this test.
575 TEST_F(DiskCacheBackendTest, NewEvictionInvalidEntryRead) { 575 TEST_F(DiskCacheBackendTest, NewEvictionInvalidEntryRead) {
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
646 SetMaxSize(kSize * 10); 646 SetMaxSize(kSize * 10);
647 InitCache(); 647 InitCache();
648 648
649 std::string first("some key"); 649 std::string first("some key");
650 std::string second("something else"); 650 std::string second("something else");
651 disk_cache::Entry* entry; 651 disk_cache::Entry* entry;
652 ASSERT_EQ(net::OK, CreateEntry(first, &entry)); 652 ASSERT_EQ(net::OK, CreateEntry(first, &entry));
653 653
654 scoped_refptr<net::IOBuffer> buffer = new net::IOBuffer(kSize); 654 scoped_refptr<net::IOBuffer> buffer = new net::IOBuffer(kSize);
655 memset(buffer->data(), 0, kSize); 655 memset(buffer->data(), 0, kSize);
656 EXPECT_EQ(kSize, entry->WriteData(0, 0, buffer, kSize, NULL, false)); 656 EXPECT_EQ(kSize, WriteData(entry, 0, 0, buffer, kSize, false));
657 657
658 // Simulate a crash. 658 // Simulate a crash.
659 SimulateCrash(); 659 SimulateCrash();
660 660
661 ASSERT_EQ(net::OK, CreateEntry(second, &entry)); 661 ASSERT_EQ(net::OK, CreateEntry(second, &entry));
662 EXPECT_EQ(kSize, entry->WriteData(0, 0, buffer, kSize, NULL, false)); 662 EXPECT_EQ(kSize, WriteData(entry, 0, 0, buffer, kSize, false));
663 663
664 EXPECT_EQ(2, cache_->GetEntryCount()); 664 EXPECT_EQ(2, cache_->GetEntryCount());
665 SetMaxSize(kSize); 665 SetMaxSize(kSize);
666 entry->Close(); // Trim the cache. 666 entry->Close(); // Trim the cache.
667 FlushQueueForTest(); 667 FlushQueueForTest();
668 668
669 // If we evicted the entry in less than 20mS, we have one entry in the cache; 669 // If we evicted the entry in less than 20mS, we have one entry in the cache;
670 // if it took more than that, we posted a task and we'll delete the second 670 // if it took more than that, we posted a task and we'll delete the second
671 // entry too. 671 // entry too.
672 MessageLoop::current()->RunAllPending(); 672 MessageLoop::current()->RunAllPending();
(...skipping 23 matching lines...) Expand all
696 InitCache(); 696 InitCache();
697 697
698 scoped_refptr<net::IOBuffer> buffer = new net::IOBuffer(kSize); 698 scoped_refptr<net::IOBuffer> buffer = new net::IOBuffer(kSize);
699 memset(buffer->data(), 0, kSize); 699 memset(buffer->data(), 0, kSize);
700 disk_cache::Entry* entry; 700 disk_cache::Entry* entry;
701 701
702 // Writing 32 entries to this cache chains most of them. 702 // Writing 32 entries to this cache chains most of them.
703 for (int i = 0; i < 32; i++) { 703 for (int i = 0; i < 32; i++) {
704 std::string key(StringPrintf("some key %d", i)); 704 std::string key(StringPrintf("some key %d", i));
705 ASSERT_EQ(net::OK, CreateEntry(key, &entry)); 705 ASSERT_EQ(net::OK, CreateEntry(key, &entry));
706 EXPECT_EQ(kSize, entry->WriteData(0, 0, buffer, kSize, NULL, false)); 706 EXPECT_EQ(kSize, WriteData(entry, 0, 0, buffer, kSize, false));
707 entry->Close(); 707 entry->Close();
708 ASSERT_EQ(net::OK, OpenEntry(key, &entry)); 708 ASSERT_EQ(net::OK, OpenEntry(key, &entry));
709 // Note that we are not closing the entries. 709 // Note that we are not closing the entries.
710 } 710 }
711 711
712 // Simulate a crash. 712 // Simulate a crash.
713 SimulateCrash(); 713 SimulateCrash();
714 714
715 ASSERT_EQ(net::OK, CreateEntry("Something else", &entry)); 715 ASSERT_EQ(net::OK, CreateEntry("Something else", &entry));
716 EXPECT_EQ(kSize, entry->WriteData(0, 0, buffer, kSize, NULL, false)); 716 EXPECT_EQ(kSize, WriteData(entry, 0, 0, buffer, kSize, false));
717 717
718 EXPECT_EQ(33, cache_->GetEntryCount()); 718 EXPECT_EQ(33, cache_->GetEntryCount());
719 SetMaxSize(kSize); 719 SetMaxSize(kSize);
720 720
721 // For the new eviction code, all corrupt entries are on the second list so 721 // For the new eviction code, all corrupt entries are on the second list so
722 // they are not going away that easy. 722 // they are not going away that easy.
723 if (new_eviction_) { 723 if (new_eviction_) {
724 EXPECT_EQ(net::OK, DoomAllEntries()); 724 EXPECT_EQ(net::OK, DoomAllEntries());
725 } 725 }
726 726
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after
859 InitCache(); 859 InitCache();
860 860
861 std::string key("Some key"); 861 std::string key("Some key");
862 disk_cache::Entry *entry, *entry1, *entry2; 862 disk_cache::Entry *entry, *entry1, *entry2;
863 ASSERT_EQ(net::OK, CreateEntry(key, &entry1)); 863 ASSERT_EQ(net::OK, CreateEntry(key, &entry1));
864 864
865 const int kSize = 50; 865 const int kSize = 50;
866 scoped_refptr<net::IOBuffer> buffer1 = new net::IOBuffer(kSize); 866 scoped_refptr<net::IOBuffer> buffer1 = new net::IOBuffer(kSize);
867 memset(buffer1->data(), 0, kSize); 867 memset(buffer1->data(), 0, kSize);
868 base::strlcpy(buffer1->data(), "And the data to save", kSize); 868 base::strlcpy(buffer1->data(), "And the data to save", kSize);
869 EXPECT_EQ(kSize, entry1->WriteData(0, 0, buffer1, kSize, NULL, false)); 869 EXPECT_EQ(kSize, WriteData(entry1, 0, 0, buffer1, kSize, false));
870 entry1->Close(); 870 entry1->Close();
871 ASSERT_EQ(net::OK, OpenEntry(key, &entry1)); 871 ASSERT_EQ(net::OK, OpenEntry(key, &entry1));
872 EXPECT_EQ(kSize, entry1->ReadData(0, 0, buffer1, kSize, NULL)); 872 EXPECT_EQ(kSize, ReadData(entry1, 0, 0, buffer1, kSize));
873 873
874 std::string key2("Another key"); 874 std::string key2("Another key");
875 ASSERT_EQ(net::OK, CreateEntry(key2, &entry2)); 875 ASSERT_EQ(net::OK, CreateEntry(key2, &entry2));
876 entry2->Close(); 876 entry2->Close();
877 ASSERT_EQ(2, cache_->GetEntryCount()); 877 ASSERT_EQ(2, cache_->GetEntryCount());
878 878
879 SimulateCrash(); 879 SimulateCrash();
880 880
881 void* iter = NULL; 881 void* iter = NULL;
882 int count = 0; 882 int count = 0;
(...skipping 609 matching lines...) Expand 10 before | Expand all | Expand 10 after
1492 CacheTestFillBuffer(key2, sizeof(key2), true); 1492 CacheTestFillBuffer(key2, sizeof(key2), true);
1493 CacheTestFillBuffer(key3, sizeof(key3), true); 1493 CacheTestFillBuffer(key3, sizeof(key3), true);
1494 key2[sizeof(key2) - 1] = '\0'; 1494 key2[sizeof(key2) - 1] = '\0';
1495 key3[sizeof(key3) - 1] = '\0'; 1495 key3[sizeof(key3) - 1] = '\0';
1496 ASSERT_EQ(net::OK, CreateEntry(key2, &entry2)); 1496 ASSERT_EQ(net::OK, CreateEntry(key2, &entry2));
1497 ASSERT_EQ(net::OK, CreateEntry(key3, &entry3)); 1497 ASSERT_EQ(net::OK, CreateEntry(key3, &entry3));
1498 1498
1499 const int kBufSize = 20000; 1499 const int kBufSize = 20000;
1500 scoped_refptr<net::IOBuffer> buf = new net::IOBuffer(kBufSize); 1500 scoped_refptr<net::IOBuffer> buf = new net::IOBuffer(kBufSize);
1501 memset(buf->data(), 0, kBufSize); 1501 memset(buf->data(), 0, kBufSize);
1502 EXPECT_EQ(100, entry2->WriteData(0, 0, buf, 100, NULL, false)); 1502 EXPECT_EQ(100, WriteData(entry2, 0, 0, buf, 100, false));
1503 EXPECT_EQ(kBufSize, entry3->WriteData(0, 0, buf, kBufSize, NULL, false)); 1503 EXPECT_EQ(kBufSize, WriteData(entry3, 0, 0, buf, kBufSize, false));
1504 1504
1505 // This line should disable the cache but not delete it. 1505 // This line should disable the cache but not delete it.
1506 EXPECT_NE(net::OK, OpenNextEntry(&iter, &entry4)); 1506 EXPECT_NE(net::OK, OpenNextEntry(&iter, &entry4));
1507 EXPECT_EQ(4, cache_->GetEntryCount()); 1507 EXPECT_EQ(4, cache_->GetEntryCount());
1508 1508
1509 EXPECT_NE(net::OK, CreateEntry("cache is disabled", &entry4)); 1509 EXPECT_NE(net::OK, CreateEntry("cache is disabled", &entry4));
1510 1510
1511 EXPECT_EQ(100, entry2->ReadData(0, 0, buf, 100, NULL)); 1511 EXPECT_EQ(100, ReadData(entry2, 0, 0, buf, 100));
1512 EXPECT_EQ(100, entry2->WriteData(0, 0, buf, 100, NULL, false)); 1512 EXPECT_EQ(100, WriteData(entry2, 0, 0, buf, 100, false));
1513 EXPECT_EQ(100, entry2->WriteData(1, 0, buf, 100, NULL, false)); 1513 EXPECT_EQ(100, WriteData(entry2, 1, 0, buf, 100, false));
1514 1514
1515 EXPECT_EQ(kBufSize, entry3->ReadData(0, 0, buf, kBufSize, NULL)); 1515 EXPECT_EQ(kBufSize, ReadData(entry3, 0, 0, buf, kBufSize));
1516 EXPECT_EQ(kBufSize, entry3->WriteData(0, 0, buf, kBufSize, NULL, false)); 1516 EXPECT_EQ(kBufSize, WriteData(entry3, 0, 0, buf, kBufSize, false));
1517 EXPECT_EQ(kBufSize, entry3->WriteData(1, 0, buf, kBufSize, NULL, false)); 1517 EXPECT_EQ(kBufSize, WriteData(entry3, 1, 0, buf, kBufSize, false));
1518 1518
1519 std::string key = entry2->GetKey(); 1519 std::string key = entry2->GetKey();
1520 EXPECT_EQ(sizeof(key2) - 1, key.size()); 1520 EXPECT_EQ(sizeof(key2) - 1, key.size());
1521 key = entry3->GetKey(); 1521 key = entry3->GetKey();
1522 EXPECT_EQ(sizeof(key3) - 1, key.size()); 1522 EXPECT_EQ(sizeof(key3) - 1, key.size());
1523 1523
1524 entry1->Close(); 1524 entry1->Close();
1525 entry2->Close(); 1525 entry2->Close();
1526 entry3->Close(); 1526 entry3->Close();
1527 FlushQueueForTest(); // Flushing the Close posts a task to restart the cache. 1527 FlushQueueForTest(); // Flushing the Close posts a task to restart the cache.
(...skipping 212 matching lines...) Expand 10 before | Expand all | Expand 10 after
1740 // another. 1740 // another.
1741 TEST_F(DiskCacheBackendTest, Histograms) { 1741 TEST_F(DiskCacheBackendTest, Histograms) {
1742 SetDirectMode(); 1742 SetDirectMode();
1743 InitCache(); 1743 InitCache();
1744 disk_cache::BackendImpl* backend_ = cache_impl_; // Needed be the macro. 1744 disk_cache::BackendImpl* backend_ = cache_impl_; // Needed be the macro.
1745 1745
1746 for (int i = 1; i < 3; i++) { 1746 for (int i = 1; i < 3; i++) {
1747 CACHE_UMA(HOURS, "FillupTime", i, 28); 1747 CACHE_UMA(HOURS, "FillupTime", i, 28);
1748 } 1748 }
1749 } 1749 }
1750
1751 // Make sure that we keep the total memory used by the internal buffers under
1752 // control.
1753 TEST_F(DiskCacheBackendTest, TotalBuffersSize1) {
1754 SetDirectMode();
1755 InitCache();
1756 std::string key("the first key");
1757 disk_cache::Entry* entry;
1758 ASSERT_EQ(net::OK, CreateEntry(key, &entry));
1759
1760 const int kSize = 200;
1761 scoped_refptr<net::IOBuffer> buffer = new net::IOBuffer(kSize);
1762 CacheTestFillBuffer(buffer->data(), kSize, true);
1763
1764 for (int i = 0; i < 10; i++) {
1765 SCOPED_TRACE(i);
1766 // Allocate 2MB for this entry.
1767 EXPECT_EQ(kSize, WriteData(entry, 0, 0, buffer, kSize, true));
1768 EXPECT_EQ(kSize, WriteData(entry, 1, 0, buffer, kSize, true));
1769 EXPECT_EQ(kSize, WriteData(entry, 0, 1024 * 1024, buffer, kSize, false));
1770 EXPECT_EQ(kSize, WriteData(entry, 1, 1024 * 1024, buffer, kSize, false));
1771
1772 // Delete one of the buffers and truncate the other.
1773 EXPECT_EQ(0, WriteData(entry, 0, 0, buffer, 0, true));
1774 EXPECT_EQ(0, WriteData(entry, 1, 10, buffer, 0, true));
1775
1776 // Delete the second buffer, writing 10 bytes to disk.
1777 entry->Close();
1778 ASSERT_EQ(net::OK, OpenEntry(key, &entry));
1779 }
1780
1781 entry->Close();
1782 EXPECT_EQ(0, cache_impl_->GetTotalBuffersSize());
1783 }
1784
1785 // This test assumes at least 150MB of system memory.
1786 TEST_F(DiskCacheBackendTest, TotalBuffersSize2) {
1787 SetDirectMode();
1788 InitCache();
1789
1790 const int kOneMB = 1024 * 1024;
1791 EXPECT_TRUE(cache_impl_->IsAllocAllowed(0, kOneMB));
1792 EXPECT_EQ(kOneMB, cache_impl_->GetTotalBuffersSize());
1793
1794 EXPECT_TRUE(cache_impl_->IsAllocAllowed(0, kOneMB));
1795 EXPECT_EQ(kOneMB * 2, cache_impl_->GetTotalBuffersSize());
1796
1797 EXPECT_TRUE(cache_impl_->IsAllocAllowed(0, kOneMB));
1798 EXPECT_EQ(kOneMB * 3, cache_impl_->GetTotalBuffersSize());
1799
1800 cache_impl_->BufferDeleted(kOneMB);
1801 EXPECT_EQ(kOneMB * 2, cache_impl_->GetTotalBuffersSize());
1802
1803 // Check the upper limit.
1804 EXPECT_FALSE(cache_impl_->IsAllocAllowed(0, 30 * kOneMB));
1805
1806 for (int i = 0; i < 30; i++)
1807 cache_impl_->IsAllocAllowed(0, kOneMB); // Ignore the result.
1808
1809 EXPECT_FALSE(cache_impl_->IsAllocAllowed(0, kOneMB));
1810 }
gavinp 2010/08/19 18:17:22 NIT: add a newline here
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698