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

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 a bug with polling. 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 233 matching lines...) Expand 10 before | Expand all | Expand 10 after
473 } 630 }
474 631
475 // Test nudges / polls don't run in config mode and config tasks do. 632 // Test nudges / polls don't run in config mode and config tasks do.
476 TEST_F(SyncerThread2Test, ConfigurationMode) { 633 TEST_F(SyncerThread2Test, ConfigurationMode) {
477 TimeDelta poll(TimeDelta::FromMilliseconds(15)); 634 TimeDelta poll(TimeDelta::FromMilliseconds(15));
478 SyncShareRecords records; 635 SyncShareRecords records;
479 base::WaitableEvent done(false, false); 636 base::WaitableEvent done(false, false);
480 base::WaitableEvent* dummy = NULL; 637 base::WaitableEvent* dummy = NULL;
481 syncer_thread()->OnReceivedLongPollIntervalUpdate(poll); 638 syncer_thread()->OnReceivedLongPollIntervalUpdate(poll);
482 EXPECT_CALL(*syncer(), SyncShare(_,_,_)) 639 EXPECT_CALL(*syncer(), SyncShare(_,_,_))
483 .WillOnce(DoAll(Invoke(sessions::test_util::SimulateSuccess), 640 .WillOnce((Invoke(sessions::test_util::SimulateSuccess),
484 WithArg<0>(RecordSyncShare(&records, 1U, dummy)))); 641 WithArg<0>(RecordSyncShare(&records, 1U, dummy))));
485 syncer_thread()->Start(SyncerThread::CONFIGURATION_MODE, NULL); 642 syncer_thread()->Start(SyncerThread::CONFIGURATION_MODE, NULL);
486 syncable::ModelTypeBitSet nudge_types; 643 syncable::ModelTypeBitSet nudge_types;
487 nudge_types[syncable::AUTOFILL] = true; 644 nudge_types[syncable::AUTOFILL] = true;
488 syncer_thread()->ScheduleNudge(zero(), NUDGE_SOURCE_LOCAL, nudge_types, 645 syncer_thread()->ScheduleNudge(zero(), NUDGE_SOURCE_LOCAL, nudge_types,
489 FROM_HERE); 646 FROM_HERE);
490 syncer_thread()->ScheduleNudge(zero(), NUDGE_SOURCE_LOCAL, nudge_types, 647 syncer_thread()->ScheduleNudge(zero(), NUDGE_SOURCE_LOCAL, nudge_types,
491 FROM_HERE); 648 FROM_HERE);
492 649
493 syncable::ModelTypeBitSet config_types; 650 syncable::ModelTypeBitSet config_types;
(...skipping 242 matching lines...) Expand 10 before | Expand all | Expand 10 after
736 // TODO(tim): Implement this test and then the functionality! 893 // TODO(tim): Implement this test and then the functionality!
737 TEST_F(SyncerThread2Test, DISABLED_NoConfigDuringNormal) { 894 TEST_F(SyncerThread2Test, DISABLED_NoConfigDuringNormal) {
738 } 895 }
739 896
740 // Test that starting the syncer thread without a valid connection doesn't 897 // Test that starting the syncer thread without a valid connection doesn't
741 // break things when a connection is detected. 898 // break things when a connection is detected.
742 TEST_F(SyncerThread2Test, StartWhenNotConnected) { 899 TEST_F(SyncerThread2Test, StartWhenNotConnected) {
743 base::WaitableEvent done(false, false); 900 base::WaitableEvent done(false, false);
744 MessageLoop cur; 901 MessageLoop cur;
745 connection()->SetServerNotReachable(); 902 connection()->SetServerNotReachable();
903 EXPECT_CALL(*syncer(), SyncShare(_,_,_)).WillOnce(SignalEvent(&done));
746 syncer_thread()->Start(SyncerThread::NORMAL_MODE, NULL); 904 syncer_thread()->Start(SyncerThread::NORMAL_MODE, NULL);
747 syncer_thread()->ScheduleNudge(zero(), NUDGE_SOURCE_LOCAL, ModelTypeBitSet(), 905 syncer_thread()->ScheduleNudge(zero(), NUDGE_SOURCE_LOCAL, ModelTypeBitSet(),
748 FROM_HERE); 906 FROM_HERE);
749 FlushLastTask(&done); 907 FlushLastTask(&done);
750 908
751 connection()->SetServerReachable(); 909 connection()->SetServerReachable();
752 cur.PostTask(FROM_HERE, NewRunnableFunction( 910 cur.PostTask(FROM_HERE, NewRunnableFunction(
753 &SyncerThread2Test::QuitMessageLoop)); 911 &SyncerThread2Test::QuitMessageLoop));
754 cur.Run(); 912 cur.Run();
755 913
756 // By now, the server connection event should have been posted to the 914 // By now, the server connection event should have been posted to the
757 // SyncerThread. 915 // SyncerThread.
758 FlushLastTask(&done); 916 FlushLastTask(&done);
759 EXPECT_CALL(*syncer(), SyncShare(_,_,_)).WillOnce(SignalEvent(&done));
760 syncer_thread()->ScheduleNudge(zero(), NUDGE_SOURCE_LOCAL, ModelTypeBitSet(),
761 FROM_HERE);
762 done.TimedWait(timeout()); 917 done.TimedWait(timeout());
763 } 918 }
764 919
765 TEST_F(SyncerThread2Test, SetsPreviousRoutingInfo) { 920 TEST_F(SyncerThread2Test, SetsPreviousRoutingInfo) {
766 base::WaitableEvent done(false, false); 921 base::WaitableEvent done(false, false);
767 ModelSafeRoutingInfo info; 922 ModelSafeRoutingInfo info;
768 EXPECT_TRUE(info == context()->previous_session_routing_info()); 923 EXPECT_TRUE(info == context()->previous_session_routing_info());
769 ModelSafeRoutingInfo expected; 924 ModelSafeRoutingInfo expected;
770 context()->registrar()->GetModelSafeRoutingInfo(&expected); 925 context()->registrar()->GetModelSafeRoutingInfo(&expected);
771 ASSERT_FALSE(expected.empty()); 926 ASSERT_FALSE(expected.empty());
772 EXPECT_CALL(*syncer(), SyncShare(_,_,_)).Times(1); 927 EXPECT_CALL(*syncer(), SyncShare(_,_,_)).Times(1);
773 928
774 syncer_thread()->Start(SyncerThread::NORMAL_MODE, NULL); 929 syncer_thread()->Start(SyncerThread::NORMAL_MODE, NULL);
775 syncer_thread()->ScheduleNudge(zero(), NUDGE_SOURCE_LOCAL, ModelTypeBitSet(), 930 syncer_thread()->ScheduleNudge(zero(), NUDGE_SOURCE_LOCAL, ModelTypeBitSet(),
776 FROM_HERE); 931 FROM_HERE);
777 FlushLastTask(&done); 932 FlushLastTask(&done);
778 syncer_thread()->Stop(); 933 syncer_thread()->Stop();
779 934
780 EXPECT_TRUE(expected == context()->previous_session_routing_info()); 935 EXPECT_TRUE(expected == context()->previous_session_routing_info());
781 } 936 }
782 937
783 } // namespace s3 938 } // namespace s3
784 } // namespace browser_sync 939 } // namespace browser_sync
785 940
786 // SyncerThread won't outlive the test! 941 // SyncerThread won't outlive the test!
787 DISABLE_RUNNABLE_METHOD_REFCOUNT(browser_sync::s3::SyncerThread2Test); 942 DISABLE_RUNNABLE_METHOD_REFCOUNT(browser_sync::s3::SyncerThread2Test);
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698