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

Side by Side Diff: chrome/browser/sync/test/integration/single_client_backup_rollback_test.cc

Issue 483883003: [Sync] Fix backup/rollback tests race conditions (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
« no previous file with comments | « chrome/browser/sync/profile_sync_service_unittest.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 "base/command_line.h" 5 #include "base/command_line.h"
6 #include "base/file_util.h" 6 #include "base/file_util.h"
7 #include "base/message_loop/message_loop.h" 7 #include "base/message_loop/message_loop.h"
8 #include "base/prefs/pref_service.h" 8 #include "base/prefs/pref_service.h"
9 #include "base/run_loop.h" 9 #include "base/run_loop.h"
10 #include "base/test/test_timeouts.h"
11 #include "chrome/browser/browsing_data/browsing_data_remover.h"
10 #include "chrome/browser/profiles/profile.h" 12 #include "chrome/browser/profiles/profile.h"
11 #include "chrome/browser/sync/profile_sync_service.h" 13 #include "chrome/browser/sync/profile_sync_service.h"
12 #include "chrome/browser/sync/test/integration/bookmarks_helper.h" 14 #include "chrome/browser/sync/test/integration/bookmarks_helper.h"
13 #include "chrome/browser/sync/test/integration/preferences_helper.h" 15 #include "chrome/browser/sync/test/integration/preferences_helper.h"
14 #include "chrome/browser/sync/test/integration/sync_integration_test_util.h" 16 #include "chrome/browser/sync/test/integration/sync_integration_test_util.h"
15 #include "chrome/browser/sync/test/integration/sync_test.h" 17 #include "chrome/browser/sync/test/integration/sync_test.h"
16 #include "chrome/common/chrome_switches.h" 18 #include "chrome/common/chrome_switches.h"
17 #include "chrome/common/pref_names.h" 19 #include "chrome/common/pref_names.h"
18 #include "components/bookmarks/browser/bookmark_model.h" 20 #include "components/bookmarks/browser/bookmark_model.h"
19 #include "sync/internal_api/public/util/sync_db_util.h" 21 #include "sync/internal_api/public/util/sync_db_util.h"
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
65 } 67 }
66 68
67 private: 69 private:
68 void CheckDbCallback(base::Time* time_out, base::Time time_in) { 70 void CheckDbCallback(base::Time* time_out, base::Time time_in) {
69 *time_out = syncer::ProtoTimeToTime(syncer::TimeToProtoTime(time_in)); 71 *time_out = syncer::ProtoTimeToTime(syncer::TimeToProtoTime(time_in));
70 } 72 }
71 73
72 DISALLOW_COPY_AND_ASSIGN(SingleClientBackupRollbackTest); 74 DISALLOW_COPY_AND_ASSIGN(SingleClientBackupRollbackTest);
73 }; 75 };
74 76
75 class SyncBackendStoppedChecker { 77 // Waits until the ProfileSyncService's backend is in IDLE mode.
78 class SyncBackendStoppedChecker : public ProfileSyncServiceBase::Observer {
76 public: 79 public:
77 explicit SyncBackendStoppedChecker(ProfileSyncService* service, 80 explicit SyncBackendStoppedChecker(ProfileSyncService* service)
78 base::TimeDelta timeout)
79 : pss_(service), 81 : pss_(service),
80 timeout_(timeout) {} 82 timeout_(TestTimeouts::action_max_timeout()),
83 done_(false) {}
84
85 virtual void OnStateChanged() OVERRIDE {
86 if (ProfileSyncService::IDLE == pss_->backend_mode()) {
87 done_ = true;
88 run_loop_.Quit();
89 }
90 }
81 91
82 bool Wait() { 92 bool Wait() {
83 expiration_ = base::TimeTicks::Now() + timeout_; 93 pss_->AddObserver(this);
94 if (ProfileSyncService::IDLE == pss_->backend_mode())
95 return true;
84 base::MessageLoop::current()->PostDelayedTask( 96 base::MessageLoop::current()->PostDelayedTask(
85 FROM_HERE, 97 FROM_HERE,
86 base::Bind(&SyncBackendStoppedChecker::PeriodicCheck, 98 run_loop_.QuitClosure(),
87 base::Unretained(this)), 99 timeout_);
88 base::TimeDelta::FromSeconds(1)); 100 run_loop_.Run();
89 base::MessageLoop::current()->Run(); 101 pss_->RemoveObserver(this);
90 return ProfileSyncService::IDLE == pss_->backend_mode(); 102 return done_;
91 } 103 }
92 104
93 private: 105 private:
94 void PeriodicCheck() { 106
95 if (ProfileSyncService::IDLE == pss_->backend_mode() || 107 ProfileSyncService* const pss_;
96 base::TimeTicks::Now() > expiration_) { 108 const base::TimeDelta timeout_;
97 base::MessageLoop::current()->Quit(); 109 base::RunLoop run_loop_;
98 } else { 110 bool done_;
99 base::MessageLoop::current()->PostDelayedTask( 111 };
100 FROM_HERE, 112
101 base::Bind(&SyncBackendStoppedChecker::PeriodicCheck, 113 // Waits until a rollback finishes.
102 base::Unretained(this)), 114 class SyncRollbackChecker : public ProfileSyncServiceBase::Observer,
103 base::TimeDelta::FromSeconds(1)); 115 public BrowsingDataRemover::Observer {
116 public:
117 explicit SyncRollbackChecker(ProfileSyncService* service)
118 : pss_(service),
119 timeout_(TestTimeouts::action_max_timeout()),
120 rollback_started_(false),
121 clear_done_(false) {}
122
123 // ProfileSyncServiceBase::Observer implementation.
124 virtual void OnStateChanged() OVERRIDE {
125 if (ProfileSyncService::ROLLBACK == pss_->backend_mode()) {
126 rollback_started_ = true;
127 if (clear_done_)
128 run_loop_.Quit();
104 } 129 }
105 } 130 }
106 131
107 ProfileSyncService* pss_; 132 // BrowsingDataRemoverObserver::Observer implementation.
108 base::TimeDelta timeout_; 133 virtual void OnBrowsingDataRemoverDone() OVERRIDE {
109 base::TimeTicks expiration_; 134 clear_done_ = true;
135 if (rollback_started_) {
136 run_loop_.Quit();
137 }
138 }
139
140 bool Wait() {
141 pss_->AddObserver(this);
142 pss_->SetBrowsingDataRemoverObserverForTesting(this);
143 base::MessageLoop::current()->PostDelayedTask(
144 FROM_HERE,
145 run_loop_.QuitClosure(),
146 timeout_);
147 run_loop_.Run();
148 pss_->RemoveObserver(this);
149 return rollback_started_ && clear_done_;
150 }
151
152 ProfileSyncService* const pss_;
153 const base::TimeDelta timeout_;
154 base::RunLoop run_loop_;
155 bool rollback_started_;
156 bool clear_done_;
110 }; 157 };
111 158
112 #if defined(ENABLE_PRE_SYNC_BACKUP) 159 #if defined(ENABLE_PRE_SYNC_BACKUP)
113 #define MAYBE_TestBackup TestBackup 160 #define MAYBE_TestBackup TestBackup
114 #else 161 #else
115 #define MAYBE_TestBackup DISABLED_TestBackup 162 #define MAYBE_TestBackup DISABLED_TestBackup
116 #endif 163 #endif
117 IN_PROC_BROWSER_TEST_F(SingleClientBackupRollbackTest, 164 IN_PROC_BROWSER_TEST_F(SingleClientBackupRollbackTest,
118 MAYBE_TestBackup) { 165 MAYBE_TestBackup) {
119 ASSERT_TRUE(SetupClients()) << "SetupClients() failed."; 166 ASSERT_TRUE(SetupClients()) << "SetupClients() failed.";
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
185 ASSERT_TRUE(AwaitCommitActivityCompletion(GetSyncService(0))); 232 ASSERT_TRUE(AwaitCommitActivityCompletion(GetSyncService(0)));
186 ASSERT_TRUE(ModelMatchesVerifier(0)); 233 ASSERT_TRUE(ModelMatchesVerifier(0));
187 234
188 // Let server to return rollback command on next sync request. 235 // Let server to return rollback command on next sync request.
189 GetFakeServer()->TriggerError(sync_pb::SyncEnums::USER_ROLLBACK); 236 GetFakeServer()->TriggerError(sync_pb::SyncEnums::USER_ROLLBACK);
190 237
191 // Make another change to trigger downloading of rollback command. 238 // Make another change to trigger downloading of rollback command.
192 Remove(0, tier1_b, 0); 239 Remove(0, tier1_b, 0);
193 240
194 // Wait for rollback to finish and sync backend is completely shut down. 241 // Wait for rollback to finish and sync backend is completely shut down.
195 SyncBackendStoppedChecker checker(GetSyncService(0), 242 SyncRollbackChecker rollback_checker(GetSyncService(0));
196 base::TimeDelta::FromSeconds(5)); 243 ASSERT_TRUE(rollback_checker.Wait());
197 ASSERT_TRUE(checker.Wait()); 244 SyncBackendStoppedChecker shutdown_checker(GetSyncService(0));
245 ASSERT_TRUE(shutdown_checker.Wait());
198 246
199 // Verify bookmarks are restored. 247 // Verify bookmarks are restored.
200 ASSERT_EQ(1, tier1_a->child_count()); 248 ASSERT_EQ(1, tier1_a->child_count());
201 const BookmarkNode* url1 = tier1_a->GetChild(0); 249 const BookmarkNode* url1 = tier1_a->GetChild(0);
202 ASSERT_EQ(GURL("http://mail.google.com"), url1->url()); 250 ASSERT_EQ(GURL("http://mail.google.com"), url1->url());
203 251
204 ASSERT_EQ(1, tier1_b->child_count()); 252 ASSERT_EQ(1, tier1_b->child_count());
205 const BookmarkNode* url2 = tier1_b->GetChild(0); 253 const BookmarkNode* url2 = tier1_b->GetChild(0);
206 ASSERT_EQ(GURL("http://www.nhl.com"), url2->url()); 254 ASSERT_EQ(GURL("http://www.nhl.com"), url2->url());
207
208 // Backup DB should be deleted after rollback is done.
209 ASSERT_FALSE(base::PathExists(
210 GetProfile(0)->GetPath().Append(FILE_PATH_LITERAL("Sync Data Backup"))));
211 } 255 }
212 256
213 #if defined(ENABLE_PRE_SYNC_BACKUP) 257 #if defined(ENABLE_PRE_SYNC_BACKUP)
214 #define MAYBE_TestRollbackDisabled TestRollbackDisabled 258 #define MAYBE_TestRollbackDisabled TestRollbackDisabled
215 #else 259 #else
216 #define MAYBE_TestRollbackDisabled DISABLED_TestRollbackDisabled 260 #define MAYBE_TestRollbackDisabled DISABLED_TestRollbackDisabled
217 #endif 261 #endif
218 IN_PROC_BROWSER_TEST_F(SingleClientBackupRollbackTest, 262 IN_PROC_BROWSER_TEST_F(SingleClientBackupRollbackTest,
219 MAYBE_TestRollbackDisabled) { 263 MAYBE_TestRollbackDisabled) {
220 DisableRollback(); 264 DisableRollback();
(...skipping 20 matching lines...) Expand all
241 GURL("http://www.yahoo.com"))); 285 GURL("http://www.yahoo.com")));
242 ASSERT_TRUE(AwaitCommitActivityCompletion(GetSyncService((0)))); 286 ASSERT_TRUE(AwaitCommitActivityCompletion(GetSyncService((0))));
243 ASSERT_TRUE(ModelMatchesVerifier(0)); 287 ASSERT_TRUE(ModelMatchesVerifier(0));
244 288
245 // Let server to return rollback command on next sync request. 289 // Let server to return rollback command on next sync request.
246 GetFakeServer()->TriggerError(sync_pb::SyncEnums::USER_ROLLBACK); 290 GetFakeServer()->TriggerError(sync_pb::SyncEnums::USER_ROLLBACK);
247 291
248 // Make another change to trigger downloading of rollback command. 292 // Make another change to trigger downloading of rollback command.
249 Remove(0, GetOtherNode(0), 0); 293 Remove(0, GetOtherNode(0), 0);
250 294
251 // Wait for rollback to finish and sync backend is completely shut down. 295 // Wait for sync backend is completely shut down.
252 SyncBackendStoppedChecker checker(GetSyncService(0), 296 SyncBackendStoppedChecker shutdown_checker(GetSyncService(0));
253 base::TimeDelta::FromSeconds(5)); 297 ASSERT_TRUE(shutdown_checker.Wait());
254 ASSERT_TRUE(checker.Wait());
255 298
256 // With rollback disabled, bookmarks in backup DB should not be restored. 299 // With rollback disabled, bookmarks in backup DB should not be restored.
257 // Only bookmark added during sync is present. 300 // Only bookmark added during sync is present.
258 ASSERT_EQ(1, GetOtherNode(0)->child_count()); 301 ASSERT_EQ(1, GetOtherNode(0)->child_count());
259 ASSERT_EQ(GURL("http://www.yahoo.com"), 302 ASSERT_EQ(GURL("http://www.yahoo.com"),
260 GetOtherNode(0)->GetChild(0)->url()); 303 GetOtherNode(0)->GetChild(0)->url());
261
262 // Backup DB should be deleted after.
263 ASSERT_FALSE(base::PathExists(
264 GetProfile(0)->GetPath().Append(FILE_PATH_LITERAL("Sync Data Backup"))));
265 } 304 }
266 305
267 #if defined(ENABLE_PRE_SYNC_BACKUP) 306 #if defined(ENABLE_PRE_SYNC_BACKUP)
268 #define MAYBE_TestSyncDisabled TestSyncDisabled 307 #define MAYBE_TestSyncDisabled TestSyncDisabled
269 #else 308 #else
270 #define MAYBE_TestSyncDisabled DISABLED_TestSyncDisabled 309 #define MAYBE_TestSyncDisabled DISABLED_TestSyncDisabled
271 #endif 310 #endif
272 IN_PROC_BROWSER_TEST_F(SingleClientBackupRollbackTest, 311 IN_PROC_BROWSER_TEST_F(SingleClientBackupRollbackTest,
273 MAYBE_TestSyncDisabled) { 312 MAYBE_TestSyncDisabled) {
274 ASSERT_TRUE(SetupClients()) << "SetupClients() failed."; 313 ASSERT_TRUE(SetupClients()) << "SetupClients() failed.";
(...skipping 18 matching lines...) Expand all
293 GURL("http://www.yahoo.com"))); 332 GURL("http://www.yahoo.com")));
294 ASSERT_TRUE(AwaitCommitActivityCompletion(GetSyncService((0)))); 333 ASSERT_TRUE(AwaitCommitActivityCompletion(GetSyncService((0))));
295 ASSERT_TRUE(ModelMatchesVerifier(0)); 334 ASSERT_TRUE(ModelMatchesVerifier(0));
296 335
297 // Let server to return birthday error on next sync request. 336 // Let server to return birthday error on next sync request.
298 GetFakeServer()->TriggerError(sync_pb::SyncEnums::NOT_MY_BIRTHDAY); 337 GetFakeServer()->TriggerError(sync_pb::SyncEnums::NOT_MY_BIRTHDAY);
299 338
300 // Make another change to trigger downloading of rollback command. 339 // Make another change to trigger downloading of rollback command.
301 Remove(0, GetOtherNode(0), 0); 340 Remove(0, GetOtherNode(0), 0);
302 341
303 // Wait for rollback to finish and sync backend is completely shut down. 342 // Wait sync backend is completely shut down.
304 SyncBackendStoppedChecker checker(GetSyncService(0), 343 SyncBackendStoppedChecker shutdown_checker(GetSyncService(0));
305 base::TimeDelta::FromSeconds(5)); 344 ASSERT_TRUE(shutdown_checker.Wait());
306 ASSERT_TRUE(checker.Wait());
307 345
308 // Shouldn't restore bookmarks with sign-out only. 346 // Shouldn't restore bookmarks with sign-out only.
309 ASSERT_EQ(1, GetOtherNode(0)->child_count()); 347 ASSERT_EQ(1, GetOtherNode(0)->child_count());
310 ASSERT_EQ(GURL("http://www.yahoo.com"), 348 ASSERT_EQ(GURL("http://www.yahoo.com"),
311 GetOtherNode(0)->GetChild(0)->url()); 349 GetOtherNode(0)->GetChild(0)->url());
312
313 // Backup DB should be deleted after.
314 ASSERT_FALSE(base::PathExists(
315 GetProfile(0)->GetPath().Append(FILE_PATH_LITERAL("Sync Data Backup"))));
316 } 350 }
317 351
318 #if defined(ENABLE_PRE_SYNC_BACKUP) 352 #if defined(ENABLE_PRE_SYNC_BACKUP)
319 #define MAYBE_RollbackNoBackup RollbackNoBackup 353 #define MAYBE_RollbackNoBackup RollbackNoBackup
320 #else 354 #else
321 #define MAYBE_RollbackNoBackup DISABLED_RollbackNoBackup 355 #define MAYBE_RollbackNoBackup DISABLED_RollbackNoBackup
322 #endif 356 #endif
323 IN_PROC_BROWSER_TEST_F(SingleClientBackupRollbackTest, 357 IN_PROC_BROWSER_TEST_F(SingleClientBackupRollbackTest,
324 MAYBE_RollbackNoBackup) { 358 MAYBE_RollbackNoBackup) {
325 ASSERT_TRUE(SetupClients()) << "SetupClients() failed."; 359 ASSERT_TRUE(SetupClients()) << "SetupClients() failed.";
(...skipping 17 matching lines...) Expand all
343 base::DeleteFile( 377 base::DeleteFile(
344 GetProfile(0)->GetPath().Append(FILE_PATH_LITERAL("Sync Data Backup")), 378 GetProfile(0)->GetPath().Append(FILE_PATH_LITERAL("Sync Data Backup")),
345 true); 379 true);
346 380
347 // Let server to return rollback command on next sync request. 381 // Let server to return rollback command on next sync request.
348 GetFakeServer()->TriggerError(sync_pb::SyncEnums::USER_ROLLBACK); 382 GetFakeServer()->TriggerError(sync_pb::SyncEnums::USER_ROLLBACK);
349 383
350 // Make another change to trigger downloading of rollback command. 384 // Make another change to trigger downloading of rollback command.
351 Remove(0, GetOtherNode(0), 0); 385 Remove(0, GetOtherNode(0), 0);
352 386
353 // Wait for backend to completely shut down. 387 // Wait for rollback to finish and sync backend is completely shut down.
354 SyncBackendStoppedChecker checker(GetSyncService(0), 388 SyncRollbackChecker rollback_checker(GetSyncService(0));
355 base::TimeDelta::FromSeconds(15)); 389 ASSERT_TRUE(rollback_checker.Wait());
390 SyncBackendStoppedChecker checker(GetSyncService(0));
356 ASSERT_TRUE(checker.Wait()); 391 ASSERT_TRUE(checker.Wait());
357 392
358 // Without backup DB, bookmarks remain at the state when sync stops. 393 // Without backup DB, bookmarks remain at the state when sync stops.
359 ASSERT_EQ(1, GetOtherNode(0)->child_count()); 394 ASSERT_EQ(1, GetOtherNode(0)->child_count());
360 ASSERT_EQ(GURL("http://www.nhl.com"), 395 ASSERT_EQ(GURL("http://www.nhl.com"),
361 GetOtherNode(0)->GetChild(0)->url()); 396 GetOtherNode(0)->GetChild(0)->url());
362 } 397 }
363 398
364 #if defined(ENABLE_PRE_SYNC_BACKUP) 399 #if defined(ENABLE_PRE_SYNC_BACKUP)
365 #define MAYBE_DontChangeBookmarkOrdering DontChangeBookmarkOrdering 400 #define MAYBE_DontChangeBookmarkOrdering DontChangeBookmarkOrdering
(...skipping 20 matching lines...) Expand all
386 ASSERT_TRUE(AwaitCommitActivityCompletion(GetSyncService(0))); 421 ASSERT_TRUE(AwaitCommitActivityCompletion(GetSyncService(0)));
387 ASSERT_TRUE(ModelMatchesVerifier(0)); 422 ASSERT_TRUE(ModelMatchesVerifier(0));
388 423
389 // Let server to return rollback command on next sync request. 424 // Let server to return rollback command on next sync request.
390 GetFakeServer()->TriggerError(sync_pb::SyncEnums::USER_ROLLBACK); 425 GetFakeServer()->TriggerError(sync_pb::SyncEnums::USER_ROLLBACK);
391 426
392 // Make another change to trigger downloading of rollback command. 427 // Make another change to trigger downloading of rollback command.
393 Remove(0, sub_folder, 0); 428 Remove(0, sub_folder, 0);
394 429
395 // Wait for rollback to finish and sync backend is completely shut down. 430 // Wait for rollback to finish and sync backend is completely shut down.
396 SyncBackendStoppedChecker checker(GetSyncService(0), 431 SyncRollbackChecker rollback_checker(GetSyncService(0));
haitaol1 2014/08/21 20:50:32 You always check rollback and backend shutdown bac
Nicolas Zea 2014/08/21 20:52:32 Some of these tests don't require rollback (i.e. t
397 base::TimeDelta::FromSeconds(5)); 432 ASSERT_TRUE(rollback_checker.Wait());
398 ASSERT_TRUE(checker.Wait()); 433 SyncBackendStoppedChecker shutdown_checker(GetSyncService(0));
434 ASSERT_TRUE(shutdown_checker.Wait());
399 435
400 // Verify bookmarks are unchanged. 436 // Verify bookmarks are unchanged.
401 ASSERT_EQ(3, sub_folder->child_count()); 437 ASSERT_EQ(3, sub_folder->child_count());
402 ASSERT_EQ(GURL(kUrl1), sub_folder->GetChild(0)->url()); 438 ASSERT_EQ(GURL(kUrl1), sub_folder->GetChild(0)->url());
403 ASSERT_EQ(GURL(kUrl2), sub_folder->GetChild(1)->url()); 439 ASSERT_EQ(GURL(kUrl2), sub_folder->GetChild(1)->url());
404 ASSERT_EQ(GURL(kUrl3), sub_folder->GetChild(2)->url()); 440 ASSERT_EQ(GURL(kUrl3), sub_folder->GetChild(2)->url());
405 } 441 }
OLDNEW
« no previous file with comments | « chrome/browser/sync/profile_sync_service_unittest.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698