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