OLD | NEW |
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 Loading... |
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 SyncSouceInfo::TypePayloadMap to the pending nudge info |
| 173 // stored in the SyncerThread vault. |
| 174 bool CompareNudgeTypesToVault(const sessions::TypePayloadMap& lhs) { |
| 175 const sessions::TypePayloadMap& vault_nudge_types = |
| 176 syncer_thread()->vault_.pending_nudge_types_; |
| 177 return lhs == vault_nudge_types; |
| 178 } |
| 179 |
| 180 // Compare a provided ModelTypeBitset to the pending nudge info stored in the |
| 181 // SyncerThread vault. Nudge info in vault must not have any non-empty |
| 182 // payloads. |
| 183 bool CompareNudgeTypesBitSetToVault(const syncable::ModelTypeBitSet& lhs) { |
| 184 sessions::TypePayloadMap model_types_with_payloads = |
| 185 sessions::ModelTypeBitSetToTypePayloadMap(lhs, std::string()); |
| 186 return CompareNudgeTypesToVault(model_types_with_payloads); |
| 187 } |
| 188 |
| 189 |
172 private: | 190 private: |
173 | 191 |
174 virtual void OnSyncEngineEvent(const SyncEngineEvent& event) { | 192 virtual void OnSyncEngineEvent(const SyncEngineEvent& event) { |
175 if (event.what_happened == SyncEngineEvent::SYNC_CYCLE_ENDED) | 193 if (event.what_happened == SyncEngineEvent::SYNC_CYCLE_ENDED) |
176 sync_cycle_ended_event_.Signal(); | 194 sync_cycle_ended_event_.Signal(); |
177 } | 195 } |
178 | 196 |
179 protected: | 197 protected: |
180 TimeDelta max_wait_time_; | 198 TimeDelta max_wait_time_; |
181 SyncSessionContext* context_; | 199 SyncSessionContext* context_; |
(...skipping 279 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
461 false); | 479 false); |
462 | 480 |
463 ASSERT_EQ(SyncerThread::kDefaultLongPollIntervalSeconds, | 481 ASSERT_EQ(SyncerThread::kDefaultLongPollIntervalSeconds, |
464 interval.poll_delta.InSeconds()); | 482 interval.poll_delta.InSeconds()); |
465 ASSERT_EQ(WaitInterval::NORMAL, interval.mode); | 483 ASSERT_EQ(WaitInterval::NORMAL, interval.mode); |
466 ASSERT_FALSE(interval.had_nudge_during_backoff); | 484 ASSERT_FALSE(interval.had_nudge_during_backoff); |
467 ASSERT_FALSE(continue_sync_cycle_param); | 485 ASSERT_FALSE(continue_sync_cycle_param); |
468 } | 486 } |
469 | 487 |
470 { | 488 { |
471 | |
472 // Now try with unsynced local items. | 489 // Now try with unsynced local items. |
473 context->set_last_snapshot(SessionSnapshotForTest(0, 1)); | 490 context->set_last_snapshot(SessionSnapshotForTest(0, 1)); |
474 bool continue_sync_cycle_param = false; | 491 bool continue_sync_cycle_param = false; |
475 | 492 |
476 WaitInterval interval = syncer_thread->CalculatePollingWaitTime( | 493 WaitInterval interval = syncer_thread->CalculatePollingWaitTime( |
477 0, | 494 0, |
478 &user_idle_milliseconds_param, | 495 &user_idle_milliseconds_param, |
479 &continue_sync_cycle_param, | 496 &continue_sync_cycle_param, |
480 false); | 497 false); |
481 | 498 |
(...skipping 23 matching lines...) Expand all Loading... |
505 | 522 |
506 ASSERT_EQ(SyncerThread::kDefaultLongPollIntervalSeconds, | 523 ASSERT_EQ(SyncerThread::kDefaultLongPollIntervalSeconds, |
507 interval.poll_delta.InSeconds()); | 524 interval.poll_delta.InSeconds()); |
508 ASSERT_EQ(WaitInterval::NORMAL, interval.mode); | 525 ASSERT_EQ(WaitInterval::NORMAL, interval.mode); |
509 ASSERT_FALSE(interval.had_nudge_during_backoff); | 526 ASSERT_FALSE(interval.had_nudge_during_backoff); |
510 ASSERT_FALSE(continue_sync_cycle_param); | 527 ASSERT_FALSE(continue_sync_cycle_param); |
511 } | 528 } |
512 | 529 |
513 // Regression for exponential backoff reset when the syncer is nudged. | 530 // Regression for exponential backoff reset when the syncer is nudged. |
514 { | 531 { |
515 | |
516 context->set_last_snapshot(SessionSnapshotForTest(0, 1)); | 532 context->set_last_snapshot(SessionSnapshotForTest(0, 1)); |
517 bool continue_sync_cycle_param = false; | 533 bool continue_sync_cycle_param = false; |
518 | 534 |
519 // Expect move from default polling interval to exponential backoff due to | 535 // Expect move from default polling interval to exponential backoff due to |
520 // unsynced_count != 0. | 536 // unsynced_count != 0. |
521 WaitInterval interval = syncer_thread->CalculatePollingWaitTime( | 537 WaitInterval interval = syncer_thread->CalculatePollingWaitTime( |
522 3600, | 538 3600, |
523 &user_idle_milliseconds_param, | 539 &user_idle_milliseconds_param, |
524 &continue_sync_cycle_param, | 540 &continue_sync_cycle_param, |
525 false); | 541 false); |
(...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
725 // The SyncerThread should be waiting for the poll now. Nudge it to sync | 741 // The SyncerThread should be waiting for the poll now. Nudge it to sync |
726 // immediately (5ms). | 742 // immediately (5ms). |
727 syncable::ModelTypeBitSet model_types; | 743 syncable::ModelTypeBitSet model_types; |
728 model_types[syncable::BOOKMARKS] = true; | 744 model_types[syncable::BOOKMARKS] = true; |
729 | 745 |
730 // Paused so we can verify the nudge types safely. | 746 // Paused so we can verify the nudge types safely. |
731 syncer_thread()->RequestPause(); | 747 syncer_thread()->RequestPause(); |
732 syncer_thread()->NudgeSyncerWithDataTypes(5, | 748 syncer_thread()->NudgeSyncerWithDataTypes(5, |
733 SyncerThread::kUnknown, | 749 SyncerThread::kUnknown, |
734 model_types); | 750 model_types); |
735 EXPECT_EQ(model_types, syncer_thread()->vault_.pending_nudge_types_); | 751 EXPECT_TRUE(CompareNudgeTypesBitSetToVault(model_types)); |
736 syncer_thread()->RequestResume(); | 752 syncer_thread()->RequestResume(); |
737 | 753 |
738 interceptor.WaitForSyncShare(1, TimeDelta::FromSeconds(1)); | 754 interceptor.WaitForSyncShare(1, TimeDelta::FromSeconds(1)); |
739 EXPECT_EQ(static_cast<unsigned int>(2), | 755 EXPECT_EQ(static_cast<unsigned int>(2), |
740 interceptor.times_sync_occured().size()); | 756 interceptor.times_sync_occured().size()); |
741 | 757 |
742 // SyncerThread should be waiting again. Signal it to stop. | 758 // SyncerThread should be waiting again. Signal it to stop. |
743 EXPECT_TRUE(syncer_thread()->Stop(2000)); | 759 EXPECT_TRUE(syncer_thread()->Stop(2000)); |
744 EXPECT_TRUE(syncer_thread()->vault_.pending_nudge_types_.none()); | 760 EXPECT_TRUE(syncer_thread()->vault_.pending_nudge_types_.empty()); |
745 } | 761 } |
746 | 762 |
747 TEST_F(SyncerThreadWithSyncerTest, NudgeWithDataTypesCoalesced) { | 763 TEST_F(SyncerThreadWithSyncerTest, NudgeWithDataTypesCoalesced) { |
748 SyncShareIntercept interceptor; | 764 SyncShareIntercept interceptor; |
749 connection()->SetMidCommitObserver(&interceptor); | 765 connection()->SetMidCommitObserver(&interceptor); |
750 // We don't want a poll to happen during this test (except the first one). | 766 // We don't want a poll to happen during this test (except the first one). |
751 PreventThreadFromPolling(); | 767 PreventThreadFromPolling(); |
752 EXPECT_TRUE(syncer_thread()->Start()); | 768 EXPECT_TRUE(syncer_thread()->Start()); |
753 metadb()->Open(); | 769 metadb()->Open(); |
754 syncer_thread()->CreateSyncer(metadb()->name()); | 770 syncer_thread()->CreateSyncer(metadb()->name()); |
755 const TimeDelta poll_interval = TimeDelta::FromMinutes(5); | 771 const TimeDelta poll_interval = TimeDelta::FromMinutes(5); |
756 interceptor.WaitForSyncShare(1, poll_interval + poll_interval); | 772 interceptor.WaitForSyncShare(1, poll_interval + poll_interval); |
757 EXPECT_EQ(static_cast<unsigned int>(1), | 773 EXPECT_EQ(static_cast<unsigned int>(1), |
758 interceptor.times_sync_occured().size()); | 774 interceptor.times_sync_occured().size()); |
759 | 775 |
760 // The SyncerThread should be waiting for the poll now. Nudge it to sync | 776 // The SyncerThread should be waiting for the poll now. Nudge it to sync |
761 // immediately (5ms). | 777 // immediately (5ms). |
762 syncable::ModelTypeBitSet model_types; | 778 syncable::ModelTypeBitSet model_types; |
763 model_types[syncable::BOOKMARKS] = true; | 779 model_types[syncable::BOOKMARKS] = true; |
764 | 780 |
765 // Paused so we can verify the nudge types safely. | 781 // Paused so we can verify the nudge types safely. |
766 syncer_thread()->RequestPause(); | 782 syncer_thread()->RequestPause(); |
767 syncer_thread()->NudgeSyncerWithDataTypes(100, | 783 syncer_thread()->NudgeSyncerWithDataTypes(100, |
768 SyncerThread::kUnknown, | 784 SyncerThread::kUnknown, |
769 model_types); | 785 model_types); |
770 EXPECT_EQ(model_types, syncer_thread()->vault_.pending_nudge_types_); | 786 EXPECT_TRUE(CompareNudgeTypesBitSetToVault(model_types)); |
771 | 787 |
772 model_types[syncable::BOOKMARKS] = false; | 788 model_types[syncable::BOOKMARKS] = false; |
773 model_types[syncable::AUTOFILL] = true; | 789 model_types[syncable::AUTOFILL] = true; |
774 syncer_thread()->NudgeSyncerWithDataTypes(0, | 790 syncer_thread()->NudgeSyncerWithDataTypes(0, |
775 SyncerThread::kUnknown, | 791 SyncerThread::kUnknown, |
776 model_types); | 792 model_types); |
777 | 793 |
778 // Reset BOOKMARKS for expectations. | 794 // Reset BOOKMARKS for expectations. |
779 model_types[syncable::BOOKMARKS] = true; | 795 model_types[syncable::BOOKMARKS] = true; |
780 EXPECT_EQ(model_types, syncer_thread()->vault_.pending_nudge_types_); | 796 EXPECT_TRUE(CompareNudgeTypesBitSetToVault(model_types)); |
781 | 797 |
782 syncer_thread()->RequestResume(); | 798 syncer_thread()->RequestResume(); |
783 | 799 |
784 interceptor.WaitForSyncShare(1, TimeDelta::FromSeconds(1)); | 800 interceptor.WaitForSyncShare(1, TimeDelta::FromSeconds(1)); |
785 EXPECT_EQ(static_cast<unsigned int>(2), | 801 EXPECT_EQ(static_cast<unsigned int>(2), |
786 interceptor.times_sync_occured().size()); | 802 interceptor.times_sync_occured().size()); |
787 | 803 |
788 // SyncerThread should be waiting again. Signal it to stop. | 804 // SyncerThread should be waiting again. Signal it to stop. |
789 EXPECT_TRUE(syncer_thread()->Stop(2000)); | 805 EXPECT_TRUE(syncer_thread()->Stop(2000)); |
790 EXPECT_TRUE(syncer_thread()->vault_.pending_nudge_types_.none()); | 806 EXPECT_TRUE(syncer_thread()->vault_.pending_nudge_types_.empty()); |
| 807 } |
| 808 |
| 809 TEST_F(SyncerThreadWithSyncerTest, NudgeWithPayloads) { |
| 810 SyncShareIntercept interceptor; |
| 811 connection()->SetMidCommitObserver(&interceptor); |
| 812 // We don't want a poll to happen during this test (except the first one). |
| 813 PreventThreadFromPolling(); |
| 814 EXPECT_TRUE(syncer_thread()->Start()); |
| 815 metadb()->Open(); |
| 816 syncer_thread()->CreateSyncer(metadb()->name()); |
| 817 const TimeDelta poll_interval = TimeDelta::FromMinutes(5); |
| 818 interceptor.WaitForSyncShare(1, poll_interval + poll_interval); |
| 819 EXPECT_EQ(static_cast<unsigned int>(1), |
| 820 interceptor.times_sync_occured().size()); |
| 821 |
| 822 // The SyncerThread should be waiting for the poll now. Nudge it to sync |
| 823 // immediately (5ms). |
| 824 sessions::TypePayloadMap nudge_types; |
| 825 nudge_types[syncable::BOOKMARKS] = "test"; |
| 826 |
| 827 // Paused so we can verify the nudge types safely. |
| 828 syncer_thread()->RequestPause(); |
| 829 syncer_thread()->NudgeSyncerWithPayloads(5, |
| 830 SyncerThread::kUnknown, |
| 831 nudge_types); |
| 832 EXPECT_TRUE(CompareNudgeTypesToVault(nudge_types)); |
| 833 syncer_thread()->RequestResume(); |
| 834 |
| 835 interceptor.WaitForSyncShare(1, TimeDelta::FromSeconds(1)); |
| 836 EXPECT_EQ(static_cast<unsigned int>(2), |
| 837 interceptor.times_sync_occured().size()); |
| 838 |
| 839 // SyncerThread should be waiting again. Signal it to stop. |
| 840 EXPECT_TRUE(syncer_thread()->Stop(2000)); |
| 841 EXPECT_TRUE(syncer_thread()->vault_.pending_nudge_types_.empty()); |
| 842 } |
| 843 |
| 844 TEST_F(SyncerThreadWithSyncerTest, NudgeWithPayloadsCoalesced) { |
| 845 SyncShareIntercept interceptor; |
| 846 connection()->SetMidCommitObserver(&interceptor); |
| 847 // We don't want a poll to happen during this test (except the first one). |
| 848 PreventThreadFromPolling(); |
| 849 EXPECT_TRUE(syncer_thread()->Start()); |
| 850 metadb()->Open(); |
| 851 syncer_thread()->CreateSyncer(metadb()->name()); |
| 852 const TimeDelta poll_interval = TimeDelta::FromMinutes(5); |
| 853 interceptor.WaitForSyncShare(1, poll_interval + poll_interval); |
| 854 EXPECT_EQ(static_cast<unsigned int>(1), |
| 855 interceptor.times_sync_occured().size()); |
| 856 |
| 857 // The SyncerThread should be waiting for the poll now. Nudge it to sync |
| 858 // immediately (5ms). |
| 859 sessions::TypePayloadMap nudge_types; |
| 860 nudge_types[syncable::BOOKMARKS] = "books"; |
| 861 |
| 862 // Paused so we can verify the nudge types safely. |
| 863 syncer_thread()->RequestPause(); |
| 864 syncer_thread()->NudgeSyncerWithPayloads(100, |
| 865 SyncerThread::kUnknown, |
| 866 nudge_types); |
| 867 EXPECT_TRUE(CompareNudgeTypesToVault(nudge_types)); |
| 868 |
| 869 nudge_types.erase(syncable::BOOKMARKS); |
| 870 nudge_types[syncable::AUTOFILL] = "auto"; |
| 871 syncer_thread()->NudgeSyncerWithPayloads(0, |
| 872 SyncerThread::kUnknown, |
| 873 nudge_types); |
| 874 |
| 875 // Reset BOOKMARKS for expectations. |
| 876 nudge_types[syncable::BOOKMARKS] = "books"; |
| 877 EXPECT_TRUE(CompareNudgeTypesToVault(nudge_types)); |
| 878 |
| 879 syncer_thread()->RequestResume(); |
| 880 |
| 881 interceptor.WaitForSyncShare(1, TimeDelta::FromSeconds(1)); |
| 882 EXPECT_EQ(static_cast<unsigned int>(2), |
| 883 interceptor.times_sync_occured().size()); |
| 884 |
| 885 // SyncerThread should be waiting again. Signal it to stop. |
| 886 EXPECT_TRUE(syncer_thread()->Stop(2000)); |
| 887 EXPECT_TRUE(syncer_thread()->vault_.pending_nudge_types_.empty()); |
791 } | 888 } |
792 | 889 |
793 TEST_F(SyncerThreadWithSyncerTest, Throttling) { | 890 TEST_F(SyncerThreadWithSyncerTest, Throttling) { |
794 SyncShareIntercept interceptor; | 891 SyncShareIntercept interceptor; |
795 connection()->SetMidCommitObserver(&interceptor); | 892 connection()->SetMidCommitObserver(&interceptor); |
796 const TimeDelta poll_interval = TimeDelta::FromMilliseconds(10); | 893 const TimeDelta poll_interval = TimeDelta::FromMilliseconds(10); |
797 syncer_thread()->SetSyncerShortPollInterval(poll_interval); | 894 syncer_thread()->SetSyncerShortPollInterval(poll_interval); |
798 | 895 |
799 EXPECT_TRUE(syncer_thread()->Start()); | 896 EXPECT_TRUE(syncer_thread()->Start()); |
800 metadb()->Open(); | 897 metadb()->Open(); |
(...skipping 325 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1126 EXPECT_CALL(listener, OnSyncEngineEvent( | 1223 EXPECT_CALL(listener, OnSyncEngineEvent( |
1127 Field(&SyncEngineEvent::what_happened, | 1224 Field(&SyncEngineEvent::what_happened, |
1128 SyncEngineEvent::SYNCER_THREAD_EXITING))); | 1225 SyncEngineEvent::SYNCER_THREAD_EXITING))); |
1129 | 1226 |
1130 ASSERT_TRUE(Resume(&listener)); | 1227 ASSERT_TRUE(Resume(&listener)); |
1131 ASSERT_TRUE(sync_cycle_ended_event.TimedWait(max_wait_time_)); | 1228 ASSERT_TRUE(sync_cycle_ended_event.TimedWait(max_wait_time_)); |
1132 EXPECT_TRUE(syncer_thread()->Stop(2000)); | 1229 EXPECT_TRUE(syncer_thread()->Stop(2000)); |
1133 } | 1230 } |
1134 | 1231 |
1135 } // namespace browser_sync | 1232 } // namespace browser_sync |
OLD | NEW |