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
3 // found in the LICENSE file.
6 #include "chrome/browser/sync/test/integration/bookmarks_helper.h"
7 #include "chrome/browser/sync/test/integration/profile_sync_service_harness.h"
8 #include "chrome/browser/sync/test/integration/retry_verifier.h"
9 #include "chrome/browser/sync/test/integration/status_change_checker.h"
10 #include "chrome/browser/sync/test/integration/sync_test.h"
14 using bookmarks_helper::AddFolder
;
15 using bookmarks_helper::ModelMatchesVerifier
;
16 using syncer::sessions::SyncSessionSnapshot
;
18 class SyncExponentialBackoffTest
: public SyncTest
{
20 SyncExponentialBackoffTest() : SyncTest(SINGLE_CLIENT
) {}
21 virtual ~SyncExponentialBackoffTest() {}
24 DISALLOW_COPY_AND_ASSIGN(SyncExponentialBackoffTest
);
27 // Helper class that checks if a sync client has successfully gone through
28 // exponential backoff after it encounters an error.
29 class ExponentialBackoffChecker
: public StatusChangeChecker
{
31 explicit ExponentialBackoffChecker(const ProfileSyncServiceHarness
* harness
)
32 : StatusChangeChecker("ExponentialBackoffChecker"),
35 const SyncSessionSnapshot
& snap
= harness_
->GetLastSessionSnapshot();
36 retry_verifier_
.Initialize(snap
);
39 virtual ~ExponentialBackoffChecker() {}
41 // Checks if backoff is complete. Called repeatedly each time PSS notifies
42 // observers of a state change.
43 virtual bool IsExitConditionSatisfied() OVERRIDE
{
44 const SyncSessionSnapshot
& snap
= harness_
->GetLastSessionSnapshot();
45 retry_verifier_
.VerifyRetryInterval(snap
);
46 return (retry_verifier_
.done() && retry_verifier_
.Succeeded());
50 // The sync client for which backoff is being verified.
51 const ProfileSyncServiceHarness
* harness_
;
53 // Keeps track of the number of attempts at exponential backoff and its
54 // related bookkeeping information for verification.
55 RetryVerifier retry_verifier_
;
57 DISALLOW_COPY_AND_ASSIGN(ExponentialBackoffChecker
);
60 IN_PROC_BROWSER_TEST_F(SyncExponentialBackoffTest
, OfflineToOnline
) {
61 ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
63 // Add an item and ensure that sync is successful.
64 ASSERT_TRUE(AddFolder(0, 0, L
"folder1"));
65 ASSERT_TRUE(GetClient(0)->AwaitFullSyncCompletion());
67 // Trigger a network error at the client side.
68 DisableNetwork(GetProfile(0));
70 // Add a new item to trigger another sync cycle.
71 ASSERT_TRUE(AddFolder(0, 0, L
"folder2"));
73 // Verify that the client goes into exponential backoff while it is unable to
74 // reach the sync server.
75 ExponentialBackoffChecker
exponential_backoff_checker(GetClient(0));
76 ASSERT_TRUE(GetClient(0)->AwaitStatusChange(&exponential_backoff_checker
,
77 "Checking exponential backoff"));
79 // Recover from the network error.
80 EnableNetwork(GetProfile(0));
82 // Verify that sync was able to recover.
83 ASSERT_TRUE(GetClient(0)->AwaitFullSyncCompletion());
84 ASSERT_TRUE(ModelMatchesVerifier(0));
87 IN_PROC_BROWSER_TEST_F(SyncExponentialBackoffTest
, TransientErrorTest
) {
88 ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
90 // Add an item and ensure that sync is successful.
91 ASSERT_TRUE(AddFolder(0, 0, L
"folder1"));
92 ASSERT_TRUE(GetClient(0)->AwaitFullSyncCompletion());
94 // Trigger a transient error on the server.
95 TriggerTransientError();
97 // Add a new item to trigger another sync cycle.
98 ASSERT_TRUE(AddFolder(0, 0, L
"folder2"));
100 // Verify that the client goes into exponential backoff while it is unable to
101 // reach the sync server.
102 ExponentialBackoffChecker
exponential_backoff_checker(GetClient(0));
103 ASSERT_TRUE(GetClient(0)->AwaitStatusChange(&exponential_backoff_checker
,
104 "Checking exponential backoff"));