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 datatype payload map to the datatype payloads stored |
| 173 // in the SyncerThread vault. |
| 174 bool ComparePayloadsToVault(const std::map<syncable::ModelType, std::string>& |
| 175 lhs) { |
| 176 for (size_t i = syncable::FIRST_REAL_MODEL_TYPE; |
| 177 i < syncable::MODEL_TYPE_COUNT; |
| 178 ++i) { |
| 179 syncable::ModelType type = syncable::ModelTypeFromInt(i); |
| 180 std::map<syncable::ModelType, std::string>::const_iterator payload = |
| 181 lhs.find(type); |
| 182 const std::map<syncable::ModelType, std::string>& vault_payloads = |
| 183 syncer_thread()->vault_.datatype_payloads_; |
| 184 if (payload != lhs.end()) { |
| 185 if (vault_payloads.count(type) == 0 || |
| 186 payload->second != vault_payloads.find(type)->second) { |
| 187 return false; |
| 188 } |
| 189 } else if (vault_payloads.count(type) > 0) { |
| 190 return false; |
| 191 } |
| 192 } |
| 193 return true; |
| 194 } |
| 195 |
172 private: | 196 private: |
173 | 197 |
174 virtual void OnSyncEngineEvent(const SyncEngineEvent& event) { | 198 virtual void OnSyncEngineEvent(const SyncEngineEvent& event) { |
175 if (event.what_happened == SyncEngineEvent::SYNC_CYCLE_ENDED) | 199 if (event.what_happened == SyncEngineEvent::SYNC_CYCLE_ENDED) |
176 sync_cycle_ended_event_.Signal(); | 200 sync_cycle_ended_event_.Signal(); |
177 } | 201 } |
178 | 202 |
179 protected: | 203 protected: |
180 TimeDelta max_wait_time_; | 204 TimeDelta max_wait_time_; |
181 SyncSessionContext* context_; | 205 SyncSessionContext* context_; |
(...skipping 279 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
461 false); | 485 false); |
462 | 486 |
463 ASSERT_EQ(SyncerThread::kDefaultLongPollIntervalSeconds, | 487 ASSERT_EQ(SyncerThread::kDefaultLongPollIntervalSeconds, |
464 interval.poll_delta.InSeconds()); | 488 interval.poll_delta.InSeconds()); |
465 ASSERT_EQ(WaitInterval::NORMAL, interval.mode); | 489 ASSERT_EQ(WaitInterval::NORMAL, interval.mode); |
466 ASSERT_FALSE(interval.had_nudge_during_backoff); | 490 ASSERT_FALSE(interval.had_nudge_during_backoff); |
467 ASSERT_FALSE(continue_sync_cycle_param); | 491 ASSERT_FALSE(continue_sync_cycle_param); |
468 } | 492 } |
469 | 493 |
470 { | 494 { |
471 | |
472 // Now try with unsynced local items. | 495 // Now try with unsynced local items. |
473 context->set_last_snapshot(SessionSnapshotForTest(0, 1)); | 496 context->set_last_snapshot(SessionSnapshotForTest(0, 1)); |
474 bool continue_sync_cycle_param = false; | 497 bool continue_sync_cycle_param = false; |
475 | 498 |
476 WaitInterval interval = syncer_thread->CalculatePollingWaitTime( | 499 WaitInterval interval = syncer_thread->CalculatePollingWaitTime( |
477 0, | 500 0, |
478 &user_idle_milliseconds_param, | 501 &user_idle_milliseconds_param, |
479 &continue_sync_cycle_param, | 502 &continue_sync_cycle_param, |
480 false); | 503 false); |
481 | 504 |
(...skipping 23 matching lines...) Expand all Loading... |
505 | 528 |
506 ASSERT_EQ(SyncerThread::kDefaultLongPollIntervalSeconds, | 529 ASSERT_EQ(SyncerThread::kDefaultLongPollIntervalSeconds, |
507 interval.poll_delta.InSeconds()); | 530 interval.poll_delta.InSeconds()); |
508 ASSERT_EQ(WaitInterval::NORMAL, interval.mode); | 531 ASSERT_EQ(WaitInterval::NORMAL, interval.mode); |
509 ASSERT_FALSE(interval.had_nudge_during_backoff); | 532 ASSERT_FALSE(interval.had_nudge_during_backoff); |
510 ASSERT_FALSE(continue_sync_cycle_param); | 533 ASSERT_FALSE(continue_sync_cycle_param); |
511 } | 534 } |
512 | 535 |
513 // Regression for exponential backoff reset when the syncer is nudged. | 536 // Regression for exponential backoff reset when the syncer is nudged. |
514 { | 537 { |
515 | |
516 context->set_last_snapshot(SessionSnapshotForTest(0, 1)); | 538 context->set_last_snapshot(SessionSnapshotForTest(0, 1)); |
517 bool continue_sync_cycle_param = false; | 539 bool continue_sync_cycle_param = false; |
518 | 540 |
519 // Expect move from default polling interval to exponential backoff due to | 541 // Expect move from default polling interval to exponential backoff due to |
520 // unsynced_count != 0. | 542 // unsynced_count != 0. |
521 WaitInterval interval = syncer_thread->CalculatePollingWaitTime( | 543 WaitInterval interval = syncer_thread->CalculatePollingWaitTime( |
522 3600, | 544 3600, |
523 &user_idle_milliseconds_param, | 545 &user_idle_milliseconds_param, |
524 &continue_sync_cycle_param, | 546 &continue_sync_cycle_param, |
525 false); | 547 false); |
(...skipping 257 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
783 | 805 |
784 interceptor.WaitForSyncShare(1, TimeDelta::FromSeconds(1)); | 806 interceptor.WaitForSyncShare(1, TimeDelta::FromSeconds(1)); |
785 EXPECT_EQ(static_cast<unsigned int>(2), | 807 EXPECT_EQ(static_cast<unsigned int>(2), |
786 interceptor.times_sync_occured().size()); | 808 interceptor.times_sync_occured().size()); |
787 | 809 |
788 // SyncerThread should be waiting again. Signal it to stop. | 810 // SyncerThread should be waiting again. Signal it to stop. |
789 EXPECT_TRUE(syncer_thread()->Stop(2000)); | 811 EXPECT_TRUE(syncer_thread()->Stop(2000)); |
790 EXPECT_TRUE(syncer_thread()->vault_.pending_nudge_types_.none()); | 812 EXPECT_TRUE(syncer_thread()->vault_.pending_nudge_types_.none()); |
791 } | 813 } |
792 | 814 |
| 815 TEST_F(SyncerThreadWithSyncerTest, NudgeWithPayloads) { |
| 816 SyncShareIntercept interceptor; |
| 817 connection()->SetMidCommitObserver(&interceptor); |
| 818 // We don't want a poll to happen during this test (except the first one). |
| 819 PreventThreadFromPolling(); |
| 820 EXPECT_TRUE(syncer_thread()->Start()); |
| 821 metadb()->Open(); |
| 822 syncer_thread()->CreateSyncer(metadb()->name()); |
| 823 const TimeDelta poll_interval = TimeDelta::FromMinutes(5); |
| 824 interceptor.WaitForSyncShare(1, poll_interval + poll_interval); |
| 825 EXPECT_EQ(static_cast<unsigned int>(1), |
| 826 interceptor.times_sync_occured().size()); |
| 827 |
| 828 // The SyncerThread should be waiting for the poll now. Nudge it to sync |
| 829 // immediately (5ms). |
| 830 std::map<syncable::ModelType, std::string> payloads; |
| 831 payloads[syncable::BOOKMARKS] = "test"; |
| 832 |
| 833 // Paused so we can verify the nudge types safely. |
| 834 syncer_thread()->RequestPause(); |
| 835 syncer_thread()->NudgeSyncerWithPayloads(5, |
| 836 SyncerThread::kUnknown, |
| 837 payloads); |
| 838 EXPECT_TRUE(ComparePayloadsToVault(payloads)); |
| 839 syncable::ModelTypeBitSet model_types; |
| 840 model_types[syncable::BOOKMARKS] = true; |
| 841 EXPECT_EQ(model_types, syncer_thread()->vault_.pending_nudge_types_); |
| 842 syncer_thread()->RequestResume(); |
| 843 |
| 844 interceptor.WaitForSyncShare(1, TimeDelta::FromSeconds(1)); |
| 845 EXPECT_EQ(static_cast<unsigned int>(2), |
| 846 interceptor.times_sync_occured().size()); |
| 847 |
| 848 // SyncerThread should be waiting again. Signal it to stop. |
| 849 EXPECT_TRUE(syncer_thread()->Stop(2000)); |
| 850 EXPECT_TRUE(syncer_thread()->vault_.pending_nudge_types_.none()); |
| 851 EXPECT_TRUE(syncer_thread()->vault_.datatype_payloads_.empty()); |
| 852 } |
| 853 |
| 854 TEST_F(SyncerThreadWithSyncerTest, NudgeWithPayloadsCoalesced) { |
| 855 SyncShareIntercept interceptor; |
| 856 connection()->SetMidCommitObserver(&interceptor); |
| 857 // We don't want a poll to happen during this test (except the first one). |
| 858 PreventThreadFromPolling(); |
| 859 EXPECT_TRUE(syncer_thread()->Start()); |
| 860 metadb()->Open(); |
| 861 syncer_thread()->CreateSyncer(metadb()->name()); |
| 862 const TimeDelta poll_interval = TimeDelta::FromMinutes(5); |
| 863 interceptor.WaitForSyncShare(1, poll_interval + poll_interval); |
| 864 EXPECT_EQ(static_cast<unsigned int>(1), |
| 865 interceptor.times_sync_occured().size()); |
| 866 |
| 867 // The SyncerThread should be waiting for the poll now. Nudge it to sync |
| 868 // immediately (5ms). |
| 869 std::map<syncable::ModelType, std::string> payloads; |
| 870 payloads[syncable::BOOKMARKS] = "books"; |
| 871 syncable::ModelTypeBitSet model_types; |
| 872 model_types[syncable::BOOKMARKS] = true; |
| 873 |
| 874 // Paused so we can verify the nudge types safely. |
| 875 syncer_thread()->RequestPause(); |
| 876 syncer_thread()->NudgeSyncerWithPayloads(100, |
| 877 SyncerThread::kUnknown, |
| 878 payloads); |
| 879 EXPECT_EQ(model_types, syncer_thread()->vault_.pending_nudge_types_); |
| 880 EXPECT_TRUE(ComparePayloadsToVault(payloads)); |
| 881 |
| 882 payloads.erase(syncable::BOOKMARKS); |
| 883 payloads[syncable::AUTOFILL] = "auto"; |
| 884 model_types[syncable::AUTOFILL] = true; |
| 885 syncer_thread()->NudgeSyncerWithPayloads(0, |
| 886 SyncerThread::kUnknown, |
| 887 payloads); |
| 888 |
| 889 // Reset BOOKMARKS for expectations. |
| 890 payloads[syncable::BOOKMARKS] = "books"; |
| 891 EXPECT_EQ(model_types, syncer_thread()->vault_.pending_nudge_types_); |
| 892 EXPECT_TRUE(ComparePayloadsToVault(payloads)); |
| 893 |
| 894 syncer_thread()->RequestResume(); |
| 895 |
| 896 interceptor.WaitForSyncShare(1, TimeDelta::FromSeconds(1)); |
| 897 EXPECT_EQ(static_cast<unsigned int>(2), |
| 898 interceptor.times_sync_occured().size()); |
| 899 |
| 900 // SyncerThread should be waiting again. Signal it to stop. |
| 901 EXPECT_TRUE(syncer_thread()->Stop(2000)); |
| 902 EXPECT_TRUE(syncer_thread()->vault_.pending_nudge_types_.none()); |
| 903 EXPECT_TRUE(syncer_thread()->vault_.datatype_payloads_.empty()); |
| 904 } |
| 905 |
793 TEST_F(SyncerThreadWithSyncerTest, Throttling) { | 906 TEST_F(SyncerThreadWithSyncerTest, Throttling) { |
794 SyncShareIntercept interceptor; | 907 SyncShareIntercept interceptor; |
795 connection()->SetMidCommitObserver(&interceptor); | 908 connection()->SetMidCommitObserver(&interceptor); |
796 const TimeDelta poll_interval = TimeDelta::FromMilliseconds(10); | 909 const TimeDelta poll_interval = TimeDelta::FromMilliseconds(10); |
797 syncer_thread()->SetSyncerShortPollInterval(poll_interval); | 910 syncer_thread()->SetSyncerShortPollInterval(poll_interval); |
798 | 911 |
799 EXPECT_TRUE(syncer_thread()->Start()); | 912 EXPECT_TRUE(syncer_thread()->Start()); |
800 metadb()->Open(); | 913 metadb()->Open(); |
801 syncer_thread()->CreateSyncer(metadb()->name()); | 914 syncer_thread()->CreateSyncer(metadb()->name()); |
802 | 915 |
(...skipping 323 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1126 EXPECT_CALL(listener, OnSyncEngineEvent( | 1239 EXPECT_CALL(listener, OnSyncEngineEvent( |
1127 Field(&SyncEngineEvent::what_happened, | 1240 Field(&SyncEngineEvent::what_happened, |
1128 SyncEngineEvent::SYNCER_THREAD_EXITING))); | 1241 SyncEngineEvent::SYNCER_THREAD_EXITING))); |
1129 | 1242 |
1130 ASSERT_TRUE(Resume(&listener)); | 1243 ASSERT_TRUE(Resume(&listener)); |
1131 ASSERT_TRUE(sync_cycle_ended_event.TimedWait(max_wait_time_)); | 1244 ASSERT_TRUE(sync_cycle_ended_event.TimedWait(max_wait_time_)); |
1132 EXPECT_TRUE(syncer_thread()->Stop(2000)); | 1245 EXPECT_TRUE(syncer_thread()->Stop(2000)); |
1133 } | 1246 } |
1134 | 1247 |
1135 } // namespace browser_sync | 1248 } // namespace browser_sync |
OLD | NEW |