OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "components/sync/engine_impl/sync_scheduler_impl.h" | 5 #include "components/sync/engine_impl/sync_scheduler_impl.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <vector> | 10 #include <vector> |
(...skipping 262 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
273 | 273 |
274 void NewSchedulerForLocalBackend() { | 274 void NewSchedulerForLocalBackend() { |
275 // The old syncer is destroyed with the scheduler that owns it. | 275 // The old syncer is destroyed with the scheduler that owns it. |
276 syncer_ = new testing::StrictMock<MockSyncer>(); | 276 syncer_ = new testing::StrictMock<MockSyncer>(); |
277 scheduler_ = base::MakeUnique<SyncSchedulerImpl>( | 277 scheduler_ = base::MakeUnique<SyncSchedulerImpl>( |
278 "TestSyncScheduler", BackoffDelayProvider::FromDefaults(), context(), | 278 "TestSyncScheduler", BackoffDelayProvider::FromDefaults(), context(), |
279 syncer_, true); | 279 syncer_, true); |
280 scheduler_->SetDefaultNudgeDelay(default_delay()); | 280 scheduler_->SetDefaultNudgeDelay(default_delay()); |
281 } | 281 } |
282 | 282 |
| 283 bool BlockTimerIsRunning() const { |
| 284 return scheduler_->pending_wakeup_timer_.IsRunning(); |
| 285 } |
| 286 |
283 private: | 287 private: |
284 syncable::Directory* directory() { | 288 syncable::Directory* directory() { |
285 return test_user_share_.user_share()->directory.get(); | 289 return test_user_share_.user_share()->directory.get(); |
286 } | 290 } |
287 | 291 |
288 base::MessageLoop loop_; | 292 base::MessageLoop loop_; |
289 TestUserShare test_user_share_; | 293 TestUserShare test_user_share_; |
290 CancelationSignal cancelation_signal_; | 294 CancelationSignal cancelation_signal_; |
291 std::unique_ptr<MockConnectionManager> connection_; | 295 std::unique_ptr<MockConnectionManager> connection_; |
292 std::unique_ptr<ModelTypeRegistry> model_type_registry_; | 296 std::unique_ptr<ModelTypeRegistry> model_type_registry_; |
(...skipping 702 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
995 EXPECT_CALL(*syncer(), NormalSyncShare(_, _, _)) | 999 EXPECT_CALL(*syncer(), NormalSyncShare(_, _, _)) |
996 .WillOnce(DoAll(WithArg<2>(test_util::SimulatePartialFailure(types)), | 1000 .WillOnce(DoAll(WithArg<2>(test_util::SimulatePartialFailure(types)), |
997 Return(true))) | 1001 Return(true))) |
998 .RetiresOnSaturation(); | 1002 .RetiresOnSaturation(); |
999 | 1003 |
1000 StartSyncScheduler(base::Time()); | 1004 StartSyncScheduler(base::Time()); |
1001 scheduler()->ScheduleLocalNudge(types, FROM_HERE); | 1005 scheduler()->ScheduleLocalNudge(types, FROM_HERE); |
1002 PumpLoop(); // To get PerformDelayedNudge called. | 1006 PumpLoop(); // To get PerformDelayedNudge called. |
1003 PumpLoop(); // To get TrySyncCycleJob called | 1007 PumpLoop(); // To get TrySyncCycleJob called |
1004 EXPECT_TRUE(GetBackedOffTypes().HasAll(types)); | 1008 EXPECT_TRUE(GetBackedOffTypes().HasAll(types)); |
| 1009 EXPECT_TRUE(BlockTimerIsRunning()); |
1005 EXPECT_FALSE(scheduler()->IsBackingOff()); | 1010 EXPECT_FALSE(scheduler()->IsBackingOff()); |
1006 EXPECT_FALSE(scheduler()->IsCurrentlyThrottled()); | 1011 EXPECT_FALSE(scheduler()->IsCurrentlyThrottled()); |
1007 | 1012 |
1008 TimeDelta throttle1(TimeDelta::FromMilliseconds(150)); | 1013 TimeDelta throttle1(TimeDelta::FromMilliseconds(150)); |
1009 | 1014 |
1010 EXPECT_CALL(*syncer(), NormalSyncShare(_, _, _)) | 1015 EXPECT_CALL(*syncer(), NormalSyncShare(_, _, _)) |
1011 .WillOnce(DoAll(WithArg<2>(test_util::SimulateThrottled(throttle1)), | 1016 .WillOnce(DoAll(WithArg<2>(test_util::SimulateThrottled(throttle1)), |
1012 Return(false))) | 1017 Return(false))) |
1013 .RetiresOnSaturation(); | 1018 .RetiresOnSaturation(); |
1014 | 1019 |
1015 // Sync still can throttle. | 1020 // Sync still can throttle. |
1016 const ModelTypeSet unbacked_off_types(TYPED_URLS); | 1021 const ModelTypeSet unbacked_off_types(TYPED_URLS); |
1017 scheduler()->ScheduleLocalNudge(unbacked_off_types, FROM_HERE); | 1022 scheduler()->ScheduleLocalNudge(unbacked_off_types, FROM_HERE); |
1018 PumpLoop(); // TO get TypesUnblock called. | 1023 PumpLoop(); // TO get TypesUnblock called. |
1019 PumpLoop(); // To get TrySyncCycleJob called. | 1024 PumpLoop(); // To get TrySyncCycleJob called. |
1020 | 1025 |
1021 EXPECT_TRUE(GetBackedOffTypes().HasAll(types)); | 1026 EXPECT_TRUE(GetBackedOffTypes().HasAll(types)); |
| 1027 EXPECT_TRUE(BlockTimerIsRunning()); |
1022 EXPECT_FALSE(scheduler()->IsBackingOff()); | 1028 EXPECT_FALSE(scheduler()->IsBackingOff()); |
1023 EXPECT_TRUE(scheduler()->IsCurrentlyThrottled()); | 1029 EXPECT_TRUE(scheduler()->IsCurrentlyThrottled()); |
1024 | 1030 |
| 1031 // Unthrottled client, but the backingoff datatype is still in backoff and |
| 1032 // scheduled. |
| 1033 EXPECT_CALL(*syncer(), NormalSyncShare(_, _, _)) |
| 1034 .WillOnce(DoAll(Invoke(test_util::SimulateNormalSuccess), |
| 1035 QuitLoopNowAction(true))); |
| 1036 RunLoop(); |
| 1037 EXPECT_FALSE(scheduler()->IsCurrentlyThrottled()); |
| 1038 EXPECT_TRUE(GetBackedOffTypes().HasAll(types)); |
| 1039 EXPECT_TRUE(BlockTimerIsRunning()); |
| 1040 |
1025 StopSyncScheduler(); | 1041 StopSyncScheduler(); |
1026 } | 1042 } |
1027 | 1043 |
1028 TEST_F(SyncSchedulerImplTest, TypeThrottlingBackingOffBlocksNudge) { | 1044 TEST_F(SyncSchedulerImplTest, TypeThrottlingBackingOffBlocksNudge) { |
1029 UseMockDelayProvider(); | 1045 UseMockDelayProvider(); |
1030 EXPECT_CALL(*delay(), GetDelay(_)).WillRepeatedly(Return(long_delay())); | 1046 EXPECT_CALL(*delay(), GetDelay(_)).WillRepeatedly(Return(long_delay())); |
1031 | 1047 |
1032 TimeDelta poll(TimeDelta::FromDays(1)); | 1048 TimeDelta poll(TimeDelta::FromDays(1)); |
1033 TimeDelta throttle(TimeDelta::FromSeconds(60)); | 1049 TimeDelta throttle(TimeDelta::FromSeconds(60)); |
1034 scheduler()->OnReceivedLongPollIntervalUpdate(poll); | 1050 scheduler()->OnReceivedLongPollIntervalUpdate(poll); |
(...skipping 20 matching lines...) Expand all Loading... |
1055 Return(true))) | 1071 Return(true))) |
1056 .RetiresOnSaturation(); | 1072 .RetiresOnSaturation(); |
1057 | 1073 |
1058 scheduler()->ScheduleLocalNudge(backed_off_types, FROM_HERE); | 1074 scheduler()->ScheduleLocalNudge(backed_off_types, FROM_HERE); |
1059 | 1075 |
1060 PumpLoop(); // To get PerformDelayedNudge called. | 1076 PumpLoop(); // To get PerformDelayedNudge called. |
1061 PumpLoop(); // To get TrySyncCycleJob called | 1077 PumpLoop(); // To get TrySyncCycleJob called |
1062 | 1078 |
1063 EXPECT_TRUE(GetThrottledTypes().HasAll(throttled_types)); | 1079 EXPECT_TRUE(GetThrottledTypes().HasAll(throttled_types)); |
1064 EXPECT_TRUE(GetBackedOffTypes().HasAll(backed_off_types)); | 1080 EXPECT_TRUE(GetBackedOffTypes().HasAll(backed_off_types)); |
| 1081 EXPECT_TRUE(BlockTimerIsRunning()); |
1065 EXPECT_FALSE(scheduler()->IsBackingOff()); | 1082 EXPECT_FALSE(scheduler()->IsBackingOff()); |
1066 EXPECT_FALSE(scheduler()->IsCurrentlyThrottled()); | 1083 EXPECT_FALSE(scheduler()->IsCurrentlyThrottled()); |
1067 | 1084 |
1068 // This won't cause a sync cycle because the types are throttled or backed | 1085 // This won't cause a sync cycle because the types are throttled or backed |
1069 // off. | 1086 // off. |
1070 scheduler()->ScheduleLocalNudge(Union(throttled_types, backed_off_types), | 1087 scheduler()->ScheduleLocalNudge(Union(throttled_types, backed_off_types), |
1071 FROM_HERE); | 1088 FROM_HERE); |
1072 PumpLoop(); | 1089 PumpLoop(); |
1073 | 1090 |
1074 StopSyncScheduler(); | 1091 StopSyncScheduler(); |
(...skipping 678 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1753 base::TimeDelta second_blocking_time = GetTypeBlockingTime(THEMES); | 1770 base::TimeDelta second_blocking_time = GetTypeBlockingTime(THEMES); |
1754 | 1771 |
1755 // The Exponential backoff should be between previous backoff 1.5 and 2.5 | 1772 // The Exponential backoff should be between previous backoff 1.5 and 2.5 |
1756 // times. | 1773 // times. |
1757 EXPECT_LE(first_blocking_time * 1.5, second_blocking_time); | 1774 EXPECT_LE(first_blocking_time * 1.5, second_blocking_time); |
1758 EXPECT_GE(first_blocking_time * 2.5, second_blocking_time); | 1775 EXPECT_GE(first_blocking_time * 2.5, second_blocking_time); |
1759 | 1776 |
1760 StopSyncScheduler(); | 1777 StopSyncScheduler(); |
1761 } | 1778 } |
1762 | 1779 |
| 1780 // If a datatype is in backoff or throttling, pending_wakeup_timer_ should |
| 1781 // schedule a delay job for OnTypesUnblocked. SyncScheduler sometimes use |
| 1782 // pending_wakeup_timer_ to schdule PerformDelayedNudge job before |
| 1783 // OnTypesUnblocked got run. This test will verify after ran |
| 1784 // PerformDelayedNudge, OnTypesUnblocked will be rescheduled if any datatype is |
| 1785 // in backoff or throttling. |
| 1786 TEST_F(SyncSchedulerImplTest, TypeBackoffAndSuccessfulSync) { |
| 1787 UseMockDelayProvider(); |
| 1788 EXPECT_CALL(*delay(), GetDelay(_)).WillRepeatedly(Return(long_delay())); |
| 1789 |
| 1790 TimeDelta poll(TimeDelta::FromDays(1)); |
| 1791 scheduler()->OnReceivedLongPollIntervalUpdate(poll); |
| 1792 |
| 1793 const ModelTypeSet types(THEMES); |
| 1794 |
| 1795 // Set backoff datatype. |
| 1796 ::testing::InSequence seq; |
| 1797 EXPECT_CALL(*syncer(), NormalSyncShare(_, _, _)) |
| 1798 .WillOnce(DoAll(WithArg<2>(test_util::SimulatePartialFailure(types)), |
| 1799 Return(true))) |
| 1800 .RetiresOnSaturation(); |
| 1801 |
| 1802 StartSyncScheduler(base::Time()); |
| 1803 scheduler()->ScheduleLocalNudge(types, FROM_HERE); |
| 1804 PumpLoop(); // To get PerformDelayedNudge called. |
| 1805 PumpLoop(); // To get TrySyncCycleJob called |
| 1806 EXPECT_TRUE(GetBackedOffTypes().HasAll(types)); |
| 1807 EXPECT_TRUE(BlockTimerIsRunning()); |
| 1808 EXPECT_FALSE(scheduler()->IsBackingOff()); |
| 1809 EXPECT_FALSE(scheduler()->IsCurrentlyThrottled()); |
| 1810 |
| 1811 SyncShareTimes times; |
| 1812 EXPECT_CALL(*syncer(), NormalSyncShare(_, _, _)) |
| 1813 .WillOnce(DoAll(Invoke(test_util::SimulateNormalSuccess), |
| 1814 RecordSyncShare(×, true))) |
| 1815 .RetiresOnSaturation(); |
| 1816 |
| 1817 // Do a successful Sync. |
| 1818 const ModelTypeSet unbacked_off_types(TYPED_URLS); |
| 1819 scheduler()->ScheduleLocalNudge(unbacked_off_types, FROM_HERE); |
| 1820 PumpLoop(); // TO get PerformDelayedNudge called. |
| 1821 PumpLoop(); // To get TrySyncCycleJob called. |
| 1822 |
| 1823 // Timer is still running for backoff datatype after Sync success. |
| 1824 EXPECT_TRUE(GetBackedOffTypes().HasAll(types)); |
| 1825 EXPECT_TRUE(BlockTimerIsRunning()); |
| 1826 EXPECT_FALSE(scheduler()->IsBackingOff()); |
| 1827 EXPECT_FALSE(scheduler()->IsCurrentlyThrottled()); |
| 1828 |
| 1829 StopSyncScheduler(); |
| 1830 } |
| 1831 |
| 1832 // Verify that the timer is scheduled for an unblock job after one datatype is |
| 1833 // unblocked, and there is another one still blocked. |
| 1834 TEST_F(SyncSchedulerImplTest, TypeBackingOffAndFailureSync) { |
| 1835 UseMockDelayProvider(); |
| 1836 EXPECT_CALL(*delay(), GetDelay(_)) |
| 1837 .WillOnce(Return(long_delay())) |
| 1838 .RetiresOnSaturation(); |
| 1839 |
| 1840 TimeDelta poll(TimeDelta::FromDays(1)); |
| 1841 scheduler()->OnReceivedLongPollIntervalUpdate(poll); |
| 1842 |
| 1843 // Set a backoff datatype. |
| 1844 const ModelTypeSet themes_types(THEMES); |
| 1845 ::testing::InSequence seq; |
| 1846 EXPECT_CALL(*syncer(), NormalSyncShare(_, _, _)) |
| 1847 .WillOnce( |
| 1848 DoAll(WithArg<2>(test_util::SimulatePartialFailure(themes_types)), |
| 1849 Return(true))) |
| 1850 .RetiresOnSaturation(); |
| 1851 |
| 1852 StartSyncScheduler(base::Time()); |
| 1853 scheduler()->ScheduleLocalNudge(themes_types, FROM_HERE); |
| 1854 PumpLoop(); // To get PerformDelayedNudge called. |
| 1855 PumpLoop(); // To get TrySyncCycleJob called |
| 1856 EXPECT_TRUE(GetBackedOffTypes().HasAll(themes_types)); |
| 1857 EXPECT_TRUE(BlockTimerIsRunning()); |
| 1858 EXPECT_FALSE(scheduler()->IsBackingOff()); |
| 1859 EXPECT_FALSE(scheduler()->IsCurrentlyThrottled()); |
| 1860 |
| 1861 // Set anther backoff datatype. |
| 1862 const ModelTypeSet typed_urls_types(TYPED_URLS); |
| 1863 EXPECT_CALL(*syncer(), NormalSyncShare(_, _, _)) |
| 1864 .WillOnce( |
| 1865 DoAll(WithArg<2>(test_util::SimulatePartialFailure(typed_urls_types)), |
| 1866 Return(true))) |
| 1867 .RetiresOnSaturation(); |
| 1868 EXPECT_CALL(*delay(), GetDelay(_)) |
| 1869 .WillOnce(Return(default_delay())) |
| 1870 .RetiresOnSaturation(); |
| 1871 |
| 1872 scheduler()->ScheduleLocalNudge(typed_urls_types, FROM_HERE); |
| 1873 PumpLoop(); // TO get PerformDelayedNudge called. |
| 1874 PumpLoop(); // To get TrySyncCycleJob called. |
| 1875 |
| 1876 EXPECT_TRUE(GetBackedOffTypes().HasAll(themes_types)); |
| 1877 EXPECT_TRUE(GetBackedOffTypes().HasAll(typed_urls_types)); |
| 1878 EXPECT_TRUE(BlockTimerIsRunning()); |
| 1879 EXPECT_FALSE(scheduler()->IsBackingOff()); |
| 1880 EXPECT_FALSE(scheduler()->IsCurrentlyThrottled()); |
| 1881 |
| 1882 // Unblock one datatype. |
| 1883 SyncShareTimes times; |
| 1884 EXPECT_CALL(*syncer(), NormalSyncShare(_, _, _)) |
| 1885 .WillRepeatedly(DoAll(Invoke(test_util::SimulateNormalSuccess), |
| 1886 RecordSyncShare(×, true))); |
| 1887 EXPECT_CALL(*delay(), GetDelay(_)).WillRepeatedly(Return(long_delay())); |
| 1888 |
| 1889 PumpLoop(); // TO get OnTypesUnblocked called. |
| 1890 PumpLoop(); // To get TrySyncCycleJob called. |
| 1891 |
| 1892 // Timer is still scheduled for another backoff datatype. |
| 1893 EXPECT_TRUE(GetBackedOffTypes().HasAll(themes_types)); |
| 1894 EXPECT_FALSE(GetBackedOffTypes().HasAll(typed_urls_types)); |
| 1895 EXPECT_TRUE(BlockTimerIsRunning()); |
| 1896 EXPECT_FALSE(scheduler()->IsBackingOff()); |
| 1897 EXPECT_FALSE(scheduler()->IsCurrentlyThrottled()); |
| 1898 |
| 1899 StopSyncScheduler(); |
| 1900 } |
| 1901 |
1763 } // namespace syncer | 1902 } // namespace syncer |
OLD | NEW |