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

Side by Side Diff: sync/syncable/syncable_unittest.cc

Issue 11441026: [Sync] Add support for loading, updating and querying delete journals in (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: make IsDeleteJournalEnabled() public Created 8 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
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 <string> 5 #include <string>
6 6
7 #include "base/basictypes.h" 7 #include "base/basictypes.h"
8 #include "base/compiler_specific.h" 8 #include "base/compiler_specific.h"
9 #include "base/file_path.h" 9 #include "base/file_path.h"
10 #include "base/file_util.h" 10 #include "base/file_util.h"
11 #include "base/files/scoped_temp_dir.h" 11 #include "base/files/scoped_temp_dir.h"
12 #include "base/location.h" 12 #include "base/location.h"
13 #include "base/logging.h" 13 #include "base/logging.h"
14 #include "base/memory/scoped_ptr.h" 14 #include "base/memory/scoped_ptr.h"
15 #include "base/message_loop.h" 15 #include "base/message_loop.h"
16 #include "base/stl_util.h"
16 #include "base/stringprintf.h" 17 #include "base/stringprintf.h"
17 #include "base/synchronization/condition_variable.h" 18 #include "base/synchronization/condition_variable.h"
18 #include "base/test/values_test_util.h" 19 #include "base/test/values_test_util.h"
19 #include "base/threading/platform_thread.h" 20 #include "base/threading/platform_thread.h"
20 #include "base/values.h" 21 #include "base/values.h"
21 #include "sync/internal_api/public/base/node_ordinal.h" 22 #include "sync/internal_api/public/base/node_ordinal.h"
22 #include "sync/protocol/bookmark_specifics.pb.h" 23 #include "sync/protocol/bookmark_specifics.pb.h"
23 #include "sync/syncable/directory_backing_store.h" 24 #include "sync/syncable/directory_backing_store.h"
24 #include "sync/syncable/directory_change_delegate.h" 25 #include "sync/syncable/directory_change_delegate.h"
25 #include "sync/syncable/in_memory_directory_backing_store.h" 26 #include "sync/syncable/in_memory_directory_backing_store.h"
(...skipping 566 matching lines...) Expand 10 before | Expand all | Expand 10 after
592 // Fake SaveChanges() and make sure we got what we expected. 593 // Fake SaveChanges() and make sure we got what we expected.
593 { 594 {
594 Directory::SaveChangesSnapshot snapshot; 595 Directory::SaveChangesSnapshot snapshot;
595 base::AutoLock scoped_lock(dir_->kernel_->save_changes_mutex); 596 base::AutoLock scoped_lock(dir_->kernel_->save_changes_mutex);
596 dir_->TakeSnapshotForSaveChanges(&snapshot); 597 dir_->TakeSnapshotForSaveChanges(&snapshot);
597 // Make sure there's an entry for each new metahandle. Make sure all 598 // Make sure there's an entry for each new metahandle. Make sure all
598 // entries are marked dirty. 599 // entries are marked dirty.
599 ASSERT_EQ(expected_dirty_metahandles.size(), snapshot.dirty_metas.size()); 600 ASSERT_EQ(expected_dirty_metahandles.size(), snapshot.dirty_metas.size());
600 for (EntryKernelSet::const_iterator i = snapshot.dirty_metas.begin(); 601 for (EntryKernelSet::const_iterator i = snapshot.dirty_metas.begin();
601 i != snapshot.dirty_metas.end(); ++i) { 602 i != snapshot.dirty_metas.end(); ++i) {
602 ASSERT_TRUE(i->is_dirty()); 603 ASSERT_TRUE((*i)->is_dirty());
603 } 604 }
604 dir_->VacuumAfterSaveChanges(snapshot); 605 dir_->VacuumAfterSaveChanges(snapshot);
605 } 606 }
606 // Put a new value with existing transactions as well as adding new ones. 607 // Put a new value with existing transactions as well as adding new ones.
607 { 608 {
608 WriteTransaction trans(FROM_HERE, UNITTEST, dir_.get()); 609 WriteTransaction trans(FROM_HERE, UNITTEST, dir_.get());
609 std::vector<int64> new_dirty_metahandles; 610 std::vector<int64> new_dirty_metahandles;
610 for (std::vector<int64>::const_iterator i = 611 for (std::vector<int64>::const_iterator i =
611 expected_dirty_metahandles.begin(); 612 expected_dirty_metahandles.begin();
612 i != expected_dirty_metahandles.end(); ++i) { 613 i != expected_dirty_metahandles.end(); ++i) {
(...skipping 12 matching lines...) Expand all
625 // Fake SaveChanges() and make sure we got what we expected. 626 // Fake SaveChanges() and make sure we got what we expected.
626 { 627 {
627 Directory::SaveChangesSnapshot snapshot; 628 Directory::SaveChangesSnapshot snapshot;
628 base::AutoLock scoped_lock(dir_->kernel_->save_changes_mutex); 629 base::AutoLock scoped_lock(dir_->kernel_->save_changes_mutex);
629 dir_->TakeSnapshotForSaveChanges(&snapshot); 630 dir_->TakeSnapshotForSaveChanges(&snapshot);
630 // Make sure there's an entry for each new metahandle. Make sure all 631 // Make sure there's an entry for each new metahandle. Make sure all
631 // entries are marked dirty. 632 // entries are marked dirty.
632 EXPECT_EQ(expected_dirty_metahandles.size(), snapshot.dirty_metas.size()); 633 EXPECT_EQ(expected_dirty_metahandles.size(), snapshot.dirty_metas.size());
633 for (EntryKernelSet::const_iterator i = snapshot.dirty_metas.begin(); 634 for (EntryKernelSet::const_iterator i = snapshot.dirty_metas.begin();
634 i != snapshot.dirty_metas.end(); ++i) { 635 i != snapshot.dirty_metas.end(); ++i) {
635 EXPECT_TRUE(i->is_dirty()); 636 EXPECT_TRUE((*i)->is_dirty());
636 } 637 }
637 dir_->VacuumAfterSaveChanges(snapshot); 638 dir_->VacuumAfterSaveChanges(snapshot);
638 } 639 }
639 } 640 }
640 641
641 TEST_F(SyncableDirectoryTest, TakeSnapshotGetsOnlyDirtyHandlesTest) { 642 TEST_F(SyncableDirectoryTest, TakeSnapshotGetsOnlyDirtyHandlesTest) {
642 const int metahandles_to_create = 100; 643 const int metahandles_to_create = 100;
643 644
644 // half of 2 * metahandles_to_create 645 // half of 2 * metahandles_to_create
645 const unsigned int number_changed = 100u; 646 const unsigned int number_changed = 100u;
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
714 // Fake SaveChanges() and make sure we got what we expected. 715 // Fake SaveChanges() and make sure we got what we expected.
715 { 716 {
716 Directory::SaveChangesSnapshot snapshot; 717 Directory::SaveChangesSnapshot snapshot;
717 base::AutoLock scoped_lock(dir_->kernel_->save_changes_mutex); 718 base::AutoLock scoped_lock(dir_->kernel_->save_changes_mutex);
718 dir_->TakeSnapshotForSaveChanges(&snapshot); 719 dir_->TakeSnapshotForSaveChanges(&snapshot);
719 // Make sure there's an entry for each changed metahandle. Make sure all 720 // Make sure there's an entry for each changed metahandle. Make sure all
720 // entries are marked dirty. 721 // entries are marked dirty.
721 EXPECT_EQ(number_changed, snapshot.dirty_metas.size()); 722 EXPECT_EQ(number_changed, snapshot.dirty_metas.size());
722 for (EntryKernelSet::const_iterator i = snapshot.dirty_metas.begin(); 723 for (EntryKernelSet::const_iterator i = snapshot.dirty_metas.begin();
723 i != snapshot.dirty_metas.end(); ++i) { 724 i != snapshot.dirty_metas.end(); ++i) {
724 EXPECT_TRUE(i->is_dirty()); 725 EXPECT_TRUE((*i)->is_dirty());
725 } 726 }
726 dir_->VacuumAfterSaveChanges(snapshot); 727 dir_->VacuumAfterSaveChanges(snapshot);
727 } 728 }
728 } 729 }
729 730
731 // Test delete journals management.
732 TEST_F(SyncableDirectoryTest, ManageDeleteJournals) {
733 sync_pb::EntitySpecifics bookmark_specifics;
734 AddDefaultFieldValue(BOOKMARKS, &bookmark_specifics);
735 bookmark_specifics.mutable_bookmark()->set_url("url");
736
737 Id id1 = TestIdFactory::FromNumber(-1);
738 Id id2 = TestIdFactory::FromNumber(-2);
739 int64 handle1 = 0;
740 int64 handle2 = 0;
741 {
742 // Create two bookmark entries and save in database.
743 CreateEntry("item1", id1);
744 CreateEntry("item2", id2);
745 {
746 WriteTransaction trans(FROM_HERE, UNITTEST, dir_.get());
747 MutableEntry item1(&trans, GET_BY_ID, id1);
748 ASSERT_TRUE(item1.good());
749 handle1 = item1.Get(META_HANDLE);
750 item1.Put(SPECIFICS, bookmark_specifics);
751 item1.Put(SERVER_SPECIFICS, bookmark_specifics);
752 MutableEntry item2(&trans, GET_BY_ID, id2);
753 ASSERT_TRUE(item2.good());
754 handle2 = item2.Get(META_HANDLE);
755 item2.Put(SPECIFICS, bookmark_specifics);
756 item2.Put(SERVER_SPECIFICS, bookmark_specifics);
757 }
758 ASSERT_EQ(OPENED, SimulateSaveAndReloadDir());
759 }
760
761 { // Test adding and saving delete journals.
762 DeleteJournal* delete_journal = dir_->delete_journal();
763 {
764 WriteTransaction trans(FROM_HERE, UNITTEST, dir_.get());
765 EntryKernelSet journal_entries;
766 delete_journal->GetDeleteJournals(&trans, BOOKMARKS, &journal_entries);
767 ASSERT_EQ(0u, journal_entries.size());
768
769 // Set SERVER_IS_DEL of the entries to true and they should be added to
770 // delete journals.
771 MutableEntry item1(&trans, GET_BY_ID, id1);
772 ASSERT_TRUE(item1.good());
773 item1.Put(SERVER_IS_DEL, true);
774 MutableEntry item2(&trans, GET_BY_ID, id2);
775 ASSERT_TRUE(item2.good());
776 item2.Put(SERVER_IS_DEL, true);
777 EntryKernel tmp;
778 tmp.put(ID, id1);
779 EXPECT_TRUE(delete_journal->delete_journals_.count(&tmp));
780 tmp.put(ID, id2);
781 EXPECT_TRUE(delete_journal->delete_journals_.count(&tmp));
782 }
783
784 // Save delete journals in database and verify memory clearing.
785 ASSERT_TRUE(dir_->SaveChanges());
786 EXPECT_EQ(0u, delete_journal->GetDeleteJournalSize());
787 ASSERT_EQ(OPENED, SimulateSaveAndReloadDir());
788 }
789
790 {
791 {
792 // Test reading delete journals from database.
793 WriteTransaction trans(FROM_HERE, UNITTEST, dir_.get());
794 DeleteJournal* delete_journal = dir_->delete_journal();
795 EntryKernelSet journal_entries;
796 delete_journal->GetDeleteJournals(&trans, BOOKMARKS, &journal_entries);
797 ASSERT_EQ(2u, journal_entries.size());
798 EntryKernel tmp;
799 tmp.put(META_HANDLE, handle1);
800 EXPECT_TRUE(journal_entries.count(&tmp));
801 tmp.put(META_HANDLE, handle2);
802 EXPECT_TRUE(journal_entries.count(&tmp));
803
804 // Purge item2.
805 MetahandleSet to_purge;
806 to_purge.insert(handle2);
807 delete_journal->PurgeDeleteJournals(&trans, to_purge);
808
809 // Verify that item2 is purged from journals in memory and will be
810 // purged from database.
811 tmp.put(ID, id2);
812 EXPECT_FALSE(delete_journal->delete_journals_.count(&tmp));
813 EXPECT_EQ(1u, delete_journal->delete_journals_to_purge_.size());
814 EXPECT_TRUE(delete_journal->delete_journals_to_purge_.count(handle2));
815 }
816 ASSERT_EQ(OPENED, SimulateSaveAndReloadDir());
817 }
818
819 {
820 {
821 // Verify purged entry is gone in database.
822 WriteTransaction trans(FROM_HERE, UNITTEST, dir_.get());
823 DeleteJournal* delete_journal = dir_->delete_journal();
824 EntryKernelSet journal_entries;
825 delete_journal->GetDeleteJournals(&trans, BOOKMARKS, &journal_entries);
826 ASSERT_EQ(1u, journal_entries.size());
827 EntryKernel tmp;
828 tmp.put(ID, id1);
829 tmp.put(META_HANDLE, handle1);
830 EXPECT_TRUE(journal_entries.count(&tmp));
831
832 // Undelete item1.
833 MutableEntry item1(&trans, GET_BY_ID, id1);
834 ASSERT_TRUE(item1.good());
835 item1.Put(SERVER_IS_DEL, false);
836 EXPECT_TRUE(delete_journal->delete_journals_.empty());
837 EXPECT_EQ(1u, delete_journal->delete_journals_to_purge_.size());
838 EXPECT_TRUE(delete_journal->delete_journals_to_purge_.count(handle1));
839 }
840 ASSERT_EQ(OPENED, SimulateSaveAndReloadDir());
841 }
842
843 {
844 // Verify undeleted entry is gone from database.
845 ReadTransaction trans(FROM_HERE, dir_.get());
846 DeleteJournal* delete_journal = dir_->delete_journal();
847 ASSERT_EQ(0u, delete_journal->GetDeleteJournalSize());
848 }
849 }
850
730 const char SyncableDirectoryTest::kName[] = "Foo"; 851 const char SyncableDirectoryTest::kName[] = "Foo";
731 852
732 namespace { 853 namespace {
733 854
734 TEST_F(SyncableDirectoryTest, TestBasicLookupNonExistantID) { 855 TEST_F(SyncableDirectoryTest, TestBasicLookupNonExistantID) {
735 ReadTransaction rtrans(FROM_HERE, dir_.get()); 856 ReadTransaction rtrans(FROM_HERE, dir_.get());
736 Entry e(&rtrans, GET_BY_ID, TestIdFactory::FromNumber(-99)); 857 Entry e(&rtrans, GET_BY_ID, TestIdFactory::FromNumber(-99));
737 ASSERT_FALSE(e.good()); 858 ASSERT_FALSE(e.good());
738 } 859 }
739 860
(...skipping 1395 matching lines...) Expand 10 before | Expand all | Expand 10 after
2135 EXPECT_TRUE(CreateWithDefaultTag(factory_.NewServerId(), true)); 2256 EXPECT_TRUE(CreateWithDefaultTag(factory_.NewServerId(), true));
2136 EXPECT_FALSE(CreateWithDefaultTag(factory_.NewServerId(), true)); 2257 EXPECT_FALSE(CreateWithDefaultTag(factory_.NewServerId(), true));
2137 EXPECT_FALSE(CreateWithDefaultTag(factory_.NewServerId(), false)); 2258 EXPECT_FALSE(CreateWithDefaultTag(factory_.NewServerId(), false));
2138 EXPECT_FALSE(CreateWithDefaultTag(factory_.NewLocalId(), false)); 2259 EXPECT_FALSE(CreateWithDefaultTag(factory_.NewLocalId(), false));
2139 EXPECT_FALSE(CreateWithDefaultTag(factory_.NewLocalId(), true)); 2260 EXPECT_FALSE(CreateWithDefaultTag(factory_.NewLocalId(), true));
2140 } 2261 }
2141 2262
2142 } // namespace 2263 } // namespace
2143 } // namespace syncable 2264 } // namespace syncable
2144 } // namespace syncer 2265 } // namespace syncer
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698