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

Side by Side Diff: chrome/browser/sync/engine/syncer_thread2_unittest.cc

Issue 6812004: sync: Make nudge + config jobs reliable in SyncerThread2 (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fixing CR feedback. Created 9 years, 8 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) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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/synchronization/waitable_event.h" 5 #include "base/synchronization/waitable_event.h"
6 #include "base/test/test_timeouts.h" 6 #include "base/test/test_timeouts.h"
7 #include "chrome/browser/sync/engine/mock_model_safe_workers.h" 7 #include "chrome/browser/sync/engine/mock_model_safe_workers.h"
8 #include "chrome/browser/sync/engine/syncer.h" 8 #include "chrome/browser/sync/engine/syncer.h"
9 #include "chrome/browser/sync/engine/syncer_thread2.h" 9 #include "chrome/browser/sync/engine/syncer_thread2.h"
10 #include "chrome/browser/sync/sessions/test_util.h" 10 #include "chrome/browser/sync/sessions/test_util.h"
(...skipping 29 matching lines...) Expand all
40 namespace s3 { 40 namespace s3 {
41 41
42 // Used when tests want to record syncing activity to examine later. 42 // Used when tests want to record syncing activity to examine later.
43 struct SyncShareRecords { 43 struct SyncShareRecords {
44 std::vector<TimeTicks> times; 44 std::vector<TimeTicks> times;
45 std::vector<linked_ptr<SyncSessionSnapshot> > snapshots; 45 std::vector<linked_ptr<SyncSessionSnapshot> > snapshots;
46 }; 46 };
47 47
48 // Convenient to use in tests wishing to analyze SyncShare calls over time. 48 // Convenient to use in tests wishing to analyze SyncShare calls over time.
49 static const size_t kMinNumSamples = 5; 49 static const size_t kMinNumSamples = 5;
50 50 struct SyncerThread::SyncSessionJob;
51 struct SyncerThread::WaitInterval;
51 class SyncerThread2Test : public testing::Test { 52 class SyncerThread2Test : public testing::Test {
52 public: 53 public:
53 class MockDelayProvider : public SyncerThread::DelayProvider { 54 class MockDelayProvider : public SyncerThread::DelayProvider {
54 public: 55 public:
55 MOCK_METHOD1(GetDelay, TimeDelta(const TimeDelta&)); 56 MOCK_METHOD1(GetDelay, TimeDelta(const TimeDelta&));
56 }; 57 };
57 58
58 virtual void SetUp() { 59 virtual void SetUp() {
59 syncdb_.SetUp(); 60 syncdb_.SetUp();
60 syncer_ = new MockSyncer(); 61 syncer_ = new MockSyncer();
61 delay_ = NULL; 62 delay_ = NULL;
62 registrar_.reset(MockModelSafeWorkerRegistrar::PassiveBookmarks()); 63 registrar_.reset(MockModelSafeWorkerRegistrar::PassiveBookmarks());
63 connection_.reset(new MockConnectionManager(syncdb_.manager(), "Test")); 64 connection_.reset(new MockConnectionManager(syncdb_.manager(), "Test"));
64 connection_->SetServerReachable(); 65 connection_->SetServerReachable();
65 context_ = new SyncSessionContext(connection_.get(), syncdb_.manager(), 66 context_ = new SyncSessionContext(connection_.get(), syncdb_.manager(),
66 registrar_.get(), std::vector<SyncEngineEventListener*>()); 67 registrar_.get(), std::vector<SyncEngineEventListener*>());
67 context_->set_notifications_enabled(true); 68 context_->set_notifications_enabled(true);
68 context_->set_account_name("Test"); 69 context_->set_account_name("Test");
69 syncer_thread_.reset(new SyncerThread(context_, syncer_)); 70 syncer_thread_.reset(new SyncerThread(context_, syncer_));
70 } 71 }
71 72
73 virtual void SetUpWithTypes(syncable::ModelTypeBitSet types) {
74 syncdb_.SetUp();
75 syncer_ = new MockSyncer();
76 delay_ = NULL;
77 registrar_.reset(MockModelSafeWorkerRegistrar::PassiveForTypes(types));
78 connection_.reset(new MockConnectionManager(syncdb_.manager(), "Test"));
79 connection_->SetServerReachable();
80 context_ = new SyncSessionContext(connection_.get(), syncdb_.manager(),
81 registrar_.get(), std::vector<SyncEngineEventListener*>());
82 context_->set_notifications_enabled(true);
83 context_->set_account_name("Test");
84 syncer_thread_.reset(new SyncerThread(context_, syncer_));
85 }
86
72 SyncerThread* syncer_thread() { return syncer_thread_.get(); } 87 SyncerThread* syncer_thread() { return syncer_thread_.get(); }
73 MockSyncer* syncer() { return syncer_; } 88 MockSyncer* syncer() { return syncer_; }
74 MockDelayProvider* delay() { return delay_; } 89 MockDelayProvider* delay() { return delay_; }
75 MockConnectionManager* connection() { return connection_.get(); } 90 MockConnectionManager* connection() { return connection_.get(); }
76 TimeDelta zero() { return TimeDelta::FromSeconds(0); } 91 TimeDelta zero() { return TimeDelta::FromSeconds(0); }
77 TimeDelta timeout() { 92 TimeDelta timeout() {
78 return TimeDelta::FromMilliseconds(TestTimeouts::action_timeout_ms()); 93 return TimeDelta::FromMilliseconds(TestTimeouts::action_timeout_ms());
79 } 94 }
80 95
81 virtual void TearDown() { 96 virtual void TearDown() {
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after
220 FROM_HERE); 235 FROM_HERE);
221 done.TimedWait(timeout()); 236 done.TimedWait(timeout());
222 237
223 EXPECT_EQ(1U, records2.snapshots.size()); 238 EXPECT_EQ(1U, records2.snapshots.size());
224 EXPECT_TRUE(CompareModelTypeBitSetToModelTypePayloadMap(model_types, 239 EXPECT_TRUE(CompareModelTypeBitSetToModelTypePayloadMap(model_types,
225 records2.snapshots[0]->source.types)); 240 records2.snapshots[0]->source.types));
226 EXPECT_EQ(GetUpdatesCallerInfo::LOCAL, 241 EXPECT_EQ(GetUpdatesCallerInfo::LOCAL,
227 records2.snapshots[0]->source.updates_source); 242 records2.snapshots[0]->source.updates_source);
228 } 243 }
229 244
245 // Make sure a regular config command is scheduled fine in the absence of any
246 // errors.
247 TEST_F(SyncerThread2Test, Config) {
248 base::WaitableEvent done(false, false);
249 SyncShareRecords records;
250 syncable::ModelTypeBitSet model_types;
251 model_types[syncable::BOOKMARKS] = true;
252
253 EXPECT_CALL(*syncer(), SyncShare(_,_,_))
254 .WillOnce(DoAll(Invoke(sessions::test_util::SimulateSuccess),
255 WithArg<0>(RecordSyncShare(&records, 1U, &done))));
256
257 syncer_thread()->Start(SyncerThread::CONFIGURATION_MODE, NULL);
258
259 syncer_thread()->ScheduleConfig(model_types);
260 done.TimedWait(timeout());
261
262 EXPECT_EQ(1U, records.snapshots.size());
263 EXPECT_TRUE(CompareModelTypeBitSetToModelTypePayloadMap(model_types,
264 records.snapshots[0]->source.types));
265 EXPECT_EQ(GetUpdatesCallerInfo::FIRST_UPDATE,
266 records.snapshots[0]->source.updates_source);
267 }
268
269 // Simulate a failure and make sure the config request is retried.
270 TEST_F(SyncerThread2Test, ConfigWithBackingOff) {
271 base::WaitableEvent done(false, false);
272 base::WaitableEvent* dummy = NULL;
273 UseMockDelayProvider();
274 EXPECT_CALL(*delay(), GetDelay(_))
275 .WillRepeatedly(Return(TimeDelta::FromMilliseconds(1)));
276 SyncShareRecords records;
277 syncable::ModelTypeBitSet model_types;
278 model_types[syncable::BOOKMARKS] = true;
279
280 EXPECT_CALL(*syncer(), SyncShare(_,_,_))
281 .WillOnce(DoAll(Invoke(sessions::test_util::SimulateCommitFailed),
282 WithArg<0>(RecordSyncShare(&records, 1U, dummy))))
283 .WillOnce(DoAll(Invoke(sessions::test_util::SimulateSuccess),
284 WithArg<0>(RecordSyncShare(&records, 1U, &done))));
285
286 syncer_thread()->Start(SyncerThread::CONFIGURATION_MODE, NULL);
287
288 syncer_thread()->ScheduleConfig(model_types);
289 done.TimedWait(timeout());
290
291 EXPECT_EQ(2U, records.snapshots.size());
292 EXPECT_TRUE(CompareModelTypeBitSetToModelTypePayloadMap(model_types,
293 records.snapshots[1]->source.types));
294 EXPECT_EQ(GetUpdatesCallerInfo::SYNC_CYCLE_CONTINUATION,
295 records.snapshots[1]->source.updates_source);
296 }
297
298 // Issue 2 config commands. Second one right after the first has failed
299 // and make sure LATEST is executed.
300 TEST_F(SyncerThread2Test, MultipleConfigWithBackingOff) {
301 syncable::ModelTypeBitSet model_types1, model_types2;
302 model_types1[syncable::BOOKMARKS] = true;
303 model_types2[syncable::AUTOFILL] = true;
304 SetUpWithTypes(model_types1 | model_types2);
305 base::WaitableEvent done(false, false);
306 base::WaitableEvent done1(false, false);
307 base::WaitableEvent* dummy = NULL;
308 UseMockDelayProvider();
309 EXPECT_CALL(*delay(), GetDelay(_))
310 .WillRepeatedly(Return(TimeDelta::FromMilliseconds(30)));
311 SyncShareRecords records;
312
313 EXPECT_CALL(*syncer(), SyncShare(_,_,_))
314 .WillOnce(DoAll(Invoke(sessions::test_util::SimulateCommitFailed),
315 WithArg<0>(RecordSyncShare(&records, 1U, dummy))))
316 .WillOnce(DoAll(Invoke(sessions::test_util::SimulateCommitFailed),
317 WithArg<0>(RecordSyncShare(&records, 1U, &done1))))
318 .WillOnce(DoAll(Invoke(sessions::test_util::SimulateSuccess),
319 WithArg<0>(RecordSyncShare(&records, 1U, &done))));
320
321 syncer_thread()->Start(SyncerThread::CONFIGURATION_MODE, NULL);
322
323 syncer_thread()->ScheduleConfig(model_types1);
324
325 // done1 indicates the first config failed.
326 done1.TimedWait(timeout());
327 syncer_thread()->ScheduleConfig(model_types2);
328 done.TimedWait(timeout());
329
330 EXPECT_EQ(3U, records.snapshots.size());
331 EXPECT_TRUE(CompareModelTypeBitSetToModelTypePayloadMap(model_types2,
332 records.snapshots[2]->source.types));
333 EXPECT_EQ(GetUpdatesCallerInfo::FIRST_UPDATE,
334 records.snapshots[2]->source.updates_source);
335 }
336
337 // Issue a nudge when the config has failed. Make sure both the config and
338 // nudge are executed.
339 TEST_F(SyncerThread2Test, NudgeWithConfigWithBackingOff) {
340 syncable::ModelTypeBitSet model_types;
341 model_types[syncable::BOOKMARKS] = true;
342 base::WaitableEvent done(false, false);
343 base::WaitableEvent done1(false, false);
344 base::WaitableEvent done2(false, false);
345 base::WaitableEvent* dummy = NULL;
346 UseMockDelayProvider();
347 EXPECT_CALL(*delay(), GetDelay(_))
348 .WillRepeatedly(Return(TimeDelta::FromMilliseconds(50)));
349 SyncShareRecords records;
350
351 EXPECT_CALL(*syncer(), SyncShare(_,_,_))
352 .WillOnce(DoAll(Invoke(sessions::test_util::SimulateCommitFailed),
353 WithArg<0>(RecordSyncShare(&records, 1U, dummy))))
354 .WillOnce(DoAll(Invoke(sessions::test_util::SimulateCommitFailed),
355 WithArg<0>(RecordSyncShare(&records, 1U, &done1))))
356 .WillOnce(DoAll(Invoke(sessions::test_util::SimulateSuccess),
357 WithArg<0>(RecordSyncShare(&records, 1U, &done2))))
358 .WillOnce(DoAll(Invoke(sessions::test_util::SimulateSuccess),
359 WithArg<0>(RecordSyncShare(&records, 1U, &done))));
360
361 syncer_thread()->Start(SyncerThread::CONFIGURATION_MODE, NULL);
362
363 syncer_thread()->ScheduleConfig(model_types);
364 done1.TimedWait(timeout());
365 syncer_thread()->ScheduleNudge(zero(), NUDGE_SOURCE_LOCAL, model_types,
366 FROM_HERE);
367
368 // done2 indicates config suceeded. Now change the mode so nudge can execute.
369 done2.TimedWait(timeout());
370 syncer_thread()->Start(SyncerThread::NORMAL_MODE, NULL);
371 done.TimedWait(timeout());
372 EXPECT_EQ(4U, records.snapshots.size());
373
374 EXPECT_TRUE(CompareModelTypeBitSetToModelTypePayloadMap(model_types,
375 records.snapshots[2]->source.types));
376 EXPECT_EQ(GetUpdatesCallerInfo::SYNC_CYCLE_CONTINUATION,
377 records.snapshots[2]->source.updates_source);
378
379 EXPECT_TRUE(CompareModelTypeBitSetToModelTypePayloadMap(model_types,
380 records.snapshots[3]->source.types));
381 EXPECT_EQ(GetUpdatesCallerInfo::LOCAL,
382 records.snapshots[3]->source.updates_source);
383
384 }
385
386
230 // Test that nudges are coalesced. 387 // Test that nudges are coalesced.
231 TEST_F(SyncerThread2Test, NudgeCoalescing) { 388 TEST_F(SyncerThread2Test, NudgeCoalescing) {
232 syncer_thread()->Start(SyncerThread::NORMAL_MODE, NULL); 389 syncer_thread()->Start(SyncerThread::NORMAL_MODE, NULL);
233 base::WaitableEvent done(false, false); 390 base::WaitableEvent done(false, false);
234 SyncShareRecords r; 391 SyncShareRecords r;
235 EXPECT_CALL(*syncer(), SyncShare(_,_,_)) 392 EXPECT_CALL(*syncer(), SyncShare(_,_,_))
236 .WillOnce(DoAll(Invoke(sessions::test_util::SimulateSuccess), 393 .WillOnce(DoAll(Invoke(sessions::test_util::SimulateSuccess),
237 WithArg<0>(RecordSyncShare(&r, 1U, &done)))); 394 WithArg<0>(RecordSyncShare(&r, 1U, &done))));
238 syncable::ModelTypeBitSet types1, types2, types3; 395 syncable::ModelTypeBitSet types1, types2, types3;
239 types1[syncable::BOOKMARKS] = true; 396 types1[syncable::BOOKMARKS] = true;
(...skipping 229 matching lines...) Expand 10 before | Expand all | Expand 10 after
469 } 626 }
470 627
471 // Test nudges / polls don't run in config mode and config tasks do. 628 // Test nudges / polls don't run in config mode and config tasks do.
472 TEST_F(SyncerThread2Test, ConfigurationMode) { 629 TEST_F(SyncerThread2Test, ConfigurationMode) {
473 TimeDelta poll(TimeDelta::FromMilliseconds(15)); 630 TimeDelta poll(TimeDelta::FromMilliseconds(15));
474 SyncShareRecords records; 631 SyncShareRecords records;
475 base::WaitableEvent done(false, false); 632 base::WaitableEvent done(false, false);
476 base::WaitableEvent* dummy = NULL; 633 base::WaitableEvent* dummy = NULL;
477 syncer_thread()->OnReceivedLongPollIntervalUpdate(poll); 634 syncer_thread()->OnReceivedLongPollIntervalUpdate(poll);
478 EXPECT_CALL(*syncer(), SyncShare(_,_,_)) 635 EXPECT_CALL(*syncer(), SyncShare(_,_,_))
479 .WillOnce(DoAll(Invoke(sessions::test_util::SimulateSuccess), 636 .WillOnce((Invoke(sessions::test_util::SimulateSuccess),
480 WithArg<0>(RecordSyncShare(&records, 1U, dummy)))); 637 WithArg<0>(RecordSyncShare(&records, 1U, dummy))));
481 syncer_thread()->Start(SyncerThread::CONFIGURATION_MODE, NULL); 638 syncer_thread()->Start(SyncerThread::CONFIGURATION_MODE, NULL);
482 syncable::ModelTypeBitSet nudge_types; 639 syncable::ModelTypeBitSet nudge_types;
483 nudge_types[syncable::AUTOFILL] = true; 640 nudge_types[syncable::AUTOFILL] = true;
484 syncer_thread()->ScheduleNudge(zero(), NUDGE_SOURCE_LOCAL, nudge_types, 641 syncer_thread()->ScheduleNudge(zero(), NUDGE_SOURCE_LOCAL, nudge_types,
485 FROM_HERE); 642 FROM_HERE);
486 syncer_thread()->ScheduleNudge(zero(), NUDGE_SOURCE_LOCAL, nudge_types, 643 syncer_thread()->ScheduleNudge(zero(), NUDGE_SOURCE_LOCAL, nudge_types,
487 FROM_HERE); 644 FROM_HERE);
488 645
489 syncable::ModelTypeBitSet config_types; 646 syncable::ModelTypeBitSet config_types;
(...skipping 242 matching lines...) Expand 10 before | Expand all | Expand 10 after
732 // TODO(tim): Implement this test and then the functionality! 889 // TODO(tim): Implement this test and then the functionality!
733 TEST_F(SyncerThread2Test, DISABLED_NoConfigDuringNormal) { 890 TEST_F(SyncerThread2Test, DISABLED_NoConfigDuringNormal) {
734 } 891 }
735 892
736 // Test that starting the syncer thread without a valid connection doesn't 893 // Test that starting the syncer thread without a valid connection doesn't
737 // break things when a connection is detected. 894 // break things when a connection is detected.
738 TEST_F(SyncerThread2Test, StartWhenNotConnected) { 895 TEST_F(SyncerThread2Test, StartWhenNotConnected) {
739 base::WaitableEvent done(false, false); 896 base::WaitableEvent done(false, false);
740 MessageLoop cur; 897 MessageLoop cur;
741 connection()->SetServerNotReachable(); 898 connection()->SetServerNotReachable();
899 EXPECT_CALL(*syncer(), SyncShare(_,_,_)).WillOnce(SignalEvent(&done));
742 syncer_thread()->Start(SyncerThread::NORMAL_MODE, NULL); 900 syncer_thread()->Start(SyncerThread::NORMAL_MODE, NULL);
743 syncer_thread()->ScheduleNudge(zero(), NUDGE_SOURCE_LOCAL, ModelTypeBitSet(), 901 syncer_thread()->ScheduleNudge(zero(), NUDGE_SOURCE_LOCAL, ModelTypeBitSet(),
744 FROM_HERE); 902 FROM_HERE);
745 FlushLastTask(&done); 903 FlushLastTask(&done);
746 904
747 connection()->SetServerReachable(); 905 connection()->SetServerReachable();
748 cur.PostTask(FROM_HERE, NewRunnableFunction( 906 cur.PostTask(FROM_HERE, NewRunnableFunction(
749 &SyncerThread2Test::QuitMessageLoop)); 907 &SyncerThread2Test::QuitMessageLoop));
750 cur.Run(); 908 cur.Run();
751 909
752 // By now, the server connection event should have been posted to the 910 // By now, the server connection event should have been posted to the
753 // SyncerThread. 911 // SyncerThread.
754 FlushLastTask(&done); 912 FlushLastTask(&done);
755 EXPECT_CALL(*syncer(), SyncShare(_,_,_)).WillOnce(SignalEvent(&done));
756 syncer_thread()->ScheduleNudge(zero(), NUDGE_SOURCE_LOCAL, ModelTypeBitSet(),
757 FROM_HERE);
758 done.TimedWait(timeout()); 913 done.TimedWait(timeout());
759 } 914 }
760 915
761 TEST_F(SyncerThread2Test, SetsPreviousRoutingInfo) { 916 TEST_F(SyncerThread2Test, SetsPreviousRoutingInfo) {
762 base::WaitableEvent done(false, false); 917 base::WaitableEvent done(false, false);
763 ModelSafeRoutingInfo info; 918 ModelSafeRoutingInfo info;
764 EXPECT_TRUE(info == context()->previous_session_routing_info()); 919 EXPECT_TRUE(info == context()->previous_session_routing_info());
765 ModelSafeRoutingInfo expected; 920 ModelSafeRoutingInfo expected;
766 context()->registrar()->GetModelSafeRoutingInfo(&expected); 921 context()->registrar()->GetModelSafeRoutingInfo(&expected);
767 ASSERT_FALSE(expected.empty()); 922 ASSERT_FALSE(expected.empty());
768 EXPECT_CALL(*syncer(), SyncShare(_,_,_)).Times(1); 923 EXPECT_CALL(*syncer(), SyncShare(_,_,_)).Times(1);
769 924
770 syncer_thread()->Start(SyncerThread::NORMAL_MODE, NULL); 925 syncer_thread()->Start(SyncerThread::NORMAL_MODE, NULL);
771 syncer_thread()->ScheduleNudge(zero(), NUDGE_SOURCE_LOCAL, ModelTypeBitSet(), 926 syncer_thread()->ScheduleNudge(zero(), NUDGE_SOURCE_LOCAL, ModelTypeBitSet(),
772 FROM_HERE); 927 FROM_HERE);
773 FlushLastTask(&done); 928 FlushLastTask(&done);
774 syncer_thread()->Stop(); 929 syncer_thread()->Stop();
775 930
776 EXPECT_TRUE(expected == context()->previous_session_routing_info()); 931 EXPECT_TRUE(expected == context()->previous_session_routing_info());
777 } 932 }
778 933
779 } // namespace s3 934 } // namespace s3
780 } // namespace browser_sync 935 } // namespace browser_sync
781 936
782 // SyncerThread won't outlive the test! 937 // SyncerThread won't outlive the test!
783 DISABLE_RUNNABLE_METHOD_REFCOUNT(browser_sync::s3::SyncerThread2Test); 938 DISABLE_RUNNABLE_METHOD_REFCOUNT(browser_sync::s3::SyncerThread2Test);
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698