| 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/scoped_ptr.h" | 8 #include "base/scoped_ptr.h" |
| 9 #include "base/synchronization/lock.h" | 9 #include "base/synchronization/lock.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 | |
| 190 private: | 172 private: |
| 191 | 173 |
| 192 virtual void OnSyncEngineEvent(const SyncEngineEvent& event) { | 174 virtual void OnSyncEngineEvent(const SyncEngineEvent& event) { |
| 193 if (event.what_happened == SyncEngineEvent::SYNC_CYCLE_ENDED) | 175 if (event.what_happened == SyncEngineEvent::SYNC_CYCLE_ENDED) |
| 194 sync_cycle_ended_event_.Signal(); | 176 sync_cycle_ended_event_.Signal(); |
| 195 } | 177 } |
| 196 | 178 |
| 197 protected: | 179 protected: |
| 198 TimeDelta max_wait_time_; | 180 TimeDelta max_wait_time_; |
| 199 SyncSessionContext* context_; | 181 SyncSessionContext* context_; |
| (...skipping 279 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 479 false); | 461 false); |
| 480 | 462 |
| 481 ASSERT_EQ(SyncerThread::kDefaultLongPollIntervalSeconds, | 463 ASSERT_EQ(SyncerThread::kDefaultLongPollIntervalSeconds, |
| 482 interval.poll_delta.InSeconds()); | 464 interval.poll_delta.InSeconds()); |
| 483 ASSERT_EQ(WaitInterval::NORMAL, interval.mode); | 465 ASSERT_EQ(WaitInterval::NORMAL, interval.mode); |
| 484 ASSERT_FALSE(interval.had_nudge_during_backoff); | 466 ASSERT_FALSE(interval.had_nudge_during_backoff); |
| 485 ASSERT_FALSE(continue_sync_cycle_param); | 467 ASSERT_FALSE(continue_sync_cycle_param); |
| 486 } | 468 } |
| 487 | 469 |
| 488 { | 470 { |
| 471 |
| 489 // Now try with unsynced local items. | 472 // Now try with unsynced local items. |
| 490 context->set_last_snapshot(SessionSnapshotForTest(0, 1)); | 473 context->set_last_snapshot(SessionSnapshotForTest(0, 1)); |
| 491 bool continue_sync_cycle_param = false; | 474 bool continue_sync_cycle_param = false; |
| 492 | 475 |
| 493 WaitInterval interval = syncer_thread->CalculatePollingWaitTime( | 476 WaitInterval interval = syncer_thread->CalculatePollingWaitTime( |
| 494 0, | 477 0, |
| 495 &user_idle_milliseconds_param, | 478 &user_idle_milliseconds_param, |
| 496 &continue_sync_cycle_param, | 479 &continue_sync_cycle_param, |
| 497 false); | 480 false); |
| 498 | 481 |
| (...skipping 23 matching lines...) Expand all Loading... |
| 522 | 505 |
| 523 ASSERT_EQ(SyncerThread::kDefaultLongPollIntervalSeconds, | 506 ASSERT_EQ(SyncerThread::kDefaultLongPollIntervalSeconds, |
| 524 interval.poll_delta.InSeconds()); | 507 interval.poll_delta.InSeconds()); |
| 525 ASSERT_EQ(WaitInterval::NORMAL, interval.mode); | 508 ASSERT_EQ(WaitInterval::NORMAL, interval.mode); |
| 526 ASSERT_FALSE(interval.had_nudge_during_backoff); | 509 ASSERT_FALSE(interval.had_nudge_during_backoff); |
| 527 ASSERT_FALSE(continue_sync_cycle_param); | 510 ASSERT_FALSE(continue_sync_cycle_param); |
| 528 } | 511 } |
| 529 | 512 |
| 530 // Regression for exponential backoff reset when the syncer is nudged. | 513 // Regression for exponential backoff reset when the syncer is nudged. |
| 531 { | 514 { |
| 515 |
| 532 context->set_last_snapshot(SessionSnapshotForTest(0, 1)); | 516 context->set_last_snapshot(SessionSnapshotForTest(0, 1)); |
| 533 bool continue_sync_cycle_param = false; | 517 bool continue_sync_cycle_param = false; |
| 534 | 518 |
| 535 // Expect move from default polling interval to exponential backoff due to | 519 // Expect move from default polling interval to exponential backoff due to |
| 536 // unsynced_count != 0. | 520 // unsynced_count != 0. |
| 537 WaitInterval interval = syncer_thread->CalculatePollingWaitTime( | 521 WaitInterval interval = syncer_thread->CalculatePollingWaitTime( |
| 538 3600, | 522 3600, |
| 539 &user_idle_milliseconds_param, | 523 &user_idle_milliseconds_param, |
| 540 &continue_sync_cycle_param, | 524 &continue_sync_cycle_param, |
| 541 false); | 525 false); |
| (...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 741 // The SyncerThread should be waiting for the poll now. Nudge it to sync | 725 // The SyncerThread should be waiting for the poll now. Nudge it to sync |
| 742 // immediately (5ms). | 726 // immediately (5ms). |
| 743 syncable::ModelTypeBitSet model_types; | 727 syncable::ModelTypeBitSet model_types; |
| 744 model_types[syncable::BOOKMARKS] = true; | 728 model_types[syncable::BOOKMARKS] = true; |
| 745 | 729 |
| 746 // Paused so we can verify the nudge types safely. | 730 // Paused so we can verify the nudge types safely. |
| 747 syncer_thread()->RequestPause(); | 731 syncer_thread()->RequestPause(); |
| 748 syncer_thread()->NudgeSyncerWithDataTypes(5, | 732 syncer_thread()->NudgeSyncerWithDataTypes(5, |
| 749 SyncerThread::kUnknown, | 733 SyncerThread::kUnknown, |
| 750 model_types); | 734 model_types); |
| 751 EXPECT_TRUE(CompareNudgeTypesBitSetToVault(model_types)); | 735 EXPECT_EQ(model_types, syncer_thread()->vault_.pending_nudge_types_); |
| 752 syncer_thread()->RequestResume(); | 736 syncer_thread()->RequestResume(); |
| 753 | 737 |
| 754 interceptor.WaitForSyncShare(1, TimeDelta::FromSeconds(1)); | 738 interceptor.WaitForSyncShare(1, TimeDelta::FromSeconds(1)); |
| 755 EXPECT_EQ(static_cast<unsigned int>(2), | 739 EXPECT_EQ(static_cast<unsigned int>(2), |
| 756 interceptor.times_sync_occured().size()); | 740 interceptor.times_sync_occured().size()); |
| 757 | 741 |
| 758 // SyncerThread should be waiting again. Signal it to stop. | 742 // SyncerThread should be waiting again. Signal it to stop. |
| 759 EXPECT_TRUE(syncer_thread()->Stop(2000)); | 743 EXPECT_TRUE(syncer_thread()->Stop(2000)); |
| 760 EXPECT_TRUE(syncer_thread()->vault_.pending_nudge_types_.empty()); | 744 EXPECT_TRUE(syncer_thread()->vault_.pending_nudge_types_.none()); |
| 761 } | 745 } |
| 762 | 746 |
| 763 TEST_F(SyncerThreadWithSyncerTest, NudgeWithDataTypesCoalesced) { | 747 TEST_F(SyncerThreadWithSyncerTest, NudgeWithDataTypesCoalesced) { |
| 764 SyncShareIntercept interceptor; | 748 SyncShareIntercept interceptor; |
| 765 connection()->SetMidCommitObserver(&interceptor); | 749 connection()->SetMidCommitObserver(&interceptor); |
| 766 // We don't want a poll to happen during this test (except the first one). | 750 // We don't want a poll to happen during this test (except the first one). |
| 767 PreventThreadFromPolling(); | 751 PreventThreadFromPolling(); |
| 768 EXPECT_TRUE(syncer_thread()->Start()); | 752 EXPECT_TRUE(syncer_thread()->Start()); |
| 769 metadb()->Open(); | 753 metadb()->Open(); |
| 770 syncer_thread()->CreateSyncer(metadb()->name()); | 754 syncer_thread()->CreateSyncer(metadb()->name()); |
| 771 const TimeDelta poll_interval = TimeDelta::FromMinutes(5); | 755 const TimeDelta poll_interval = TimeDelta::FromMinutes(5); |
| 772 interceptor.WaitForSyncShare(1, poll_interval + poll_interval); | 756 interceptor.WaitForSyncShare(1, poll_interval + poll_interval); |
| 773 EXPECT_EQ(static_cast<unsigned int>(1), | 757 EXPECT_EQ(static_cast<unsigned int>(1), |
| 774 interceptor.times_sync_occured().size()); | 758 interceptor.times_sync_occured().size()); |
| 775 | 759 |
| 776 // The SyncerThread should be waiting for the poll now. Nudge it to sync | 760 // The SyncerThread should be waiting for the poll now. Nudge it to sync |
| 777 // immediately (5ms). | 761 // immediately (5ms). |
| 778 syncable::ModelTypeBitSet model_types; | 762 syncable::ModelTypeBitSet model_types; |
| 779 model_types[syncable::BOOKMARKS] = true; | 763 model_types[syncable::BOOKMARKS] = true; |
| 780 | 764 |
| 781 // Paused so we can verify the nudge types safely. | 765 // Paused so we can verify the nudge types safely. |
| 782 syncer_thread()->RequestPause(); | 766 syncer_thread()->RequestPause(); |
| 783 syncer_thread()->NudgeSyncerWithDataTypes(100, | 767 syncer_thread()->NudgeSyncerWithDataTypes(100, |
| 784 SyncerThread::kUnknown, | 768 SyncerThread::kUnknown, |
| 785 model_types); | 769 model_types); |
| 786 EXPECT_TRUE(CompareNudgeTypesBitSetToVault(model_types)); | 770 EXPECT_EQ(model_types, syncer_thread()->vault_.pending_nudge_types_); |
| 787 | 771 |
| 788 model_types[syncable::BOOKMARKS] = false; | 772 model_types[syncable::BOOKMARKS] = false; |
| 789 model_types[syncable::AUTOFILL] = true; | 773 model_types[syncable::AUTOFILL] = true; |
| 790 syncer_thread()->NudgeSyncerWithDataTypes(0, | 774 syncer_thread()->NudgeSyncerWithDataTypes(0, |
| 791 SyncerThread::kUnknown, | 775 SyncerThread::kUnknown, |
| 792 model_types); | 776 model_types); |
| 793 | 777 |
| 794 // Reset BOOKMARKS for expectations. | 778 // Reset BOOKMARKS for expectations. |
| 795 model_types[syncable::BOOKMARKS] = true; | 779 model_types[syncable::BOOKMARKS] = true; |
| 796 EXPECT_TRUE(CompareNudgeTypesBitSetToVault(model_types)); | 780 EXPECT_EQ(model_types, syncer_thread()->vault_.pending_nudge_types_); |
| 797 | 781 |
| 798 syncer_thread()->RequestResume(); | 782 syncer_thread()->RequestResume(); |
| 799 | 783 |
| 800 interceptor.WaitForSyncShare(1, TimeDelta::FromSeconds(1)); | 784 interceptor.WaitForSyncShare(1, TimeDelta::FromSeconds(1)); |
| 801 EXPECT_EQ(static_cast<unsigned int>(2), | 785 EXPECT_EQ(static_cast<unsigned int>(2), |
| 802 interceptor.times_sync_occured().size()); | 786 interceptor.times_sync_occured().size()); |
| 803 | 787 |
| 804 // SyncerThread should be waiting again. Signal it to stop. | 788 // SyncerThread should be waiting again. Signal it to stop. |
| 805 EXPECT_TRUE(syncer_thread()->Stop(2000)); | 789 EXPECT_TRUE(syncer_thread()->Stop(2000)); |
| 806 EXPECT_TRUE(syncer_thread()->vault_.pending_nudge_types_.empty()); | 790 EXPECT_TRUE(syncer_thread()->vault_.pending_nudge_types_.none()); |
| 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()); | |
| 888 } | 791 } |
| 889 | 792 |
| 890 TEST_F(SyncerThreadWithSyncerTest, Throttling) { | 793 TEST_F(SyncerThreadWithSyncerTest, Throttling) { |
| 891 SyncShareIntercept interceptor; | 794 SyncShareIntercept interceptor; |
| 892 connection()->SetMidCommitObserver(&interceptor); | 795 connection()->SetMidCommitObserver(&interceptor); |
| 893 const TimeDelta poll_interval = TimeDelta::FromMilliseconds(10); | 796 const TimeDelta poll_interval = TimeDelta::FromMilliseconds(10); |
| 894 syncer_thread()->SetSyncerShortPollInterval(poll_interval); | 797 syncer_thread()->SetSyncerShortPollInterval(poll_interval); |
| 895 | 798 |
| 896 EXPECT_TRUE(syncer_thread()->Start()); | 799 EXPECT_TRUE(syncer_thread()->Start()); |
| 897 metadb()->Open(); | 800 metadb()->Open(); |
| (...skipping 325 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1223 EXPECT_CALL(listener, OnSyncEngineEvent( | 1126 EXPECT_CALL(listener, OnSyncEngineEvent( |
| 1224 Field(&SyncEngineEvent::what_happened, | 1127 Field(&SyncEngineEvent::what_happened, |
| 1225 SyncEngineEvent::SYNCER_THREAD_EXITING))); | 1128 SyncEngineEvent::SYNCER_THREAD_EXITING))); |
| 1226 | 1129 |
| 1227 ASSERT_TRUE(Resume(&listener)); | 1130 ASSERT_TRUE(Resume(&listener)); |
| 1228 ASSERT_TRUE(sync_cycle_ended_event.TimedWait(max_wait_time_)); | 1131 ASSERT_TRUE(sync_cycle_ended_event.TimedWait(max_wait_time_)); |
| 1229 EXPECT_TRUE(syncer_thread()->Stop(2000)); | 1132 EXPECT_TRUE(syncer_thread()->Stop(2000)); |
| 1230 } | 1133 } |
| 1231 | 1134 |
| 1232 } // namespace browser_sync | 1135 } // namespace browser_sync |
| OLD | NEW |