OLD | NEW |
1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 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 Loading... |
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 Loading... |
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 Loading... |
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 { |
| 787 ReadTransaction trans(FROM_HERE, dir_.get()); |
| 788 EXPECT_EQ(0u, delete_journal->GetDeleteJournalSize(&trans)); |
| 789 } |
| 790 ASSERT_EQ(OPENED, SimulateSaveAndReloadDir()); |
| 791 } |
| 792 |
| 793 { |
| 794 { |
| 795 // Test reading delete journals from database. |
| 796 WriteTransaction trans(FROM_HERE, UNITTEST, dir_.get()); |
| 797 DeleteJournal* delete_journal = dir_->delete_journal(); |
| 798 EntryKernelSet journal_entries; |
| 799 delete_journal->GetDeleteJournals(&trans, BOOKMARKS, &journal_entries); |
| 800 ASSERT_EQ(2u, journal_entries.size()); |
| 801 EntryKernel tmp; |
| 802 tmp.put(META_HANDLE, handle1); |
| 803 EXPECT_TRUE(journal_entries.count(&tmp)); |
| 804 tmp.put(META_HANDLE, handle2); |
| 805 EXPECT_TRUE(journal_entries.count(&tmp)); |
| 806 |
| 807 // Purge item2. |
| 808 MetahandleSet to_purge; |
| 809 to_purge.insert(handle2); |
| 810 delete_journal->PurgeDeleteJournals(&trans, to_purge); |
| 811 |
| 812 // Verify that item2 is purged from journals in memory and will be |
| 813 // purged from database. |
| 814 tmp.put(ID, id2); |
| 815 EXPECT_FALSE(delete_journal->delete_journals_.count(&tmp)); |
| 816 EXPECT_EQ(1u, delete_journal->delete_journals_to_purge_.size()); |
| 817 EXPECT_TRUE(delete_journal->delete_journals_to_purge_.count(handle2)); |
| 818 } |
| 819 ASSERT_EQ(OPENED, SimulateSaveAndReloadDir()); |
| 820 } |
| 821 |
| 822 { |
| 823 { |
| 824 // Verify purged entry is gone in database. |
| 825 WriteTransaction trans(FROM_HERE, UNITTEST, dir_.get()); |
| 826 DeleteJournal* delete_journal = dir_->delete_journal(); |
| 827 EntryKernelSet journal_entries; |
| 828 delete_journal->GetDeleteJournals(&trans, BOOKMARKS, &journal_entries); |
| 829 ASSERT_EQ(1u, journal_entries.size()); |
| 830 EntryKernel tmp; |
| 831 tmp.put(ID, id1); |
| 832 tmp.put(META_HANDLE, handle1); |
| 833 EXPECT_TRUE(journal_entries.count(&tmp)); |
| 834 |
| 835 // Undelete item1. |
| 836 MutableEntry item1(&trans, GET_BY_ID, id1); |
| 837 ASSERT_TRUE(item1.good()); |
| 838 item1.Put(SERVER_IS_DEL, false); |
| 839 EXPECT_TRUE(delete_journal->delete_journals_.empty()); |
| 840 EXPECT_EQ(1u, delete_journal->delete_journals_to_purge_.size()); |
| 841 EXPECT_TRUE(delete_journal->delete_journals_to_purge_.count(handle1)); |
| 842 } |
| 843 ASSERT_EQ(OPENED, SimulateSaveAndReloadDir()); |
| 844 } |
| 845 |
| 846 { |
| 847 // Verify undeleted entry is gone from database. |
| 848 ReadTransaction trans(FROM_HERE, dir_.get()); |
| 849 DeleteJournal* delete_journal = dir_->delete_journal(); |
| 850 ASSERT_EQ(0u, delete_journal->GetDeleteJournalSize(&trans)); |
| 851 } |
| 852 } |
| 853 |
730 const char SyncableDirectoryTest::kName[] = "Foo"; | 854 const char SyncableDirectoryTest::kName[] = "Foo"; |
731 | 855 |
732 namespace { | 856 namespace { |
733 | 857 |
734 TEST_F(SyncableDirectoryTest, TestBasicLookupNonExistantID) { | 858 TEST_F(SyncableDirectoryTest, TestBasicLookupNonExistantID) { |
735 ReadTransaction rtrans(FROM_HERE, dir_.get()); | 859 ReadTransaction rtrans(FROM_HERE, dir_.get()); |
736 Entry e(&rtrans, GET_BY_ID, TestIdFactory::FromNumber(-99)); | 860 Entry e(&rtrans, GET_BY_ID, TestIdFactory::FromNumber(-99)); |
737 ASSERT_FALSE(e.good()); | 861 ASSERT_FALSE(e.good()); |
738 } | 862 } |
739 | 863 |
(...skipping 1395 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2135 EXPECT_TRUE(CreateWithDefaultTag(factory_.NewServerId(), true)); | 2259 EXPECT_TRUE(CreateWithDefaultTag(factory_.NewServerId(), true)); |
2136 EXPECT_FALSE(CreateWithDefaultTag(factory_.NewServerId(), true)); | 2260 EXPECT_FALSE(CreateWithDefaultTag(factory_.NewServerId(), true)); |
2137 EXPECT_FALSE(CreateWithDefaultTag(factory_.NewServerId(), false)); | 2261 EXPECT_FALSE(CreateWithDefaultTag(factory_.NewServerId(), false)); |
2138 EXPECT_FALSE(CreateWithDefaultTag(factory_.NewLocalId(), false)); | 2262 EXPECT_FALSE(CreateWithDefaultTag(factory_.NewLocalId(), false)); |
2139 EXPECT_FALSE(CreateWithDefaultTag(factory_.NewLocalId(), true)); | 2263 EXPECT_FALSE(CreateWithDefaultTag(factory_.NewLocalId(), true)); |
2140 } | 2264 } |
2141 | 2265 |
2142 } // namespace | 2266 } // namespace |
2143 } // namespace syncable | 2267 } // namespace syncable |
2144 } // namespace syncer | 2268 } // namespace syncer |
OLD | NEW |