sync_exponential_backoff_test.cc revision 5d1f7b1de12d16ceb2c938c56701a3e8bfa558f7
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.
4
5#include "base/bind.h"
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"
11
12namespace {
13
14using bookmarks_helper::AddFolder;
15using bookmarks_helper::ModelMatchesVerifier;
16using syncer::sessions::SyncSessionSnapshot;
17
18class SyncExponentialBackoffTest : public SyncTest {
19 public:
20  SyncExponentialBackoffTest() : SyncTest(SINGLE_CLIENT) {}
21  virtual ~SyncExponentialBackoffTest() {}
22
23 private:
24  DISALLOW_COPY_AND_ASSIGN(SyncExponentialBackoffTest);
25};
26
27// Helper class that checks if a sync client has successfully gone through
28// exponential backoff after it encounters an error.
29class ExponentialBackoffChecker : public StatusChangeChecker {
30 public:
31  explicit ExponentialBackoffChecker(const ProfileSyncServiceHarness* harness)
32      : StatusChangeChecker("ExponentialBackoffChecker"),
33        harness_(harness) {
34    DCHECK(harness);
35    const SyncSessionSnapshot& snap = harness_->GetLastSessionSnapshot();
36    retry_verifier_.Initialize(snap);
37  }
38
39  virtual ~ExponentialBackoffChecker() {}
40
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());
47  }
48
49 private:
50  // The sync client for which backoff is being verified.
51  const ProfileSyncServiceHarness* harness_;
52
53  // Keeps track of the number of attempts at exponential backoff and its
54  // related bookkeeping information for verification.
55  RetryVerifier retry_verifier_;
56
57  DISALLOW_COPY_AND_ASSIGN(ExponentialBackoffChecker);
58};
59
60IN_PROC_BROWSER_TEST_F(SyncExponentialBackoffTest, OfflineToOnline) {
61  ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
62
63  // Add an item and ensure that sync is successful.
64  ASSERT_TRUE(AddFolder(0, 0, L"folder1"));
65  ASSERT_TRUE(GetClient(0)->AwaitCommitActivityCompletion());
66
67  // Trigger a network error at the client side.
68  DisableNetwork(GetProfile(0));
69
70  // Add a new item to trigger another sync cycle.
71  ASSERT_TRUE(AddFolder(0, 0, L"folder2"));
72
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"));
78
79  // Recover from the network error.
80  EnableNetwork(GetProfile(0));
81
82  // Verify that sync was able to recover.
83  ASSERT_TRUE(GetClient(0)->AwaitCommitActivityCompletion());
84  ASSERT_TRUE(ModelMatchesVerifier(0));
85}
86
87IN_PROC_BROWSER_TEST_F(SyncExponentialBackoffTest, TransientErrorTest) {
88  ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
89
90  // Add an item and ensure that sync is successful.
91  ASSERT_TRUE(AddFolder(0, 0, L"folder1"));
92  ASSERT_TRUE(GetClient(0)->AwaitCommitActivityCompletion());
93
94  // Trigger a transient error on the server.
95  TriggerTransientError();
96
97  // Add a new item to trigger another sync cycle.
98  ASSERT_TRUE(AddFolder(0, 0, L"folder2"));
99
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"));
105}
106
107}  // namespace
108