Provide GUID in ONC for all networks in ManagedNetworkConfigurationHandler
[chromium-blink-merge.git] / sync / sessions / nudge_tracker_unittest.cc
blobc6a9dfb808de851eee8c2c76d677646a338519a3
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 // Local changes.
373 nudge_tracker_.RecordLocalChange(ModelTypeSet(SESSIONS));
374 EXPECT_TRUE(nudge_tracker_.IsSyncRequired());
375 nudge_tracker_.RecordSuccessfulSyncCycle();
376 EXPECT_FALSE(nudge_tracker_.IsSyncRequired());
378 // Refresh requests.
379 nudge_tracker_.RecordLocalRefreshRequest(ModelTypeSet(SESSIONS));
380 EXPECT_TRUE(nudge_tracker_.IsSyncRequired());
381 nudge_tracker_.RecordSuccessfulSyncCycle();
382 EXPECT_FALSE(nudge_tracker_.IsSyncRequired());
384 // Invalidations.
385 nudge_tracker_.RecordRemoteInvalidation(PREFERENCES,
386 BuildInvalidation(1, "hint"));
387 EXPECT_TRUE(nudge_tracker_.IsSyncRequired());
388 nudge_tracker_.RecordSuccessfulSyncCycle();
389 EXPECT_FALSE(nudge_tracker_.IsSyncRequired());
392 // Basic tests for the IsGetUpdatesRequired() flag.
393 TEST_F(NudgeTrackerTest, IsGetUpdatesRequired) {
394 EXPECT_FALSE(nudge_tracker_.IsGetUpdatesRequired());
396 // Initial sync request.
397 nudge_tracker_.RecordInitialSyncRequired(BOOKMARKS);
398 EXPECT_TRUE(nudge_tracker_.IsGetUpdatesRequired());
399 nudge_tracker_.RecordSuccessfulSyncCycle();
400 EXPECT_FALSE(nudge_tracker_.IsGetUpdatesRequired());
402 // Local changes.
403 nudge_tracker_.RecordLocalChange(ModelTypeSet(SESSIONS));
404 EXPECT_FALSE(nudge_tracker_.IsGetUpdatesRequired());
405 nudge_tracker_.RecordSuccessfulSyncCycle();
406 EXPECT_FALSE(nudge_tracker_.IsGetUpdatesRequired());
408 // Refresh requests.
409 nudge_tracker_.RecordLocalRefreshRequest(ModelTypeSet(SESSIONS));
410 EXPECT_TRUE(nudge_tracker_.IsGetUpdatesRequired());
411 nudge_tracker_.RecordSuccessfulSyncCycle();
412 EXPECT_FALSE(nudge_tracker_.IsGetUpdatesRequired());
414 // Invalidations.
415 nudge_tracker_.RecordRemoteInvalidation(PREFERENCES,
416 BuildInvalidation(1, "hint"));
417 EXPECT_TRUE(nudge_tracker_.IsGetUpdatesRequired());
418 nudge_tracker_.RecordSuccessfulSyncCycle();
419 EXPECT_FALSE(nudge_tracker_.IsGetUpdatesRequired());
422 // Test IsSyncRequired() responds correctly to data type throttling.
423 TEST_F(NudgeTrackerTest, IsSyncRequired_Throttling) {
424 const base::TimeTicks t0 = base::TimeTicks::FromInternalValue(1234);
425 const base::TimeDelta throttle_length = base::TimeDelta::FromMinutes(10);
426 const base::TimeTicks t1 = t0 + throttle_length;
428 EXPECT_FALSE(nudge_tracker_.IsSyncRequired());
430 // A local change to sessions enables the flag.
431 nudge_tracker_.RecordLocalChange(ModelTypeSet(SESSIONS));
432 EXPECT_TRUE(nudge_tracker_.IsSyncRequired());
434 // But the throttling of sessions unsets it.
435 nudge_tracker_.SetTypesThrottledUntil(ModelTypeSet(SESSIONS),
436 throttle_length,
437 t0);
438 EXPECT_FALSE(nudge_tracker_.IsSyncRequired());
440 // A refresh request for bookmarks means we have reason to sync again.
441 nudge_tracker_.RecordLocalRefreshRequest(ModelTypeSet(BOOKMARKS));
442 EXPECT_TRUE(nudge_tracker_.IsSyncRequired());
444 // A successful sync cycle means we took care of bookmarks.
445 nudge_tracker_.RecordSuccessfulSyncCycle();
446 EXPECT_FALSE(nudge_tracker_.IsSyncRequired());
448 // But we still haven't dealt with sessions. We'll need to remember
449 // that sessions are out of sync and re-enable the flag when their
450 // throttling interval expires.
451 nudge_tracker_.UpdateTypeThrottlingState(t1);
452 EXPECT_FALSE(nudge_tracker_.IsTypeThrottled(SESSIONS));
453 EXPECT_TRUE(nudge_tracker_.IsSyncRequired());
456 // Test IsGetUpdatesRequired() responds correctly to data type throttling.
457 TEST_F(NudgeTrackerTest, IsGetUpdatesRequired_Throttling) {
458 const base::TimeTicks t0 = base::TimeTicks::FromInternalValue(1234);
459 const base::TimeDelta throttle_length = base::TimeDelta::FromMinutes(10);
460 const base::TimeTicks t1 = t0 + throttle_length;
462 EXPECT_FALSE(nudge_tracker_.IsGetUpdatesRequired());
464 // A refresh request to sessions enables the flag.
465 nudge_tracker_.RecordLocalRefreshRequest(ModelTypeSet(SESSIONS));
466 EXPECT_TRUE(nudge_tracker_.IsGetUpdatesRequired());
468 // But the throttling of sessions unsets it.
469 nudge_tracker_.SetTypesThrottledUntil(ModelTypeSet(SESSIONS),
470 throttle_length,
471 t0);
472 EXPECT_FALSE(nudge_tracker_.IsGetUpdatesRequired());
474 // A refresh request for bookmarks means we have reason to sync again.
475 nudge_tracker_.RecordLocalRefreshRequest(ModelTypeSet(BOOKMARKS));
476 EXPECT_TRUE(nudge_tracker_.IsGetUpdatesRequired());
478 // A successful sync cycle means we took care of bookmarks.
479 nudge_tracker_.RecordSuccessfulSyncCycle();
480 EXPECT_FALSE(nudge_tracker_.IsGetUpdatesRequired());
482 // But we still haven't dealt with sessions. We'll need to remember
483 // that sessions are out of sync and re-enable the flag when their
484 // throttling interval expires.
485 nudge_tracker_.UpdateTypeThrottlingState(t1);
486 EXPECT_FALSE(nudge_tracker_.IsTypeThrottled(SESSIONS));
487 EXPECT_TRUE(nudge_tracker_.IsGetUpdatesRequired());
490 // Tests throttling-related getter functions when no types are throttled.
491 TEST_F(NudgeTrackerTest, NoTypesThrottled) {
492 EXPECT_FALSE(nudge_tracker_.IsAnyTypeThrottled());
493 EXPECT_FALSE(nudge_tracker_.IsTypeThrottled(SESSIONS));
494 EXPECT_TRUE(nudge_tracker_.GetThrottledTypes().Empty());
497 // Tests throttling-related getter functions when some types are throttled.
498 TEST_F(NudgeTrackerTest, ThrottleAndUnthrottle) {
499 const base::TimeTicks t0 = base::TimeTicks::FromInternalValue(1234);
500 const base::TimeDelta throttle_length = base::TimeDelta::FromMinutes(10);
501 const base::TimeTicks t1 = t0 + throttle_length;
503 nudge_tracker_.SetTypesThrottledUntil(ModelTypeSet(SESSIONS, PREFERENCES),
504 throttle_length,
505 t0);
507 EXPECT_TRUE(nudge_tracker_.IsAnyTypeThrottled());
508 EXPECT_TRUE(nudge_tracker_.IsTypeThrottled(SESSIONS));
509 EXPECT_TRUE(nudge_tracker_.IsTypeThrottled(PREFERENCES));
510 EXPECT_FALSE(nudge_tracker_.GetThrottledTypes().Empty());
511 EXPECT_EQ(throttle_length, nudge_tracker_.GetTimeUntilNextUnthrottle(t0));
513 nudge_tracker_.UpdateTypeThrottlingState(t1);
515 EXPECT_FALSE(nudge_tracker_.IsAnyTypeThrottled());
516 EXPECT_FALSE(nudge_tracker_.IsTypeThrottled(SESSIONS));
517 EXPECT_TRUE(nudge_tracker_.GetThrottledTypes().Empty());
520 TEST_F(NudgeTrackerTest, OverlappingThrottleIntervals) {
521 const base::TimeTicks t0 = base::TimeTicks::FromInternalValue(1234);
522 const base::TimeDelta throttle1_length = base::TimeDelta::FromMinutes(10);
523 const base::TimeDelta throttle2_length = base::TimeDelta::FromMinutes(20);
524 const base::TimeTicks t1 = t0 + throttle1_length;
525 const base::TimeTicks t2 = t0 + throttle2_length;
527 // Setup the longer of two intervals.
528 nudge_tracker_.SetTypesThrottledUntil(ModelTypeSet(SESSIONS, PREFERENCES),
529 throttle2_length,
530 t0);
531 EXPECT_TRUE(ModelTypeSetEquals(
532 ModelTypeSet(SESSIONS, PREFERENCES),
533 nudge_tracker_.GetThrottledTypes()));
534 EXPECT_EQ(throttle2_length,
535 nudge_tracker_.GetTimeUntilNextUnthrottle(t0));
537 // Setup the shorter interval.
538 nudge_tracker_.SetTypesThrottledUntil(ModelTypeSet(SESSIONS, BOOKMARKS),
539 throttle1_length,
540 t0);
541 EXPECT_TRUE(ModelTypeSetEquals(
542 ModelTypeSet(SESSIONS, PREFERENCES, BOOKMARKS),
543 nudge_tracker_.GetThrottledTypes()));
544 EXPECT_EQ(throttle1_length,
545 nudge_tracker_.GetTimeUntilNextUnthrottle(t0));
547 // Expire the first interval.
548 nudge_tracker_.UpdateTypeThrottlingState(t1);
550 // SESSIONS appeared in both intervals. We expect it will be throttled for
551 // the longer of the two, so it's still throttled at time t1.
552 EXPECT_TRUE(ModelTypeSetEquals(
553 ModelTypeSet(SESSIONS, PREFERENCES),
554 nudge_tracker_.GetThrottledTypes()));
555 EXPECT_EQ(throttle2_length - throttle1_length,
556 nudge_tracker_.GetTimeUntilNextUnthrottle(t1));
558 // Expire the second interval.
559 nudge_tracker_.UpdateTypeThrottlingState(t2);
560 EXPECT_TRUE(nudge_tracker_.GetThrottledTypes().Empty());
563 TEST_F(NudgeTrackerTest, Retry) {
564 const base::TimeTicks t0 = base::TimeTicks::FromInternalValue(12345);
565 const base::TimeTicks t3 = t0 + base::TimeDelta::FromSeconds(3);
566 const base::TimeTicks t4 = t0 + base::TimeDelta::FromSeconds(4);
568 // Set retry for t3.
569 nudge_tracker_.SetNextRetryTime(t3);
571 // Not due yet at t0.
572 nudge_tracker_.SetSyncCycleStartTime(t0);
573 EXPECT_FALSE(nudge_tracker_.IsRetryRequired());
574 EXPECT_FALSE(nudge_tracker_.IsGetUpdatesRequired());
576 // Successful sync cycle at t0 changes nothing.
577 nudge_tracker_.RecordSuccessfulSyncCycle();
578 EXPECT_FALSE(nudge_tracker_.IsRetryRequired());
579 EXPECT_FALSE(nudge_tracker_.IsGetUpdatesRequired());
581 // At t4, the retry becomes due.
582 nudge_tracker_.SetSyncCycleStartTime(t4);
583 EXPECT_TRUE(nudge_tracker_.IsRetryRequired());
584 EXPECT_TRUE(nudge_tracker_.IsGetUpdatesRequired());
586 // A sync cycle unsets the flag.
587 nudge_tracker_.RecordSuccessfulSyncCycle();
588 EXPECT_FALSE(nudge_tracker_.IsRetryRequired());
590 // It's still unset at the start of the next sync cycle.
591 nudge_tracker_.SetSyncCycleStartTime(t4);
592 EXPECT_FALSE(nudge_tracker_.IsRetryRequired());
595 // Test a mid-cycle update when IsRetryRequired() was true before the cycle
596 // began.
597 TEST_F(NudgeTrackerTest, IsRetryRequired_MidCycleUpdate1) {
598 const base::TimeTicks t0 = base::TimeTicks::FromInternalValue(12345);
599 const base::TimeTicks t1 = t0 + base::TimeDelta::FromSeconds(1);
600 const base::TimeTicks t2 = t0 + base::TimeDelta::FromSeconds(2);
601 const base::TimeTicks t5 = t0 + base::TimeDelta::FromSeconds(5);
602 const base::TimeTicks t6 = t0 + base::TimeDelta::FromSeconds(6);
604 nudge_tracker_.SetNextRetryTime(t0);
605 nudge_tracker_.SetSyncCycleStartTime(t1);
607 EXPECT_TRUE(nudge_tracker_.IsRetryRequired());
609 // Pretend that we were updated mid-cycle. SetSyncCycleStartTime is
610 // called only at the start of the sync cycle, so don't call it here.
611 // The update should have no effect on IsRetryRequired().
612 nudge_tracker_.SetNextRetryTime(t5);
614 EXPECT_TRUE(nudge_tracker_.IsRetryRequired());
616 // Verify that the successful sync cycle clears the flag.
617 nudge_tracker_.RecordSuccessfulSyncCycle();
618 EXPECT_FALSE(nudge_tracker_.IsRetryRequired());
620 // Verify expecations around the new retry time.
621 nudge_tracker_.SetSyncCycleStartTime(t2);
622 EXPECT_FALSE(nudge_tracker_.IsRetryRequired());
624 nudge_tracker_.SetSyncCycleStartTime(t6);
625 EXPECT_TRUE(nudge_tracker_.IsRetryRequired());
628 // Test a mid-cycle update when IsRetryRequired() was false before the cycle
629 // began.
630 TEST_F(NudgeTrackerTest, IsRetryRequired_MidCycleUpdate2) {
631 const base::TimeTicks t0 = base::TimeTicks::FromInternalValue(12345);
632 const base::TimeTicks t1 = t0 + base::TimeDelta::FromSeconds(1);
633 const base::TimeTicks t3 = t0 + base::TimeDelta::FromSeconds(3);
634 const base::TimeTicks t5 = t0 + base::TimeDelta::FromSeconds(5);
635 const base::TimeTicks t6 = t0 + base::TimeDelta::FromSeconds(6);
637 // Schedule a future retry, and a nudge unrelated to it.
638 nudge_tracker_.RecordLocalChange(ModelTypeSet(BOOKMARKS));
639 nudge_tracker_.SetNextRetryTime(t1);
640 nudge_tracker_.SetSyncCycleStartTime(t0);
641 EXPECT_FALSE(nudge_tracker_.IsRetryRequired());
643 // Pretend this happened in mid-cycle. This should have no effect on
644 // IsRetryRequired().
645 nudge_tracker_.SetNextRetryTime(t5);
646 EXPECT_FALSE(nudge_tracker_.IsRetryRequired());
648 // The cycle succeeded.
649 nudge_tracker_.RecordSuccessfulSyncCycle();
651 // The time t3 is greater than the GU retry time scheduled at the beginning of
652 // the test, but later than the retry time that overwrote it during the
653 // pretend 'sync cycle'.
654 nudge_tracker_.SetSyncCycleStartTime(t3);
655 EXPECT_FALSE(nudge_tracker_.IsRetryRequired());
657 // Finally, the retry established during the sync cycle becomes due.
658 nudge_tracker_.SetSyncCycleStartTime(t6);
659 EXPECT_TRUE(nudge_tracker_.IsRetryRequired());
662 // Simulate the case where a sync cycle fails.
663 TEST_F(NudgeTrackerTest, IsRetryRequired_FailedCycle) {
664 const base::TimeTicks t0 = base::TimeTicks::FromInternalValue(12345);
665 const base::TimeTicks t1 = t0 + base::TimeDelta::FromSeconds(1);
666 const base::TimeTicks t2 = t0 + base::TimeDelta::FromSeconds(2);
668 nudge_tracker_.SetNextRetryTime(t0);
669 nudge_tracker_.SetSyncCycleStartTime(t1);
670 EXPECT_TRUE(nudge_tracker_.IsRetryRequired());
672 // The nudge tracker receives no notifications for a failed sync cycle.
673 // Pretend one happened here.
674 EXPECT_TRUE(nudge_tracker_.IsRetryRequired());
676 // Think of this as the retry cycle.
677 nudge_tracker_.SetSyncCycleStartTime(t2);
678 EXPECT_TRUE(nudge_tracker_.IsRetryRequired());
680 // The second cycle is a success.
681 nudge_tracker_.RecordSuccessfulSyncCycle();
682 EXPECT_FALSE(nudge_tracker_.IsRetryRequired());
685 // Simulate a partially failed sync cycle. The callback to update the GU retry
686 // was invoked, but the sync cycle did not complete successfully.
687 TEST_F(NudgeTrackerTest, IsRetryRequired_FailedCycleIncludesUpdate) {
688 const base::TimeTicks t0 = base::TimeTicks::FromInternalValue(12345);
689 const base::TimeTicks t1 = t0 + base::TimeDelta::FromSeconds(1);
690 const base::TimeTicks t3 = t0 + base::TimeDelta::FromSeconds(3);
691 const base::TimeTicks t4 = t0 + base::TimeDelta::FromSeconds(4);
692 const base::TimeTicks t5 = t0 + base::TimeDelta::FromSeconds(5);
693 const base::TimeTicks t6 = t0 + base::TimeDelta::FromSeconds(6);
695 nudge_tracker_.SetNextRetryTime(t0);
696 nudge_tracker_.SetSyncCycleStartTime(t1);
697 EXPECT_TRUE(nudge_tracker_.IsRetryRequired());
699 // The cycle is in progress. A new GU Retry time is received.
700 // The flag is not because this cycle is still in progress.
701 nudge_tracker_.SetNextRetryTime(t5);
702 EXPECT_TRUE(nudge_tracker_.IsRetryRequired());
704 // The nudge tracker receives no notifications for a failed sync cycle.
705 // Pretend the cycle failed here.
707 // The next sync cycle starts. The new GU time has not taken effect by this
708 // time, but the NudgeTracker hasn't forgotten that we have not yet serviced
709 // the retry from the previous cycle.
710 nudge_tracker_.SetSyncCycleStartTime(t3);
711 EXPECT_TRUE(nudge_tracker_.IsRetryRequired());
713 // It succeeds. The retry time is not updated, so it should remain at t5.
714 nudge_tracker_.RecordSuccessfulSyncCycle();
716 // Another sync cycle. This one is still before the scheduled retry. It does
717 // not change the scheduled retry time.
718 nudge_tracker_.SetSyncCycleStartTime(t4);
719 EXPECT_FALSE(nudge_tracker_.IsRetryRequired());
720 nudge_tracker_.RecordSuccessfulSyncCycle();
722 // The retry scheduled way back during the first cycle of this test finally
723 // becomes due. Perform a successful sync cycle to service it.
724 nudge_tracker_.SetSyncCycleStartTime(t6);
725 EXPECT_TRUE(nudge_tracker_.IsRetryRequired());
726 nudge_tracker_.RecordSuccessfulSyncCycle();
729 // Test the default nudge delays for various types.
730 TEST_F(NudgeTrackerTest, NudgeDelayTest) {
731 // Set to a known value to compare against.
732 nudge_tracker_.SetDefaultNudgeDelay(base::TimeDelta());
734 // Bookmarks and preference both have "slow nudge" delays.
735 EXPECT_EQ(nudge_tracker_.RecordLocalChange(ModelTypeSet(BOOKMARKS)),
736 nudge_tracker_.RecordLocalChange(ModelTypeSet(PREFERENCES)));
738 // Typed URLs has a default delay.
739 EXPECT_EQ(nudge_tracker_.RecordLocalChange(ModelTypeSet(TYPED_URLS)),
740 base::TimeDelta());
742 // "Slow nudge" delays are longer than the default.
743 EXPECT_GT(nudge_tracker_.RecordLocalChange(ModelTypeSet(BOOKMARKS)),
744 base::TimeDelta());
746 // Sessions is longer than "slow nudge".
747 EXPECT_GT(nudge_tracker_.RecordLocalChange(ModelTypeSet(SESSIONS)),
748 nudge_tracker_.RecordLocalChange(ModelTypeSet(BOOKMARKS)));
750 // Favicons have the same delay as sessions.
751 EXPECT_EQ(nudge_tracker_.RecordLocalChange(ModelTypeSet(SESSIONS)),
752 nudge_tracker_.RecordLocalChange(ModelTypeSet(FAVICON_TRACKING)));
754 // Autofill has the longer delay of all.
755 EXPECT_GT(nudge_tracker_.RecordLocalChange(ModelTypeSet(AUTOFILL)),
756 nudge_tracker_.RecordLocalChange(ModelTypeSet(SESSIONS)));
758 // A nudge with no types takes the longest delay.
759 EXPECT_EQ(nudge_tracker_.RecordLocalChange(ModelTypeSet(AUTOFILL)),
760 nudge_tracker_.RecordLocalChange(ModelTypeSet()));
762 // The actual nudge delay should be the shortest of the set.
763 EXPECT_EQ(
764 nudge_tracker_.RecordLocalChange(ModelTypeSet(TYPED_URLS)),
765 nudge_tracker_.RecordLocalChange(ModelTypeSet(TYPED_URLS, AUTOFILL)));
768 // Test that custom nudge delays are used over the defaults.
769 TEST_F(NudgeTrackerTest, CustomDelayTest) {
770 // Set some custom delays.
771 std::map<ModelType, base::TimeDelta> delay_map;
772 delay_map[BOOKMARKS] = base::TimeDelta::FromSeconds(10);
773 delay_map[SESSIONS] = base::TimeDelta::FromSeconds(2);
774 nudge_tracker_.OnReceivedCustomNudgeDelays(delay_map);
776 // Only those with custom delays should be affected, not another type.
777 EXPECT_NE(nudge_tracker_.RecordLocalChange(ModelTypeSet(BOOKMARKS)),
778 nudge_tracker_.RecordLocalChange(ModelTypeSet(PREFERENCES)));
780 EXPECT_EQ(base::TimeDelta::FromSeconds(10),
781 nudge_tracker_.RecordLocalChange(ModelTypeSet(BOOKMARKS)));
782 EXPECT_EQ(base::TimeDelta::FromSeconds(2),
783 nudge_tracker_.RecordLocalChange(ModelTypeSet(SESSIONS)));
786 // Check that custom nudge delays can never result in a value shorter than the
787 // minimum nudge delay.
788 TEST_F(NudgeTrackerTest, NoTypesShorterThanDefault) {
789 // Set delay to a known value.
790 nudge_tracker_.SetDefaultNudgeDelay(base::TimeDelta::FromMilliseconds(500));
792 std::map<ModelType, base::TimeDelta> delay_map;
793 ModelTypeSet protocol_types = syncer::ProtocolTypes();
794 for (ModelTypeSet::Iterator iter = protocol_types.First(); iter.Good();
795 iter.Inc()) {
796 delay_map[iter.Get()] = base::TimeDelta();
798 nudge_tracker_.OnReceivedCustomNudgeDelays(delay_map);
800 // All types should still have a nudge greater than or equal to the minimum.
801 for (ModelTypeSet::Iterator iter = protocol_types.First(); iter.Good();
802 iter.Inc()) {
803 EXPECT_GE(nudge_tracker_.RecordLocalChange(ModelTypeSet(iter.Get())),
804 base::TimeDelta::FromMilliseconds(500));
808 class NudgeTrackerAckTrackingTest : public NudgeTrackerTest {
809 public:
810 NudgeTrackerAckTrackingTest() {}
812 bool IsInvalidationUnacknowledged(int tracking_id) {
813 return tracker_.IsUnacked(tracking_id);
816 bool IsInvalidationAcknowledged(int tracking_id) {
817 return tracker_.IsAcknowledged(tracking_id);
820 bool IsInvalidationDropped(int tracking_id) {
821 return tracker_.IsDropped(tracking_id);
824 int SendInvalidation(ModelType type, int version, const std::string& hint) {
825 // Build and register the invalidation.
826 scoped_ptr<TrackableMockInvalidation> inv =
827 tracker_.IssueInvalidation(version, hint);
828 int id = inv->GetTrackingId();
830 // Send it to the NudgeTracker.
831 nudge_tracker_.RecordRemoteInvalidation(type, inv.Pass());
833 // Return its ID to the test framework for use in assertions.
834 return id;
837 int SendUnknownVersionInvalidation(ModelType type) {
838 // Build and register the invalidation.
839 scoped_ptr<TrackableMockInvalidation> inv =
840 tracker_.IssueUnknownVersionInvalidation();
841 int id = inv->GetTrackingId();
843 // Send it to the NudgeTracker.
844 nudge_tracker_.RecordRemoteInvalidation(type, inv.Pass());
846 // Return its ID to the test framework for use in assertions.
847 return id;
850 bool AllInvalidationsAccountedFor() const {
851 return tracker_.AllInvalidationsAccountedFor();
854 void RecordSuccessfulSyncCycle() {
855 nudge_tracker_.RecordSuccessfulSyncCycle();
858 private:
859 MockInvalidationTracker tracker_;
862 // Test the acknowledgement of a single invalidation.
863 TEST_F(NudgeTrackerAckTrackingTest, SimpleAcknowledgement) {
864 int inv_id = SendInvalidation(BOOKMARKS, 10, "hint");
866 EXPECT_TRUE(IsInvalidationUnacknowledged(inv_id));
868 RecordSuccessfulSyncCycle();
869 EXPECT_TRUE(IsInvalidationAcknowledged(inv_id));
871 EXPECT_TRUE(AllInvalidationsAccountedFor());
874 // Test the acknowledgement of many invalidations.
875 TEST_F(NudgeTrackerAckTrackingTest, ManyAcknowledgements) {
876 int inv1_id = SendInvalidation(BOOKMARKS, 10, "hint");
877 int inv2_id = SendInvalidation(BOOKMARKS, 14, "hint2");
878 int inv3_id = SendInvalidation(PREFERENCES, 8, "hint3");
880 EXPECT_TRUE(IsInvalidationUnacknowledged(inv1_id));
881 EXPECT_TRUE(IsInvalidationUnacknowledged(inv2_id));
882 EXPECT_TRUE(IsInvalidationUnacknowledged(inv3_id));
884 RecordSuccessfulSyncCycle();
885 EXPECT_TRUE(IsInvalidationAcknowledged(inv1_id));
886 EXPECT_TRUE(IsInvalidationAcknowledged(inv2_id));
887 EXPECT_TRUE(IsInvalidationAcknowledged(inv3_id));
889 EXPECT_TRUE(AllInvalidationsAccountedFor());
892 // Test dropping when the buffer overflows and subsequent drop recovery.
893 TEST_F(NudgeTrackerAckTrackingTest, OverflowAndRecover) {
894 std::vector<int> invalidation_ids;
896 int inv10_id = SendInvalidation(BOOKMARKS, 10, "hint");
897 for (size_t i = 1; i < GetHintBufferSize(); ++i) {
898 invalidation_ids.push_back(SendInvalidation(BOOKMARKS, i + 10, "hint"));
901 for (std::vector<int>::iterator it = invalidation_ids.begin();
902 it != invalidation_ids.end();
903 ++it) {
904 EXPECT_TRUE(IsInvalidationUnacknowledged(*it));
907 // This invalidation, though arriving the most recently, has the oldest
908 // version number so it should be dropped first.
909 int inv5_id = SendInvalidation(BOOKMARKS, 5, "old_hint");
910 EXPECT_TRUE(IsInvalidationDropped(inv5_id));
912 // This invalidation has a larger version number, so it will force a
913 // previously delivered invalidation to be dropped.
914 int inv100_id = SendInvalidation(BOOKMARKS, 100, "new_hint");
915 EXPECT_TRUE(IsInvalidationDropped(inv10_id));
917 // This should recover from the drop and bring us back into sync.
918 RecordSuccessfulSyncCycle();
920 for (std::vector<int>::iterator it = invalidation_ids.begin();
921 it != invalidation_ids.end();
922 ++it) {
923 EXPECT_TRUE(IsInvalidationAcknowledged(*it));
925 EXPECT_TRUE(IsInvalidationAcknowledged(inv100_id));
927 EXPECT_TRUE(AllInvalidationsAccountedFor());
930 // Test receipt of an unknown version invalidation from the server.
931 TEST_F(NudgeTrackerAckTrackingTest, UnknownVersionFromServer_Simple) {
932 int inv_id = SendUnknownVersionInvalidation(BOOKMARKS);
933 EXPECT_TRUE(IsInvalidationUnacknowledged(inv_id));
934 RecordSuccessfulSyncCycle();
935 EXPECT_TRUE(IsInvalidationAcknowledged(inv_id));
936 EXPECT_TRUE(AllInvalidationsAccountedFor());
939 // Test receipt of multiple unknown version invalidations from the server.
940 TEST_F(NudgeTrackerAckTrackingTest, UnknownVersionFromServer_Complex) {
941 int inv1_id = SendUnknownVersionInvalidation(BOOKMARKS);
942 int inv2_id = SendInvalidation(BOOKMARKS, 10, "hint");
943 int inv3_id = SendUnknownVersionInvalidation(BOOKMARKS);
944 int inv4_id = SendUnknownVersionInvalidation(BOOKMARKS);
945 int inv5_id = SendInvalidation(BOOKMARKS, 20, "hint2");
947 // These invalidations have been overridden, so they got acked early.
948 EXPECT_TRUE(IsInvalidationAcknowledged(inv1_id));
949 EXPECT_TRUE(IsInvalidationAcknowledged(inv3_id));
951 // These invalidations are still waiting to be used.
952 EXPECT_TRUE(IsInvalidationUnacknowledged(inv2_id));
953 EXPECT_TRUE(IsInvalidationUnacknowledged(inv4_id));
954 EXPECT_TRUE(IsInvalidationUnacknowledged(inv5_id));
956 // Finish the sync cycle and expect all remaining invalidations to be acked.
957 RecordSuccessfulSyncCycle();
958 EXPECT_TRUE(IsInvalidationAcknowledged(inv1_id));
959 EXPECT_TRUE(IsInvalidationAcknowledged(inv2_id));
960 EXPECT_TRUE(IsInvalidationAcknowledged(inv3_id));
961 EXPECT_TRUE(IsInvalidationAcknowledged(inv4_id));
962 EXPECT_TRUE(IsInvalidationAcknowledged(inv5_id));
964 EXPECT_TRUE(AllInvalidationsAccountedFor());
967 } // namespace sessions
968 } // namespace syncer