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

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

Issue 6182004: [SYNC] Refactor SyncSourceInfo and add support in chrome invalidation client ... (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Rebase Created 9 years, 11 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) 2009 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2009 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 <list> 5 #include <list>
6 #include <map> 6 #include <map>
7 7
8 #include "base/lock.h" 8 #include "base/lock.h"
9 #include "base/scoped_ptr.h" 9 #include "base/scoped_ptr.h"
10 #include "base/time.h" 10 #include "base/time.h"
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after
162 if (!syncer_thread()->RequestResume()) 162 if (!syncer_thread()->RequestResume())
163 return false; 163 return false;
164 return event.TimedWait(max_wait_time_); 164 return event.TimedWait(max_wait_time_);
165 } 165 }
166 166
167 void PreventThreadFromPolling() { 167 void PreventThreadFromPolling() {
168 const TimeDelta poll_interval = TimeDelta::FromMinutes(5); 168 const TimeDelta poll_interval = TimeDelta::FromMinutes(5);
169 syncer_thread()->SetSyncerShortPollInterval(poll_interval); 169 syncer_thread()->SetSyncerShortPollInterval(poll_interval);
170 } 170 }
171 171
172 // Compare a provided datatype payload map to the datatype payloads stored
173 // in the SyncerThread vault.
174 bool ComparePayloadsToVault(const std::map<syncable::ModelType, std::string>&
175 lhs) {
176 for (size_t i = syncable::FIRST_REAL_MODEL_TYPE;
177 i < syncable::MODEL_TYPE_COUNT;
178 ++i) {
179 std::map<syncable::ModelType, std::string>::const_iterator payload =
180 lhs.find(syncable::ModelTypeFromInt(i));
181 if (payload != lhs.end()) {
182 if (payload->second != syncer_thread()->vault_.datatype_payloads_[i]) {
183 return false;
184 }
185 } else {
186 if (syncer_thread()->vault_.datatype_payloads_[i] != std::string("")) {
187 return false;
188 }
189 }
190 }
191 return true;
192 }
193
172 private: 194 private:
173 195
174 virtual void OnSyncEngineEvent(const SyncEngineEvent& event) { 196 virtual void OnSyncEngineEvent(const SyncEngineEvent& event) {
175 if (event.what_happened == SyncEngineEvent::SYNC_CYCLE_ENDED) 197 if (event.what_happened == SyncEngineEvent::SYNC_CYCLE_ENDED)
176 sync_cycle_ended_event_.Signal(); 198 sync_cycle_ended_event_.Signal();
177 } 199 }
178 200
179 protected: 201 protected:
180 TimeDelta max_wait_time_; 202 TimeDelta max_wait_time_;
181 SyncSessionContext* context_; 203 SyncSessionContext* context_;
(...skipping 279 matching lines...) Expand 10 before | Expand all | Expand 10 after
461 false); 483 false);
462 484
463 ASSERT_EQ(SyncerThread::kDefaultLongPollIntervalSeconds, 485 ASSERT_EQ(SyncerThread::kDefaultLongPollIntervalSeconds,
464 interval.poll_delta.InSeconds()); 486 interval.poll_delta.InSeconds());
465 ASSERT_EQ(WaitInterval::NORMAL, interval.mode); 487 ASSERT_EQ(WaitInterval::NORMAL, interval.mode);
466 ASSERT_FALSE(interval.had_nudge_during_backoff); 488 ASSERT_FALSE(interval.had_nudge_during_backoff);
467 ASSERT_FALSE(continue_sync_cycle_param); 489 ASSERT_FALSE(continue_sync_cycle_param);
468 } 490 }
469 491
470 { 492 {
471
472 // Now try with unsynced local items. 493 // Now try with unsynced local items.
473 context->set_last_snapshot(SessionSnapshotForTest(0, 1)); 494 context->set_last_snapshot(SessionSnapshotForTest(0, 1));
474 bool continue_sync_cycle_param = false; 495 bool continue_sync_cycle_param = false;
475 496
476 WaitInterval interval = syncer_thread->CalculatePollingWaitTime( 497 WaitInterval interval = syncer_thread->CalculatePollingWaitTime(
477 0, 498 0,
478 &user_idle_milliseconds_param, 499 &user_idle_milliseconds_param,
479 &continue_sync_cycle_param, 500 &continue_sync_cycle_param,
480 false); 501 false);
481 502
(...skipping 23 matching lines...) Expand all
505 526
506 ASSERT_EQ(SyncerThread::kDefaultLongPollIntervalSeconds, 527 ASSERT_EQ(SyncerThread::kDefaultLongPollIntervalSeconds,
507 interval.poll_delta.InSeconds()); 528 interval.poll_delta.InSeconds());
508 ASSERT_EQ(WaitInterval::NORMAL, interval.mode); 529 ASSERT_EQ(WaitInterval::NORMAL, interval.mode);
509 ASSERT_FALSE(interval.had_nudge_during_backoff); 530 ASSERT_FALSE(interval.had_nudge_during_backoff);
510 ASSERT_FALSE(continue_sync_cycle_param); 531 ASSERT_FALSE(continue_sync_cycle_param);
511 } 532 }
512 533
513 // Regression for exponential backoff reset when the syncer is nudged. 534 // Regression for exponential backoff reset when the syncer is nudged.
514 { 535 {
515
516 context->set_last_snapshot(SessionSnapshotForTest(0, 1)); 536 context->set_last_snapshot(SessionSnapshotForTest(0, 1));
517 bool continue_sync_cycle_param = false; 537 bool continue_sync_cycle_param = false;
518 538
519 // Expect move from default polling interval to exponential backoff due to 539 // Expect move from default polling interval to exponential backoff due to
520 // unsynced_count != 0. 540 // unsynced_count != 0.
521 WaitInterval interval = syncer_thread->CalculatePollingWaitTime( 541 WaitInterval interval = syncer_thread->CalculatePollingWaitTime(
522 3600, 542 3600,
523 &user_idle_milliseconds_param, 543 &user_idle_milliseconds_param,
524 &continue_sync_cycle_param, 544 &continue_sync_cycle_param,
525 false); 545 false);
(...skipping 257 matching lines...) Expand 10 before | Expand all | Expand 10 after
783 803
784 interceptor.WaitForSyncShare(1, TimeDelta::FromSeconds(1)); 804 interceptor.WaitForSyncShare(1, TimeDelta::FromSeconds(1));
785 EXPECT_EQ(static_cast<unsigned int>(2), 805 EXPECT_EQ(static_cast<unsigned int>(2),
786 interceptor.times_sync_occured().size()); 806 interceptor.times_sync_occured().size());
787 807
788 // SyncerThread should be waiting again. Signal it to stop. 808 // SyncerThread should be waiting again. Signal it to stop.
789 EXPECT_TRUE(syncer_thread()->Stop(2000)); 809 EXPECT_TRUE(syncer_thread()->Stop(2000));
790 EXPECT_TRUE(syncer_thread()->vault_.pending_nudge_types_.none()); 810 EXPECT_TRUE(syncer_thread()->vault_.pending_nudge_types_.none());
791 } 811 }
792 812
813 TEST_F(SyncerThreadWithSyncerTest, NudgeWithPayloads) {
814 SyncShareIntercept interceptor;
815 connection()->SetMidCommitObserver(&interceptor);
816 // We don't want a poll to happen during this test (except the first one).
817 PreventThreadFromPolling();
818 EXPECT_TRUE(syncer_thread()->Start());
819 metadb()->Open();
820 syncer_thread()->CreateSyncer(metadb()->name());
821 const TimeDelta poll_interval = TimeDelta::FromMinutes(5);
822 interceptor.WaitForSyncShare(1, poll_interval + poll_interval);
823 EXPECT_EQ(static_cast<unsigned int>(1),
824 interceptor.times_sync_occured().size());
825
826 // The SyncerThread should be waiting for the poll now. Nudge it to sync
827 // immediately (5ms).
828 std::map<syncable::ModelType, std::string> payloads;
829 payloads[syncable::BOOKMARKS] = "test";
830
831 // Paused so we can verify the nudge types safely.
832 syncer_thread()->RequestPause();
833 syncer_thread()->NudgeSyncerWithPayloads(5,
834 SyncerThread::kUnknown,
835 payloads);
836 EXPECT_TRUE(ComparePayloadsToVault(payloads));
837 syncable::ModelTypeBitSet model_types;
838 model_types[syncable::BOOKMARKS] = true;
839 EXPECT_EQ(model_types, syncer_thread()->vault_.pending_nudge_types_);
840 syncer_thread()->RequestResume();
841
842 interceptor.WaitForSyncShare(1, TimeDelta::FromSeconds(1));
843 EXPECT_EQ(static_cast<unsigned int>(2),
844 interceptor.times_sync_occured().size());
845
846 // SyncerThread should be waiting again. Signal it to stop.
847 EXPECT_TRUE(syncer_thread()->Stop(2000));
848 EXPECT_TRUE(syncer_thread()->vault_.pending_nudge_types_.none());
849 EXPECT_TRUE(syncer_thread()->vault_.datatype_payloads_.empty());
850 }
851
852 TEST_F(SyncerThreadWithSyncerTest, NudgeWithPayloadsCoalesced) {
853 SyncShareIntercept interceptor;
854 connection()->SetMidCommitObserver(&interceptor);
855 // We don't want a poll to happen during this test (except the first one).
856 PreventThreadFromPolling();
857 EXPECT_TRUE(syncer_thread()->Start());
858 metadb()->Open();
859 syncer_thread()->CreateSyncer(metadb()->name());
860 const TimeDelta poll_interval = TimeDelta::FromMinutes(5);
861 interceptor.WaitForSyncShare(1, poll_interval + poll_interval);
862 EXPECT_EQ(static_cast<unsigned int>(1),
863 interceptor.times_sync_occured().size());
864
865 // The SyncerThread should be waiting for the poll now. Nudge it to sync
866 // immediately (5ms).
867 std::map<syncable::ModelType, std::string> payloads;
868 payloads[syncable::BOOKMARKS] = "books";
869 syncable::ModelTypeBitSet model_types;
870 model_types[syncable::BOOKMARKS] = true;
871
872 // Paused so we can verify the nudge types safely.
873 syncer_thread()->RequestPause();
874 syncer_thread()->NudgeSyncerWithPayloads(100,
875 SyncerThread::kUnknown,
876 payloads);
877 EXPECT_EQ(model_types, syncer_thread()->vault_.pending_nudge_types_);
878 EXPECT_TRUE(ComparePayloadsToVault(payloads));
879
880 payloads.erase(syncable::BOOKMARKS);
881 payloads[syncable::AUTOFILL] = "auto";
882 model_types[syncable::AUTOFILL] = true;
883 syncer_thread()->NudgeSyncerWithPayloads(0,
884 SyncerThread::kUnknown,
885 payloads);
886
887 // Reset BOOKMARKS for expectations.
888 payloads[syncable::BOOKMARKS] = "books";
889 EXPECT_EQ(model_types, syncer_thread()->vault_.pending_nudge_types_);
890 EXPECT_TRUE(ComparePayloadsToVault(payloads));
891
892 syncer_thread()->RequestResume();
893
894 interceptor.WaitForSyncShare(1, TimeDelta::FromSeconds(1));
895 EXPECT_EQ(static_cast<unsigned int>(2),
896 interceptor.times_sync_occured().size());
897
898 // SyncerThread should be waiting again. Signal it to stop.
899 EXPECT_TRUE(syncer_thread()->Stop(2000));
900 EXPECT_TRUE(syncer_thread()->vault_.pending_nudge_types_.none());
901 EXPECT_TRUE(syncer_thread()->vault_.datatype_payloads_.empty());
902 }
903
793 TEST_F(SyncerThreadWithSyncerTest, Throttling) { 904 TEST_F(SyncerThreadWithSyncerTest, Throttling) {
794 SyncShareIntercept interceptor; 905 SyncShareIntercept interceptor;
795 connection()->SetMidCommitObserver(&interceptor); 906 connection()->SetMidCommitObserver(&interceptor);
796 const TimeDelta poll_interval = TimeDelta::FromMilliseconds(10); 907 const TimeDelta poll_interval = TimeDelta::FromMilliseconds(10);
797 syncer_thread()->SetSyncerShortPollInterval(poll_interval); 908 syncer_thread()->SetSyncerShortPollInterval(poll_interval);
798 909
799 EXPECT_TRUE(syncer_thread()->Start()); 910 EXPECT_TRUE(syncer_thread()->Start());
800 metadb()->Open(); 911 metadb()->Open();
801 syncer_thread()->CreateSyncer(metadb()->name()); 912 syncer_thread()->CreateSyncer(metadb()->name());
802 913
(...skipping 323 matching lines...) Expand 10 before | Expand all | Expand 10 after
1126 EXPECT_CALL(listener, OnSyncEngineEvent( 1237 EXPECT_CALL(listener, OnSyncEngineEvent(
1127 Field(&SyncEngineEvent::what_happened, 1238 Field(&SyncEngineEvent::what_happened,
1128 SyncEngineEvent::SYNCER_THREAD_EXITING))); 1239 SyncEngineEvent::SYNCER_THREAD_EXITING)));
1129 1240
1130 ASSERT_TRUE(Resume(&listener)); 1241 ASSERT_TRUE(Resume(&listener));
1131 ASSERT_TRUE(sync_cycle_ended_event.TimedWait(max_wait_time_)); 1242 ASSERT_TRUE(sync_cycle_ended_event.TimedWait(max_wait_time_));
1132 EXPECT_TRUE(syncer_thread()->Stop(2000)); 1243 EXPECT_TRUE(syncer_thread()->Stop(2000));
1133 } 1244 }
1134 1245
1135 } // namespace browser_sync 1246 } // namespace browser_sync
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698