| 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 #include <set> | 7 #include <set> |
| 8 | 8 |
| 9 #include "base/lock.h" | 9 #include "base/lock.h" |
| 10 #include "base/scoped_ptr.h" | 10 #include "base/scoped_ptr.h" |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
| 22 | 22 |
| 23 using base::TimeTicks; | 23 using base::TimeTicks; |
| 24 using base::TimeDelta; | 24 using base::TimeDelta; |
| 25 using base::WaitableEvent; | 25 using base::WaitableEvent; |
| 26 using testing::_; | 26 using testing::_; |
| 27 using testing::AnyNumber; | 27 using testing::AnyNumber; |
| 28 using testing::Field; | 28 using testing::Field; |
| 29 | 29 |
| 30 namespace browser_sync { | 30 namespace browser_sync { |
| 31 using sessions::ErrorCounters; |
| 31 using sessions::SyncSessionContext; | 32 using sessions::SyncSessionContext; |
| 33 using sessions::SyncSessionSnapshot; |
| 34 using sessions::SyncerStatus; |
| 32 | 35 |
| 33 typedef testing::Test SyncerThreadTest; | 36 typedef testing::Test SyncerThreadTest; |
| 34 typedef SyncerThread::WaitInterval WaitInterval; | 37 typedef SyncerThread::WaitInterval WaitInterval; |
| 35 | 38 |
| 36 ACTION_P(SignalEvent, event) { | 39 ACTION_P(SignalEvent, event) { |
| 37 event->Signal(); | 40 event->Signal(); |
| 38 } | 41 } |
| 39 | 42 |
| 43 SyncSessionSnapshot SessionSnapshotForTest( |
| 44 int64 num_server_changes_remaining, int64 max_local_timestamp, |
| 45 int64 unsynced_count) { |
| 46 return SyncSessionSnapshot(SyncerStatus(), ErrorCounters(), |
| 47 num_server_changes_remaining, max_local_timestamp, false, |
| 48 syncable::ModelTypeBitSet(), false, false, unsynced_count, 0, false); |
| 49 } |
| 50 |
| 40 class ListenerMock : public ChannelEventHandler<SyncerEvent> { | 51 class ListenerMock : public ChannelEventHandler<SyncerEvent> { |
| 41 public: | 52 public: |
| 42 MOCK_METHOD1(HandleChannelEvent, void(const SyncerEvent&)); | 53 MOCK_METHOD1(HandleChannelEvent, void(const SyncerEvent&)); |
| 43 }; | 54 }; |
| 44 | 55 |
| 45 class SyncerThreadWithSyncerTest : public testing::Test, | 56 class SyncerThreadWithSyncerTest : public testing::Test, |
| 46 public ModelSafeWorkerRegistrar, | 57 public ModelSafeWorkerRegistrar, |
| 47 public ChannelEventHandler<SyncerEvent> { | 58 public ChannelEventHandler<SyncerEvent> { |
| 48 public: | 59 public: |
| 49 SyncerThreadWithSyncerTest() | 60 SyncerThreadWithSyncerTest() |
| 50 : max_wait_time_(TimeDelta::FromSeconds(10)), | 61 : max_wait_time_(TimeDelta::FromSeconds(10)), |
| 51 sync_cycle_ended_event_(false, false) {} | 62 sync_cycle_ended_event_(false, false) {} |
| 52 virtual void SetUp() { | 63 virtual void SetUp() { |
| 53 metadb_.SetUp(); | 64 metadb_.SetUp(); |
| 54 connection_.reset(new MockConnectionManager(metadb_.manager(), | 65 connection_.reset(new MockConnectionManager(metadb_.manager(), |
| 55 metadb_.name())); | 66 metadb_.name())); |
| 56 allstatus_.reset(new AllStatus()); | |
| 57 worker_ = new ModelSafeWorker(); | 67 worker_ = new ModelSafeWorker(); |
| 58 SyncSessionContext* context = new SyncSessionContext(connection_.get(), | 68 SyncSessionContext* context = new SyncSessionContext(connection_.get(), |
| 59 NULL, metadb_.manager(), this); | 69 NULL, metadb_.manager(), this); |
| 60 syncer_thread_ = new SyncerThread(context, allstatus_.get()); | 70 syncer_thread_ = new SyncerThread(context); |
| 61 syncer_event_hookup_.reset( | 71 syncer_event_hookup_.reset( |
| 62 syncer_thread_->relay_channel()->AddObserver(this)); | 72 syncer_thread_->relay_channel()->AddObserver(this)); |
| 63 allstatus_->WatchSyncerThread(syncer_thread_); | |
| 64 syncer_thread_->SetConnected(true); | 73 syncer_thread_->SetConnected(true); |
| 65 syncable::ModelTypeBitSet expected_types; | 74 syncable::ModelTypeBitSet expected_types; |
| 66 expected_types[syncable::BOOKMARKS] = true; | 75 expected_types[syncable::BOOKMARKS] = true; |
| 67 connection_->ExpectGetUpdatesRequestTypes(expected_types); | 76 connection_->ExpectGetUpdatesRequestTypes(expected_types); |
| 68 } | 77 } |
| 69 virtual void TearDown() { | 78 virtual void TearDown() { |
| 70 syncer_event_hookup_.reset(); | 79 syncer_event_hookup_.reset(); |
| 71 allstatus_.reset(); | |
| 72 syncer_thread_ = NULL; | 80 syncer_thread_ = NULL; |
| 73 connection_.reset(); | 81 connection_.reset(); |
| 74 metadb_.TearDown(); | 82 metadb_.TearDown(); |
| 75 } | 83 } |
| 76 | 84 |
| 77 // ModelSafeWorkerRegistrar implementation. | 85 // ModelSafeWorkerRegistrar implementation. |
| 78 virtual void GetWorkers(std::vector<ModelSafeWorker*>* out) { | 86 virtual void GetWorkers(std::vector<ModelSafeWorker*>* out) { |
| 79 out->push_back(worker_.get()); | 87 out->push_back(worker_.get()); |
| 80 } | 88 } |
| 81 | 89 |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 152 if (event.what_happened == SyncerEvent::SYNC_CYCLE_ENDED) | 160 if (event.what_happened == SyncerEvent::SYNC_CYCLE_ENDED) |
| 153 sync_cycle_ended_event_.Signal(); | 161 sync_cycle_ended_event_.Signal(); |
| 154 } | 162 } |
| 155 | 163 |
| 156 protected: | 164 protected: |
| 157 TimeDelta max_wait_time_; | 165 TimeDelta max_wait_time_; |
| 158 | 166 |
| 159 private: | 167 private: |
| 160 ManuallyOpenedTestDirectorySetterUpper metadb_; | 168 ManuallyOpenedTestDirectorySetterUpper metadb_; |
| 161 scoped_ptr<MockConnectionManager> connection_; | 169 scoped_ptr<MockConnectionManager> connection_; |
| 162 scoped_ptr<AllStatus> allstatus_; | |
| 163 scoped_refptr<SyncerThread> syncer_thread_; | 170 scoped_refptr<SyncerThread> syncer_thread_; |
| 164 scoped_refptr<ModelSafeWorker> worker_; | 171 scoped_refptr<ModelSafeWorker> worker_; |
| 165 scoped_ptr<ChannelHookup<SyncerEvent> > syncer_event_hookup_; | 172 scoped_ptr<ChannelHookup<SyncerEvent> > syncer_event_hookup_; |
| 166 base::WaitableEvent sync_cycle_ended_event_; | 173 base::WaitableEvent sync_cycle_ended_event_; |
| 167 DISALLOW_COPY_AND_ASSIGN(SyncerThreadWithSyncerTest); | 174 DISALLOW_COPY_AND_ASSIGN(SyncerThreadWithSyncerTest); |
| 168 }; | 175 }; |
| 169 | 176 |
| 170 class SyncShareIntercept | 177 class SyncShareIntercept |
| 171 : public MockConnectionManager::ResponseCodeOverrideRequestor, | 178 : public MockConnectionManager::ResponseCodeOverrideRequestor, |
| 172 public MockConnectionManager::MidCommitObserver { | 179 public MockConnectionManager::MidCommitObserver { |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 204 } | 211 } |
| 205 private: | 212 private: |
| 206 std::vector<TimeTicks> times_sync_occured_; | 213 std::vector<TimeTicks> times_sync_occured_; |
| 207 base::WaitableEvent sync_occured_; | 214 base::WaitableEvent sync_occured_; |
| 208 bool allow_multiple_interceptions_; | 215 bool allow_multiple_interceptions_; |
| 209 DISALLOW_COPY_AND_ASSIGN(SyncShareIntercept); | 216 DISALLOW_COPY_AND_ASSIGN(SyncShareIntercept); |
| 210 }; | 217 }; |
| 211 | 218 |
| 212 TEST_F(SyncerThreadTest, Construction) { | 219 TEST_F(SyncerThreadTest, Construction) { |
| 213 SyncSessionContext* context = new SyncSessionContext(NULL, NULL, NULL, NULL); | 220 SyncSessionContext* context = new SyncSessionContext(NULL, NULL, NULL, NULL); |
| 214 scoped_refptr<SyncerThread> syncer_thread(new SyncerThread(context, NULL)); | 221 scoped_refptr<SyncerThread> syncer_thread(new SyncerThread(context)); |
| 215 } | 222 } |
| 216 | 223 |
| 217 TEST_F(SyncerThreadTest, StartStop) { | 224 TEST_F(SyncerThreadTest, StartStop) { |
| 218 SyncSessionContext* context = new SyncSessionContext(NULL, NULL, NULL, NULL); | 225 SyncSessionContext* context = new SyncSessionContext(NULL, NULL, NULL, NULL); |
| 219 scoped_refptr<SyncerThread> syncer_thread(new SyncerThread(context, NULL)); | 226 scoped_refptr<SyncerThread> syncer_thread(new SyncerThread(context)); |
| 220 EXPECT_TRUE(syncer_thread->Start()); | 227 EXPECT_TRUE(syncer_thread->Start()); |
| 221 EXPECT_TRUE(syncer_thread->Stop(2000)); | 228 EXPECT_TRUE(syncer_thread->Stop(2000)); |
| 222 | 229 |
| 223 // Do it again for good measure. I caught some bugs by adding this so | 230 // Do it again for good measure. I caught some bugs by adding this so |
| 224 // I would recommend keeping it. | 231 // I would recommend keeping it. |
| 225 EXPECT_TRUE(syncer_thread->Start()); | 232 EXPECT_TRUE(syncer_thread->Start()); |
| 226 EXPECT_TRUE(syncer_thread->Stop(2000)); | 233 EXPECT_TRUE(syncer_thread->Stop(2000)); |
| 227 } | 234 } |
| 228 | 235 |
| 236 TEST(SyncerThread, GetRecommendedDelay) { |
| 237 EXPECT_LE(0, SyncerThread::GetRecommendedDelaySeconds(0)); |
| 238 EXPECT_LE(1, SyncerThread::GetRecommendedDelaySeconds(1)); |
| 239 EXPECT_LE(50, SyncerThread::GetRecommendedDelaySeconds(50)); |
| 240 EXPECT_LE(10, SyncerThread::GetRecommendedDelaySeconds(10)); |
| 241 EXPECT_EQ(SyncerThread::kMaxBackoffSeconds, |
| 242 SyncerThread::GetRecommendedDelaySeconds( |
| 243 SyncerThread::kMaxBackoffSeconds)); |
| 244 EXPECT_EQ(SyncerThread::kMaxBackoffSeconds, |
| 245 SyncerThread::GetRecommendedDelaySeconds( |
| 246 SyncerThread::kMaxBackoffSeconds+1)); |
| 247 } |
| 248 |
| 229 TEST_F(SyncerThreadTest, CalculateSyncWaitTime) { | 249 TEST_F(SyncerThreadTest, CalculateSyncWaitTime) { |
| 230 SyncSessionContext* context = new SyncSessionContext(NULL, NULL, NULL, NULL); | 250 SyncSessionContext* context = new SyncSessionContext(NULL, NULL, NULL, NULL); |
| 231 scoped_refptr<SyncerThread> syncer_thread(new SyncerThread(context, NULL)); | 251 scoped_refptr<SyncerThread> syncer_thread(new SyncerThread(context)); |
| 232 syncer_thread->DisableIdleDetection(); | 252 syncer_thread->DisableIdleDetection(); |
| 233 | 253 |
| 234 // Syncer_polling_interval_ is less than max poll interval. | 254 // Syncer_polling_interval_ is less than max poll interval. |
| 235 TimeDelta syncer_polling_interval = TimeDelta::FromSeconds(1); | 255 TimeDelta syncer_polling_interval = TimeDelta::FromSeconds(1); |
| 236 | 256 |
| 237 syncer_thread->SetSyncerPollingInterval(syncer_polling_interval); | 257 syncer_thread->SetSyncerPollingInterval(syncer_polling_interval); |
| 238 | 258 |
| 239 // user_idle_ms is less than 10 * (syncer_polling_interval*1000). | 259 // user_idle_ms is less than 10 * (syncer_polling_interval*1000). |
| 240 ASSERT_EQ(syncer_polling_interval.InMilliseconds(), | 260 ASSERT_EQ(syncer_polling_interval.InMilliseconds(), |
| 241 syncer_thread->CalculateSyncWaitTime(1000, 0)); | 261 syncer_thread->CalculateSyncWaitTime(1000, 0)); |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 281 over_sync_max_interval)); | 301 over_sync_max_interval)); |
| 282 ASSERT_TRUE(last_poll_time * 3 >= | 302 ASSERT_TRUE(last_poll_time * 3 >= |
| 283 syncer_thread->CalculateSyncWaitTime(last_poll_time, | 303 syncer_thread->CalculateSyncWaitTime(last_poll_time, |
| 284 over_sync_max_interval)); | 304 over_sync_max_interval)); |
| 285 } | 305 } |
| 286 | 306 |
| 287 TEST_F(SyncerThreadTest, CalculatePollingWaitTime) { | 307 TEST_F(SyncerThreadTest, CalculatePollingWaitTime) { |
| 288 // Set up the environment. | 308 // Set up the environment. |
| 289 int user_idle_milliseconds_param = 0; | 309 int user_idle_milliseconds_param = 0; |
| 290 SyncSessionContext* context = new SyncSessionContext(NULL, NULL, NULL, NULL); | 310 SyncSessionContext* context = new SyncSessionContext(NULL, NULL, NULL, NULL); |
| 291 scoped_refptr<SyncerThread> syncer_thread(new SyncerThread(context, NULL)); | 311 scoped_refptr<SyncerThread> syncer_thread(new SyncerThread(context)); |
| 292 syncer_thread->DisableIdleDetection(); | 312 syncer_thread->DisableIdleDetection(); |
| 293 // Hold the lock to appease asserts in code. | 313 // Hold the lock to appease asserts in code. |
| 294 AutoLock lock(syncer_thread->lock_); | 314 AutoLock lock(syncer_thread->lock_); |
| 295 | 315 |
| 296 // Notifications disabled should result in a polling interval of | 316 // Notifications disabled should result in a polling interval of |
| 297 // kDefaultShortPollInterval. | 317 // kDefaultShortPollInterval. |
| 298 { | 318 { |
| 299 AllStatus::Status status = {}; | 319 context->set_notifications_enabled(false); |
| 300 status.notifications_enabled = 0; | |
| 301 bool continue_sync_cycle_param = false; | 320 bool continue_sync_cycle_param = false; |
| 302 | 321 |
| 303 // No work and no backoff. | 322 // No work and no backoff. |
| 304 WaitInterval interval = syncer_thread->CalculatePollingWaitTime( | 323 WaitInterval interval = syncer_thread->CalculatePollingWaitTime( |
| 305 status, | |
| 306 0, | 324 0, |
| 307 &user_idle_milliseconds_param, | 325 &user_idle_milliseconds_param, |
| 308 &continue_sync_cycle_param, | 326 &continue_sync_cycle_param, |
| 309 false); | 327 false); |
| 310 | 328 |
| 311 ASSERT_EQ(SyncerThread::kDefaultShortPollIntervalSeconds, | 329 ASSERT_EQ(SyncerThread::kDefaultShortPollIntervalSeconds, |
| 312 interval.poll_delta.InSeconds()); | 330 interval.poll_delta.InSeconds()); |
| 313 ASSERT_EQ(WaitInterval::NORMAL, interval.mode); | 331 ASSERT_EQ(WaitInterval::NORMAL, interval.mode); |
| 314 ASSERT_FALSE(interval.had_nudge_during_backoff); | 332 ASSERT_FALSE(interval.had_nudge_during_backoff); |
| 315 ASSERT_FALSE(continue_sync_cycle_param); | 333 ASSERT_FALSE(continue_sync_cycle_param); |
| 316 | 334 |
| 317 // In this case the continue_sync_cycle is turned off. | 335 // In this case the continue_sync_cycle is turned off. |
| 318 continue_sync_cycle_param = true; | 336 continue_sync_cycle_param = true; |
| 319 interval = syncer_thread->CalculatePollingWaitTime( | 337 interval = syncer_thread->CalculatePollingWaitTime( |
| 320 status, | |
| 321 0, | 338 0, |
| 322 &user_idle_milliseconds_param, | 339 &user_idle_milliseconds_param, |
| 323 &continue_sync_cycle_param, | 340 &continue_sync_cycle_param, |
| 324 false); | 341 false); |
| 325 | 342 |
| 326 ASSERT_EQ(SyncerThread::kDefaultShortPollIntervalSeconds, | 343 ASSERT_EQ(SyncerThread::kDefaultShortPollIntervalSeconds, |
| 327 interval.poll_delta.InSeconds()); | 344 interval.poll_delta.InSeconds()); |
| 328 ASSERT_EQ(WaitInterval::NORMAL, interval.mode); | 345 ASSERT_EQ(WaitInterval::NORMAL, interval.mode); |
| 329 ASSERT_FALSE(interval.had_nudge_during_backoff); | 346 ASSERT_FALSE(interval.had_nudge_during_backoff); |
| 330 ASSERT_FALSE(continue_sync_cycle_param); | 347 ASSERT_FALSE(continue_sync_cycle_param); |
| 331 } | 348 } |
| 332 | 349 |
| 333 // Notifications enabled should result in a polling interval of | 350 // Notifications enabled should result in a polling interval of |
| 334 // SyncerThread::kDefaultLongPollIntervalSeconds. | 351 // SyncerThread::kDefaultLongPollIntervalSeconds. |
| 335 { | 352 { |
| 336 AllStatus::Status status = {}; | 353 context->set_notifications_enabled(true); |
| 337 status.notifications_enabled = 1; | |
| 338 bool continue_sync_cycle_param = false; | 354 bool continue_sync_cycle_param = false; |
| 339 | 355 |
| 340 // No work and no backoff. | 356 // No work and no backoff. |
| 341 WaitInterval interval = syncer_thread->CalculatePollingWaitTime( | 357 WaitInterval interval = syncer_thread->CalculatePollingWaitTime( |
| 342 status, | |
| 343 0, | 358 0, |
| 344 &user_idle_milliseconds_param, | 359 &user_idle_milliseconds_param, |
| 345 &continue_sync_cycle_param, | 360 &continue_sync_cycle_param, |
| 346 false); | 361 false); |
| 347 | 362 |
| 348 ASSERT_EQ(SyncerThread::kDefaultLongPollIntervalSeconds, | 363 ASSERT_EQ(SyncerThread::kDefaultLongPollIntervalSeconds, |
| 349 interval.poll_delta.InSeconds()); | 364 interval.poll_delta.InSeconds()); |
| 350 ASSERT_EQ(WaitInterval::NORMAL, interval.mode); | 365 ASSERT_EQ(WaitInterval::NORMAL, interval.mode); |
| 351 ASSERT_FALSE(interval.had_nudge_during_backoff); | 366 ASSERT_FALSE(interval.had_nudge_during_backoff); |
| 352 ASSERT_FALSE(continue_sync_cycle_param); | 367 ASSERT_FALSE(continue_sync_cycle_param); |
| 353 | 368 |
| 354 // In this case the continue_sync_cycle is turned off. | 369 // In this case the continue_sync_cycle is turned off. |
| 355 continue_sync_cycle_param = true; | 370 continue_sync_cycle_param = true; |
| 356 interval = syncer_thread->CalculatePollingWaitTime( | 371 interval = syncer_thread->CalculatePollingWaitTime( |
| 357 status, | |
| 358 0, | 372 0, |
| 359 &user_idle_milliseconds_param, | 373 &user_idle_milliseconds_param, |
| 360 &continue_sync_cycle_param, | 374 &continue_sync_cycle_param, |
| 361 false); | 375 false); |
| 362 | 376 |
| 363 ASSERT_EQ(SyncerThread::kDefaultLongPollIntervalSeconds, | 377 ASSERT_EQ(SyncerThread::kDefaultLongPollIntervalSeconds, |
| 364 interval.poll_delta.InSeconds()); | 378 interval.poll_delta.InSeconds()); |
| 365 ASSERT_EQ(WaitInterval::NORMAL, interval.mode); | 379 ASSERT_EQ(WaitInterval::NORMAL, interval.mode); |
| 366 ASSERT_FALSE(interval.had_nudge_during_backoff); | 380 ASSERT_FALSE(interval.had_nudge_during_backoff); |
| 367 ASSERT_FALSE(continue_sync_cycle_param); | 381 ASSERT_FALSE(continue_sync_cycle_param); |
| 368 } | 382 } |
| 369 | 383 |
| 370 // There are two states which can cause a continuation, either the updates | 384 // There are two states which can cause a continuation, either the updates |
| 371 // available do not match the updates received, or the unsynced count is | 385 // available do not match the updates received, or the unsynced count is |
| 372 // non-zero. | 386 // non-zero. |
| 373 { | 387 { |
| 374 AllStatus::Status status = {}; | 388 // More server changes remaining to download. |
| 375 status.updates_available = 1; | 389 context->set_last_snapshot(SessionSnapshotForTest(1, 0, 0)); |
| 376 status.updates_received = 0; | |
| 377 bool continue_sync_cycle_param = false; | 390 bool continue_sync_cycle_param = false; |
| 378 | 391 |
| 379 WaitInterval interval = syncer_thread->CalculatePollingWaitTime( | 392 WaitInterval interval = syncer_thread->CalculatePollingWaitTime( |
| 380 status, | |
| 381 0, | 393 0, |
| 382 &user_idle_milliseconds_param, | 394 &user_idle_milliseconds_param, |
| 383 &continue_sync_cycle_param, | 395 &continue_sync_cycle_param, |
| 384 false); | 396 false); |
| 385 | 397 |
| 386 ASSERT_LE(0, interval.poll_delta.InSeconds()); | 398 ASSERT_LE(0, interval.poll_delta.InSeconds()); |
| 387 ASSERT_EQ(WaitInterval::NORMAL, interval.mode); | 399 ASSERT_EQ(WaitInterval::NORMAL, interval.mode); |
| 388 ASSERT_FALSE(interval.had_nudge_during_backoff); | 400 ASSERT_FALSE(interval.had_nudge_during_backoff); |
| 389 ASSERT_TRUE(continue_sync_cycle_param); | 401 ASSERT_TRUE(continue_sync_cycle_param); |
| 390 | 402 |
| 391 continue_sync_cycle_param = false; | 403 continue_sync_cycle_param = false; |
| 392 interval = syncer_thread->CalculatePollingWaitTime( | 404 interval = syncer_thread->CalculatePollingWaitTime( |
| 393 status, | |
| 394 0, | 405 0, |
| 395 &user_idle_milliseconds_param, | 406 &user_idle_milliseconds_param, |
| 396 &continue_sync_cycle_param, | 407 &continue_sync_cycle_param, |
| 397 false); | 408 false); |
| 398 | 409 |
| 399 ASSERT_GE(3, interval.poll_delta.InSeconds()); | 410 ASSERT_GE(3, interval.poll_delta.InSeconds()); |
| 400 ASSERT_EQ(WaitInterval::NORMAL, interval.mode); | 411 ASSERT_EQ(WaitInterval::NORMAL, interval.mode); |
| 401 ASSERT_FALSE(interval.had_nudge_during_backoff); | 412 ASSERT_FALSE(interval.had_nudge_during_backoff); |
| 402 ASSERT_TRUE(continue_sync_cycle_param); | 413 ASSERT_TRUE(continue_sync_cycle_param); |
| 403 | 414 |
| 404 interval = syncer_thread->CalculatePollingWaitTime( | 415 interval = syncer_thread->CalculatePollingWaitTime( |
| 405 status, | |
| 406 0, | 416 0, |
| 407 &user_idle_milliseconds_param, | 417 &user_idle_milliseconds_param, |
| 408 &continue_sync_cycle_param, | 418 &continue_sync_cycle_param, |
| 409 false); | 419 false); |
| 410 | 420 |
| 411 ASSERT_LE(0, interval.poll_delta.InSeconds()); | 421 ASSERT_LE(0, interval.poll_delta.InSeconds()); |
| 412 ASSERT_EQ(WaitInterval::EXPONENTIAL_BACKOFF, interval.mode); | 422 ASSERT_EQ(WaitInterval::EXPONENTIAL_BACKOFF, interval.mode); |
| 413 ASSERT_FALSE(interval.had_nudge_during_backoff); | 423 ASSERT_FALSE(interval.had_nudge_during_backoff); |
| 414 | 424 |
| 415 interval = syncer_thread->CalculatePollingWaitTime( | 425 interval = syncer_thread->CalculatePollingWaitTime( |
| 416 status, | |
| 417 0, | 426 0, |
| 418 &user_idle_milliseconds_param, | 427 &user_idle_milliseconds_param, |
| 419 &continue_sync_cycle_param, | 428 &continue_sync_cycle_param, |
| 420 false); | 429 false); |
| 421 | 430 |
| 422 ASSERT_GE(2, interval.poll_delta.InSeconds()); | 431 ASSERT_GE(2, interval.poll_delta.InSeconds()); |
| 423 ASSERT_EQ(WaitInterval::EXPONENTIAL_BACKOFF, interval.mode); | 432 ASSERT_EQ(WaitInterval::EXPONENTIAL_BACKOFF, interval.mode); |
| 424 ASSERT_FALSE(interval.had_nudge_during_backoff); | 433 ASSERT_FALSE(interval.had_nudge_during_backoff); |
| 425 ASSERT_TRUE(continue_sync_cycle_param); | 434 ASSERT_TRUE(continue_sync_cycle_param); |
| 426 | 435 |
| 427 status.updates_received = 1; | 436 // Now simulate no more server changes remaining. |
| 437 context->set_last_snapshot(SessionSnapshotForTest(1, 1, 0)); |
| 428 interval = syncer_thread->CalculatePollingWaitTime( | 438 interval = syncer_thread->CalculatePollingWaitTime( |
| 429 status, | |
| 430 0, | 439 0, |
| 431 &user_idle_milliseconds_param, | 440 &user_idle_milliseconds_param, |
| 432 &continue_sync_cycle_param, | 441 &continue_sync_cycle_param, |
| 433 false); | 442 false); |
| 434 | 443 |
| 435 ASSERT_EQ(SyncerThread::kDefaultShortPollIntervalSeconds, | 444 ASSERT_EQ(SyncerThread::kDefaultLongPollIntervalSeconds, |
| 436 interval.poll_delta.InSeconds()); | 445 interval.poll_delta.InSeconds()); |
| 437 ASSERT_EQ(WaitInterval::NORMAL, interval.mode); | 446 ASSERT_EQ(WaitInterval::NORMAL, interval.mode); |
| 438 ASSERT_FALSE(interval.had_nudge_during_backoff); | 447 ASSERT_FALSE(interval.had_nudge_during_backoff); |
| 439 ASSERT_FALSE(continue_sync_cycle_param); | 448 ASSERT_FALSE(continue_sync_cycle_param); |
| 440 } | 449 } |
| 441 | 450 |
| 442 { | 451 { |
| 443 AllStatus::Status status = {}; | 452 |
| 444 status.unsynced_count = 1; | 453 // Now try with unsynced local items. |
| 454 context->set_last_snapshot(SessionSnapshotForTest(0, 0, 1)); |
| 445 bool continue_sync_cycle_param = false; | 455 bool continue_sync_cycle_param = false; |
| 446 | 456 |
| 447 WaitInterval interval = syncer_thread->CalculatePollingWaitTime( | 457 WaitInterval interval = syncer_thread->CalculatePollingWaitTime( |
| 448 status, | |
| 449 0, | 458 0, |
| 450 &user_idle_milliseconds_param, | 459 &user_idle_milliseconds_param, |
| 451 &continue_sync_cycle_param, | 460 &continue_sync_cycle_param, |
| 452 false); | 461 false); |
| 453 | 462 |
| 454 ASSERT_LE(0, interval.poll_delta.InSeconds()); | 463 ASSERT_LE(0, interval.poll_delta.InSeconds()); |
| 455 ASSERT_EQ(WaitInterval::NORMAL, interval.mode); | 464 ASSERT_EQ(WaitInterval::NORMAL, interval.mode); |
| 456 ASSERT_FALSE(interval.had_nudge_during_backoff); | 465 ASSERT_FALSE(interval.had_nudge_during_backoff); |
| 457 ASSERT_TRUE(continue_sync_cycle_param); | 466 ASSERT_TRUE(continue_sync_cycle_param); |
| 458 | 467 |
| 459 continue_sync_cycle_param = false; | 468 continue_sync_cycle_param = false; |
| 460 interval = syncer_thread->CalculatePollingWaitTime( | 469 interval = syncer_thread->CalculatePollingWaitTime( |
| 461 status, | |
| 462 0, | 470 0, |
| 463 &user_idle_milliseconds_param, | 471 &user_idle_milliseconds_param, |
| 464 &continue_sync_cycle_param, | 472 &continue_sync_cycle_param, |
| 465 false); | 473 false); |
| 466 | 474 |
| 467 ASSERT_GE(2, interval.poll_delta.InSeconds()); | 475 ASSERT_GE(2, interval.poll_delta.InSeconds()); |
| 468 ASSERT_EQ(WaitInterval::NORMAL, interval.mode); | 476 ASSERT_EQ(WaitInterval::NORMAL, interval.mode); |
| 469 ASSERT_FALSE(interval.had_nudge_during_backoff); | 477 ASSERT_FALSE(interval.had_nudge_during_backoff); |
| 470 ASSERT_TRUE(continue_sync_cycle_param); | 478 ASSERT_TRUE(continue_sync_cycle_param); |
| 471 | 479 |
| 472 status.unsynced_count = 0; | 480 context->set_last_snapshot(SessionSnapshotForTest(0, 0, 0)); |
| 473 interval = syncer_thread->CalculatePollingWaitTime( | 481 interval = syncer_thread->CalculatePollingWaitTime( |
| 474 status, | |
| 475 4, | 482 4, |
| 476 &user_idle_milliseconds_param, | 483 &user_idle_milliseconds_param, |
| 477 &continue_sync_cycle_param, | 484 &continue_sync_cycle_param, |
| 478 false); | 485 false); |
| 479 | 486 |
| 480 ASSERT_EQ(SyncerThread::kDefaultShortPollIntervalSeconds, | 487 ASSERT_EQ(SyncerThread::kDefaultLongPollIntervalSeconds, |
| 481 interval.poll_delta.InSeconds()); | 488 interval.poll_delta.InSeconds()); |
| 482 ASSERT_EQ(WaitInterval::NORMAL, interval.mode); | 489 ASSERT_EQ(WaitInterval::NORMAL, interval.mode); |
| 483 ASSERT_FALSE(interval.had_nudge_during_backoff); | 490 ASSERT_FALSE(interval.had_nudge_during_backoff); |
| 484 ASSERT_FALSE(continue_sync_cycle_param); | 491 ASSERT_FALSE(continue_sync_cycle_param); |
| 485 } | 492 } |
| 486 | 493 |
| 487 // Regression for exponential backoff reset when the syncer is nudged. | 494 // Regression for exponential backoff reset when the syncer is nudged. |
| 488 { | 495 { |
| 489 AllStatus::Status status = {}; | 496 |
| 490 status.unsynced_count = 1; | 497 context->set_last_snapshot(SessionSnapshotForTest(0, 0, 1)); |
| 491 bool continue_sync_cycle_param = false; | 498 bool continue_sync_cycle_param = false; |
| 492 | 499 |
| 493 // Expect move from default polling interval to exponential backoff due to | 500 // Expect move from default polling interval to exponential backoff due to |
| 494 // unsynced_count != 0. | 501 // unsynced_count != 0. |
| 495 WaitInterval interval = syncer_thread->CalculatePollingWaitTime( | 502 WaitInterval interval = syncer_thread->CalculatePollingWaitTime( |
| 496 status, | |
| 497 3600, | 503 3600, |
| 498 &user_idle_milliseconds_param, | 504 &user_idle_milliseconds_param, |
| 499 &continue_sync_cycle_param, | 505 &continue_sync_cycle_param, |
| 500 false); | 506 false); |
| 501 | 507 |
| 502 ASSERT_LE(0, interval.poll_delta.InSeconds()); | 508 ASSERT_LE(0, interval.poll_delta.InSeconds()); |
| 503 ASSERT_EQ(WaitInterval::NORMAL, interval.mode); | 509 ASSERT_EQ(WaitInterval::NORMAL, interval.mode); |
| 504 ASSERT_FALSE(interval.had_nudge_during_backoff); | 510 ASSERT_FALSE(interval.had_nudge_during_backoff); |
| 505 ASSERT_TRUE(continue_sync_cycle_param); | 511 ASSERT_TRUE(continue_sync_cycle_param); |
| 506 | 512 |
| 507 continue_sync_cycle_param = false; | 513 continue_sync_cycle_param = false; |
| 508 interval = syncer_thread->CalculatePollingWaitTime( | 514 interval = syncer_thread->CalculatePollingWaitTime( |
| 509 status, | |
| 510 3600, | 515 3600, |
| 511 &user_idle_milliseconds_param, | 516 &user_idle_milliseconds_param, |
| 512 &continue_sync_cycle_param, | 517 &continue_sync_cycle_param, |
| 513 false); | 518 false); |
| 514 | 519 |
| 515 ASSERT_GE(2, interval.poll_delta.InSeconds()); | 520 ASSERT_GE(2, interval.poll_delta.InSeconds()); |
| 516 ASSERT_EQ(WaitInterval::NORMAL, interval.mode); | 521 ASSERT_EQ(WaitInterval::NORMAL, interval.mode); |
| 517 ASSERT_FALSE(interval.had_nudge_during_backoff); | 522 ASSERT_FALSE(interval.had_nudge_during_backoff); |
| 518 ASSERT_TRUE(continue_sync_cycle_param); | 523 ASSERT_TRUE(continue_sync_cycle_param); |
| 519 | 524 |
| 520 // Expect exponential backoff. | 525 // Expect exponential backoff. |
| 521 interval = syncer_thread->CalculatePollingWaitTime( | 526 interval = syncer_thread->CalculatePollingWaitTime( |
| 522 status, | |
| 523 2, | 527 2, |
| 524 &user_idle_milliseconds_param, | 528 &user_idle_milliseconds_param, |
| 525 &continue_sync_cycle_param, | 529 &continue_sync_cycle_param, |
| 526 false); | 530 false); |
| 527 | 531 |
| 528 ASSERT_LE(2, interval.poll_delta.InSeconds()); | 532 ASSERT_LE(2, interval.poll_delta.InSeconds()); |
| 529 ASSERT_EQ(WaitInterval::EXPONENTIAL_BACKOFF, interval.mode); | 533 ASSERT_EQ(WaitInterval::EXPONENTIAL_BACKOFF, interval.mode); |
| 530 ASSERT_FALSE(interval.had_nudge_during_backoff); | 534 ASSERT_FALSE(interval.had_nudge_during_backoff); |
| 531 ASSERT_TRUE(continue_sync_cycle_param); | 535 ASSERT_TRUE(continue_sync_cycle_param); |
| 532 | 536 |
| 533 interval = syncer_thread->CalculatePollingWaitTime( | 537 interval = syncer_thread->CalculatePollingWaitTime( |
| 534 status, | |
| 535 2, | 538 2, |
| 536 &user_idle_milliseconds_param, | 539 &user_idle_milliseconds_param, |
| 537 &continue_sync_cycle_param, | 540 &continue_sync_cycle_param, |
| 538 false); | 541 false); |
| 539 | 542 |
| 540 ASSERT_GE(6, interval.poll_delta.InSeconds()); | 543 ASSERT_GE(6, interval.poll_delta.InSeconds()); |
| 541 ASSERT_EQ(WaitInterval::EXPONENTIAL_BACKOFF, interval.mode); | 544 ASSERT_EQ(WaitInterval::EXPONENTIAL_BACKOFF, interval.mode); |
| 542 ASSERT_FALSE(interval.had_nudge_during_backoff); | 545 ASSERT_FALSE(interval.had_nudge_during_backoff); |
| 543 ASSERT_TRUE(continue_sync_cycle_param); | 546 ASSERT_TRUE(continue_sync_cycle_param); |
| 544 | 547 |
| 545 syncer_thread->vault_.current_wait_interval_ = interval; | 548 syncer_thread->vault_.current_wait_interval_ = interval; |
| 546 | 549 |
| 547 interval = syncer_thread->CalculatePollingWaitTime( | 550 interval = syncer_thread->CalculatePollingWaitTime( |
| 548 status, | |
| 549 static_cast<int>(interval.poll_delta.InSeconds()), | 551 static_cast<int>(interval.poll_delta.InSeconds()), |
| 550 &user_idle_milliseconds_param, | 552 &user_idle_milliseconds_param, |
| 551 &continue_sync_cycle_param, | 553 &continue_sync_cycle_param, |
| 552 true); | 554 true); |
| 553 | 555 |
| 554 // Don't change poll on a failed nudge during backoff. | 556 // Don't change poll on a failed nudge during backoff. |
| 555 ASSERT_TRUE(syncer_thread->vault_.current_wait_interval_.poll_delta == | 557 ASSERT_TRUE(syncer_thread->vault_.current_wait_interval_.poll_delta == |
| 556 interval.poll_delta); | 558 interval.poll_delta); |
| 557 ASSERT_EQ(WaitInterval::EXPONENTIAL_BACKOFF, interval.mode); | 559 ASSERT_EQ(WaitInterval::EXPONENTIAL_BACKOFF, interval.mode); |
| 558 ASSERT_TRUE(interval.had_nudge_during_backoff); | 560 ASSERT_TRUE(interval.had_nudge_during_backoff); |
| 559 ASSERT_TRUE(continue_sync_cycle_param); | 561 ASSERT_TRUE(continue_sync_cycle_param); |
| 560 | 562 |
| 561 // If we got a nudge and we weren't in backoff mode, we see exponential | 563 // If we got a nudge and we weren't in backoff mode, we see exponential |
| 562 // backoff. | 564 // backoff. |
| 563 syncer_thread->vault_.current_wait_interval_.mode = WaitInterval::NORMAL; | 565 syncer_thread->vault_.current_wait_interval_.mode = WaitInterval::NORMAL; |
| 564 interval = syncer_thread->CalculatePollingWaitTime( | 566 interval = syncer_thread->CalculatePollingWaitTime( |
| 565 status, | |
| 566 2, | 567 2, |
| 567 &user_idle_milliseconds_param, | 568 &user_idle_milliseconds_param, |
| 568 &continue_sync_cycle_param, | 569 &continue_sync_cycle_param, |
| 569 true); | 570 true); |
| 570 | 571 |
| 571 // 5 and 3 are bounds on the backoff randomization formula given input of 2. | 572 // 5 and 3 are bounds on the backoff randomization formula given input of 2. |
| 572 ASSERT_GE(5, interval.poll_delta.InSeconds()); | 573 ASSERT_GE(5, interval.poll_delta.InSeconds()); |
| 573 ASSERT_LE(3, interval.poll_delta.InSeconds()); | 574 ASSERT_LE(3, interval.poll_delta.InSeconds()); |
| 574 ASSERT_EQ(WaitInterval::EXPONENTIAL_BACKOFF, interval.mode); | 575 ASSERT_EQ(WaitInterval::EXPONENTIAL_BACKOFF, interval.mode); |
| 575 ASSERT_FALSE(interval.had_nudge_during_backoff); | 576 ASSERT_FALSE(interval.had_nudge_during_backoff); |
| 576 ASSERT_TRUE(continue_sync_cycle_param); | 577 ASSERT_TRUE(continue_sync_cycle_param); |
| 577 | 578 |
| 578 // And if another interval expires, we get a bigger backoff. | 579 // And if another interval expires, we get a bigger backoff. |
| 579 WaitInterval new_interval = syncer_thread->CalculatePollingWaitTime( | 580 WaitInterval new_interval = syncer_thread->CalculatePollingWaitTime( |
| 580 status, | |
| 581 static_cast<int>(interval.poll_delta.InSeconds()), | 581 static_cast<int>(interval.poll_delta.InSeconds()), |
| 582 &user_idle_milliseconds_param, | 582 &user_idle_milliseconds_param, |
| 583 &continue_sync_cycle_param, | 583 &continue_sync_cycle_param, |
| 584 false); | 584 false); |
| 585 | 585 |
| 586 ASSERT_GE(12, new_interval.poll_delta.InSeconds()); | 586 ASSERT_GE(12, new_interval.poll_delta.InSeconds()); |
| 587 ASSERT_LE(5, new_interval.poll_delta.InSeconds()); | 587 ASSERT_LE(5, new_interval.poll_delta.InSeconds()); |
| 588 ASSERT_EQ(WaitInterval::EXPONENTIAL_BACKOFF, interval.mode); | 588 ASSERT_EQ(WaitInterval::EXPONENTIAL_BACKOFF, interval.mode); |
| 589 ASSERT_FALSE(new_interval.had_nudge_during_backoff); | 589 ASSERT_FALSE(new_interval.had_nudge_during_backoff); |
| 590 ASSERT_TRUE(continue_sync_cycle_param); | 590 ASSERT_TRUE(continue_sync_cycle_param); |
| 591 | 591 |
| 592 // A nudge resets the continue_sync_cycle_param value, so our backoff | 592 // A nudge resets the continue_sync_cycle_param value, so our backoff |
| 593 // should return to the minimum. | 593 // should return to the minimum. |
| 594 continue_sync_cycle_param = false; | 594 continue_sync_cycle_param = false; |
| 595 interval = syncer_thread->CalculatePollingWaitTime( | 595 interval = syncer_thread->CalculatePollingWaitTime( |
| 596 status, | |
| 597 3600, | 596 3600, |
| 598 &user_idle_milliseconds_param, | 597 &user_idle_milliseconds_param, |
| 599 &continue_sync_cycle_param, | 598 &continue_sync_cycle_param, |
| 600 true); | 599 true); |
| 601 | 600 |
| 602 ASSERT_LE(0, interval.poll_delta.InSeconds()); | 601 ASSERT_LE(0, interval.poll_delta.InSeconds()); |
| 603 ASSERT_EQ(WaitInterval::NORMAL, interval.mode); | 602 ASSERT_EQ(WaitInterval::NORMAL, interval.mode); |
| 604 ASSERT_FALSE(interval.had_nudge_during_backoff); | 603 ASSERT_FALSE(interval.had_nudge_during_backoff); |
| 605 ASSERT_TRUE(continue_sync_cycle_param); | 604 ASSERT_TRUE(continue_sync_cycle_param); |
| 606 | 605 |
| 607 continue_sync_cycle_param = false; | 606 continue_sync_cycle_param = false; |
| 608 interval = syncer_thread->CalculatePollingWaitTime( | 607 interval = syncer_thread->CalculatePollingWaitTime( |
| 609 status, | |
| 610 3600, | 608 3600, |
| 611 &user_idle_milliseconds_param, | 609 &user_idle_milliseconds_param, |
| 612 &continue_sync_cycle_param, | 610 &continue_sync_cycle_param, |
| 613 true); | 611 true); |
| 614 | 612 |
| 615 ASSERT_GE(2, interval.poll_delta.InSeconds()); | 613 ASSERT_GE(2, interval.poll_delta.InSeconds()); |
| 616 ASSERT_EQ(WaitInterval::NORMAL, interval.mode); | 614 ASSERT_EQ(WaitInterval::NORMAL, interval.mode); |
| 617 ASSERT_FALSE(interval.had_nudge_during_backoff); | 615 ASSERT_FALSE(interval.had_nudge_during_backoff); |
| 618 ASSERT_TRUE(continue_sync_cycle_param); | 616 ASSERT_TRUE(continue_sync_cycle_param); |
| 619 | 617 |
| 620 // Setting unsynced_count = 0 returns us to the default polling interval. | 618 // Setting unsynced_count = 0 returns us to the default polling interval. |
| 621 status.unsynced_count = 0; | 619 context->set_last_snapshot(SessionSnapshotForTest(0, 0, 0)); |
| 622 interval = syncer_thread->CalculatePollingWaitTime( | 620 interval = syncer_thread->CalculatePollingWaitTime( |
| 623 status, | |
| 624 4, | 621 4, |
| 625 &user_idle_milliseconds_param, | 622 &user_idle_milliseconds_param, |
| 626 &continue_sync_cycle_param, | 623 &continue_sync_cycle_param, |
| 627 true); | 624 true); |
| 628 | 625 |
| 629 ASSERT_EQ(SyncerThread::kDefaultShortPollIntervalSeconds, | 626 ASSERT_EQ(SyncerThread::kDefaultLongPollIntervalSeconds, |
| 630 interval.poll_delta.InSeconds()); | 627 interval.poll_delta.InSeconds()); |
| 631 ASSERT_EQ(WaitInterval::NORMAL, interval.mode); | 628 ASSERT_EQ(WaitInterval::NORMAL, interval.mode); |
| 632 ASSERT_FALSE(interval.had_nudge_during_backoff); | 629 ASSERT_FALSE(interval.had_nudge_during_backoff); |
| 633 ASSERT_FALSE(continue_sync_cycle_param); | 630 ASSERT_FALSE(continue_sync_cycle_param); |
| 634 } | 631 } |
| 635 } | 632 } |
| 636 | 633 |
| 637 TEST_F(SyncerThreadWithSyncerTest, Polling) { | 634 TEST_F(SyncerThreadWithSyncerTest, Polling) { |
| 638 SyncShareIntercept interceptor; | 635 SyncShareIntercept interceptor; |
| 639 connection()->SetMidCommitObserver(&interceptor); | 636 connection()->SetMidCommitObserver(&interceptor); |
| (...skipping 365 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1005 WillOnce(SignalEvent(&sync_cycle_ended_event)); | 1002 WillOnce(SignalEvent(&sync_cycle_ended_event)); |
| 1006 EXPECT_CALL(listener, HandleChannelEvent( | 1003 EXPECT_CALL(listener, HandleChannelEvent( |
| 1007 Field(&SyncerEvent::what_happened, SyncerEvent::SYNCER_THREAD_EXITING))); | 1004 Field(&SyncerEvent::what_happened, SyncerEvent::SYNCER_THREAD_EXITING))); |
| 1008 | 1005 |
| 1009 ASSERT_TRUE(Resume(&listener)); | 1006 ASSERT_TRUE(Resume(&listener)); |
| 1010 ASSERT_TRUE(sync_cycle_ended_event.TimedWait(max_wait_time_)); | 1007 ASSERT_TRUE(sync_cycle_ended_event.TimedWait(max_wait_time_)); |
| 1011 EXPECT_TRUE(syncer_thread()->Stop(2000)); | 1008 EXPECT_TRUE(syncer_thread()->Stop(2000)); |
| 1012 } | 1009 } |
| 1013 | 1010 |
| 1014 } // namespace browser_sync | 1011 } // namespace browser_sync |
| OLD | NEW |