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