Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(89)

Side by Side Diff: chrome/browser/sync/engine/sync_scheduler_whitebox_unittest.cc

Issue 9699057: [Sync] Move 'sync' target to sync/ (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Address Tim's comments Created 8 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2012 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/message_loop.h"
6 #include "base/time.h"
7 #include "chrome/browser/sync/engine/sync_scheduler.h"
8 #include "chrome/browser/sync/sessions/sync_session_context.h"
9 #include "chrome/browser/sync/sessions/test_util.h"
10 #include "chrome/browser/sync/test/engine/fake_model_safe_worker_registrar.h"
11 #include "chrome/browser/sync/test/engine/test_directory_setter_upper.h"
12 #include "chrome/browser/sync/test/engine/mock_connection_manager.h"
13 #include "chrome/browser/sync/test/fake_extensions_activity_monitor.h"
14 #include "testing/gmock/include/gmock/gmock.h"
15 #include "testing/gtest/include/gtest/gtest.h"
16
17 using base::TimeDelta;
18 using base::TimeTicks;
19
20 namespace browser_sync {
21 using browser_sync::Syncer;
22 using sessions::SyncSession;
23 using sessions::SyncSessionContext;
24 using sessions::SyncSourceInfo;
25 using sync_pb::GetUpdatesCallerInfo;
26
27 class SyncSchedulerWhiteboxTest : public testing::Test {
28 public:
29 virtual void SetUp() {
30 dir_maker_.SetUp();
31 Syncer* syncer = new Syncer();
32 ModelSafeRoutingInfo routes;
33 routes[syncable::BOOKMARKS] = GROUP_UI;
34 routes[syncable::NIGORI] = GROUP_PASSIVE;
35 registrar_.reset(new FakeModelSafeWorkerRegistrar(routes));
36 connection_.reset(new MockConnectionManager(NULL));
37 connection_->SetServerReachable();
38 context_ =
39 new SyncSessionContext(
40 connection_.get(), dir_maker_.directory(),
41 registrar_.get(), &extensions_activity_monitor_,
42 std::vector<SyncEngineEventListener*>(), NULL);
43 context_->set_notifications_enabled(true);
44 context_->set_account_name("Test");
45 scheduler_.reset(
46 new SyncScheduler("TestSyncSchedulerWhitebox", context_, syncer));
47 }
48
49 virtual void TearDown() {
50 scheduler_.reset();
51 }
52
53 void SetMode(SyncScheduler::Mode mode) {
54 scheduler_->mode_ = mode;
55 }
56
57 void SetLastSyncedTime(base::TimeTicks ticks) {
58 scheduler_->last_sync_session_end_time_ = ticks;
59 }
60
61 void SetServerConnection(bool connected) {
62 scheduler_->server_connection_ok_ = connected;
63 }
64
65 void ResetWaitInterval() {
66 scheduler_->wait_interval_.reset();
67 }
68
69 void SetWaitIntervalToThrottled() {
70 scheduler_->wait_interval_.reset(new SyncScheduler::WaitInterval(
71 SyncScheduler::WaitInterval::THROTTLED, TimeDelta::FromSeconds(1)));
72 }
73
74 void SetWaitIntervalToExponentialBackoff() {
75 scheduler_->wait_interval_.reset(
76 new SyncScheduler::WaitInterval(
77 SyncScheduler::WaitInterval::EXPONENTIAL_BACKOFF,
78 TimeDelta::FromSeconds(1)));
79 }
80
81 void SetWaitIntervalHadNudge(bool had_nudge) {
82 scheduler_->wait_interval_->had_nudge = had_nudge;
83 }
84
85 SyncScheduler::JobProcessDecision DecideOnJob(
86 const SyncScheduler::SyncSessionJob& job) {
87 return scheduler_->DecideOnJob(job);
88 }
89
90 void InitializeSyncerOnNormalMode() {
91 SetMode(SyncScheduler::NORMAL_MODE);
92 ResetWaitInterval();
93 SetServerConnection(true);
94 SetLastSyncedTime(base::TimeTicks::Now());
95 }
96
97 SyncScheduler::JobProcessDecision CreateAndDecideJob(
98 SyncScheduler::SyncSessionJob::SyncSessionJobPurpose purpose) {
99 SyncSession* s = scheduler_->CreateSyncSession(SyncSourceInfo());
100 SyncScheduler::SyncSessionJob job(purpose, TimeTicks::Now(),
101 make_linked_ptr(s),
102 false,
103 FROM_HERE);
104 return DecideOnJob(job);
105 }
106
107 SyncSessionContext* context() { return context_; }
108
109 protected:
110 scoped_ptr<SyncScheduler> scheduler_;
111
112 private:
113 MessageLoop message_loop_;
114 scoped_ptr<MockConnectionManager> connection_;
115 SyncSessionContext* context_;
116 scoped_ptr<FakeModelSafeWorkerRegistrar> registrar_;
117 FakeExtensionsActivityMonitor extensions_activity_monitor_;
118 TestDirectorySetterUpper dir_maker_;
119 };
120
121 TEST_F(SyncSchedulerWhiteboxTest, SaveNudge) {
122 InitializeSyncerOnNormalMode();
123
124 // Now set the mode to configure.
125 SetMode(SyncScheduler::CONFIGURATION_MODE);
126
127 SyncScheduler::JobProcessDecision decision =
128 CreateAndDecideJob(SyncScheduler::SyncSessionJob::NUDGE);
129
130 EXPECT_EQ(decision, SyncScheduler::SAVE);
131 }
132
133 TEST_F(SyncSchedulerWhiteboxTest, SaveNudgeWhileTypeThrottled) {
134 InitializeSyncerOnNormalMode();
135
136 syncable::ModelTypeSet types;
137 types.Put(syncable::BOOKMARKS);
138
139 // Mark bookmarks as throttled.
140 context()->SetUnthrottleTime(types,
141 base::TimeTicks::Now() + base::TimeDelta::FromHours(2));
142
143 syncable::ModelTypePayloadMap types_with_payload;
144 types_with_payload[syncable::BOOKMARKS] = "";
145
146 SyncSourceInfo info(GetUpdatesCallerInfo::LOCAL, types_with_payload);
147 SyncSession* s = scheduler_->CreateSyncSession(info);
148
149 // Now schedule a nudge with just bookmarks and the change is local.
150 SyncScheduler::SyncSessionJob job(SyncScheduler::SyncSessionJob::NUDGE,
151 TimeTicks::Now(),
152 make_linked_ptr(s),
153 false,
154 FROM_HERE);
155
156 SyncScheduler::JobProcessDecision decision = DecideOnJob(job);
157 EXPECT_EQ(decision, SyncScheduler::SAVE);
158 }
159
160 TEST_F(SyncSchedulerWhiteboxTest, ContinueNudge) {
161 InitializeSyncerOnNormalMode();
162
163 SyncScheduler::JobProcessDecision decision = CreateAndDecideJob(
164 SyncScheduler::SyncSessionJob::NUDGE);
165
166 EXPECT_EQ(decision, SyncScheduler::CONTINUE);
167 }
168
169 TEST_F(SyncSchedulerWhiteboxTest, DropPoll) {
170 InitializeSyncerOnNormalMode();
171 SetMode(SyncScheduler::CONFIGURATION_MODE);
172
173 SyncScheduler::JobProcessDecision decision = CreateAndDecideJob(
174 SyncScheduler::SyncSessionJob::POLL);
175
176 EXPECT_EQ(decision, SyncScheduler::DROP);
177 }
178
179 TEST_F(SyncSchedulerWhiteboxTest, ContinuePoll) {
180 InitializeSyncerOnNormalMode();
181
182 SyncScheduler::JobProcessDecision decision = CreateAndDecideJob(
183 SyncScheduler::SyncSessionJob::POLL);
184
185 EXPECT_EQ(decision, SyncScheduler::CONTINUE);
186 }
187
188 TEST_F(SyncSchedulerWhiteboxTest, ContinueConfiguration) {
189 InitializeSyncerOnNormalMode();
190 SetMode(SyncScheduler::CONFIGURATION_MODE);
191
192 SyncScheduler::JobProcessDecision decision = CreateAndDecideJob(
193 SyncScheduler::SyncSessionJob::CONFIGURATION);
194
195 EXPECT_EQ(decision, SyncScheduler::CONTINUE);
196 }
197
198 TEST_F(SyncSchedulerWhiteboxTest, SaveConfigurationWhileThrottled) {
199 InitializeSyncerOnNormalMode();
200 SetMode(SyncScheduler::CONFIGURATION_MODE);
201
202 SetWaitIntervalToThrottled();
203
204 SyncScheduler::JobProcessDecision decision = CreateAndDecideJob(
205 SyncScheduler::SyncSessionJob::CONFIGURATION);
206
207 EXPECT_EQ(decision, SyncScheduler::SAVE);
208 }
209
210 TEST_F(SyncSchedulerWhiteboxTest, SaveNudgeWhileThrottled) {
211 InitializeSyncerOnNormalMode();
212 SetMode(SyncScheduler::CONFIGURATION_MODE);
213
214 SetWaitIntervalToThrottled();
215
216 SyncScheduler::JobProcessDecision decision = CreateAndDecideJob(
217 SyncScheduler::SyncSessionJob::NUDGE);
218
219 EXPECT_EQ(decision, SyncScheduler::SAVE);
220 }
221
222 TEST_F(SyncSchedulerWhiteboxTest,
223 ContinueClearUserDataUnderAllCircumstances) {
224 InitializeSyncerOnNormalMode();
225
226 SetMode(SyncScheduler::CONFIGURATION_MODE);
227 SetWaitIntervalToThrottled();
228 SyncScheduler::JobProcessDecision decision = CreateAndDecideJob(
229 SyncScheduler::SyncSessionJob::CLEAR_USER_DATA);
230 EXPECT_EQ(decision, SyncScheduler::CONTINUE);
231
232 SetMode(SyncScheduler::NORMAL_MODE);
233 SetWaitIntervalToExponentialBackoff();
234 decision = CreateAndDecideJob(
235 SyncScheduler::SyncSessionJob::CLEAR_USER_DATA);
236 EXPECT_EQ(decision, SyncScheduler::CONTINUE);
237 }
238
239 TEST_F(SyncSchedulerWhiteboxTest, ContinueNudgeWhileExponentialBackOff) {
240 InitializeSyncerOnNormalMode();
241 SetMode(SyncScheduler::NORMAL_MODE);
242 SetWaitIntervalToExponentialBackoff();
243
244 SyncScheduler::JobProcessDecision decision = CreateAndDecideJob(
245 SyncScheduler::SyncSessionJob::NUDGE);
246
247 EXPECT_EQ(decision, SyncScheduler::CONTINUE);
248 }
249
250 TEST_F(SyncSchedulerWhiteboxTest, DropNudgeWhileExponentialBackOff) {
251 InitializeSyncerOnNormalMode();
252 SetMode(SyncScheduler::NORMAL_MODE);
253 SetWaitIntervalToExponentialBackoff();
254 SetWaitIntervalHadNudge(true);
255
256 SyncScheduler::JobProcessDecision decision = CreateAndDecideJob(
257 SyncScheduler::SyncSessionJob::NUDGE);
258
259 EXPECT_EQ(decision, SyncScheduler::DROP);
260 }
261
262 TEST_F(SyncSchedulerWhiteboxTest, ContinueCanaryJobConfig) {
263 InitializeSyncerOnNormalMode();
264 SetMode(SyncScheduler::CONFIGURATION_MODE);
265 SetWaitIntervalToExponentialBackoff();
266
267 struct SyncScheduler::SyncSessionJob job;
268 job.purpose = SyncScheduler::SyncSessionJob::CONFIGURATION;
269 job.scheduled_start = TimeTicks::Now();
270 job.is_canary_job = true;
271 SyncScheduler::JobProcessDecision decision = DecideOnJob(job);
272
273 EXPECT_EQ(decision, SyncScheduler::CONTINUE);
274 }
275
276 } // namespace browser_sync
OLDNEW
« no previous file with comments | « chrome/browser/sync/engine/sync_scheduler_unittest.cc ('k') | chrome/browser/sync/engine/syncapi_internal.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698