| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/bind.h" | 5 #include "base/bind.h" |
| 6 #include "base/macros.h" | 6 #include "base/macros.h" |
| 7 #include "base/strings/stringprintf.h" | 7 #include "base/strings/stringprintf.h" |
| 8 #include "chrome/browser/sync/test/integration/bookmarks_helper.h" | 8 #include "chrome/browser/sync/test/integration/bookmarks_helper.h" |
| 9 #include "chrome/browser/sync/test/integration/retry_verifier.h" | 9 #include "chrome/browser/sync/test/integration/retry_verifier.h" |
| 10 #include "chrome/browser/sync/test/integration/single_client_status_change_check
er.h" | 10 #include "chrome/browser/sync/test/integration/single_client_status_change_check
er.h" |
| 11 #include "chrome/browser/sync/test/integration/sync_integration_test_util.h" | |
| 12 #include "chrome/browser/sync/test/integration/sync_test.h" | 11 #include "chrome/browser/sync/test/integration/sync_test.h" |
| 12 #include "chrome/browser/sync/test/integration/updated_progress_marker_checker.h
" |
| 13 #include "components/browser_sync/profile_sync_service.h" | 13 #include "components/browser_sync/profile_sync_service.h" |
| 14 #include "net/base/network_change_notifier.h" | 14 #include "net/base/network_change_notifier.h" |
| 15 | 15 |
| 16 namespace { | 16 namespace { |
| 17 | 17 |
| 18 using bookmarks_helper::AddFolder; | 18 using bookmarks_helper::AddFolder; |
| 19 using bookmarks_helper::ModelMatchesVerifier; | 19 using bookmarks_helper::ModelMatchesVerifier; |
| 20 using syncer::SyncCycleSnapshot; | 20 using syncer::SyncCycleSnapshot; |
| 21 using sync_integration_test_util::AwaitCommitActivityCompletion; | |
| 22 | 21 |
| 23 class SyncExponentialBackoffTest : public SyncTest { | 22 class SyncExponentialBackoffTest : public SyncTest { |
| 24 public: | 23 public: |
| 25 SyncExponentialBackoffTest() : SyncTest(SINGLE_CLIENT) {} | 24 SyncExponentialBackoffTest() : SyncTest(SINGLE_CLIENT) {} |
| 26 ~SyncExponentialBackoffTest() override {} | 25 ~SyncExponentialBackoffTest() override {} |
| 27 | 26 |
| 28 private: | 27 private: |
| 29 DISALLOW_COPY_AND_ASSIGN(SyncExponentialBackoffTest); | 28 DISALLOW_COPY_AND_ASSIGN(SyncExponentialBackoffTest); |
| 30 }; | 29 }; |
| 31 | 30 |
| 32 // Helper class that checks if a sync client has successfully gone through | 31 // Helper class that checks if a sync client has successfully gone through |
| 33 // exponential backoff after it encounters an error. | 32 // exponential backoff after it encounters an error. |
| 34 class ExponentialBackoffChecker : public SingleClientStatusChangeChecker { | 33 class ExponentialBackoffChecker : public SingleClientStatusChangeChecker { |
| 35 public: | 34 public: |
| 36 explicit ExponentialBackoffChecker(browser_sync::ProfileSyncService* pss) | 35 explicit ExponentialBackoffChecker(browser_sync::ProfileSyncService* pss) |
| 37 : SingleClientStatusChangeChecker(pss) { | 36 : SingleClientStatusChangeChecker(pss) { |
| 38 const SyncCycleSnapshot& snap = service()->GetLastCycleSnapshot(); | 37 const SyncCycleSnapshot& snap = service()->GetLastCycleSnapshot(); |
| 39 retry_verifier_.Initialize(snap); | 38 retry_verifier_.Initialize(snap); |
| 40 } | 39 } |
| 41 | 40 |
| 42 ~ExponentialBackoffChecker() override {} | |
| 43 | |
| 44 // Checks if backoff is complete. Called repeatedly each time PSS notifies | 41 // Checks if backoff is complete. Called repeatedly each time PSS notifies |
| 45 // observers of a state change. | 42 // observers of a state change. |
| 46 bool IsExitConditionSatisfied() override { | 43 bool IsExitConditionSatisfied() override { |
| 47 const SyncCycleSnapshot& snap = service()->GetLastCycleSnapshot(); | 44 const SyncCycleSnapshot& snap = service()->GetLastCycleSnapshot(); |
| 48 retry_verifier_.VerifyRetryInterval(snap); | 45 retry_verifier_.VerifyRetryInterval(snap); |
| 49 return (retry_verifier_.done() && retry_verifier_.Succeeded()); | 46 return (retry_verifier_.done() && retry_verifier_.Succeeded()); |
| 50 } | 47 } |
| 51 | 48 |
| 52 std::string GetDebugMessage() const override { | 49 std::string GetDebugMessage() const override { |
| 53 return base::StringPrintf("Verifying backoff intervals (%d/%d)", | 50 return base::StringPrintf("Verifying backoff intervals (%d/%d)", |
| 54 retry_verifier_.retry_count(), | 51 retry_verifier_.retry_count(), |
| 55 RetryVerifier::kMaxRetry); | 52 RetryVerifier::kMaxRetry); |
| 56 } | 53 } |
| 57 | 54 |
| 58 private: | 55 private: |
| 59 // Keeps track of the number of attempts at exponential backoff and its | 56 // Keeps track of the number of attempts at exponential backoff and its |
| 60 // related bookkeeping information for verification. | 57 // related bookkeeping information for verification. |
| 61 RetryVerifier retry_verifier_; | 58 RetryVerifier retry_verifier_; |
| 62 | 59 |
| 63 DISALLOW_COPY_AND_ASSIGN(ExponentialBackoffChecker); | 60 DISALLOW_COPY_AND_ASSIGN(ExponentialBackoffChecker); |
| 64 }; | 61 }; |
| 65 | 62 |
| 66 IN_PROC_BROWSER_TEST_F(SyncExponentialBackoffTest, OfflineToOnline) { | 63 IN_PROC_BROWSER_TEST_F(SyncExponentialBackoffTest, OfflineToOnline) { |
| 67 ASSERT_TRUE(SetupSync()) << "SetupSync() failed."; | 64 ASSERT_TRUE(SetupSync()) << "SetupSync() failed."; |
| 68 | 65 |
| 69 // Add an item and ensure that sync is successful. | 66 // Add an item and ensure that sync is successful. |
| 70 ASSERT_TRUE(AddFolder(0, 0, "folder1")); | 67 ASSERT_TRUE(AddFolder(0, 0, "folder1")); |
| 71 ASSERT_TRUE(AwaitCommitActivityCompletion(GetSyncService(0))); | 68 ASSERT_TRUE(UpdatedProgressMarkerChecker(GetSyncService(0)).Wait()); |
| 72 | 69 |
| 73 GetFakeServer()->DisableNetwork(); | 70 GetFakeServer()->DisableNetwork(); |
| 74 | 71 |
| 75 // Add a new item to trigger another sync cycle. | 72 // Add a new item to trigger another sync cycle. |
| 76 ASSERT_TRUE(AddFolder(0, 0, "folder2")); | 73 ASSERT_TRUE(AddFolder(0, 0, "folder2")); |
| 77 | 74 |
| 78 // Verify that the client goes into exponential backoff while it is unable to | 75 // Verify that the client goes into exponential backoff while it is unable to |
| 79 // reach the sync server. | 76 // reach the sync server. |
| 80 ExponentialBackoffChecker exponential_backoff_checker(GetSyncService(0)); | 77 ASSERT_TRUE(ExponentialBackoffChecker(GetSyncService(0)).Wait()); |
| 81 exponential_backoff_checker.Wait(); | |
| 82 ASSERT_FALSE(exponential_backoff_checker.TimedOut()); | |
| 83 | 78 |
| 84 // Trigger network change notification and remember time when it happened. | 79 // Trigger network change notification and remember time when it happened. |
| 85 // Ensure that scheduler runs canary job immediately. | 80 // Ensure that scheduler runs canary job immediately. |
| 86 GetFakeServer()->EnableNetwork(); | 81 GetFakeServer()->EnableNetwork(); |
| 87 net::NetworkChangeNotifier::NotifyObserversOfConnectionTypeChangeForTests( | 82 net::NetworkChangeNotifier::NotifyObserversOfConnectionTypeChangeForTests( |
| 88 net::NetworkChangeNotifier::CONNECTION_ETHERNET); | 83 net::NetworkChangeNotifier::CONNECTION_ETHERNET); |
| 89 | 84 |
| 90 base::Time network_notification_time = base::Time::Now(); | 85 base::Time network_notification_time = base::Time::Now(); |
| 91 | 86 |
| 92 // Verify that sync was able to recover. | 87 // Verify that sync was able to recover. |
| 93 ASSERT_TRUE(AwaitCommitActivityCompletion(GetSyncService(0))); | 88 ASSERT_TRUE(UpdatedProgressMarkerChecker(GetSyncService(0)).Wait()); |
| 94 ASSERT_TRUE(ModelMatchesVerifier(0)); | 89 ASSERT_TRUE(ModelMatchesVerifier(0)); |
| 95 | 90 |
| 96 // Verify that recovery time is short. Without canary job recovery time would | 91 // Verify that recovery time is short. Without canary job recovery time would |
| 97 // be more than 5 seconds. | 92 // be more than 5 seconds. |
| 98 base::TimeDelta recovery_time = | 93 base::TimeDelta recovery_time = |
| 99 GetSyncService(0)->GetLastCycleSnapshot().sync_start_time() - | 94 GetSyncService(0)->GetLastCycleSnapshot().sync_start_time() - |
| 100 network_notification_time; | 95 network_notification_time; |
| 101 ASSERT_LE(recovery_time, base::TimeDelta::FromSeconds(2)); | 96 ASSERT_LE(recovery_time, base::TimeDelta::FromSeconds(2)); |
| 102 } | 97 } |
| 103 | 98 |
| 104 IN_PROC_BROWSER_TEST_F(SyncExponentialBackoffTest, TransientErrorTest) { | 99 IN_PROC_BROWSER_TEST_F(SyncExponentialBackoffTest, TransientErrorTest) { |
| 105 ASSERT_TRUE(SetupSync()) << "SetupSync() failed."; | 100 ASSERT_TRUE(SetupSync()) << "SetupSync() failed."; |
| 106 | 101 |
| 107 // Add an item and ensure that sync is successful. | 102 // Add an item and ensure that sync is successful. |
| 108 ASSERT_TRUE(AddFolder(0, 0, "folder1")); | 103 ASSERT_TRUE(AddFolder(0, 0, "folder1")); |
| 109 ASSERT_TRUE(AwaitCommitActivityCompletion(GetSyncService(0))); | 104 ASSERT_TRUE(UpdatedProgressMarkerChecker(GetSyncService(0)).Wait()); |
| 110 | 105 |
| 111 GetFakeServer()->TriggerError(sync_pb::SyncEnums::TRANSIENT_ERROR); | 106 GetFakeServer()->TriggerError(sync_pb::SyncEnums::TRANSIENT_ERROR); |
| 112 | 107 |
| 113 // Add a new item to trigger another sync cycle. | 108 // Add a new item to trigger another sync cycle. |
| 114 ASSERT_TRUE(AddFolder(0, 0, "folder2")); | 109 ASSERT_TRUE(AddFolder(0, 0, "folder2")); |
| 115 | 110 |
| 116 // Verify that the client goes into exponential backoff while it is unable to | 111 // Verify that the client goes into exponential backoff while it is unable to |
| 117 // reach the sync server. | 112 // reach the sync server. |
| 118 ExponentialBackoffChecker exponential_backoff_checker(GetSyncService(0)); | 113 ASSERT_TRUE(ExponentialBackoffChecker(GetSyncService(0)).Wait()); |
| 119 exponential_backoff_checker.Wait(); | |
| 120 ASSERT_FALSE(exponential_backoff_checker.TimedOut()); | |
| 121 } | 114 } |
| 122 | 115 |
| 123 } // namespace | 116 } // namespace |
| OLD | NEW |