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

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

Issue 13743003: sync: Finish the SyncScheduler refactor (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix ModelNeutralState forward decl Created 7 years, 8 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
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 "sync/engine/backoff_delay_provider.h"
8 #include "sync/engine/sync_scheduler_impl.h"
9 #include "sync/engine/throttled_data_type_tracker.h"
10 #include "sync/internal_api/public/engine/polling_constants.h"
11 #include "sync/sessions/sync_session_context.h"
12 #include "sync/sessions/test_util.h"
13 #include "sync/test/engine/fake_model_worker.h"
14 #include "sync/test/engine/mock_connection_manager.h"
15 #include "sync/test/engine/test_directory_setter_upper.h"
16 #include "sync/test/fake_extensions_activity_monitor.h"
17 #include "testing/gmock/include/gmock/gmock.h"
18 #include "testing/gtest/include/gtest/gtest.h"
19
20 using base::TimeDelta;
21 using base::TimeTicks;
22
23 namespace syncer {
24 using sessions::SyncSession;
25 using sessions::SyncSessionContext;
26 using sessions::SyncSourceInfo;
27 using sync_pb::GetUpdatesCallerInfo;
28
29 class SyncSchedulerWhiteboxTest : public testing::Test {
30 public:
31 virtual void SetUp() {
32 dir_maker_.SetUp();
33 Syncer* syncer = new Syncer();
34
35 ModelSafeRoutingInfo routes;
36 routes[BOOKMARKS] = GROUP_UI;
37 routes[NIGORI] = GROUP_PASSIVE;
38
39 workers_.push_back(make_scoped_refptr(new FakeModelWorker(GROUP_UI)));
40 workers_.push_back(make_scoped_refptr(new FakeModelWorker(GROUP_PASSIVE)));
41
42 std::vector<ModelSafeWorker*> workers;
43 for (std::vector<scoped_refptr<FakeModelWorker> >::iterator it =
44 workers_.begin(); it != workers_.end(); ++it) {
45 workers.push_back(it->get());
46 }
47
48 connection_.reset(new MockConnectionManager(NULL));
49 throttled_data_type_tracker_.reset(new ThrottledDataTypeTracker(NULL));
50 context_.reset(
51 new SyncSessionContext(
52 connection_.get(), dir_maker_.directory(),
53 workers, &extensions_activity_monitor_,
54 throttled_data_type_tracker_.get(),
55 std::vector<SyncEngineEventListener*>(), NULL, NULL,
56 true, // enable keystore encryption
57 "fake_invalidator_client_id"));
58 context_->set_notifications_enabled(true);
59 context_->set_account_name("Test");
60 scheduler_.reset(
61 new SyncSchedulerImpl("TestSyncSchedulerWhitebox",
62 BackoffDelayProvider::FromDefaults(),
63 context(),
64 syncer));
65 }
66
67 virtual void TearDown() {
68 scheduler_.reset();
69 }
70
71 void SetMode(SyncScheduler::Mode mode) {
72 scheduler_->mode_ = mode;
73 }
74
75 void ResetWaitInterval() {
76 scheduler_->wait_interval_.reset();
77 }
78
79 void SetWaitIntervalToThrottled() {
80 scheduler_->wait_interval_.reset(new SyncSchedulerImpl::WaitInterval(
81 SyncSchedulerImpl::WaitInterval::THROTTLED, TimeDelta::FromSeconds(1)));
82 }
83
84 void SetWaitIntervalToExponentialBackoff() {
85 scheduler_->wait_interval_.reset(
86 new SyncSchedulerImpl::WaitInterval(
87 SyncSchedulerImpl::WaitInterval::EXPONENTIAL_BACKOFF,
88 TimeDelta::FromSeconds(1)));
89 }
90
91 SyncSchedulerImpl::JobProcessDecision DecideOnJob(
92 const SyncSessionJob& job,
93 SyncSchedulerImpl::JobPriority priority) {
94 return scheduler_->DecideOnJob(job, priority);
95 }
96
97 void InitializeSyncerOnNormalMode() {
98 SetMode(SyncScheduler::NORMAL_MODE);
99 ResetWaitInterval();
100 }
101
102 SyncSchedulerImpl::JobProcessDecision CreateAndDecideJob(
103 SyncSessionJob::Purpose purpose) {
104 SyncSessionJob job(purpose, TimeTicks::Now(),
105 SyncSourceInfo(), ConfigurationParams());
106 return DecideOnJob(job, SyncSchedulerImpl::NORMAL_PRIORITY);
107 }
108
109 bool ShouldPoll() {
110 return scheduler_->ShouldPoll();
111 }
112
113 SyncSessionContext* context() { return context_.get(); }
114
115 private:
116 MessageLoop message_loop_;
117 scoped_ptr<MockConnectionManager> connection_;
118 scoped_ptr<SyncSessionContext> context_;
119 std::vector<scoped_refptr<FakeModelWorker> > workers_;
120 FakeExtensionsActivityMonitor extensions_activity_monitor_;
121 scoped_ptr<ThrottledDataTypeTracker> throttled_data_type_tracker_;
122 TestDirectorySetterUpper dir_maker_;
123
124 protected:
125 // Declared here to ensure it is destructed before the objects it references.
126 scoped_ptr<SyncSchedulerImpl> scheduler_;
127 };
128
129 TEST_F(SyncSchedulerWhiteboxTest, SaveNudge) {
130 InitializeSyncerOnNormalMode();
131
132 // Now set the mode to configure.
133 SetMode(SyncScheduler::CONFIGURATION_MODE);
134
135 SyncSchedulerImpl::JobProcessDecision decision =
136 CreateAndDecideJob(SyncSessionJob::NUDGE);
137
138 EXPECT_EQ(decision, SyncSchedulerImpl::SAVE);
139 }
140
141 TEST_F(SyncSchedulerWhiteboxTest, SaveNudgeWhileTypeThrottled) {
142 InitializeSyncerOnNormalMode();
143
144 const ModelTypeSet types(BOOKMARKS);
145
146 // Mark bookmarks as throttled.
147 context()->throttled_data_type_tracker()->SetUnthrottleTime(
148 types, base::TimeTicks::Now() + base::TimeDelta::FromHours(2));
149
150 const ModelTypeInvalidationMap& invalidation_map =
151 ModelTypeSetToInvalidationMap(types, std::string());
152
153 SyncSourceInfo info(GetUpdatesCallerInfo::LOCAL, invalidation_map);
154
155 // Now schedule a nudge with just bookmarks and the change is local.
156 SyncSessionJob job(SyncSessionJob::NUDGE,
157 TimeTicks::Now(),
158 info,
159 ConfigurationParams());
160 SyncSchedulerImpl::JobProcessDecision decision =
161 DecideOnJob(job, SyncSchedulerImpl::NORMAL_PRIORITY);
162 // TODO(tim): This shouldn't drop. Bug 177659.
163 EXPECT_EQ(decision, SyncSchedulerImpl::DROP);
164 }
165
166 TEST_F(SyncSchedulerWhiteboxTest, ContinueNudge) {
167 InitializeSyncerOnNormalMode();
168
169 SyncSchedulerImpl::JobProcessDecision decision = CreateAndDecideJob(
170 SyncSessionJob::NUDGE);
171
172 EXPECT_EQ(decision, SyncSchedulerImpl::CONTINUE);
173 }
174
175 TEST_F(SyncSchedulerWhiteboxTest, ContinuePoll) {
176 InitializeSyncerOnNormalMode();
177 EXPECT_TRUE(ShouldPoll());
178 }
179
180 TEST_F(SyncSchedulerWhiteboxTest, DropPollInConfigureMode) {
181 InitializeSyncerOnNormalMode();
182 SetMode(SyncScheduler::CONFIGURATION_MODE);
183 EXPECT_FALSE(ShouldPoll());
184 }
185
186 TEST_F(SyncSchedulerWhiteboxTest, DropPollWhenThrottled) {
187 InitializeSyncerOnNormalMode();
188 SetWaitIntervalToThrottled();
189 EXPECT_FALSE(ShouldPoll());
190 }
191
192 TEST_F(SyncSchedulerWhiteboxTest, DropPollInBackoff) {
193 InitializeSyncerOnNormalMode();
194 SetWaitIntervalToExponentialBackoff();
195 EXPECT_FALSE(ShouldPoll());
196 }
197
198 TEST_F(SyncSchedulerWhiteboxTest, ContinueConfiguration) {
199 InitializeSyncerOnNormalMode();
200 SetMode(SyncScheduler::CONFIGURATION_MODE);
201
202 SyncSchedulerImpl::JobProcessDecision decision = CreateAndDecideJob(
203 SyncSessionJob::CONFIGURATION);
204
205 EXPECT_EQ(decision, SyncSchedulerImpl::CONTINUE);
206 }
207
208 TEST_F(SyncSchedulerWhiteboxTest, SaveConfigurationWhileThrottled) {
209 InitializeSyncerOnNormalMode();
210 SetMode(SyncScheduler::CONFIGURATION_MODE);
211
212 SetWaitIntervalToThrottled();
213
214 SyncSchedulerImpl::JobProcessDecision decision = CreateAndDecideJob(
215 SyncSessionJob::CONFIGURATION);
216
217 EXPECT_EQ(decision, SyncSchedulerImpl::SAVE);
218 }
219
220 TEST_F(SyncSchedulerWhiteboxTest, SaveNudgeWhileThrottled) {
221 InitializeSyncerOnNormalMode();
222 SetMode(SyncScheduler::CONFIGURATION_MODE);
223
224 SetWaitIntervalToThrottled();
225
226 SyncSchedulerImpl::JobProcessDecision decision = CreateAndDecideJob(
227 SyncSessionJob::NUDGE);
228
229 EXPECT_EQ(decision, SyncSchedulerImpl::SAVE);
230 }
231
232 TEST_F(SyncSchedulerWhiteboxTest, DropNudgeWhileExponentialBackOff) {
233 InitializeSyncerOnNormalMode();
234 SetMode(SyncScheduler::NORMAL_MODE);
235 SetWaitIntervalToExponentialBackoff();
236
237 SyncSchedulerImpl::JobProcessDecision decision = CreateAndDecideJob(
238 SyncSessionJob::NUDGE);
239
240 EXPECT_EQ(decision, SyncSchedulerImpl::DROP);
241 }
242
243 TEST_F(SyncSchedulerWhiteboxTest, ContinueCanaryJobConfig) {
244 InitializeSyncerOnNormalMode();
245 SetMode(SyncScheduler::CONFIGURATION_MODE);
246 SetWaitIntervalToExponentialBackoff();
247
248 SyncSessionJob job(SyncSessionJob::CONFIGURATION,
249 TimeTicks::Now(), SyncSourceInfo(),
250 ConfigurationParams());
251
252 SyncSchedulerImpl::JobProcessDecision decision =
253 DecideOnJob(job, SyncSchedulerImpl::CANARY_PRIORITY);
254
255 EXPECT_EQ(decision, SyncSchedulerImpl::CONTINUE);
256 }
257
258 } // namespace syncer
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698