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

Side by Side Diff: sync/internal_api/sync_backup_manager_unittest.cc

Issue 455023003: Let SyncBackupManager keep backup data in memory until shutdown. Only persist (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 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 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 "sync/internal_api/sync_backup_manager.h" 5 #include "sync/internal_api/sync_backup_manager.h"
6 6
7 #include "base/files/scoped_temp_dir.h" 7 #include "base/files/scoped_temp_dir.h"
8 #include "base/run_loop.h" 8 #include "base/run_loop.h"
9 #include "sync/internal_api/public/read_node.h" 9 #include "sync/internal_api/public/read_node.h"
10 #include "sync/internal_api/public/read_transaction.h" 10 #include "sync/internal_api/public/read_transaction.h"
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
42 MOCK_METHOD4(OnInitializationComplete, 42 MOCK_METHOD4(OnInitializationComplete,
43 void(const WeakHandle<JsBackend>&, 43 void(const WeakHandle<JsBackend>&,
44 const WeakHandle<DataTypeDebugInfoListener>&, 44 const WeakHandle<DataTypeDebugInfoListener>&,
45 bool, ModelTypeSet)); 45 bool, ModelTypeSet));
46 46
47 protected: 47 protected:
48 virtual void SetUp() OVERRIDE { 48 virtual void SetUp() OVERRIDE {
49 CHECK(temp_dir_.CreateUniqueTempDir()); 49 CHECK(temp_dir_.CreateUniqueTempDir());
50 } 50 }
51 51
52 void InitManager(SyncManager* manager, StorageOption storage_option) { 52 void InitManager(SyncManager* manager,
53 InternalComponentsFactory::StorageOption storage_option) {
53 manager_ = manager; 54 manager_ = manager;
54 EXPECT_CALL(*this, OnInitializationComplete(_, _, _, _)) 55 EXPECT_CALL(*this, OnInitializationComplete(_, _, _, _))
55 .WillOnce(WithArgs<2>(Invoke(this, 56 .WillOnce(WithArgs<2>(Invoke(this,
56 &SyncBackupManagerTest::HandleInit))); 57 &SyncBackupManagerTest::HandleInit)));
57 58
58 TestInternalComponentsFactory factory(InternalComponentsFactory::Switches(), 59 TestInternalComponentsFactory factory(
59 storage_option); 60 InternalComponentsFactory::Switches(), storage_option,
61 InternalComponentsFactory::STORAGE_ON_DISK_DEFERRED);
60 manager->AddObserver(this); 62 manager->AddObserver(this);
61 63
62 base::RunLoop run_loop; 64 base::RunLoop run_loop;
63 manager->Init(temp_dir_.path(), 65 manager->Init(temp_dir_.path(),
64 MakeWeakHandle(base::WeakPtr<JsEventHandler>()), 66 MakeWeakHandle(base::WeakPtr<JsEventHandler>()),
65 GURL("https://example.com/"), 67 GURL("https://example.com/"),
66 scoped_ptr<HttpPostProviderFactory>().Pass(), 68 scoped_ptr<HttpPostProviderFactory>().Pass(),
67 std::vector<scoped_refptr<ModelSafeWorker> >(), 69 std::vector<scoped_refptr<ModelSafeWorker> >(),
68 NULL, 70 NULL,
69 NULL, 71 NULL,
(...skipping 14 matching lines...) Expand all
84 const std::string& client_tag) { 86 const std::string& client_tag) {
85 WriteTransaction trans(FROM_HERE, user_share); 87 WriteTransaction trans(FROM_HERE, user_share);
86 ReadNode type_root(&trans); 88 ReadNode type_root(&trans);
87 EXPECT_EQ(BaseNode::INIT_OK, type_root.InitTypeRoot(type)); 89 EXPECT_EQ(BaseNode::INIT_OK, type_root.InitTypeRoot(type));
88 90
89 WriteNode node(&trans); 91 WriteNode node(&trans);
90 EXPECT_EQ(WriteNode::INIT_SUCCESS, 92 EXPECT_EQ(WriteNode::INIT_SUCCESS,
91 node.InitUniqueByCreation(type, type_root, client_tag)); 93 node.InitUniqueByCreation(type, type_root, client_tag));
92 } 94 }
93 95
94 private:
95 void ConfigureSyncer() { 96 void ConfigureSyncer() {
96 manager_->ConfigureSyncer(CONFIGURE_REASON_NEW_CLIENT, 97 manager_->ConfigureSyncer(CONFIGURE_REASON_NEW_CLIENT,
97 ModelTypeSet(SEARCH_ENGINES), 98 ModelTypeSet(SEARCH_ENGINES),
98 ModelTypeSet(), ModelTypeSet(), ModelTypeSet(), 99 ModelTypeSet(), ModelTypeSet(), ModelTypeSet(),
99 ModelSafeRoutingInfo(), 100 ModelSafeRoutingInfo(),
100 base::Bind(&OnConfigDone, true), 101 base::Bind(&OnConfigDone, true),
101 base::Bind(&OnConfigDone, false)); 102 base::Bind(&OnConfigDone, false));
102 } 103 }
103 104
104 void HandleInit(bool success) { 105 void HandleInit(bool success) {
105 if (success) { 106 if (success) {
106 loop_.PostTask(FROM_HERE, 107 loop_.PostTask(FROM_HERE,
107 base::Bind(&SyncBackupManagerTest::ConfigureSyncer, 108 base::Bind(&SyncBackupManagerTest::ConfigureSyncer,
108 base::Unretained(this))); 109 base::Unretained(this)));
109 } else { 110 } else {
110 manager_->ShutdownOnSyncThread(STOP_SYNC); 111 manager_->ShutdownOnSyncThread(STOP_SYNC);
111 } 112 }
112 } 113 }
113 114
114 base::ScopedTempDir temp_dir_; 115 base::ScopedTempDir temp_dir_;
115 base::MessageLoop loop_; // Needed for WeakHandle 116 base::MessageLoop loop_; // Needed for WeakHandle
116 SyncManager* manager_; 117 SyncManager* manager_;
117 }; 118 };
118 119
119 TEST_F(SyncBackupManagerTest, NormalizeAndPersist) { 120 TEST_F(SyncBackupManagerTest, NormalizeEntry) {
120 scoped_ptr<SyncBackupManager> manager(new SyncBackupManager); 121 scoped_ptr<SyncBackupManager> manager(new SyncBackupManager);
121 InitManager(manager.get(), STORAGE_ON_DISK); 122 InitManager(manager.get(), InternalComponentsFactory::STORAGE_IN_MEMORY);
122 123
123 CreateEntry(manager->GetUserShare(), SEARCH_ENGINES, "test"); 124 CreateEntry(manager->GetUserShare(), SEARCH_ENGINES, "test");
124 125
125 { 126 {
126 // New entry is local and unsynced at first. 127 // New entry is local and unsynced at first.
127 ReadTransaction trans(FROM_HERE, manager->GetUserShare()); 128 ReadTransaction trans(FROM_HERE, manager->GetUserShare());
128 ReadNode pref(&trans); 129 ReadNode pref(&trans);
129 EXPECT_EQ(BaseNode::INIT_OK, 130 EXPECT_EQ(BaseNode::INIT_OK,
130 pref.InitByClientTagLookup(SEARCH_ENGINES, "test")); 131 pref.InitByClientTagLookup(SEARCH_ENGINES, "test"));
131 EXPECT_FALSE(pref.GetEntry()->GetId().ServerKnows()); 132 EXPECT_FALSE(pref.GetEntry()->GetId().ServerKnows());
132 EXPECT_TRUE(pref.GetEntry()->GetIsUnsynced()); 133 EXPECT_TRUE(pref.GetEntry()->GetIsUnsynced());
133 } 134 }
134 135
135 manager->SaveChanges(); 136 manager->SaveChanges();
136 137
137 { 138 {
138 // New entry has server ID and unsynced bit is cleared after saving. 139 // New entry has server ID and unsynced bit is cleared after saving.
139 ReadTransaction trans(FROM_HERE, manager->GetUserShare()); 140 ReadTransaction trans(FROM_HERE, manager->GetUserShare());
140 ReadNode pref(&trans); 141 ReadNode pref(&trans);
141 EXPECT_EQ(BaseNode::INIT_OK, 142 EXPECT_EQ(BaseNode::INIT_OK,
142 pref.InitByClientTagLookup(SEARCH_ENGINES, "test")); 143 pref.InitByClientTagLookup(SEARCH_ENGINES, "test"));
143 EXPECT_TRUE(pref.GetEntry()->GetId().ServerKnows()); 144 EXPECT_TRUE(pref.GetEntry()->GetId().ServerKnows());
144 EXPECT_FALSE(pref.GetEntry()->GetIsUnsynced()); 145 EXPECT_FALSE(pref.GetEntry()->GetIsUnsynced());
145 } 146 }
146 manager->ShutdownOnSyncThread(STOP_SYNC); 147 }
148
149 TEST_F(SyncBackupManagerTest, PersistWithSwitchToSyncShutdown) {
150 scoped_ptr<SyncBackupManager> manager(new SyncBackupManager);
151 InitManager(manager.get(),
152 InternalComponentsFactory::STORAGE_ON_DISK_DEFERRED);
153
154 CreateEntry(manager->GetUserShare(), SEARCH_ENGINES, "test");
155 manager->SaveChanges();
156 manager->ShutdownOnSyncThread(SWITCH_MODE_SYNC);
147 157
148 // Reopen db to verify entry is persisted. 158 // Reopen db to verify entry is persisted.
149 manager.reset(new SyncBackupManager); 159 manager.reset(new SyncBackupManager);
150 InitManager(manager.get(), STORAGE_ON_DISK); 160 InitManager(manager.get(), InternalComponentsFactory::STORAGE_ON_DISK);
151 { 161 {
152 ReadTransaction trans(FROM_HERE, manager->GetUserShare()); 162 ReadTransaction trans(FROM_HERE, manager->GetUserShare());
153 ReadNode pref(&trans); 163 ReadNode pref(&trans);
154 EXPECT_EQ(BaseNode::INIT_OK, 164 EXPECT_EQ(BaseNode::INIT_OK,
155 pref.InitByClientTagLookup(SEARCH_ENGINES, "test")); 165 pref.InitByClientTagLookup(SEARCH_ENGINES, "test"));
156 EXPECT_TRUE(pref.GetEntry()->GetId().ServerKnows()); 166 EXPECT_TRUE(pref.GetEntry()->GetId().ServerKnows());
157 EXPECT_FALSE(pref.GetEntry()->GetIsUnsynced()); 167 EXPECT_FALSE(pref.GetEntry()->GetIsUnsynced());
158 } 168 }
159 } 169 }
160 170
171 TEST_F(SyncBackupManagerTest, DontPersistWithOtherShutdown) {
172 scoped_ptr<SyncBackupManager> manager(new SyncBackupManager);
173 InitManager(manager.get(),
174 InternalComponentsFactory::STORAGE_ON_DISK_DEFERRED);
175
176 CreateEntry(manager->GetUserShare(), SEARCH_ENGINES, "test");
177 manager->SaveChanges();
178 manager->ShutdownOnSyncThread(STOP_SYNC);
179 EXPECT_FALSE(base::PathExists(
180 temp_dir_.path().Append(syncable::Directory::kSyncDatabaseFilename)));
181 }
182
161 TEST_F(SyncBackupManagerTest, FailToInitialize) { 183 TEST_F(SyncBackupManagerTest, FailToInitialize) {
162 // Test graceful shutdown on initialization failure. 184 // Test graceful shutdown on initialization failure.
163 scoped_ptr<SyncBackupManager> manager(new SyncBackupManager); 185 scoped_ptr<SyncBackupManager> manager(new SyncBackupManager);
164 InitManager(manager.get(), STORAGE_INVALID); 186 InitManager(manager.get(), InternalComponentsFactory::STORAGE_INVALID);
165 } 187 }
166 188
167 } // anonymous namespace 189 } // anonymous namespace
168 190
169 } // namespace syncer 191 } // namespace syncer
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698