sync_exponential_backoff_test.cc revision a1401311d1ab56c4ed0a474bd38c108f75cb0cd9
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 "base/strings/stringprintf.h"
7#include "chrome/browser/sync/test/integration/bookmarks_helper.h"
8#include "chrome/browser/sync/test/integration/profile_sync_service_harness.h"
9#include "chrome/browser/sync/test/integration/retry_verifier.h"
10#include "chrome/browser/sync/test/integration/single_client_status_change_checker.h"
11#include "chrome/browser/sync/test/integration/sync_test.h"
12
13namespace {
14
15using bookmarks_helper::AddFolder;
16using bookmarks_helper::ModelMatchesVerifier;
17using syncer::sessions::SyncSessionSnapshot;
18
19class SyncExponentialBackoffTest : public SyncTest {
20 public:
21  SyncExponentialBackoffTest() : SyncTest(SINGLE_CLIENT) {}
22  virtual ~SyncExponentialBackoffTest() {}
23
24 private:
25  DISALLOW_COPY_AND_ASSIGN(SyncExponentialBackoffTest);
26};
27
28// Helper class that checks if a sync client has successfully gone through
29// exponential backoff after it encounters an error.
30class ExponentialBackoffChecker : public SingleClientStatusChangeChecker {
31 public:
32  explicit ExponentialBackoffChecker(ProfileSyncService* pss)
33        : SingleClientStatusChangeChecker(pss) {
34    const SyncSessionSnapshot& snap = service()->GetLastSessionSnapshot();
35    retry_verifier_.Initialize(snap);
36  }
37
38  virtual ~ExponentialBackoffChecker() {}
39
40  // Checks if backoff is complete. Called repeatedly each time PSS notifies
41  // observers of a state change.
42  virtual bool IsExitConditionSatisfied() OVERRIDE {
43    const SyncSessionSnapshot& snap = service()->GetLastSessionSnapshot();
44    retry_verifier_.VerifyRetryInterval(snap);
45    return (retry_verifier_.done() && retry_verifier_.Succeeded());
46  }
47
48  virtual std::string GetDebugMessage() const OVERRIDE {
49    return base::StringPrintf("Verifying backoff intervals (%d/%d)",
50                              retry_verifier_.retry_count(),
51                              RetryVerifier::kMaxRetry);
52  }
53
54 private:
55  // Keeps track of the number of attempts at exponential backoff and its
56  // related bookkeeping information for verification.
57  RetryVerifier retry_verifier_;
58
59  DISALLOW_COPY_AND_ASSIGN(ExponentialBackoffChecker);
60};
61
62IN_PROC_BROWSER_TEST_F(SyncExponentialBackoffTest, OfflineToOnline) {
63  ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
64
65  // Add an item and ensure that sync is successful.
66  ASSERT_TRUE(AddFolder(0, 0, L"folder1"));
67  ASSERT_TRUE(GetClient(0)->AwaitCommitActivityCompletion());
68
69  // Trigger a network error at the client side.
70  DisableNetwork(GetProfile(0));
71
72  // Add a new item to trigger another sync cycle.
73  ASSERT_TRUE(AddFolder(0, 0, L"folder2"));
74
75  // Verify that the client goes into exponential backoff while it is unable to
76  // reach the sync server.
77  ExponentialBackoffChecker exponential_backoff_checker(
78      GetClient(0)->service());
79  ASSERT_TRUE(GetClient(0)->AwaitStatusChange(&exponential_backoff_checker));
80
81  // Recover from the network error.
82  EnableNetwork(GetProfile(0));
83
84  // Verify that sync was able to recover.
85  ASSERT_TRUE(GetClient(0)->AwaitCommitActivityCompletion());
86  ASSERT_TRUE(ModelMatchesVerifier(0));
87}
88
89IN_PROC_BROWSER_TEST_F(SyncExponentialBackoffTest, TransientErrorTest) {
90  ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
91
92  // Add an item and ensure that sync is successful.
93  ASSERT_TRUE(AddFolder(0, 0, L"folder1"));
94  ASSERT_TRUE(GetClient(0)->AwaitCommitActivityCompletion());
95
96  // Trigger a transient error on the server.
97  TriggerTransientError();
98
99  // Add a new item to trigger another sync cycle.
100  ASSERT_TRUE(AddFolder(0, 0, L"folder2"));
101
102  // Verify that the client goes into exponential backoff while it is unable to
103  // reach the sync server.
104  ExponentialBackoffChecker exponential_backoff_checker(
105      GetClient(0)->service());
106  ASSERT_TRUE(GetClient(0)->AwaitStatusChange(&exponential_backoff_checker));
107}
108
109}  // namespace
110