OLD | NEW |
| (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 "sync/sessions/nudge_tracker.h" | |
6 | |
7 #include <stddef.h> | |
8 #include <stdint.h> | |
9 #include <string> | |
10 #include <utility> | |
11 #include <vector> | |
12 | |
13 #include "base/message_loop/message_loop.h" | |
14 #include "base/run_loop.h" | |
15 #include "sync/internal_api/public/base/model_type_test_util.h" | |
16 #include "sync/test/mock_invalidation.h" | |
17 #include "sync/test/mock_invalidation_tracker.h" | |
18 #include "sync/test/trackable_mock_invalidation.h" | |
19 #include "testing/gtest/include/gtest/gtest.h" | |
20 | |
21 namespace syncer { | |
22 | |
23 namespace { | |
24 | |
25 testing::AssertionResult ModelTypeSetEquals(ModelTypeSet a, ModelTypeSet b) { | |
26 if (a == b) { | |
27 return testing::AssertionSuccess(); | |
28 } else { | |
29 return testing::AssertionFailure() | |
30 << "Left side " << ModelTypeSetToString(a) | |
31 << ", does not match rigth side: " << ModelTypeSetToString(b); | |
32 } | |
33 } | |
34 | |
35 } // namespace | |
36 | |
37 namespace sessions { | |
38 | |
39 class NudgeTrackerTest : public ::testing::Test { | |
40 public: | |
41 NudgeTrackerTest() { | |
42 SetInvalidationsInSync(); | |
43 } | |
44 | |
45 static size_t GetHintBufferSize() { | |
46 // Assumes that no test has adjusted this size. | |
47 return NudgeTracker::kDefaultMaxPayloadsPerType; | |
48 } | |
49 | |
50 bool InvalidationsOutOfSync() const { | |
51 // We don't currently track invalidations out of sync on a per-type basis. | |
52 sync_pb::GetUpdateTriggers gu_trigger; | |
53 nudge_tracker_.FillProtoMessage(BOOKMARKS, &gu_trigger); | |
54 return gu_trigger.invalidations_out_of_sync(); | |
55 } | |
56 | |
57 int ProtoLocallyModifiedCount(ModelType type) const { | |
58 sync_pb::GetUpdateTriggers gu_trigger; | |
59 nudge_tracker_.FillProtoMessage(type, &gu_trigger); | |
60 return gu_trigger.local_modification_nudges(); | |
61 } | |
62 | |
63 int ProtoRefreshRequestedCount(ModelType type) const { | |
64 sync_pb::GetUpdateTriggers gu_trigger; | |
65 nudge_tracker_.FillProtoMessage(type, &gu_trigger); | |
66 return gu_trigger.datatype_refresh_nudges(); | |
67 } | |
68 | |
69 void SetInvalidationsInSync() { | |
70 nudge_tracker_.OnInvalidationsEnabled(); | |
71 nudge_tracker_.RecordSuccessfulSyncCycle(); | |
72 } | |
73 | |
74 std::unique_ptr<InvalidationInterface> BuildInvalidation( | |
75 int64_t version, | |
76 const std::string& payload) { | |
77 return MockInvalidation::Build(version, payload); | |
78 } | |
79 | |
80 static std::unique_ptr<InvalidationInterface> | |
81 BuildUnknownVersionInvalidation() { | |
82 return MockInvalidation::BuildUnknownVersion(); | |
83 } | |
84 | |
85 protected: | |
86 NudgeTracker nudge_tracker_; | |
87 }; | |
88 | |
89 // Exercise an empty NudgeTracker. | |
90 // Use with valgrind to detect uninitialized members. | |
91 TEST_F(NudgeTrackerTest, EmptyNudgeTracker) { | |
92 // Now we're at the normal, "idle" state. | |
93 EXPECT_FALSE(nudge_tracker_.IsSyncRequired()); | |
94 EXPECT_FALSE(nudge_tracker_.IsGetUpdatesRequired()); | |
95 EXPECT_EQ(sync_pb::GetUpdatesCallerInfo::UNKNOWN, | |
96 nudge_tracker_.GetLegacySource()); | |
97 | |
98 sync_pb::GetUpdateTriggers gu_trigger; | |
99 nudge_tracker_.FillProtoMessage(BOOKMARKS, &gu_trigger); | |
100 | |
101 EXPECT_EQ(sync_pb::GetUpdatesCallerInfo::UNKNOWN, | |
102 nudge_tracker_.GetLegacySource()); | |
103 } | |
104 | |
105 // Verify that nudges override each other based on a priority order. | |
106 // RETRY < LOCAL < DATATYPE_REFRESH < NOTIFICATION | |
107 TEST_F(NudgeTrackerTest, SourcePriorities) { | |
108 // Start with a retry request. | |
109 const base::TimeTicks t0 = base::TimeTicks::FromInternalValue(1234); | |
110 const base::TimeTicks t1 = t0 + base::TimeDelta::FromSeconds(10); | |
111 nudge_tracker_.SetNextRetryTime(t0); | |
112 nudge_tracker_.SetSyncCycleStartTime(t1); | |
113 EXPECT_EQ(sync_pb::GetUpdatesCallerInfo::RETRY, | |
114 nudge_tracker_.GetLegacySource()); | |
115 | |
116 // Track a local nudge. | |
117 nudge_tracker_.RecordLocalChange(ModelTypeSet(BOOKMARKS)); | |
118 EXPECT_EQ(sync_pb::GetUpdatesCallerInfo::LOCAL, | |
119 nudge_tracker_.GetLegacySource()); | |
120 | |
121 // A refresh request will override it. | |
122 nudge_tracker_.RecordLocalRefreshRequest(ModelTypeSet(TYPED_URLS)); | |
123 EXPECT_EQ(sync_pb::GetUpdatesCallerInfo::DATATYPE_REFRESH, | |
124 nudge_tracker_.GetLegacySource()); | |
125 | |
126 // Another local nudge will not be enough to change it. | |
127 nudge_tracker_.RecordLocalChange(ModelTypeSet(BOOKMARKS)); | |
128 EXPECT_EQ(sync_pb::GetUpdatesCallerInfo::DATATYPE_REFRESH, | |
129 nudge_tracker_.GetLegacySource()); | |
130 | |
131 // An invalidation will override the refresh request source. | |
132 nudge_tracker_.RecordRemoteInvalidation(PREFERENCES, | |
133 BuildInvalidation(1, "hint")); | |
134 EXPECT_EQ(sync_pb::GetUpdatesCallerInfo::NOTIFICATION, | |
135 nudge_tracker_.GetLegacySource()); | |
136 | |
137 // Neither local nudges nor refresh requests will override it. | |
138 nudge_tracker_.RecordLocalChange(ModelTypeSet(BOOKMARKS)); | |
139 EXPECT_EQ(sync_pb::GetUpdatesCallerInfo::NOTIFICATION, | |
140 nudge_tracker_.GetLegacySource()); | |
141 nudge_tracker_.RecordLocalRefreshRequest(ModelTypeSet(TYPED_URLS)); | |
142 EXPECT_EQ(sync_pb::GetUpdatesCallerInfo::NOTIFICATION, | |
143 nudge_tracker_.GetLegacySource()); | |
144 } | |
145 | |
146 TEST_F(NudgeTrackerTest, SourcePriority_InitialSyncRequest) { | |
147 nudge_tracker_.RecordInitialSyncRequired(BOOKMARKS); | |
148 | |
149 // For lack of a better source, we describe an initial sync request as having | |
150 // source DATATYPE_REFRESH. | |
151 EXPECT_EQ(sync_pb::GetUpdatesCallerInfo::DATATYPE_REFRESH, | |
152 nudge_tracker_.GetLegacySource()); | |
153 | |
154 // This should never happen in practice. But, if it did, we'd want the | |
155 // initial sync required to keep the source set to DATATYPE_REFRESH. | |
156 nudge_tracker_.RecordLocalChange(ModelTypeSet(BOOKMARKS)); | |
157 EXPECT_EQ(sync_pb::GetUpdatesCallerInfo::DATATYPE_REFRESH, | |
158 nudge_tracker_.GetLegacySource()); | |
159 | |
160 // It should be safe to let NOTIFICATIONs override it. | |
161 nudge_tracker_.RecordRemoteInvalidation(BOOKMARKS, | |
162 BuildInvalidation(1, "hint")); | |
163 EXPECT_EQ(sync_pb::GetUpdatesCallerInfo::NOTIFICATION, | |
164 nudge_tracker_.GetLegacySource()); | |
165 } | |
166 | |
167 // Verifies the management of invalidation hints and GU trigger fields. | |
168 TEST_F(NudgeTrackerTest, HintCoalescing) { | |
169 // Easy case: record one hint. | |
170 { | |
171 nudge_tracker_.RecordRemoteInvalidation(BOOKMARKS, | |
172 BuildInvalidation(1, "bm_hint_1")); | |
173 | |
174 sync_pb::GetUpdateTriggers gu_trigger; | |
175 nudge_tracker_.FillProtoMessage(BOOKMARKS, &gu_trigger); | |
176 ASSERT_EQ(1, gu_trigger.notification_hint_size()); | |
177 EXPECT_EQ("bm_hint_1", gu_trigger.notification_hint(0)); | |
178 EXPECT_FALSE(gu_trigger.client_dropped_hints()); | |
179 } | |
180 | |
181 // Record a second hint for the same type. | |
182 { | |
183 nudge_tracker_.RecordRemoteInvalidation(BOOKMARKS, | |
184 BuildInvalidation(2, "bm_hint_2")); | |
185 | |
186 sync_pb::GetUpdateTriggers gu_trigger; | |
187 nudge_tracker_.FillProtoMessage(BOOKMARKS, &gu_trigger); | |
188 ASSERT_EQ(2, gu_trigger.notification_hint_size()); | |
189 | |
190 // Expect the most hint recent is last in the list. | |
191 EXPECT_EQ("bm_hint_1", gu_trigger.notification_hint(0)); | |
192 EXPECT_EQ("bm_hint_2", gu_trigger.notification_hint(1)); | |
193 EXPECT_FALSE(gu_trigger.client_dropped_hints()); | |
194 } | |
195 | |
196 // Record a hint for a different type. | |
197 { | |
198 nudge_tracker_.RecordRemoteInvalidation(PASSWORDS, | |
199 BuildInvalidation(1, "pw_hint_1")); | |
200 | |
201 // Re-verify the bookmarks to make sure they're unaffected. | |
202 sync_pb::GetUpdateTriggers bm_gu_trigger; | |
203 nudge_tracker_.FillProtoMessage(BOOKMARKS, &bm_gu_trigger); | |
204 ASSERT_EQ(2, bm_gu_trigger.notification_hint_size()); | |
205 EXPECT_EQ("bm_hint_1", bm_gu_trigger.notification_hint(0)); | |
206 EXPECT_EQ("bm_hint_2", | |
207 bm_gu_trigger.notification_hint(1)); // most recent last. | |
208 EXPECT_FALSE(bm_gu_trigger.client_dropped_hints()); | |
209 | |
210 // Verify the new type, too. | |
211 sync_pb::GetUpdateTriggers pw_gu_trigger; | |
212 nudge_tracker_.FillProtoMessage(PASSWORDS, &pw_gu_trigger); | |
213 ASSERT_EQ(1, pw_gu_trigger.notification_hint_size()); | |
214 EXPECT_EQ("pw_hint_1", pw_gu_trigger.notification_hint(0)); | |
215 EXPECT_FALSE(pw_gu_trigger.client_dropped_hints()); | |
216 } | |
217 } | |
218 | |
219 // Test the dropping of invalidation hints. Receives invalidations one by one. | |
220 TEST_F(NudgeTrackerTest, DropHintsLocally_OneAtATime) { | |
221 for (size_t i = 0; i < GetHintBufferSize(); ++i) { | |
222 nudge_tracker_.RecordRemoteInvalidation(BOOKMARKS, | |
223 BuildInvalidation(i, "hint")); | |
224 } | |
225 { | |
226 sync_pb::GetUpdateTriggers gu_trigger; | |
227 nudge_tracker_.FillProtoMessage(BOOKMARKS, &gu_trigger); | |
228 EXPECT_EQ(GetHintBufferSize(), | |
229 static_cast<size_t>(gu_trigger.notification_hint_size())); | |
230 EXPECT_FALSE(gu_trigger.client_dropped_hints()); | |
231 } | |
232 | |
233 // Force an overflow. | |
234 nudge_tracker_.RecordRemoteInvalidation(BOOKMARKS, | |
235 BuildInvalidation(1000, "new_hint")); | |
236 | |
237 { | |
238 sync_pb::GetUpdateTriggers gu_trigger; | |
239 nudge_tracker_.FillProtoMessage(BOOKMARKS, &gu_trigger); | |
240 EXPECT_TRUE(gu_trigger.client_dropped_hints()); | |
241 ASSERT_EQ(GetHintBufferSize(), | |
242 static_cast<size_t>(gu_trigger.notification_hint_size())); | |
243 | |
244 // Verify the newest hint was not dropped and is the last in the list. | |
245 EXPECT_EQ("new_hint", gu_trigger.notification_hint(GetHintBufferSize()-1)); | |
246 | |
247 // Verify the oldest hint, too. | |
248 EXPECT_EQ("hint", gu_trigger.notification_hint(0)); | |
249 } | |
250 } | |
251 | |
252 // Tests the receipt of 'unknown version' invalidations. | |
253 TEST_F(NudgeTrackerTest, DropHintsAtServer_Alone) { | |
254 // Record the unknown version invalidation. | |
255 nudge_tracker_.RecordRemoteInvalidation(BOOKMARKS, | |
256 BuildUnknownVersionInvalidation()); | |
257 { | |
258 sync_pb::GetUpdateTriggers gu_trigger; | |
259 nudge_tracker_.FillProtoMessage(BOOKMARKS, &gu_trigger); | |
260 EXPECT_TRUE(gu_trigger.server_dropped_hints()); | |
261 EXPECT_FALSE(gu_trigger.client_dropped_hints()); | |
262 ASSERT_EQ(0, gu_trigger.notification_hint_size()); | |
263 } | |
264 | |
265 // Clear status then verify. | |
266 nudge_tracker_.RecordSuccessfulSyncCycle(); | |
267 { | |
268 sync_pb::GetUpdateTriggers gu_trigger; | |
269 nudge_tracker_.FillProtoMessage(BOOKMARKS, &gu_trigger); | |
270 EXPECT_FALSE(gu_trigger.client_dropped_hints()); | |
271 EXPECT_FALSE(gu_trigger.server_dropped_hints()); | |
272 ASSERT_EQ(0, gu_trigger.notification_hint_size()); | |
273 } | |
274 } | |
275 | |
276 // Tests the receipt of 'unknown version' invalidations. This test also | |
277 // includes a known version invalidation to mix things up a bit. | |
278 TEST_F(NudgeTrackerTest, DropHintsAtServer_WithOtherInvalidations) { | |
279 // Record the two invalidations, one with unknown version, the other known. | |
280 nudge_tracker_.RecordRemoteInvalidation(BOOKMARKS, | |
281 BuildUnknownVersionInvalidation()); | |
282 nudge_tracker_.RecordRemoteInvalidation(BOOKMARKS, | |
283 BuildInvalidation(10, "hint")); | |
284 | |
285 { | |
286 sync_pb::GetUpdateTriggers gu_trigger; | |
287 nudge_tracker_.FillProtoMessage(BOOKMARKS, &gu_trigger); | |
288 EXPECT_TRUE(gu_trigger.server_dropped_hints()); | |
289 EXPECT_FALSE(gu_trigger.client_dropped_hints()); | |
290 ASSERT_EQ(1, gu_trigger.notification_hint_size()); | |
291 EXPECT_EQ("hint", gu_trigger.notification_hint(0)); | |
292 } | |
293 | |
294 // Clear status then verify. | |
295 nudge_tracker_.RecordSuccessfulSyncCycle(); | |
296 { | |
297 sync_pb::GetUpdateTriggers gu_trigger; | |
298 nudge_tracker_.FillProtoMessage(BOOKMARKS, &gu_trigger); | |
299 EXPECT_FALSE(gu_trigger.client_dropped_hints()); | |
300 EXPECT_FALSE(gu_trigger.server_dropped_hints()); | |
301 ASSERT_EQ(0, gu_trigger.notification_hint_size()); | |
302 } | |
303 } | |
304 | |
305 // Checks the behaviour of the invalidations-out-of-sync flag. | |
306 TEST_F(NudgeTrackerTest, EnableDisableInvalidations) { | |
307 // Start with invalidations offline. | |
308 nudge_tracker_.OnInvalidationsDisabled(); | |
309 EXPECT_TRUE(InvalidationsOutOfSync()); | |
310 EXPECT_TRUE(nudge_tracker_.IsGetUpdatesRequired()); | |
311 | |
312 // Simply enabling invalidations does not bring us back into sync. | |
313 nudge_tracker_.OnInvalidationsEnabled(); | |
314 EXPECT_TRUE(InvalidationsOutOfSync()); | |
315 EXPECT_TRUE(nudge_tracker_.IsGetUpdatesRequired()); | |
316 | |
317 // We must successfully complete a sync cycle while invalidations are enabled | |
318 // to be sure that we're in sync. | |
319 nudge_tracker_.RecordSuccessfulSyncCycle(); | |
320 EXPECT_FALSE(InvalidationsOutOfSync()); | |
321 EXPECT_FALSE(nudge_tracker_.IsGetUpdatesRequired()); | |
322 | |
323 // If the invalidator malfunctions, we go become unsynced again. | |
324 nudge_tracker_.OnInvalidationsDisabled(); | |
325 EXPECT_TRUE(InvalidationsOutOfSync()); | |
326 EXPECT_TRUE(nudge_tracker_.IsGetUpdatesRequired()); | |
327 | |
328 // A sync cycle while invalidations are disabled won't reset the flag. | |
329 nudge_tracker_.RecordSuccessfulSyncCycle(); | |
330 EXPECT_TRUE(InvalidationsOutOfSync()); | |
331 EXPECT_TRUE(nudge_tracker_.IsGetUpdatesRequired()); | |
332 | |
333 // Nor will the re-enabling of invalidations be sufficient, even now that | |
334 // we've had a successful sync cycle. | |
335 nudge_tracker_.RecordSuccessfulSyncCycle(); | |
336 EXPECT_TRUE(InvalidationsOutOfSync()); | |
337 EXPECT_TRUE(nudge_tracker_.IsGetUpdatesRequired()); | |
338 } | |
339 | |
340 // Tests that locally modified types are correctly written out to the | |
341 // GetUpdateTriggers proto. | |
342 TEST_F(NudgeTrackerTest, WriteLocallyModifiedTypesToProto) { | |
343 // Should not be locally modified by default. | |
344 EXPECT_EQ(0, ProtoLocallyModifiedCount(PREFERENCES)); | |
345 | |
346 // Record a local bookmark change. Verify it was registered correctly. | |
347 nudge_tracker_.RecordLocalChange(ModelTypeSet(PREFERENCES)); | |
348 EXPECT_EQ(1, ProtoLocallyModifiedCount(PREFERENCES)); | |
349 | |
350 // Record a successful sync cycle. Verify the count is cleared. | |
351 nudge_tracker_.RecordSuccessfulSyncCycle(); | |
352 EXPECT_EQ(0, ProtoLocallyModifiedCount(PREFERENCES)); | |
353 } | |
354 | |
355 // Tests that refresh requested types are correctly written out to the | |
356 // GetUpdateTriggers proto. | |
357 TEST_F(NudgeTrackerTest, WriteRefreshRequestedTypesToProto) { | |
358 // There should be no refresh requested by default. | |
359 EXPECT_EQ(0, ProtoRefreshRequestedCount(SESSIONS)); | |
360 | |
361 // Record a local refresh request. Verify it was registered correctly. | |
362 nudge_tracker_.RecordLocalRefreshRequest(ModelTypeSet(SESSIONS)); | |
363 EXPECT_EQ(1, ProtoRefreshRequestedCount(SESSIONS)); | |
364 | |
365 // Record a successful sync cycle. Verify the count is cleared. | |
366 nudge_tracker_.RecordSuccessfulSyncCycle(); | |
367 EXPECT_EQ(0, ProtoRefreshRequestedCount(SESSIONS)); | |
368 } | |
369 | |
370 // Basic tests for the IsSyncRequired() flag. | |
371 TEST_F(NudgeTrackerTest, IsSyncRequired) { | |
372 EXPECT_FALSE(nudge_tracker_.IsSyncRequired()); | |
373 | |
374 // Initial sync request. | |
375 nudge_tracker_.RecordInitialSyncRequired(BOOKMARKS); | |
376 EXPECT_TRUE(nudge_tracker_.IsSyncRequired()); | |
377 nudge_tracker_.RecordSuccessfulSyncCycle(); | |
378 EXPECT_FALSE(nudge_tracker_.IsSyncRequired()); | |
379 | |
380 // Sync request for resolve conflict. | |
381 nudge_tracker_.RecordCommitConflict(BOOKMARKS); | |
382 EXPECT_TRUE(nudge_tracker_.IsSyncRequired()); | |
383 nudge_tracker_.RecordSuccessfulSyncCycle(); | |
384 EXPECT_FALSE(nudge_tracker_.IsSyncRequired()); | |
385 | |
386 // Local changes. | |
387 nudge_tracker_.RecordLocalChange(ModelTypeSet(SESSIONS)); | |
388 EXPECT_TRUE(nudge_tracker_.IsSyncRequired()); | |
389 nudge_tracker_.RecordSuccessfulSyncCycle(); | |
390 EXPECT_FALSE(nudge_tracker_.IsSyncRequired()); | |
391 | |
392 // Refresh requests. | |
393 nudge_tracker_.RecordLocalRefreshRequest(ModelTypeSet(SESSIONS)); | |
394 EXPECT_TRUE(nudge_tracker_.IsSyncRequired()); | |
395 nudge_tracker_.RecordSuccessfulSyncCycle(); | |
396 EXPECT_FALSE(nudge_tracker_.IsSyncRequired()); | |
397 | |
398 // Invalidations. | |
399 nudge_tracker_.RecordRemoteInvalidation(PREFERENCES, | |
400 BuildInvalidation(1, "hint")); | |
401 EXPECT_TRUE(nudge_tracker_.IsSyncRequired()); | |
402 nudge_tracker_.RecordSuccessfulSyncCycle(); | |
403 EXPECT_FALSE(nudge_tracker_.IsSyncRequired()); | |
404 } | |
405 | |
406 // Basic tests for the IsGetUpdatesRequired() flag. | |
407 TEST_F(NudgeTrackerTest, IsGetUpdatesRequired) { | |
408 EXPECT_FALSE(nudge_tracker_.IsGetUpdatesRequired()); | |
409 | |
410 // Initial sync request. | |
411 nudge_tracker_.RecordInitialSyncRequired(BOOKMARKS); | |
412 EXPECT_TRUE(nudge_tracker_.IsGetUpdatesRequired()); | |
413 nudge_tracker_.RecordSuccessfulSyncCycle(); | |
414 EXPECT_FALSE(nudge_tracker_.IsGetUpdatesRequired()); | |
415 | |
416 // Local changes. | |
417 nudge_tracker_.RecordLocalChange(ModelTypeSet(SESSIONS)); | |
418 EXPECT_FALSE(nudge_tracker_.IsGetUpdatesRequired()); | |
419 nudge_tracker_.RecordSuccessfulSyncCycle(); | |
420 EXPECT_FALSE(nudge_tracker_.IsGetUpdatesRequired()); | |
421 | |
422 // Refresh requests. | |
423 nudge_tracker_.RecordLocalRefreshRequest(ModelTypeSet(SESSIONS)); | |
424 EXPECT_TRUE(nudge_tracker_.IsGetUpdatesRequired()); | |
425 nudge_tracker_.RecordSuccessfulSyncCycle(); | |
426 EXPECT_FALSE(nudge_tracker_.IsGetUpdatesRequired()); | |
427 | |
428 // Invalidations. | |
429 nudge_tracker_.RecordRemoteInvalidation(PREFERENCES, | |
430 BuildInvalidation(1, "hint")); | |
431 EXPECT_TRUE(nudge_tracker_.IsGetUpdatesRequired()); | |
432 nudge_tracker_.RecordSuccessfulSyncCycle(); | |
433 EXPECT_FALSE(nudge_tracker_.IsGetUpdatesRequired()); | |
434 } | |
435 | |
436 // Test IsSyncRequired() responds correctly to data type throttling. | |
437 TEST_F(NudgeTrackerTest, IsSyncRequired_Throttling) { | |
438 const base::TimeTicks t0 = base::TimeTicks::FromInternalValue(1234); | |
439 const base::TimeDelta throttle_length = base::TimeDelta::FromMinutes(10); | |
440 const base::TimeTicks t1 = t0 + throttle_length; | |
441 | |
442 EXPECT_FALSE(nudge_tracker_.IsSyncRequired()); | |
443 | |
444 // A local change to sessions enables the flag. | |
445 nudge_tracker_.RecordLocalChange(ModelTypeSet(SESSIONS)); | |
446 EXPECT_TRUE(nudge_tracker_.IsSyncRequired()); | |
447 | |
448 // But the throttling of sessions unsets it. | |
449 nudge_tracker_.SetTypesThrottledUntil(ModelTypeSet(SESSIONS), | |
450 throttle_length, | |
451 t0); | |
452 EXPECT_FALSE(nudge_tracker_.IsSyncRequired()); | |
453 | |
454 // A refresh request for bookmarks means we have reason to sync again. | |
455 nudge_tracker_.RecordLocalRefreshRequest(ModelTypeSet(BOOKMARKS)); | |
456 EXPECT_TRUE(nudge_tracker_.IsSyncRequired()); | |
457 | |
458 // A successful sync cycle means we took care of bookmarks. | |
459 nudge_tracker_.RecordSuccessfulSyncCycle(); | |
460 EXPECT_FALSE(nudge_tracker_.IsSyncRequired()); | |
461 | |
462 // But we still haven't dealt with sessions. We'll need to remember | |
463 // that sessions are out of sync and re-enable the flag when their | |
464 // throttling interval expires. | |
465 nudge_tracker_.UpdateTypeThrottlingState(t1); | |
466 EXPECT_FALSE(nudge_tracker_.IsTypeThrottled(SESSIONS)); | |
467 EXPECT_TRUE(nudge_tracker_.IsSyncRequired()); | |
468 } | |
469 | |
470 // Test IsGetUpdatesRequired() responds correctly to data type throttling. | |
471 TEST_F(NudgeTrackerTest, IsGetUpdatesRequired_Throttling) { | |
472 const base::TimeTicks t0 = base::TimeTicks::FromInternalValue(1234); | |
473 const base::TimeDelta throttle_length = base::TimeDelta::FromMinutes(10); | |
474 const base::TimeTicks t1 = t0 + throttle_length; | |
475 | |
476 EXPECT_FALSE(nudge_tracker_.IsGetUpdatesRequired()); | |
477 | |
478 // A refresh request to sessions enables the flag. | |
479 nudge_tracker_.RecordLocalRefreshRequest(ModelTypeSet(SESSIONS)); | |
480 EXPECT_TRUE(nudge_tracker_.IsGetUpdatesRequired()); | |
481 | |
482 // But the throttling of sessions unsets it. | |
483 nudge_tracker_.SetTypesThrottledUntil(ModelTypeSet(SESSIONS), | |
484 throttle_length, | |
485 t0); | |
486 EXPECT_FALSE(nudge_tracker_.IsGetUpdatesRequired()); | |
487 | |
488 // A refresh request for bookmarks means we have reason to sync again. | |
489 nudge_tracker_.RecordLocalRefreshRequest(ModelTypeSet(BOOKMARKS)); | |
490 EXPECT_TRUE(nudge_tracker_.IsGetUpdatesRequired()); | |
491 | |
492 // A successful sync cycle means we took care of bookmarks. | |
493 nudge_tracker_.RecordSuccessfulSyncCycle(); | |
494 EXPECT_FALSE(nudge_tracker_.IsGetUpdatesRequired()); | |
495 | |
496 // But we still haven't dealt with sessions. We'll need to remember | |
497 // that sessions are out of sync and re-enable the flag when their | |
498 // throttling interval expires. | |
499 nudge_tracker_.UpdateTypeThrottlingState(t1); | |
500 EXPECT_FALSE(nudge_tracker_.IsTypeThrottled(SESSIONS)); | |
501 EXPECT_TRUE(nudge_tracker_.IsGetUpdatesRequired()); | |
502 } | |
503 | |
504 // Tests throttling-related getter functions when no types are throttled. | |
505 TEST_F(NudgeTrackerTest, NoTypesThrottled) { | |
506 EXPECT_FALSE(nudge_tracker_.IsAnyTypeThrottled()); | |
507 EXPECT_FALSE(nudge_tracker_.IsTypeThrottled(SESSIONS)); | |
508 EXPECT_TRUE(nudge_tracker_.GetThrottledTypes().Empty()); | |
509 } | |
510 | |
511 // Tests throttling-related getter functions when some types are throttled. | |
512 TEST_F(NudgeTrackerTest, ThrottleAndUnthrottle) { | |
513 const base::TimeTicks t0 = base::TimeTicks::FromInternalValue(1234); | |
514 const base::TimeDelta throttle_length = base::TimeDelta::FromMinutes(10); | |
515 const base::TimeTicks t1 = t0 + throttle_length; | |
516 | |
517 nudge_tracker_.SetTypesThrottledUntil(ModelTypeSet(SESSIONS, PREFERENCES), | |
518 throttle_length, | |
519 t0); | |
520 | |
521 EXPECT_TRUE(nudge_tracker_.IsAnyTypeThrottled()); | |
522 EXPECT_TRUE(nudge_tracker_.IsTypeThrottled(SESSIONS)); | |
523 EXPECT_TRUE(nudge_tracker_.IsTypeThrottled(PREFERENCES)); | |
524 EXPECT_FALSE(nudge_tracker_.GetThrottledTypes().Empty()); | |
525 EXPECT_EQ(throttle_length, nudge_tracker_.GetTimeUntilNextUnthrottle(t0)); | |
526 | |
527 nudge_tracker_.UpdateTypeThrottlingState(t1); | |
528 | |
529 EXPECT_FALSE(nudge_tracker_.IsAnyTypeThrottled()); | |
530 EXPECT_FALSE(nudge_tracker_.IsTypeThrottled(SESSIONS)); | |
531 EXPECT_TRUE(nudge_tracker_.GetThrottledTypes().Empty()); | |
532 } | |
533 | |
534 TEST_F(NudgeTrackerTest, OverlappingThrottleIntervals) { | |
535 const base::TimeTicks t0 = base::TimeTicks::FromInternalValue(1234); | |
536 const base::TimeDelta throttle1_length = base::TimeDelta::FromMinutes(10); | |
537 const base::TimeDelta throttle2_length = base::TimeDelta::FromMinutes(20); | |
538 const base::TimeTicks t1 = t0 + throttle1_length; | |
539 const base::TimeTicks t2 = t0 + throttle2_length; | |
540 | |
541 // Setup the longer of two intervals. | |
542 nudge_tracker_.SetTypesThrottledUntil(ModelTypeSet(SESSIONS, PREFERENCES), | |
543 throttle2_length, | |
544 t0); | |
545 EXPECT_TRUE(ModelTypeSetEquals( | |
546 ModelTypeSet(SESSIONS, PREFERENCES), | |
547 nudge_tracker_.GetThrottledTypes())); | |
548 EXPECT_EQ(throttle2_length, | |
549 nudge_tracker_.GetTimeUntilNextUnthrottle(t0)); | |
550 | |
551 // Setup the shorter interval. | |
552 nudge_tracker_.SetTypesThrottledUntil(ModelTypeSet(SESSIONS, BOOKMARKS), | |
553 throttle1_length, | |
554 t0); | |
555 EXPECT_TRUE(ModelTypeSetEquals( | |
556 ModelTypeSet(SESSIONS, PREFERENCES, BOOKMARKS), | |
557 nudge_tracker_.GetThrottledTypes())); | |
558 EXPECT_EQ(throttle1_length, | |
559 nudge_tracker_.GetTimeUntilNextUnthrottle(t0)); | |
560 | |
561 // Expire the first interval. | |
562 nudge_tracker_.UpdateTypeThrottlingState(t1); | |
563 | |
564 // SESSIONS appeared in both intervals. We expect it will be throttled for | |
565 // the longer of the two, so it's still throttled at time t1. | |
566 EXPECT_TRUE(ModelTypeSetEquals( | |
567 ModelTypeSet(SESSIONS, PREFERENCES), | |
568 nudge_tracker_.GetThrottledTypes())); | |
569 EXPECT_EQ(throttle2_length - throttle1_length, | |
570 nudge_tracker_.GetTimeUntilNextUnthrottle(t1)); | |
571 | |
572 // Expire the second interval. | |
573 nudge_tracker_.UpdateTypeThrottlingState(t2); | |
574 EXPECT_TRUE(nudge_tracker_.GetThrottledTypes().Empty()); | |
575 } | |
576 | |
577 TEST_F(NudgeTrackerTest, Retry) { | |
578 const base::TimeTicks t0 = base::TimeTicks::FromInternalValue(12345); | |
579 const base::TimeTicks t3 = t0 + base::TimeDelta::FromSeconds(3); | |
580 const base::TimeTicks t4 = t0 + base::TimeDelta::FromSeconds(4); | |
581 | |
582 // Set retry for t3. | |
583 nudge_tracker_.SetNextRetryTime(t3); | |
584 | |
585 // Not due yet at t0. | |
586 nudge_tracker_.SetSyncCycleStartTime(t0); | |
587 EXPECT_FALSE(nudge_tracker_.IsRetryRequired()); | |
588 EXPECT_FALSE(nudge_tracker_.IsGetUpdatesRequired()); | |
589 | |
590 // Successful sync cycle at t0 changes nothing. | |
591 nudge_tracker_.RecordSuccessfulSyncCycle(); | |
592 EXPECT_FALSE(nudge_tracker_.IsRetryRequired()); | |
593 EXPECT_FALSE(nudge_tracker_.IsGetUpdatesRequired()); | |
594 | |
595 // At t4, the retry becomes due. | |
596 nudge_tracker_.SetSyncCycleStartTime(t4); | |
597 EXPECT_TRUE(nudge_tracker_.IsRetryRequired()); | |
598 EXPECT_TRUE(nudge_tracker_.IsGetUpdatesRequired()); | |
599 | |
600 // A sync cycle unsets the flag. | |
601 nudge_tracker_.RecordSuccessfulSyncCycle(); | |
602 EXPECT_FALSE(nudge_tracker_.IsRetryRequired()); | |
603 | |
604 // It's still unset at the start of the next sync cycle. | |
605 nudge_tracker_.SetSyncCycleStartTime(t4); | |
606 EXPECT_FALSE(nudge_tracker_.IsRetryRequired()); | |
607 } | |
608 | |
609 // Test a mid-cycle update when IsRetryRequired() was true before the cycle | |
610 // began. | |
611 TEST_F(NudgeTrackerTest, IsRetryRequired_MidCycleUpdate1) { | |
612 const base::TimeTicks t0 = base::TimeTicks::FromInternalValue(12345); | |
613 const base::TimeTicks t1 = t0 + base::TimeDelta::FromSeconds(1); | |
614 const base::TimeTicks t2 = t0 + base::TimeDelta::FromSeconds(2); | |
615 const base::TimeTicks t5 = t0 + base::TimeDelta::FromSeconds(5); | |
616 const base::TimeTicks t6 = t0 + base::TimeDelta::FromSeconds(6); | |
617 | |
618 nudge_tracker_.SetNextRetryTime(t0); | |
619 nudge_tracker_.SetSyncCycleStartTime(t1); | |
620 | |
621 EXPECT_TRUE(nudge_tracker_.IsRetryRequired()); | |
622 | |
623 // Pretend that we were updated mid-cycle. SetSyncCycleStartTime is | |
624 // called only at the start of the sync cycle, so don't call it here. | |
625 // The update should have no effect on IsRetryRequired(). | |
626 nudge_tracker_.SetNextRetryTime(t5); | |
627 | |
628 EXPECT_TRUE(nudge_tracker_.IsRetryRequired()); | |
629 | |
630 // Verify that the successful sync cycle clears the flag. | |
631 nudge_tracker_.RecordSuccessfulSyncCycle(); | |
632 EXPECT_FALSE(nudge_tracker_.IsRetryRequired()); | |
633 | |
634 // Verify expecations around the new retry time. | |
635 nudge_tracker_.SetSyncCycleStartTime(t2); | |
636 EXPECT_FALSE(nudge_tracker_.IsRetryRequired()); | |
637 | |
638 nudge_tracker_.SetSyncCycleStartTime(t6); | |
639 EXPECT_TRUE(nudge_tracker_.IsRetryRequired()); | |
640 } | |
641 | |
642 // Test a mid-cycle update when IsRetryRequired() was false before the cycle | |
643 // began. | |
644 TEST_F(NudgeTrackerTest, IsRetryRequired_MidCycleUpdate2) { | |
645 const base::TimeTicks t0 = base::TimeTicks::FromInternalValue(12345); | |
646 const base::TimeTicks t1 = t0 + base::TimeDelta::FromSeconds(1); | |
647 const base::TimeTicks t3 = t0 + base::TimeDelta::FromSeconds(3); | |
648 const base::TimeTicks t5 = t0 + base::TimeDelta::FromSeconds(5); | |
649 const base::TimeTicks t6 = t0 + base::TimeDelta::FromSeconds(6); | |
650 | |
651 // Schedule a future retry, and a nudge unrelated to it. | |
652 nudge_tracker_.RecordLocalChange(ModelTypeSet(BOOKMARKS)); | |
653 nudge_tracker_.SetNextRetryTime(t1); | |
654 nudge_tracker_.SetSyncCycleStartTime(t0); | |
655 EXPECT_FALSE(nudge_tracker_.IsRetryRequired()); | |
656 | |
657 // Pretend this happened in mid-cycle. This should have no effect on | |
658 // IsRetryRequired(). | |
659 nudge_tracker_.SetNextRetryTime(t5); | |
660 EXPECT_FALSE(nudge_tracker_.IsRetryRequired()); | |
661 | |
662 // The cycle succeeded. | |
663 nudge_tracker_.RecordSuccessfulSyncCycle(); | |
664 | |
665 // The time t3 is greater than the GU retry time scheduled at the beginning of | |
666 // the test, but later than the retry time that overwrote it during the | |
667 // pretend 'sync cycle'. | |
668 nudge_tracker_.SetSyncCycleStartTime(t3); | |
669 EXPECT_FALSE(nudge_tracker_.IsRetryRequired()); | |
670 | |
671 // Finally, the retry established during the sync cycle becomes due. | |
672 nudge_tracker_.SetSyncCycleStartTime(t6); | |
673 EXPECT_TRUE(nudge_tracker_.IsRetryRequired()); | |
674 } | |
675 | |
676 // Simulate the case where a sync cycle fails. | |
677 TEST_F(NudgeTrackerTest, IsRetryRequired_FailedCycle) { | |
678 const base::TimeTicks t0 = base::TimeTicks::FromInternalValue(12345); | |
679 const base::TimeTicks t1 = t0 + base::TimeDelta::FromSeconds(1); | |
680 const base::TimeTicks t2 = t0 + base::TimeDelta::FromSeconds(2); | |
681 | |
682 nudge_tracker_.SetNextRetryTime(t0); | |
683 nudge_tracker_.SetSyncCycleStartTime(t1); | |
684 EXPECT_TRUE(nudge_tracker_.IsRetryRequired()); | |
685 | |
686 // The nudge tracker receives no notifications for a failed sync cycle. | |
687 // Pretend one happened here. | |
688 EXPECT_TRUE(nudge_tracker_.IsRetryRequired()); | |
689 | |
690 // Think of this as the retry cycle. | |
691 nudge_tracker_.SetSyncCycleStartTime(t2); | |
692 EXPECT_TRUE(nudge_tracker_.IsRetryRequired()); | |
693 | |
694 // The second cycle is a success. | |
695 nudge_tracker_.RecordSuccessfulSyncCycle(); | |
696 EXPECT_FALSE(nudge_tracker_.IsRetryRequired()); | |
697 } | |
698 | |
699 // Simulate a partially failed sync cycle. The callback to update the GU retry | |
700 // was invoked, but the sync cycle did not complete successfully. | |
701 TEST_F(NudgeTrackerTest, IsRetryRequired_FailedCycleIncludesUpdate) { | |
702 const base::TimeTicks t0 = base::TimeTicks::FromInternalValue(12345); | |
703 const base::TimeTicks t1 = t0 + base::TimeDelta::FromSeconds(1); | |
704 const base::TimeTicks t3 = t0 + base::TimeDelta::FromSeconds(3); | |
705 const base::TimeTicks t4 = t0 + base::TimeDelta::FromSeconds(4); | |
706 const base::TimeTicks t5 = t0 + base::TimeDelta::FromSeconds(5); | |
707 const base::TimeTicks t6 = t0 + base::TimeDelta::FromSeconds(6); | |
708 | |
709 nudge_tracker_.SetNextRetryTime(t0); | |
710 nudge_tracker_.SetSyncCycleStartTime(t1); | |
711 EXPECT_TRUE(nudge_tracker_.IsRetryRequired()); | |
712 | |
713 // The cycle is in progress. A new GU Retry time is received. | |
714 // The flag is not because this cycle is still in progress. | |
715 nudge_tracker_.SetNextRetryTime(t5); | |
716 EXPECT_TRUE(nudge_tracker_.IsRetryRequired()); | |
717 | |
718 // The nudge tracker receives no notifications for a failed sync cycle. | |
719 // Pretend the cycle failed here. | |
720 | |
721 // The next sync cycle starts. The new GU time has not taken effect by this | |
722 // time, but the NudgeTracker hasn't forgotten that we have not yet serviced | |
723 // the retry from the previous cycle. | |
724 nudge_tracker_.SetSyncCycleStartTime(t3); | |
725 EXPECT_TRUE(nudge_tracker_.IsRetryRequired()); | |
726 | |
727 // It succeeds. The retry time is not updated, so it should remain at t5. | |
728 nudge_tracker_.RecordSuccessfulSyncCycle(); | |
729 | |
730 // Another sync cycle. This one is still before the scheduled retry. It does | |
731 // not change the scheduled retry time. | |
732 nudge_tracker_.SetSyncCycleStartTime(t4); | |
733 EXPECT_FALSE(nudge_tracker_.IsRetryRequired()); | |
734 nudge_tracker_.RecordSuccessfulSyncCycle(); | |
735 | |
736 // The retry scheduled way back during the first cycle of this test finally | |
737 // becomes due. Perform a successful sync cycle to service it. | |
738 nudge_tracker_.SetSyncCycleStartTime(t6); | |
739 EXPECT_TRUE(nudge_tracker_.IsRetryRequired()); | |
740 nudge_tracker_.RecordSuccessfulSyncCycle(); | |
741 } | |
742 | |
743 // Test the default nudge delays for various types. | |
744 TEST_F(NudgeTrackerTest, NudgeDelayTest) { | |
745 // Set to a known value to compare against. | |
746 nudge_tracker_.SetDefaultNudgeDelay(base::TimeDelta()); | |
747 | |
748 // Bookmarks and preference both have "slow nudge" delays. | |
749 EXPECT_EQ(nudge_tracker_.RecordLocalChange(ModelTypeSet(BOOKMARKS)), | |
750 nudge_tracker_.RecordLocalChange(ModelTypeSet(PREFERENCES))); | |
751 | |
752 // Typed URLs has a default delay. | |
753 EXPECT_EQ(nudge_tracker_.RecordLocalChange(ModelTypeSet(TYPED_URLS)), | |
754 base::TimeDelta()); | |
755 | |
756 // "Slow nudge" delays are longer than the default. | |
757 EXPECT_GT(nudge_tracker_.RecordLocalChange(ModelTypeSet(BOOKMARKS)), | |
758 base::TimeDelta()); | |
759 | |
760 // Sessions is longer than "slow nudge". | |
761 EXPECT_GT(nudge_tracker_.RecordLocalChange(ModelTypeSet(SESSIONS)), | |
762 nudge_tracker_.RecordLocalChange(ModelTypeSet(BOOKMARKS))); | |
763 | |
764 // Favicons have the same delay as sessions. | |
765 EXPECT_EQ(nudge_tracker_.RecordLocalChange(ModelTypeSet(SESSIONS)), | |
766 nudge_tracker_.RecordLocalChange(ModelTypeSet(FAVICON_TRACKING))); | |
767 | |
768 // Autofill has the longer delay of all. | |
769 EXPECT_GT(nudge_tracker_.RecordLocalChange(ModelTypeSet(AUTOFILL)), | |
770 nudge_tracker_.RecordLocalChange(ModelTypeSet(SESSIONS))); | |
771 | |
772 // A nudge with no types takes the longest delay. | |
773 EXPECT_EQ(nudge_tracker_.RecordLocalChange(ModelTypeSet(AUTOFILL)), | |
774 nudge_tracker_.RecordLocalChange(ModelTypeSet())); | |
775 | |
776 // The actual nudge delay should be the shortest of the set. | |
777 EXPECT_EQ( | |
778 nudge_tracker_.RecordLocalChange(ModelTypeSet(TYPED_URLS)), | |
779 nudge_tracker_.RecordLocalChange(ModelTypeSet(TYPED_URLS, AUTOFILL))); | |
780 } | |
781 | |
782 // Test that custom nudge delays are used over the defaults. | |
783 TEST_F(NudgeTrackerTest, CustomDelayTest) { | |
784 // Set some custom delays. | |
785 std::map<ModelType, base::TimeDelta> delay_map; | |
786 delay_map[BOOKMARKS] = base::TimeDelta::FromSeconds(10); | |
787 delay_map[SESSIONS] = base::TimeDelta::FromSeconds(2); | |
788 nudge_tracker_.OnReceivedCustomNudgeDelays(delay_map); | |
789 | |
790 // Only those with custom delays should be affected, not another type. | |
791 EXPECT_NE(nudge_tracker_.RecordLocalChange(ModelTypeSet(BOOKMARKS)), | |
792 nudge_tracker_.RecordLocalChange(ModelTypeSet(PREFERENCES))); | |
793 | |
794 EXPECT_EQ(base::TimeDelta::FromSeconds(10), | |
795 nudge_tracker_.RecordLocalChange(ModelTypeSet(BOOKMARKS))); | |
796 EXPECT_EQ(base::TimeDelta::FromSeconds(2), | |
797 nudge_tracker_.RecordLocalChange(ModelTypeSet(SESSIONS))); | |
798 } | |
799 | |
800 // Check that custom nudge delays can never result in a value shorter than the | |
801 // minimum nudge delay. | |
802 TEST_F(NudgeTrackerTest, NoTypesShorterThanDefault) { | |
803 // Set delay to a known value. | |
804 nudge_tracker_.SetDefaultNudgeDelay(base::TimeDelta::FromMilliseconds(500)); | |
805 | |
806 std::map<ModelType, base::TimeDelta> delay_map; | |
807 ModelTypeSet protocol_types = syncer::ProtocolTypes(); | |
808 for (ModelTypeSet::Iterator iter = protocol_types.First(); iter.Good(); | |
809 iter.Inc()) { | |
810 delay_map[iter.Get()] = base::TimeDelta(); | |
811 } | |
812 nudge_tracker_.OnReceivedCustomNudgeDelays(delay_map); | |
813 | |
814 // All types should still have a nudge greater than or equal to the minimum. | |
815 for (ModelTypeSet::Iterator iter = protocol_types.First(); iter.Good(); | |
816 iter.Inc()) { | |
817 EXPECT_GE(nudge_tracker_.RecordLocalChange(ModelTypeSet(iter.Get())), | |
818 base::TimeDelta::FromMilliseconds(500)); | |
819 } | |
820 } | |
821 | |
822 class NudgeTrackerAckTrackingTest : public NudgeTrackerTest { | |
823 public: | |
824 NudgeTrackerAckTrackingTest() {} | |
825 | |
826 bool IsInvalidationUnacknowledged(int tracking_id) { | |
827 return tracker_.IsUnacked(tracking_id); | |
828 } | |
829 | |
830 bool IsInvalidationAcknowledged(int tracking_id) { | |
831 return tracker_.IsAcknowledged(tracking_id); | |
832 } | |
833 | |
834 bool IsInvalidationDropped(int tracking_id) { | |
835 return tracker_.IsDropped(tracking_id); | |
836 } | |
837 | |
838 int SendInvalidation(ModelType type, int version, const std::string& hint) { | |
839 // Build and register the invalidation. | |
840 std::unique_ptr<TrackableMockInvalidation> inv = | |
841 tracker_.IssueInvalidation(version, hint); | |
842 int id = inv->GetTrackingId(); | |
843 | |
844 // Send it to the NudgeTracker. | |
845 nudge_tracker_.RecordRemoteInvalidation(type, std::move(inv)); | |
846 | |
847 // Return its ID to the test framework for use in assertions. | |
848 return id; | |
849 } | |
850 | |
851 int SendUnknownVersionInvalidation(ModelType type) { | |
852 // Build and register the invalidation. | |
853 std::unique_ptr<TrackableMockInvalidation> inv = | |
854 tracker_.IssueUnknownVersionInvalidation(); | |
855 int id = inv->GetTrackingId(); | |
856 | |
857 // Send it to the NudgeTracker. | |
858 nudge_tracker_.RecordRemoteInvalidation(type, std::move(inv)); | |
859 | |
860 // Return its ID to the test framework for use in assertions. | |
861 return id; | |
862 } | |
863 | |
864 bool AllInvalidationsAccountedFor() const { | |
865 return tracker_.AllInvalidationsAccountedFor(); | |
866 } | |
867 | |
868 void RecordSuccessfulSyncCycle() { | |
869 nudge_tracker_.RecordSuccessfulSyncCycle(); | |
870 } | |
871 | |
872 private: | |
873 MockInvalidationTracker tracker_; | |
874 }; | |
875 | |
876 // Test the acknowledgement of a single invalidation. | |
877 TEST_F(NudgeTrackerAckTrackingTest, SimpleAcknowledgement) { | |
878 int inv_id = SendInvalidation(BOOKMARKS, 10, "hint"); | |
879 | |
880 EXPECT_TRUE(IsInvalidationUnacknowledged(inv_id)); | |
881 | |
882 RecordSuccessfulSyncCycle(); | |
883 EXPECT_TRUE(IsInvalidationAcknowledged(inv_id)); | |
884 | |
885 EXPECT_TRUE(AllInvalidationsAccountedFor()); | |
886 } | |
887 | |
888 // Test the acknowledgement of many invalidations. | |
889 TEST_F(NudgeTrackerAckTrackingTest, ManyAcknowledgements) { | |
890 int inv1_id = SendInvalidation(BOOKMARKS, 10, "hint"); | |
891 int inv2_id = SendInvalidation(BOOKMARKS, 14, "hint2"); | |
892 int inv3_id = SendInvalidation(PREFERENCES, 8, "hint3"); | |
893 | |
894 EXPECT_TRUE(IsInvalidationUnacknowledged(inv1_id)); | |
895 EXPECT_TRUE(IsInvalidationUnacknowledged(inv2_id)); | |
896 EXPECT_TRUE(IsInvalidationUnacknowledged(inv3_id)); | |
897 | |
898 RecordSuccessfulSyncCycle(); | |
899 EXPECT_TRUE(IsInvalidationAcknowledged(inv1_id)); | |
900 EXPECT_TRUE(IsInvalidationAcknowledged(inv2_id)); | |
901 EXPECT_TRUE(IsInvalidationAcknowledged(inv3_id)); | |
902 | |
903 EXPECT_TRUE(AllInvalidationsAccountedFor()); | |
904 } | |
905 | |
906 // Test dropping when the buffer overflows and subsequent drop recovery. | |
907 TEST_F(NudgeTrackerAckTrackingTest, OverflowAndRecover) { | |
908 std::vector<int> invalidation_ids; | |
909 | |
910 int inv10_id = SendInvalidation(BOOKMARKS, 10, "hint"); | |
911 for (size_t i = 1; i < GetHintBufferSize(); ++i) { | |
912 invalidation_ids.push_back(SendInvalidation(BOOKMARKS, i + 10, "hint")); | |
913 } | |
914 | |
915 for (std::vector<int>::iterator it = invalidation_ids.begin(); | |
916 it != invalidation_ids.end(); | |
917 ++it) { | |
918 EXPECT_TRUE(IsInvalidationUnacknowledged(*it)); | |
919 } | |
920 | |
921 // This invalidation, though arriving the most recently, has the oldest | |
922 // version number so it should be dropped first. | |
923 int inv5_id = SendInvalidation(BOOKMARKS, 5, "old_hint"); | |
924 EXPECT_TRUE(IsInvalidationDropped(inv5_id)); | |
925 | |
926 // This invalidation has a larger version number, so it will force a | |
927 // previously delivered invalidation to be dropped. | |
928 int inv100_id = SendInvalidation(BOOKMARKS, 100, "new_hint"); | |
929 EXPECT_TRUE(IsInvalidationDropped(inv10_id)); | |
930 | |
931 // This should recover from the drop and bring us back into sync. | |
932 RecordSuccessfulSyncCycle(); | |
933 | |
934 for (std::vector<int>::iterator it = invalidation_ids.begin(); | |
935 it != invalidation_ids.end(); | |
936 ++it) { | |
937 EXPECT_TRUE(IsInvalidationAcknowledged(*it)); | |
938 } | |
939 EXPECT_TRUE(IsInvalidationAcknowledged(inv100_id)); | |
940 | |
941 EXPECT_TRUE(AllInvalidationsAccountedFor()); | |
942 } | |
943 | |
944 // Test receipt of an unknown version invalidation from the server. | |
945 TEST_F(NudgeTrackerAckTrackingTest, UnknownVersionFromServer_Simple) { | |
946 int inv_id = SendUnknownVersionInvalidation(BOOKMARKS); | |
947 EXPECT_TRUE(IsInvalidationUnacknowledged(inv_id)); | |
948 RecordSuccessfulSyncCycle(); | |
949 EXPECT_TRUE(IsInvalidationAcknowledged(inv_id)); | |
950 EXPECT_TRUE(AllInvalidationsAccountedFor()); | |
951 } | |
952 | |
953 // Test receipt of multiple unknown version invalidations from the server. | |
954 TEST_F(NudgeTrackerAckTrackingTest, UnknownVersionFromServer_Complex) { | |
955 int inv1_id = SendUnknownVersionInvalidation(BOOKMARKS); | |
956 int inv2_id = SendInvalidation(BOOKMARKS, 10, "hint"); | |
957 int inv3_id = SendUnknownVersionInvalidation(BOOKMARKS); | |
958 int inv4_id = SendUnknownVersionInvalidation(BOOKMARKS); | |
959 int inv5_id = SendInvalidation(BOOKMARKS, 20, "hint2"); | |
960 | |
961 // These invalidations have been overridden, so they got acked early. | |
962 EXPECT_TRUE(IsInvalidationAcknowledged(inv1_id)); | |
963 EXPECT_TRUE(IsInvalidationAcknowledged(inv3_id)); | |
964 | |
965 // These invalidations are still waiting to be used. | |
966 EXPECT_TRUE(IsInvalidationUnacknowledged(inv2_id)); | |
967 EXPECT_TRUE(IsInvalidationUnacknowledged(inv4_id)); | |
968 EXPECT_TRUE(IsInvalidationUnacknowledged(inv5_id)); | |
969 | |
970 // Finish the sync cycle and expect all remaining invalidations to be acked. | |
971 RecordSuccessfulSyncCycle(); | |
972 EXPECT_TRUE(IsInvalidationAcknowledged(inv1_id)); | |
973 EXPECT_TRUE(IsInvalidationAcknowledged(inv2_id)); | |
974 EXPECT_TRUE(IsInvalidationAcknowledged(inv3_id)); | |
975 EXPECT_TRUE(IsInvalidationAcknowledged(inv4_id)); | |
976 EXPECT_TRUE(IsInvalidationAcknowledged(inv5_id)); | |
977 | |
978 EXPECT_TRUE(AllInvalidationsAccountedFor()); | |
979 } | |
980 | |
981 } // namespace sessions | |
982 } // namespace syncer | |
OLD | NEW |