| 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/scoped_ptr.h" | 9 #include "base/scoped_ptr.h" |
| 10 #include "base/time.h" | 10 #include "base/time.h" |
| (...skipping 27 matching lines...) Expand all Loading... |
| 38 | 38 |
| 39 class ListenerMock : public ChannelEventHandler<SyncerEvent> { | 39 class ListenerMock : public ChannelEventHandler<SyncerEvent> { |
| 40 public: | 40 public: |
| 41 MOCK_METHOD1(HandleChannelEvent, void(const SyncerEvent&)); | 41 MOCK_METHOD1(HandleChannelEvent, void(const SyncerEvent&)); |
| 42 }; | 42 }; |
| 43 | 43 |
| 44 class SyncerThreadWithSyncerTest : public testing::Test, | 44 class SyncerThreadWithSyncerTest : public testing::Test, |
| 45 public ModelSafeWorkerRegistrar, | 45 public ModelSafeWorkerRegistrar, |
| 46 public ChannelEventHandler<SyncerEvent> { | 46 public ChannelEventHandler<SyncerEvent> { |
| 47 public: | 47 public: |
| 48 SyncerThreadWithSyncerTest() : sync_cycle_ended_event_(false, false) {} | 48 SyncerThreadWithSyncerTest() |
| 49 : max_wait_time_(TimeDelta::FromSeconds(10)), |
| 50 sync_cycle_ended_event_(false, false) {} |
| 49 virtual void SetUp() { | 51 virtual void SetUp() { |
| 50 metadb_.SetUp(); | 52 metadb_.SetUp(); |
| 51 connection_.reset(new MockConnectionManager(metadb_.manager(), | 53 connection_.reset(new MockConnectionManager(metadb_.manager(), |
| 52 metadb_.name())); | 54 metadb_.name())); |
| 53 allstatus_.reset(new AllStatus()); | 55 allstatus_.reset(new AllStatus()); |
| 54 worker_ = new ModelSafeWorker(); | 56 worker_ = new ModelSafeWorker(); |
| 55 SyncSessionContext* context = new SyncSessionContext(connection_.get(), | 57 SyncSessionContext* context = new SyncSessionContext(connection_.get(), |
| 56 NULL, metadb_.manager(), this); | 58 NULL, metadb_.manager(), this); |
| 57 syncer_thread_ = new SyncerThread(context, allstatus_.get()); | 59 syncer_thread_ = new SyncerThread(context, allstatus_.get()); |
| 58 syncer_event_hookup_.reset( | 60 syncer_event_hookup_.reset( |
| (...skipping 22 matching lines...) Expand all Loading... |
| 81 // the SyncerThread. | 83 // the SyncerThread. |
| 82 (*out)[syncable::BOOKMARKS] = GROUP_PASSIVE; | 84 (*out)[syncable::BOOKMARKS] = GROUP_PASSIVE; |
| 83 } | 85 } |
| 84 | 86 |
| 85 ManuallyOpenedTestDirectorySetterUpper* metadb() { return &metadb_; } | 87 ManuallyOpenedTestDirectorySetterUpper* metadb() { return &metadb_; } |
| 86 MockConnectionManager* connection() { return connection_.get(); } | 88 MockConnectionManager* connection() { return connection_.get(); } |
| 87 SyncerThread* syncer_thread() { return syncer_thread_; } | 89 SyncerThread* syncer_thread() { return syncer_thread_; } |
| 88 | 90 |
| 89 // Waits an indefinite amount of sync cycles for the syncer thread to become | 91 // Waits an indefinite amount of sync cycles for the syncer thread to become |
| 90 // throttled. Only call this if a throttle is supposed to occur! | 92 // throttled. Only call this if a throttle is supposed to occur! |
| 91 void WaitForThrottle() { | 93 bool WaitForThrottle() { |
| 92 while (!syncer_thread()->IsSyncingCurrentlySilenced()) | 94 int max_cycles = 5; |
| 93 sync_cycle_ended_event_.Wait(); | 95 while (max_cycles && !syncer_thread()->IsSyncingCurrentlySilenced()) { |
| 96 sync_cycle_ended_event_.TimedWait(max_wait_time_); |
| 97 max_cycles--; |
| 98 } |
| 99 |
| 100 return syncer_thread()->IsSyncingCurrentlySilenced(); |
| 94 } | 101 } |
| 95 | 102 |
| 96 void WaitForDisconnect() { | 103 void WaitForDisconnect() { |
| 97 // Wait for the SyncerThread to detect loss of connection, up to a max of | 104 // Wait for the SyncerThread to detect loss of connection, up to a max of |
| 98 // 10 seconds to timeout the test. | 105 // 10 seconds to timeout the test. |
| 99 AutoLock lock(syncer_thread()->lock_); | 106 AutoLock lock(syncer_thread()->lock_); |
| 100 TimeTicks start = TimeTicks::Now(); | 107 TimeTicks start = TimeTicks::Now(); |
| 101 TimeDelta ten_seconds = TimeDelta::FromSeconds(10); | 108 TimeDelta ten_seconds = TimeDelta::FromSeconds(10); |
| 102 while (syncer_thread()->vault_.connected_) { | 109 while (syncer_thread()->vault_.connected_) { |
| 103 syncer_thread()->vault_field_changed_.TimedWait(ten_seconds); | 110 syncer_thread()->vault_field_changed_.TimedWait(ten_seconds); |
| 104 if (TimeTicks::Now() - start > ten_seconds) | 111 if (TimeTicks::Now() - start > ten_seconds) |
| 105 break; | 112 break; |
| 106 } | 113 } |
| 107 EXPECT_FALSE(syncer_thread()->vault_.connected_); | 114 EXPECT_FALSE(syncer_thread()->vault_.connected_); |
| 108 } | 115 } |
| 109 | 116 |
| 110 bool Pause(ListenerMock* listener) { | 117 bool Pause(ListenerMock* listener) { |
| 111 WaitableEvent event(false, false); | 118 WaitableEvent event(false, false); |
| 112 EXPECT_CALL(*listener, HandleChannelEvent( | 119 EXPECT_CALL(*listener, HandleChannelEvent( |
| 113 Field(&SyncerEvent::what_happened, SyncerEvent::PAUSED))). | 120 Field(&SyncerEvent::what_happened, SyncerEvent::PAUSED))). |
| 114 WillOnce(SignalEvent(&event)); | 121 WillOnce(SignalEvent(&event)); |
| 115 if (!syncer_thread()->RequestPause()) | 122 if (!syncer_thread()->RequestPause()) |
| 116 return false; | 123 return false; |
| 117 event.Wait(); | 124 return event.TimedWait(max_wait_time_); |
| 118 return true; | |
| 119 } | 125 } |
| 120 | 126 |
| 121 bool Resume(ListenerMock* listener) { | 127 bool Resume(ListenerMock* listener) { |
| 122 WaitableEvent event(false, false); | 128 WaitableEvent event(false, false); |
| 123 EXPECT_CALL(*listener, HandleChannelEvent( | 129 EXPECT_CALL(*listener, HandleChannelEvent( |
| 124 Field(&SyncerEvent::what_happened, SyncerEvent::RESUMED))). | 130 Field(&SyncerEvent::what_happened, SyncerEvent::RESUMED))). |
| 125 WillOnce(SignalEvent(&event)); | 131 WillOnce(SignalEvent(&event)); |
| 126 if (!syncer_thread()->RequestResume()) | 132 if (!syncer_thread()->RequestResume()) |
| 127 return false; | 133 return false; |
| 128 event.Wait(); | 134 return event.TimedWait(max_wait_time_); |
| 129 return true; | |
| 130 } | 135 } |
| 131 | 136 |
| 132 void PreventThreadFromPolling() { | 137 void PreventThreadFromPolling() { |
| 133 const TimeDelta poll_interval = TimeDelta::FromMinutes(5); | 138 const TimeDelta poll_interval = TimeDelta::FromMinutes(5); |
| 134 syncer_thread()->SetSyncerShortPollInterval(poll_interval); | 139 syncer_thread()->SetSyncerShortPollInterval(poll_interval); |
| 135 } | 140 } |
| 136 | 141 |
| 137 private: | 142 private: |
| 138 | 143 |
| 139 void HandleChannelEvent(const SyncerEvent& event) { | 144 void HandleChannelEvent(const SyncerEvent& event) { |
| 140 if (event.what_happened == SyncerEvent::SYNC_CYCLE_ENDED) | 145 if (event.what_happened == SyncerEvent::SYNC_CYCLE_ENDED) |
| 141 sync_cycle_ended_event_.Signal(); | 146 sync_cycle_ended_event_.Signal(); |
| 142 } | 147 } |
| 143 | 148 |
| 149 protected: |
| 150 TimeDelta max_wait_time_; |
| 151 |
| 152 private: |
| 144 ManuallyOpenedTestDirectorySetterUpper metadb_; | 153 ManuallyOpenedTestDirectorySetterUpper metadb_; |
| 145 scoped_ptr<MockConnectionManager> connection_; | 154 scoped_ptr<MockConnectionManager> connection_; |
| 146 scoped_ptr<AllStatus> allstatus_; | 155 scoped_ptr<AllStatus> allstatus_; |
| 147 scoped_refptr<SyncerThread> syncer_thread_; | 156 scoped_refptr<SyncerThread> syncer_thread_; |
| 148 scoped_refptr<ModelSafeWorker> worker_; | 157 scoped_refptr<ModelSafeWorker> worker_; |
| 149 scoped_ptr<ChannelHookup<SyncerEvent> > syncer_event_hookup_; | 158 scoped_ptr<ChannelHookup<SyncerEvent> > syncer_event_hookup_; |
| 150 base::WaitableEvent sync_cycle_ended_event_; | 159 base::WaitableEvent sync_cycle_ended_event_; |
| 151 DISALLOW_COPY_AND_ASSIGN(SyncerThreadWithSyncerTest); | 160 DISALLOW_COPY_AND_ASSIGN(SyncerThreadWithSyncerTest); |
| 152 }; | 161 }; |
| 153 | 162 |
| (...skipping 540 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 694 // mode and fail on multiple requests. | 703 // mode and fail on multiple requests. |
| 695 connection()->ThrottleNextRequest(&interceptor); | 704 connection()->ThrottleNextRequest(&interceptor); |
| 696 | 705 |
| 697 // Try to trigger a sync (we have a really short poll interval already). | 706 // Try to trigger a sync (we have a really short poll interval already). |
| 698 syncer_thread()->NudgeSyncer(0, SyncerThread::kUnknown); | 707 syncer_thread()->NudgeSyncer(0, SyncerThread::kUnknown); |
| 699 syncer_thread()->NudgeSyncer(0, SyncerThread::kUnknown); | 708 syncer_thread()->NudgeSyncer(0, SyncerThread::kUnknown); |
| 700 | 709 |
| 701 // Wait until the syncer thread reports that it is throttled. Any further | 710 // Wait until the syncer thread reports that it is throttled. Any further |
| 702 // sync share interceptions will result in failure. If things are broken, | 711 // sync share interceptions will result in failure. If things are broken, |
| 703 // we may never halt. | 712 // we may never halt. |
| 704 WaitForThrottle(); | 713 ASSERT_TRUE(WaitForThrottle()); |
| 705 EXPECT_TRUE(syncer_thread()->IsSyncingCurrentlySilenced()); | 714 EXPECT_TRUE(syncer_thread()->IsSyncingCurrentlySilenced()); |
| 706 | 715 |
| 707 EXPECT_TRUE(syncer_thread()->Stop(2000)); | 716 EXPECT_TRUE(syncer_thread()->Stop(2000)); |
| 708 } | 717 } |
| 709 | 718 |
| 710 TEST_F(SyncerThreadWithSyncerTest, AuthInvalid) { | 719 TEST_F(SyncerThreadWithSyncerTest, AuthInvalid) { |
| 711 SyncShareIntercept interceptor; | 720 SyncShareIntercept interceptor; |
| 712 connection()->SetMidCommitObserver(&interceptor); | 721 connection()->SetMidCommitObserver(&interceptor); |
| 713 const TimeDelta poll_interval = TimeDelta::FromMilliseconds(1); | 722 const TimeDelta poll_interval = TimeDelta::FromMilliseconds(1); |
| 714 | 723 |
| (...skipping 27 matching lines...) Expand all Loading... |
| 742 HttpResponse::SERVER_CONNECTION_OK, | 751 HttpResponse::SERVER_CONNECTION_OK, |
| 743 true}; | 752 true}; |
| 744 connection()->channel()->NotifyListeners(e); | 753 connection()->channel()->NotifyListeners(e); |
| 745 | 754 |
| 746 interceptor.WaitForSyncShare(1, TimeDelta::FromSeconds(10)); | 755 interceptor.WaitForSyncShare(1, TimeDelta::FromSeconds(10)); |
| 747 EXPECT_FALSE(interceptor.times_sync_occured().empty()); | 756 EXPECT_FALSE(interceptor.times_sync_occured().empty()); |
| 748 | 757 |
| 749 EXPECT_TRUE(syncer_thread()->Stop(2000)); | 758 EXPECT_TRUE(syncer_thread()->Stop(2000)); |
| 750 } | 759 } |
| 751 | 760 |
| 752 // Timesout http:://crbug.com/39070 | 761 // TODO(skrul): The "Pause" and "PauseWhenNotConnected" tests are |
| 753 TEST_F(SyncerThreadWithSyncerTest, DISABLED_Pause) { | 762 // marked FLAKY because they sometimes fail on the Windows buildbots. |
| 763 // I have been unable to reproduce this hang after extensive testing |
| 764 // on a local Windows machine so these tests will remain flaky in |
| 765 // order to help diagnose the problem. |
| 766 // |
| 767 // This issue is tracked at http://crbug.com/39070. |
| 768 TEST_F(SyncerThreadWithSyncerTest, FLAKY_Pause) { |
| 754 WaitableEvent sync_cycle_ended_event(false, false); | 769 WaitableEvent sync_cycle_ended_event(false, false); |
| 755 WaitableEvent paused_event(false, false); | 770 WaitableEvent paused_event(false, false); |
| 756 WaitableEvent resumed_event(false, false); | 771 WaitableEvent resumed_event(false, false); |
| 757 PreventThreadFromPolling(); | 772 PreventThreadFromPolling(); |
| 758 | 773 |
| 759 ListenerMock listener; | 774 ListenerMock listener; |
| 760 scoped_ptr<ChannelHookup<SyncerEvent> > hookup; | 775 scoped_ptr<ChannelHookup<SyncerEvent> > hookup; |
| 761 hookup.reset(syncer_thread()->relay_channel()->AddObserver(&listener)); | 776 hookup.reset(syncer_thread()->relay_channel()->AddObserver(&listener)); |
| 762 | 777 |
| 763 EXPECT_CALL(listener, HandleChannelEvent( | 778 EXPECT_CALL(listener, HandleChannelEvent( |
| 764 Field(&SyncerEvent::what_happened, SyncerEvent::STATUS_CHANGED))). | 779 Field(&SyncerEvent::what_happened, SyncerEvent::STATUS_CHANGED))). |
| 765 Times(AnyNumber()); | 780 Times(AnyNumber()); |
| 766 | 781 |
| 767 // Wait for the initial sync to complete. | 782 // Wait for the initial sync to complete. |
| 768 EXPECT_CALL(listener, HandleChannelEvent( | 783 EXPECT_CALL(listener, HandleChannelEvent( |
| 769 Field(&SyncerEvent::what_happened, SyncerEvent::SYNC_CYCLE_ENDED))). | 784 Field(&SyncerEvent::what_happened, SyncerEvent::SYNC_CYCLE_ENDED))). |
| 770 WillOnce(SignalEvent(&sync_cycle_ended_event)); | 785 WillOnce(SignalEvent(&sync_cycle_ended_event)); |
| 771 ASSERT_TRUE(syncer_thread()->Start()); | 786 ASSERT_TRUE(syncer_thread()->Start()); |
| 772 metadb()->Open(); | 787 metadb()->Open(); |
| 773 sync_cycle_ended_event.Wait(); | 788 ASSERT_TRUE(sync_cycle_ended_event.TimedWait(max_wait_time_)); |
| 774 | 789 |
| 775 // Request a pause. | 790 // Request a pause. |
| 776 ASSERT_TRUE(Pause(&listener)); | 791 ASSERT_TRUE(Pause(&listener)); |
| 777 | 792 |
| 778 // Resuming the pause. | 793 // Resuming the pause. |
| 779 ASSERT_TRUE(Resume(&listener)); | 794 ASSERT_TRUE(Resume(&listener)); |
| 780 | 795 |
| 781 // Not paused, should fail. | 796 // Not paused, should fail. |
| 782 EXPECT_FALSE(syncer_thread()->RequestResume()); | 797 EXPECT_FALSE(syncer_thread()->RequestResume()); |
| 783 | 798 |
| 784 // Request a pause. | 799 // Request a pause. |
| 785 ASSERT_TRUE(Pause(&listener)); | 800 ASSERT_TRUE(Pause(&listener)); |
| 786 | 801 |
| 787 // Nudge the syncer, this should do nothing while we are paused. | 802 // Nudge the syncer, this should do nothing while we are paused. |
| 788 syncer_thread()->NudgeSyncer(0, SyncerThread::kUnknown); | 803 syncer_thread()->NudgeSyncer(0, SyncerThread::kUnknown); |
| 789 | 804 |
| 790 // Resuming will cause the nudge to be processed and a sync cycle to run. | 805 // Resuming will cause the nudge to be processed and a sync cycle to run. |
| 791 EXPECT_CALL(listener, HandleChannelEvent( | 806 EXPECT_CALL(listener, HandleChannelEvent( |
| 792 Field(&SyncerEvent::what_happened, SyncerEvent::SYNC_CYCLE_ENDED))). | 807 Field(&SyncerEvent::what_happened, SyncerEvent::SYNC_CYCLE_ENDED))). |
| 793 WillOnce(SignalEvent(&sync_cycle_ended_event)); | 808 WillOnce(SignalEvent(&sync_cycle_ended_event)); |
| 794 ASSERT_TRUE(Resume(&listener)); | 809 ASSERT_TRUE(Resume(&listener)); |
| 795 sync_cycle_ended_event.Wait(); | 810 ASSERT_TRUE(sync_cycle_ended_event.TimedWait(max_wait_time_)); |
| 796 | 811 |
| 797 EXPECT_TRUE(syncer_thread()->Stop(2000)); | 812 EXPECT_TRUE(syncer_thread()->Stop(2000)); |
| 798 } | 813 } |
| 799 | 814 |
| 800 TEST_F(SyncerThreadWithSyncerTest, StartWhenNotConnected) { | 815 TEST_F(SyncerThreadWithSyncerTest, StartWhenNotConnected) { |
| 801 WaitableEvent sync_cycle_ended_event(false, false); | 816 WaitableEvent sync_cycle_ended_event(false, false); |
| 802 WaitableEvent event(false, false); | 817 WaitableEvent event(false, false); |
| 803 ListenerMock listener; | 818 ListenerMock listener; |
| 804 scoped_ptr<ChannelHookup<SyncerEvent> > hookup; | 819 scoped_ptr<ChannelHookup<SyncerEvent> > hookup; |
| 805 hookup.reset(syncer_thread()->relay_channel()->AddObserver(&listener)); | 820 hookup.reset(syncer_thread()->relay_channel()->AddObserver(&listener)); |
| 806 PreventThreadFromPolling(); | 821 PreventThreadFromPolling(); |
| 807 | 822 |
| 808 EXPECT_CALL(listener, HandleChannelEvent( | 823 EXPECT_CALL(listener, HandleChannelEvent( |
| 809 Field(&SyncerEvent::what_happened, SyncerEvent::STATUS_CHANGED))). | 824 Field(&SyncerEvent::what_happened, SyncerEvent::STATUS_CHANGED))). |
| 810 Times(AnyNumber()); | 825 Times(AnyNumber()); |
| 811 | 826 |
| 812 connection()->SetServerNotReachable(); | 827 connection()->SetServerNotReachable(); |
| 813 metadb()->Open(); | 828 metadb()->Open(); |
| 814 | 829 |
| 815 // Syncer thread will always go through once cycle at the start, | 830 // Syncer thread will always go through once cycle at the start, |
| 816 // then it will wait for a connection. | 831 // then it will wait for a connection. |
| 817 EXPECT_CALL(listener, HandleChannelEvent( | 832 EXPECT_CALL(listener, HandleChannelEvent( |
| 818 Field(&SyncerEvent::what_happened, SyncerEvent::SYNC_CYCLE_ENDED))). | 833 Field(&SyncerEvent::what_happened, SyncerEvent::SYNC_CYCLE_ENDED))). |
| 819 WillOnce(SignalEvent(&sync_cycle_ended_event)); | 834 WillOnce(SignalEvent(&sync_cycle_ended_event)); |
| 820 EXPECT_CALL(listener, HandleChannelEvent( | 835 EXPECT_CALL(listener, HandleChannelEvent( |
| 821 Field(&SyncerEvent::what_happened, SyncerEvent::WAITING_FOR_CONNECTION))). | 836 Field(&SyncerEvent::what_happened, SyncerEvent::WAITING_FOR_CONNECTION))). |
| 822 WillOnce(SignalEvent(&event)); | 837 WillOnce(SignalEvent(&event)); |
| 823 ASSERT_TRUE(syncer_thread()->Start()); | 838 ASSERT_TRUE(syncer_thread()->Start()); |
| 824 sync_cycle_ended_event.Wait(); | 839 ASSERT_TRUE(sync_cycle_ended_event.TimedWait(max_wait_time_)); |
| 825 event.Wait(); | 840 ASSERT_TRUE(event.TimedWait(max_wait_time_)); |
| 826 | 841 |
| 827 // Connect, will put the syncer thread into its usually poll wait. | 842 // Connect, will put the syncer thread into its usually poll wait. |
| 828 EXPECT_CALL(listener, HandleChannelEvent( | 843 EXPECT_CALL(listener, HandleChannelEvent( |
| 829 Field(&SyncerEvent::what_happened, SyncerEvent::CONNECTED))). | 844 Field(&SyncerEvent::what_happened, SyncerEvent::CONNECTED))). |
| 830 WillOnce(SignalEvent(&event)); | 845 WillOnce(SignalEvent(&event)); |
| 831 connection()->SetServerReachable(); | 846 connection()->SetServerReachable(); |
| 832 event.Wait(); | 847 ASSERT_TRUE(event.TimedWait(max_wait_time_)); |
| 833 | 848 |
| 834 // Nudge the syncer to complete a cycle. | 849 // Nudge the syncer to complete a cycle. |
| 835 EXPECT_CALL(listener, HandleChannelEvent( | 850 EXPECT_CALL(listener, HandleChannelEvent( |
| 836 Field(&SyncerEvent::what_happened, SyncerEvent::SYNC_CYCLE_ENDED))). | 851 Field(&SyncerEvent::what_happened, SyncerEvent::SYNC_CYCLE_ENDED))). |
| 837 WillOnce(SignalEvent(&sync_cycle_ended_event)); | 852 WillOnce(SignalEvent(&sync_cycle_ended_event)); |
| 838 syncer_thread()->NudgeSyncer(0, SyncerThread::kUnknown); | 853 syncer_thread()->NudgeSyncer(0, SyncerThread::kUnknown); |
| 839 sync_cycle_ended_event.Wait(); | 854 ASSERT_TRUE(sync_cycle_ended_event.TimedWait(max_wait_time_)); |
| 840 | 855 |
| 841 EXPECT_TRUE(syncer_thread()->Stop(2000)); | 856 EXPECT_TRUE(syncer_thread()->Stop(2000)); |
| 842 } | 857 } |
| 843 | 858 |
| 844 // See bug 39070. | 859 // TODO(skrul): See TODO comment on the "Pause" test above for an |
| 860 // explanation of the usage of FLAKY here. |
| 845 TEST_F(SyncerThreadWithSyncerTest, FLAKY_PauseWhenNotConnected) { | 861 TEST_F(SyncerThreadWithSyncerTest, FLAKY_PauseWhenNotConnected) { |
| 846 WaitableEvent sync_cycle_ended_event(false, false); | 862 WaitableEvent sync_cycle_ended_event(false, false); |
| 847 WaitableEvent event(false, false); | 863 WaitableEvent event(false, false); |
| 848 ListenerMock listener; | 864 ListenerMock listener; |
| 849 scoped_ptr<ChannelHookup<SyncerEvent> > hookup; | 865 scoped_ptr<ChannelHookup<SyncerEvent> > hookup; |
| 850 hookup.reset(syncer_thread()->relay_channel()->AddObserver(&listener)); | 866 hookup.reset(syncer_thread()->relay_channel()->AddObserver(&listener)); |
| 851 PreventThreadFromPolling(); | 867 PreventThreadFromPolling(); |
| 852 | 868 |
| 853 EXPECT_CALL(listener, HandleChannelEvent( | 869 EXPECT_CALL(listener, HandleChannelEvent( |
| 854 Field(&SyncerEvent::what_happened, SyncerEvent::STATUS_CHANGED))). | 870 Field(&SyncerEvent::what_happened, SyncerEvent::STATUS_CHANGED))). |
| 855 Times(AnyNumber()); | 871 Times(AnyNumber()); |
| 856 | 872 |
| 857 // Put the thread into a "waiting for connection" state. | 873 // Put the thread into a "waiting for connection" state. |
| 858 connection()->SetServerNotReachable(); | 874 connection()->SetServerNotReachable(); |
| 859 EXPECT_CALL(listener, HandleChannelEvent( | 875 EXPECT_CALL(listener, HandleChannelEvent( |
| 860 Field(&SyncerEvent::what_happened, SyncerEvent::SYNC_CYCLE_ENDED))). | 876 Field(&SyncerEvent::what_happened, SyncerEvent::SYNC_CYCLE_ENDED))). |
| 861 WillOnce(SignalEvent(&sync_cycle_ended_event)); | 877 WillOnce(SignalEvent(&sync_cycle_ended_event)); |
| 862 EXPECT_CALL(listener, HandleChannelEvent( | 878 EXPECT_CALL(listener, HandleChannelEvent( |
| 863 Field(&SyncerEvent::what_happened, SyncerEvent::WAITING_FOR_CONNECTION))). | 879 Field(&SyncerEvent::what_happened, SyncerEvent::WAITING_FOR_CONNECTION))). |
| 864 WillOnce(SignalEvent(&event)); | 880 WillOnce(SignalEvent(&event)); |
| 865 metadb()->Open(); | 881 metadb()->Open(); |
| 866 ASSERT_TRUE(syncer_thread()->Start()); | 882 ASSERT_TRUE(syncer_thread()->Start()); |
| 867 sync_cycle_ended_event.Wait(); | 883 ASSERT_TRUE(sync_cycle_ended_event.TimedWait(max_wait_time_)); |
| 868 event.Wait(); | 884 ASSERT_TRUE(event.TimedWait(max_wait_time_)); |
| 869 | 885 |
| 870 // Pause and resume the thread while waiting for a connection. | 886 // Pause and resume the thread while waiting for a connection. |
| 871 ASSERT_TRUE(Pause(&listener)); | 887 ASSERT_TRUE(Pause(&listener)); |
| 872 ASSERT_TRUE(Resume(&listener)); | 888 ASSERT_TRUE(Resume(&listener)); |
| 873 | 889 |
| 874 // Make a connection and let the syncer cycle. | 890 // Make a connection and let the syncer cycle. |
| 875 EXPECT_CALL(listener, HandleChannelEvent( | 891 EXPECT_CALL(listener, HandleChannelEvent( |
| 876 Field(&SyncerEvent::what_happened, SyncerEvent::SYNC_CYCLE_ENDED))). | 892 Field(&SyncerEvent::what_happened, SyncerEvent::SYNC_CYCLE_ENDED))). |
| 877 WillOnce(SignalEvent(&sync_cycle_ended_event)); | 893 WillOnce(SignalEvent(&sync_cycle_ended_event)); |
| 878 EXPECT_CALL(listener, HandleChannelEvent( | 894 EXPECT_CALL(listener, HandleChannelEvent( |
| 879 Field(&SyncerEvent::what_happened, SyncerEvent::CONNECTED))). | 895 Field(&SyncerEvent::what_happened, SyncerEvent::CONNECTED))). |
| 880 WillOnce(SignalEvent(&event)); | 896 WillOnce(SignalEvent(&event)); |
| 881 connection()->SetServerReachable(); | 897 connection()->SetServerReachable(); |
| 882 event.Wait(); | 898 ASSERT_TRUE(event.TimedWait(max_wait_time_)); |
| 883 syncer_thread()->NudgeSyncer(0, SyncerThread::kUnknown); | 899 syncer_thread()->NudgeSyncer(0, SyncerThread::kUnknown); |
| 884 sync_cycle_ended_event.Wait(); | 900 ASSERT_TRUE(sync_cycle_ended_event.TimedWait(max_wait_time_)); |
| 885 | 901 |
| 886 // Disconnect and get into the waiting for a connection state. | 902 // Disconnect and get into the waiting for a connection state. |
| 887 EXPECT_CALL(listener, HandleChannelEvent( | 903 EXPECT_CALL(listener, HandleChannelEvent( |
| 888 Field(&SyncerEvent::what_happened, SyncerEvent::WAITING_FOR_CONNECTION))). | 904 Field(&SyncerEvent::what_happened, SyncerEvent::WAITING_FOR_CONNECTION))). |
| 889 WillOnce(SignalEvent(&event)); | 905 WillOnce(SignalEvent(&event)); |
| 890 connection()->SetServerNotReachable(); | 906 connection()->SetServerNotReachable(); |
| 891 event.Wait(); | 907 ASSERT_TRUE(event.TimedWait(max_wait_time_)); |
| 892 | 908 |
| 893 // Pause so we can test getting a connection while paused. | 909 // Pause so we can test getting a connection while paused. |
| 894 ASSERT_TRUE(Pause(&listener)); | 910 ASSERT_TRUE(Pause(&listener)); |
| 895 | 911 |
| 896 // Get a connection then resume. | 912 // Get a connection then resume. |
| 897 EXPECT_CALL(listener, HandleChannelEvent( | 913 EXPECT_CALL(listener, HandleChannelEvent( |
| 898 Field(&SyncerEvent::what_happened, SyncerEvent::CONNECTED))). | 914 Field(&SyncerEvent::what_happened, SyncerEvent::CONNECTED))). |
| 899 WillOnce(SignalEvent(&event)); | 915 WillOnce(SignalEvent(&event)); |
| 900 connection()->SetServerReachable(); | 916 connection()->SetServerReachable(); |
| 901 event.Wait(); | 917 ASSERT_TRUE(event.TimedWait(max_wait_time_)); |
| 902 | 918 |
| 903 ASSERT_TRUE(Resume(&listener)); | 919 ASSERT_TRUE(Resume(&listener)); |
| 904 | 920 |
| 905 // Cycle the syncer to show we are not longer paused. | 921 // Cycle the syncer to show we are not longer paused. |
| 906 EXPECT_CALL(listener, HandleChannelEvent( | 922 EXPECT_CALL(listener, HandleChannelEvent( |
| 907 Field(&SyncerEvent::what_happened, SyncerEvent::SYNC_CYCLE_ENDED))). | 923 Field(&SyncerEvent::what_happened, SyncerEvent::SYNC_CYCLE_ENDED))). |
| 908 WillOnce(SignalEvent(&sync_cycle_ended_event)); | 924 WillOnce(SignalEvent(&sync_cycle_ended_event)); |
| 909 syncer_thread()->NudgeSyncer(0, SyncerThread::kUnknown); | 925 syncer_thread()->NudgeSyncer(0, SyncerThread::kUnknown); |
| 910 sync_cycle_ended_event.Wait(); | 926 ASSERT_TRUE(sync_cycle_ended_event.TimedWait(max_wait_time_)); |
| 911 | 927 |
| 912 EXPECT_TRUE(syncer_thread()->Stop(2000)); | 928 EXPECT_TRUE(syncer_thread()->Stop(2000)); |
| 913 } | 929 } |
| 914 | 930 |
| 915 TEST_F(SyncerThreadWithSyncerTest, PauseResumeWhenNotRunning) { | 931 TEST_F(SyncerThreadWithSyncerTest, PauseResumeWhenNotRunning) { |
| 916 WaitableEvent sync_cycle_ended_event(false, false); | 932 WaitableEvent sync_cycle_ended_event(false, false); |
| 917 WaitableEvent event(false, false); | 933 WaitableEvent event(false, false); |
| 918 ListenerMock listener; | 934 ListenerMock listener; |
| 919 scoped_ptr<ChannelHookup<SyncerEvent> > hookup; | 935 scoped_ptr<ChannelHookup<SyncerEvent> > hookup; |
| 920 hookup.reset(syncer_thread()->relay_channel()->AddObserver(&listener)); | 936 hookup.reset(syncer_thread()->relay_channel()->AddObserver(&listener)); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 931 // Pause the thread then start the syncer. | 947 // Pause the thread then start the syncer. |
| 932 ASSERT_TRUE(Pause(&listener)); | 948 ASSERT_TRUE(Pause(&listener)); |
| 933 metadb()->Open(); | 949 metadb()->Open(); |
| 934 ASSERT_TRUE(syncer_thread()->Start()); | 950 ASSERT_TRUE(syncer_thread()->Start()); |
| 935 | 951 |
| 936 // Resume and let the syncer cycle. | 952 // Resume and let the syncer cycle. |
| 937 EXPECT_CALL(listener, HandleChannelEvent( | 953 EXPECT_CALL(listener, HandleChannelEvent( |
| 938 Field(&SyncerEvent::what_happened, SyncerEvent::SYNC_CYCLE_ENDED))). | 954 Field(&SyncerEvent::what_happened, SyncerEvent::SYNC_CYCLE_ENDED))). |
| 939 WillOnce(SignalEvent(&sync_cycle_ended_event)); | 955 WillOnce(SignalEvent(&sync_cycle_ended_event)); |
| 940 ASSERT_TRUE(Resume(&listener)); | 956 ASSERT_TRUE(Resume(&listener)); |
| 941 sync_cycle_ended_event.Wait(); | 957 ASSERT_TRUE(sync_cycle_ended_event.TimedWait(max_wait_time_)); |
| 942 | 958 |
| 943 EXPECT_TRUE(syncer_thread()->Stop(2000)); | 959 EXPECT_TRUE(syncer_thread()->Stop(2000)); |
| 944 } | 960 } |
| 945 | 961 |
| 946 } // namespace browser_sync | 962 } // namespace browser_sync |
| OLD | NEW |