| 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 "base/bind.h" | 5 #include "base/bind.h" |
| 6 #include "base/callback.h" | 6 #include "base/callback.h" |
| 7 #include "base/compiler_specific.h" | 7 #include "base/compiler_specific.h" |
| 8 #include "base/memory/weak_ptr.h" | 8 #include "base/memory/weak_ptr.h" |
| 9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
| 10 #include "base/test/test_timeouts.h" | 10 #include "base/test/test_timeouts.h" |
| 11 #include "sync/engine/backoff_delay_provider.h" |
| 11 #include "sync/engine/sync_scheduler_impl.h" | 12 #include "sync/engine/sync_scheduler_impl.h" |
| 12 #include "sync/engine/syncer.h" | 13 #include "sync/engine/syncer.h" |
| 13 #include "sync/engine/throttled_data_type_tracker.h" | 14 #include "sync/engine/throttled_data_type_tracker.h" |
| 14 #include "sync/sessions/test_util.h" | 15 #include "sync/sessions/test_util.h" |
| 15 #include "sync/test/callback_counter.h" | 16 #include "sync/test/callback_counter.h" |
| 16 #include "sync/test/engine/fake_model_worker.h" | 17 #include "sync/test/engine/fake_model_worker.h" |
| 17 #include "sync/test/engine/mock_connection_manager.h" | 18 #include "sync/test/engine/mock_connection_manager.h" |
| 18 #include "sync/test/engine/test_directory_setter_upper.h" | 19 #include "sync/test/engine/test_directory_setter_upper.h" |
| 19 #include "sync/test/fake_extensions_activity_monitor.h" | 20 #include "sync/test/fake_extensions_activity_monitor.h" |
| 20 #include "testing/gmock/include/gmock/gmock.h" | 21 #include "testing/gmock/include/gmock/gmock.h" |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 81 // Convenient to use in tests wishing to analyze SyncShare calls over time. | 82 // Convenient to use in tests wishing to analyze SyncShare calls over time. |
| 82 static const size_t kMinNumSamples = 5; | 83 static const size_t kMinNumSamples = 5; |
| 83 class SyncSchedulerTest : public testing::Test { | 84 class SyncSchedulerTest : public testing::Test { |
| 84 public: | 85 public: |
| 85 SyncSchedulerTest() | 86 SyncSchedulerTest() |
| 86 : weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)), | 87 : weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)), |
| 87 context_(NULL), | 88 context_(NULL), |
| 88 syncer_(NULL), | 89 syncer_(NULL), |
| 89 delay_(NULL) {} | 90 delay_(NULL) {} |
| 90 | 91 |
| 91 class MockDelayProvider : public SyncSchedulerImpl::DelayProvider { | 92 class MockDelayProvider : public BackoffDelayProvider { |
| 92 public: | 93 public: |
| 94 MockDelayProvider() : BackoffDelayProvider( |
| 95 TimeDelta::FromSeconds(kInitialBackoffRetrySeconds), |
| 96 TimeDelta::FromSeconds(kInitialBackoffShortRetrySeconds)) { |
| 97 } |
| 98 |
| 93 MOCK_METHOD1(GetDelay, TimeDelta(const TimeDelta&)); | 99 MOCK_METHOD1(GetDelay, TimeDelta(const TimeDelta&)); |
| 94 }; | 100 }; |
| 95 | 101 |
| 96 virtual void SetUp() { | 102 virtual void SetUp() { |
| 97 dir_maker_.SetUp(); | 103 dir_maker_.SetUp(); |
| 98 syncer_ = new MockSyncer(); | 104 syncer_ = new MockSyncer(); |
| 99 delay_ = NULL; | 105 delay_ = NULL; |
| 100 | 106 |
| 101 ModelSafeRoutingInfo routing_info; | 107 ModelSafeRoutingInfo routing_info; |
| 102 routing_info[BOOKMARKS] = GROUP_UI; | 108 routing_info[BOOKMARKS] = GROUP_UI; |
| (...skipping 16 matching lines...) Expand all Loading... |
| 119 throttled_data_type_tracker_.reset(new ThrottledDataTypeTracker(NULL)); | 125 throttled_data_type_tracker_.reset(new ThrottledDataTypeTracker(NULL)); |
| 120 context_.reset(new SyncSessionContext( | 126 context_.reset(new SyncSessionContext( |
| 121 connection_.get(), directory(), workers, | 127 connection_.get(), directory(), workers, |
| 122 &extensions_activity_monitor_, throttled_data_type_tracker_.get(), | 128 &extensions_activity_monitor_, throttled_data_type_tracker_.get(), |
| 123 std::vector<SyncEngineEventListener*>(), NULL, NULL, | 129 std::vector<SyncEngineEventListener*>(), NULL, NULL, |
| 124 true /* enable keystore encryption */)); | 130 true /* enable keystore encryption */)); |
| 125 context_->set_routing_info(routing_info); | 131 context_->set_routing_info(routing_info); |
| 126 context_->set_notifications_enabled(true); | 132 context_->set_notifications_enabled(true); |
| 127 context_->set_account_name("Test"); | 133 context_->set_account_name("Test"); |
| 128 scheduler_.reset( | 134 scheduler_.reset( |
| 129 new SyncSchedulerImpl("TestSyncScheduler", context(), syncer_)); | 135 new SyncSchedulerImpl("TestSyncScheduler", |
| 136 BackoffDelayProvider::FromDefaults(), |
| 137 context(), |
| 138 syncer_)); |
| 130 } | 139 } |
| 131 | 140 |
| 132 SyncSchedulerImpl* scheduler() { return scheduler_.get(); } | 141 SyncSchedulerImpl* scheduler() { return scheduler_.get(); } |
| 133 MockSyncer* syncer() { return syncer_; } | 142 MockSyncer* syncer() { return syncer_; } |
| 134 MockDelayProvider* delay() { return delay_; } | 143 MockDelayProvider* delay() { return delay_; } |
| 135 MockConnectionManager* connection() { return connection_.get(); } | 144 MockConnectionManager* connection() { return connection_.get(); } |
| 136 TimeDelta zero() { return TimeDelta::FromSeconds(0); } | 145 TimeDelta zero() { return TimeDelta::FromSeconds(0); } |
| 137 TimeDelta timeout() { | 146 TimeDelta timeout() { |
| 138 return TestTimeouts::action_timeout(); | 147 return TestTimeouts::action_timeout(); |
| 139 } | 148 } |
| (...skipping 812 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 952 zero(), NUDGE_SOURCE_LOCAL, ModelTypeSet(), FROM_HERE); | 961 zero(), NUDGE_SOURCE_LOCAL, ModelTypeSet(), FROM_HERE); |
| 953 RunLoop(); | 962 RunLoop(); |
| 954 | 963 |
| 955 ASSERT_EQ(kMinNumSamples, r.snapshots.size()); | 964 ASSERT_EQ(kMinNumSamples, r.snapshots.size()); |
| 956 EXPECT_GE(r.times[1] - r.times[0], second); | 965 EXPECT_GE(r.times[1] - r.times[0], second); |
| 957 EXPECT_GE(r.times[2] - r.times[1], third); | 966 EXPECT_GE(r.times[2] - r.times[1], third); |
| 958 EXPECT_GE(r.times[3] - r.times[2], fourth); | 967 EXPECT_GE(r.times[3] - r.times[2], fourth); |
| 959 EXPECT_GE(r.times[4] - r.times[3], fifth); | 968 EXPECT_GE(r.times[4] - r.times[3], fifth); |
| 960 } | 969 } |
| 961 | 970 |
| 962 TEST_F(SyncSchedulerTest, GetInitialBackoffDelay) { | |
| 963 sessions::ModelNeutralState state; | |
| 964 state.last_get_key_result = SYNC_SERVER_ERROR; | |
| 965 EXPECT_EQ(kInitialBackoffRetrySeconds, | |
| 966 scheduler()->GetInitialBackoffDelay(state).InSeconds()); | |
| 967 | |
| 968 state.last_get_key_result = UNSET; | |
| 969 state.last_download_updates_result = SERVER_RETURN_MIGRATION_DONE; | |
| 970 EXPECT_EQ(kInitialBackoffShortRetrySeconds, | |
| 971 scheduler()->GetInitialBackoffDelay(state).InSeconds()); | |
| 972 | |
| 973 state.last_download_updates_result = SERVER_RETURN_TRANSIENT_ERROR; | |
| 974 EXPECT_EQ(kInitialBackoffRetrySeconds, | |
| 975 scheduler()->GetInitialBackoffDelay(state).InSeconds()); | |
| 976 | |
| 977 state.last_download_updates_result = SERVER_RESPONSE_VALIDATION_FAILED; | |
| 978 EXPECT_EQ(kInitialBackoffRetrySeconds, | |
| 979 scheduler()->GetInitialBackoffDelay(state).InSeconds()); | |
| 980 | |
| 981 state.last_download_updates_result = SYNCER_OK; | |
| 982 // Note that updating credentials triggers a canary job, trumping | |
| 983 // the initial delay, but in theory we still expect this function to treat | |
| 984 // it like any other error in the system (except migration). | |
| 985 state.commit_result = SERVER_RETURN_INVALID_CREDENTIAL; | |
| 986 EXPECT_EQ(kInitialBackoffRetrySeconds, | |
| 987 scheduler()->GetInitialBackoffDelay(state).InSeconds()); | |
| 988 | |
| 989 state.commit_result = SERVER_RETURN_MIGRATION_DONE; | |
| 990 EXPECT_EQ(kInitialBackoffShortRetrySeconds, | |
| 991 scheduler()->GetInitialBackoffDelay(state).InSeconds()); | |
| 992 } | |
| 993 | |
| 994 // Test that things go back to normal once a retry makes forward progress. | 971 // Test that things go back to normal once a retry makes forward progress. |
| 995 TEST_F(SyncSchedulerTest, BackoffRelief) { | 972 TEST_F(SyncSchedulerTest, BackoffRelief) { |
| 996 SyncShareRecords r; | 973 SyncShareRecords r; |
| 997 const TimeDelta poll(TimeDelta::FromMilliseconds(10)); | 974 const TimeDelta poll(TimeDelta::FromMilliseconds(10)); |
| 998 scheduler()->OnReceivedLongPollIntervalUpdate(poll); | 975 scheduler()->OnReceivedLongPollIntervalUpdate(poll); |
| 999 UseMockDelayProvider(); | 976 UseMockDelayProvider(); |
| 1000 | 977 |
| 1001 const TimeDelta backoff = TimeDelta::FromMilliseconds(5); | 978 const TimeDelta backoff = TimeDelta::FromMilliseconds(5); |
| 1002 | 979 |
| 1003 EXPECT_CALL(*syncer(), SyncShare(_,_,_)) | 980 EXPECT_CALL(*syncer(), SyncShare(_,_,_)) |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1059 | 1036 |
| 1060 // Run the unsucessful poll. The failed poll should not trigger backoff. | 1037 // Run the unsucessful poll. The failed poll should not trigger backoff. |
| 1061 RunLoop(); | 1038 RunLoop(); |
| 1062 EXPECT_FALSE(scheduler()->IsBackingOff()); | 1039 EXPECT_FALSE(scheduler()->IsBackingOff()); |
| 1063 | 1040 |
| 1064 // Run the successful poll. | 1041 // Run the successful poll. |
| 1065 RunLoop(); | 1042 RunLoop(); |
| 1066 EXPECT_FALSE(scheduler()->IsBackingOff()); | 1043 EXPECT_FALSE(scheduler()->IsBackingOff()); |
| 1067 } | 1044 } |
| 1068 | 1045 |
| 1069 TEST_F(SyncSchedulerTest, GetRecommendedDelay) { | |
| 1070 EXPECT_LE(TimeDelta::FromSeconds(0), | |
| 1071 SyncSchedulerImpl::GetRecommendedDelay(TimeDelta::FromSeconds(0))); | |
| 1072 EXPECT_LE(TimeDelta::FromSeconds(1), | |
| 1073 SyncSchedulerImpl::GetRecommendedDelay(TimeDelta::FromSeconds(1))); | |
| 1074 EXPECT_LE(TimeDelta::FromSeconds(50), | |
| 1075 SyncSchedulerImpl::GetRecommendedDelay( | |
| 1076 TimeDelta::FromSeconds(50))); | |
| 1077 EXPECT_LE(TimeDelta::FromSeconds(10), | |
| 1078 SyncSchedulerImpl::GetRecommendedDelay( | |
| 1079 TimeDelta::FromSeconds(10))); | |
| 1080 EXPECT_EQ(TimeDelta::FromSeconds(kMaxBackoffSeconds), | |
| 1081 SyncSchedulerImpl::GetRecommendedDelay( | |
| 1082 TimeDelta::FromSeconds(kMaxBackoffSeconds))); | |
| 1083 EXPECT_EQ(TimeDelta::FromSeconds(kMaxBackoffSeconds), | |
| 1084 SyncSchedulerImpl::GetRecommendedDelay( | |
| 1085 TimeDelta::FromSeconds(kMaxBackoffSeconds + 1))); | |
| 1086 } | |
| 1087 | |
| 1088 // Test that appropriate syncer steps are requested for each job type. | 1046 // Test that appropriate syncer steps are requested for each job type. |
| 1089 TEST_F(SyncSchedulerTest, SyncerSteps) { | 1047 TEST_F(SyncSchedulerTest, SyncerSteps) { |
| 1090 // Nudges. | 1048 // Nudges. |
| 1091 EXPECT_CALL(*syncer(), SyncShare(_, SYNCER_BEGIN, SYNCER_END)) | 1049 EXPECT_CALL(*syncer(), SyncShare(_, SYNCER_BEGIN, SYNCER_END)) |
| 1092 .WillOnce(Invoke(sessions::test_util::SimulateSuccess)); | 1050 .WillOnce(Invoke(sessions::test_util::SimulateSuccess)); |
| 1093 StartSyncScheduler(SyncScheduler::NORMAL_MODE); | 1051 StartSyncScheduler(SyncScheduler::NORMAL_MODE); |
| 1094 | 1052 |
| 1095 scheduler()->ScheduleNudgeAsync( | 1053 scheduler()->ScheduleNudgeAsync( |
| 1096 zero(), NUDGE_SOURCE_LOCAL, ModelTypeSet(), FROM_HERE); | 1054 zero(), NUDGE_SOURCE_LOCAL, ModelTypeSet(), FROM_HERE); |
| 1097 PumpLoop(); | 1055 PumpLoop(); |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1158 // Should save the nudge for until after the server is reachable. | 1116 // Should save the nudge for until after the server is reachable. |
| 1159 MessageLoop::current()->RunAllPending(); | 1117 MessageLoop::current()->RunAllPending(); |
| 1160 | 1118 |
| 1161 connection()->SetServerReachable(); | 1119 connection()->SetServerReachable(); |
| 1162 connection()->UpdateConnectionStatus(); | 1120 connection()->UpdateConnectionStatus(); |
| 1163 scheduler()->OnConnectionStatusChange(); | 1121 scheduler()->OnConnectionStatusChange(); |
| 1164 MessageLoop::current()->RunAllPending(); | 1122 MessageLoop::current()->RunAllPending(); |
| 1165 } | 1123 } |
| 1166 | 1124 |
| 1167 } // namespace syncer | 1125 } // namespace syncer |
| OLD | NEW |