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

Side by Side Diff: components/sync/sessions_impl/nudge_tracker_unittest.cc

Issue 2130453004: [Sync] Move //sync to //components/sync. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase. Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "sync/sessions/nudge_tracker.h" 5 #include "components/sync/sessions_impl/nudge_tracker.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 #include <stdint.h> 8 #include <stdint.h>
9
9 #include <string> 10 #include <string>
10 #include <utility> 11 #include <utility>
11 #include <vector> 12 #include <vector>
12 13
13 #include "base/message_loop/message_loop.h" 14 #include "base/message_loop/message_loop.h"
14 #include "base/run_loop.h" 15 #include "base/run_loop.h"
15 #include "sync/internal_api/public/base/model_type_test_util.h" 16 #include "components/sync/base/model_type_test_util.h"
16 #include "sync/test/mock_invalidation.h" 17 #include "components/sync/test/mock_invalidation.h"
17 #include "sync/test/mock_invalidation_tracker.h" 18 #include "components/sync/test/mock_invalidation_tracker.h"
18 #include "sync/test/trackable_mock_invalidation.h" 19 #include "components/sync/test/trackable_mock_invalidation.h"
19 #include "testing/gtest/include/gtest/gtest.h" 20 #include "testing/gtest/include/gtest/gtest.h"
20 21
21 namespace syncer { 22 namespace syncer {
22 23
23 namespace { 24 namespace {
24 25
25 testing::AssertionResult ModelTypeSetEquals(ModelTypeSet a, ModelTypeSet b) { 26 testing::AssertionResult ModelTypeSetEquals(ModelTypeSet a, ModelTypeSet b) {
26 if (a == b) { 27 if (a == b) {
27 return testing::AssertionSuccess(); 28 return testing::AssertionSuccess();
28 } else { 29 } else {
29 return testing::AssertionFailure() 30 return testing::AssertionFailure()
30 << "Left side " << ModelTypeSetToString(a) 31 << "Left side " << ModelTypeSetToString(a)
31 << ", does not match rigth side: " << ModelTypeSetToString(b); 32 << ", does not match rigth side: " << ModelTypeSetToString(b);
32 } 33 }
33 } 34 }
34 35
35 } // namespace 36 } // namespace
36 37
37 namespace sessions { 38 namespace sessions {
38 39
39 class NudgeTrackerTest : public ::testing::Test { 40 class NudgeTrackerTest : public ::testing::Test {
40 public: 41 public:
41 NudgeTrackerTest() { 42 NudgeTrackerTest() { SetInvalidationsInSync(); }
42 SetInvalidationsInSync();
43 }
44 43
45 static size_t GetHintBufferSize() { 44 static size_t GetHintBufferSize() {
46 // Assumes that no test has adjusted this size. 45 // Assumes that no test has adjusted this size.
47 return NudgeTracker::kDefaultMaxPayloadsPerType; 46 return NudgeTracker::kDefaultMaxPayloadsPerType;
48 } 47 }
49 48
50 bool InvalidationsOutOfSync() const { 49 bool InvalidationsOutOfSync() const {
51 // We don't currently track invalidations out of sync on a per-type basis. 50 // We don't currently track invalidations out of sync on a per-type basis.
52 sync_pb::GetUpdateTriggers gu_trigger; 51 sync_pb::GetUpdateTriggers gu_trigger;
53 nudge_tracker_.FillProtoMessage(BOOKMARKS, &gu_trigger); 52 nudge_tracker_.FillProtoMessage(BOOKMARKS, &gu_trigger);
(...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after
235 BuildInvalidation(1000, "new_hint")); 234 BuildInvalidation(1000, "new_hint"));
236 235
237 { 236 {
238 sync_pb::GetUpdateTriggers gu_trigger; 237 sync_pb::GetUpdateTriggers gu_trigger;
239 nudge_tracker_.FillProtoMessage(BOOKMARKS, &gu_trigger); 238 nudge_tracker_.FillProtoMessage(BOOKMARKS, &gu_trigger);
240 EXPECT_TRUE(gu_trigger.client_dropped_hints()); 239 EXPECT_TRUE(gu_trigger.client_dropped_hints());
241 ASSERT_EQ(GetHintBufferSize(), 240 ASSERT_EQ(GetHintBufferSize(),
242 static_cast<size_t>(gu_trigger.notification_hint_size())); 241 static_cast<size_t>(gu_trigger.notification_hint_size()));
243 242
244 // Verify the newest hint was not dropped and is the last in the list. 243 // 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)); 244 EXPECT_EQ("new_hint",
245 gu_trigger.notification_hint(GetHintBufferSize() - 1));
246 246
247 // Verify the oldest hint, too. 247 // Verify the oldest hint, too.
248 EXPECT_EQ("hint", gu_trigger.notification_hint(0)); 248 EXPECT_EQ("hint", gu_trigger.notification_hint(0));
249 } 249 }
250 } 250 }
251 251
252 // Tests the receipt of 'unknown version' invalidations. 252 // Tests the receipt of 'unknown version' invalidations.
253 TEST_F(NudgeTrackerTest, DropHintsAtServer_Alone) { 253 TEST_F(NudgeTrackerTest, DropHintsAtServer_Alone) {
254 // Record the unknown version invalidation. 254 // Record the unknown version invalidation.
255 nudge_tracker_.RecordRemoteInvalidation(BOOKMARKS, 255 nudge_tracker_.RecordRemoteInvalidation(BOOKMARKS,
(...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after
439 const base::TimeDelta throttle_length = base::TimeDelta::FromMinutes(10); 439 const base::TimeDelta throttle_length = base::TimeDelta::FromMinutes(10);
440 const base::TimeTicks t1 = t0 + throttle_length; 440 const base::TimeTicks t1 = t0 + throttle_length;
441 441
442 EXPECT_FALSE(nudge_tracker_.IsSyncRequired()); 442 EXPECT_FALSE(nudge_tracker_.IsSyncRequired());
443 443
444 // A local change to sessions enables the flag. 444 // A local change to sessions enables the flag.
445 nudge_tracker_.RecordLocalChange(ModelTypeSet(SESSIONS)); 445 nudge_tracker_.RecordLocalChange(ModelTypeSet(SESSIONS));
446 EXPECT_TRUE(nudge_tracker_.IsSyncRequired()); 446 EXPECT_TRUE(nudge_tracker_.IsSyncRequired());
447 447
448 // But the throttling of sessions unsets it. 448 // But the throttling of sessions unsets it.
449 nudge_tracker_.SetTypesThrottledUntil(ModelTypeSet(SESSIONS), 449 nudge_tracker_.SetTypesThrottledUntil(ModelTypeSet(SESSIONS), throttle_length,
450 throttle_length, 450 t0);
451 t0);
452 EXPECT_FALSE(nudge_tracker_.IsSyncRequired()); 451 EXPECT_FALSE(nudge_tracker_.IsSyncRequired());
453 452
454 // A refresh request for bookmarks means we have reason to sync again. 453 // A refresh request for bookmarks means we have reason to sync again.
455 nudge_tracker_.RecordLocalRefreshRequest(ModelTypeSet(BOOKMARKS)); 454 nudge_tracker_.RecordLocalRefreshRequest(ModelTypeSet(BOOKMARKS));
456 EXPECT_TRUE(nudge_tracker_.IsSyncRequired()); 455 EXPECT_TRUE(nudge_tracker_.IsSyncRequired());
457 456
458 // A successful sync cycle means we took care of bookmarks. 457 // A successful sync cycle means we took care of bookmarks.
459 nudge_tracker_.RecordSuccessfulSyncCycle(); 458 nudge_tracker_.RecordSuccessfulSyncCycle();
460 EXPECT_FALSE(nudge_tracker_.IsSyncRequired()); 459 EXPECT_FALSE(nudge_tracker_.IsSyncRequired());
461 460
(...skipping 11 matching lines...) Expand all
473 const base::TimeDelta throttle_length = base::TimeDelta::FromMinutes(10); 472 const base::TimeDelta throttle_length = base::TimeDelta::FromMinutes(10);
474 const base::TimeTicks t1 = t0 + throttle_length; 473 const base::TimeTicks t1 = t0 + throttle_length;
475 474
476 EXPECT_FALSE(nudge_tracker_.IsGetUpdatesRequired()); 475 EXPECT_FALSE(nudge_tracker_.IsGetUpdatesRequired());
477 476
478 // A refresh request to sessions enables the flag. 477 // A refresh request to sessions enables the flag.
479 nudge_tracker_.RecordLocalRefreshRequest(ModelTypeSet(SESSIONS)); 478 nudge_tracker_.RecordLocalRefreshRequest(ModelTypeSet(SESSIONS));
480 EXPECT_TRUE(nudge_tracker_.IsGetUpdatesRequired()); 479 EXPECT_TRUE(nudge_tracker_.IsGetUpdatesRequired());
481 480
482 // But the throttling of sessions unsets it. 481 // But the throttling of sessions unsets it.
483 nudge_tracker_.SetTypesThrottledUntil(ModelTypeSet(SESSIONS), 482 nudge_tracker_.SetTypesThrottledUntil(ModelTypeSet(SESSIONS), throttle_length,
484 throttle_length, 483 t0);
485 t0);
486 EXPECT_FALSE(nudge_tracker_.IsGetUpdatesRequired()); 484 EXPECT_FALSE(nudge_tracker_.IsGetUpdatesRequired());
487 485
488 // A refresh request for bookmarks means we have reason to sync again. 486 // A refresh request for bookmarks means we have reason to sync again.
489 nudge_tracker_.RecordLocalRefreshRequest(ModelTypeSet(BOOKMARKS)); 487 nudge_tracker_.RecordLocalRefreshRequest(ModelTypeSet(BOOKMARKS));
490 EXPECT_TRUE(nudge_tracker_.IsGetUpdatesRequired()); 488 EXPECT_TRUE(nudge_tracker_.IsGetUpdatesRequired());
491 489
492 // A successful sync cycle means we took care of bookmarks. 490 // A successful sync cycle means we took care of bookmarks.
493 nudge_tracker_.RecordSuccessfulSyncCycle(); 491 nudge_tracker_.RecordSuccessfulSyncCycle();
494 EXPECT_FALSE(nudge_tracker_.IsGetUpdatesRequired()); 492 EXPECT_FALSE(nudge_tracker_.IsGetUpdatesRequired());
495 493
(...skipping 12 matching lines...) Expand all
508 EXPECT_TRUE(nudge_tracker_.GetThrottledTypes().Empty()); 506 EXPECT_TRUE(nudge_tracker_.GetThrottledTypes().Empty());
509 } 507 }
510 508
511 // Tests throttling-related getter functions when some types are throttled. 509 // Tests throttling-related getter functions when some types are throttled.
512 TEST_F(NudgeTrackerTest, ThrottleAndUnthrottle) { 510 TEST_F(NudgeTrackerTest, ThrottleAndUnthrottle) {
513 const base::TimeTicks t0 = base::TimeTicks::FromInternalValue(1234); 511 const base::TimeTicks t0 = base::TimeTicks::FromInternalValue(1234);
514 const base::TimeDelta throttle_length = base::TimeDelta::FromMinutes(10); 512 const base::TimeDelta throttle_length = base::TimeDelta::FromMinutes(10);
515 const base::TimeTicks t1 = t0 + throttle_length; 513 const base::TimeTicks t1 = t0 + throttle_length;
516 514
517 nudge_tracker_.SetTypesThrottledUntil(ModelTypeSet(SESSIONS, PREFERENCES), 515 nudge_tracker_.SetTypesThrottledUntil(ModelTypeSet(SESSIONS, PREFERENCES),
518 throttle_length, 516 throttle_length, t0);
519 t0);
520 517
521 EXPECT_TRUE(nudge_tracker_.IsAnyTypeThrottled()); 518 EXPECT_TRUE(nudge_tracker_.IsAnyTypeThrottled());
522 EXPECT_TRUE(nudge_tracker_.IsTypeThrottled(SESSIONS)); 519 EXPECT_TRUE(nudge_tracker_.IsTypeThrottled(SESSIONS));
523 EXPECT_TRUE(nudge_tracker_.IsTypeThrottled(PREFERENCES)); 520 EXPECT_TRUE(nudge_tracker_.IsTypeThrottled(PREFERENCES));
524 EXPECT_FALSE(nudge_tracker_.GetThrottledTypes().Empty()); 521 EXPECT_FALSE(nudge_tracker_.GetThrottledTypes().Empty());
525 EXPECT_EQ(throttle_length, nudge_tracker_.GetTimeUntilNextUnthrottle(t0)); 522 EXPECT_EQ(throttle_length, nudge_tracker_.GetTimeUntilNextUnthrottle(t0));
526 523
527 nudge_tracker_.UpdateTypeThrottlingState(t1); 524 nudge_tracker_.UpdateTypeThrottlingState(t1);
528 525
529 EXPECT_FALSE(nudge_tracker_.IsAnyTypeThrottled()); 526 EXPECT_FALSE(nudge_tracker_.IsAnyTypeThrottled());
530 EXPECT_FALSE(nudge_tracker_.IsTypeThrottled(SESSIONS)); 527 EXPECT_FALSE(nudge_tracker_.IsTypeThrottled(SESSIONS));
531 EXPECT_TRUE(nudge_tracker_.GetThrottledTypes().Empty()); 528 EXPECT_TRUE(nudge_tracker_.GetThrottledTypes().Empty());
532 } 529 }
533 530
534 TEST_F(NudgeTrackerTest, OverlappingThrottleIntervals) { 531 TEST_F(NudgeTrackerTest, OverlappingThrottleIntervals) {
535 const base::TimeTicks t0 = base::TimeTicks::FromInternalValue(1234); 532 const base::TimeTicks t0 = base::TimeTicks::FromInternalValue(1234);
536 const base::TimeDelta throttle1_length = base::TimeDelta::FromMinutes(10); 533 const base::TimeDelta throttle1_length = base::TimeDelta::FromMinutes(10);
537 const base::TimeDelta throttle2_length = base::TimeDelta::FromMinutes(20); 534 const base::TimeDelta throttle2_length = base::TimeDelta::FromMinutes(20);
538 const base::TimeTicks t1 = t0 + throttle1_length; 535 const base::TimeTicks t1 = t0 + throttle1_length;
539 const base::TimeTicks t2 = t0 + throttle2_length; 536 const base::TimeTicks t2 = t0 + throttle2_length;
540 537
541 // Setup the longer of two intervals. 538 // Setup the longer of two intervals.
542 nudge_tracker_.SetTypesThrottledUntil(ModelTypeSet(SESSIONS, PREFERENCES), 539 nudge_tracker_.SetTypesThrottledUntil(ModelTypeSet(SESSIONS, PREFERENCES),
543 throttle2_length, 540 throttle2_length, t0);
544 t0); 541 EXPECT_TRUE(ModelTypeSetEquals(ModelTypeSet(SESSIONS, PREFERENCES),
545 EXPECT_TRUE(ModelTypeSetEquals( 542 nudge_tracker_.GetThrottledTypes()));
546 ModelTypeSet(SESSIONS, PREFERENCES), 543 EXPECT_EQ(throttle2_length, nudge_tracker_.GetTimeUntilNextUnthrottle(t0));
547 nudge_tracker_.GetThrottledTypes()));
548 EXPECT_EQ(throttle2_length,
549 nudge_tracker_.GetTimeUntilNextUnthrottle(t0));
550 544
551 // Setup the shorter interval. 545 // Setup the shorter interval.
552 nudge_tracker_.SetTypesThrottledUntil(ModelTypeSet(SESSIONS, BOOKMARKS), 546 nudge_tracker_.SetTypesThrottledUntil(ModelTypeSet(SESSIONS, BOOKMARKS),
553 throttle1_length, 547 throttle1_length, t0);
554 t0); 548 EXPECT_TRUE(ModelTypeSetEquals(ModelTypeSet(SESSIONS, PREFERENCES, BOOKMARKS),
555 EXPECT_TRUE(ModelTypeSetEquals( 549 nudge_tracker_.GetThrottledTypes()));
556 ModelTypeSet(SESSIONS, PREFERENCES, BOOKMARKS), 550 EXPECT_EQ(throttle1_length, nudge_tracker_.GetTimeUntilNextUnthrottle(t0));
557 nudge_tracker_.GetThrottledTypes()));
558 EXPECT_EQ(throttle1_length,
559 nudge_tracker_.GetTimeUntilNextUnthrottle(t0));
560 551
561 // Expire the first interval. 552 // Expire the first interval.
562 nudge_tracker_.UpdateTypeThrottlingState(t1); 553 nudge_tracker_.UpdateTypeThrottlingState(t1);
563 554
564 // SESSIONS appeared in both intervals. We expect it will be throttled for 555 // 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. 556 // the longer of the two, so it's still throttled at time t1.
566 EXPECT_TRUE(ModelTypeSetEquals( 557 EXPECT_TRUE(ModelTypeSetEquals(ModelTypeSet(SESSIONS, PREFERENCES),
567 ModelTypeSet(SESSIONS, PREFERENCES), 558 nudge_tracker_.GetThrottledTypes()));
568 nudge_tracker_.GetThrottledTypes()));
569 EXPECT_EQ(throttle2_length - throttle1_length, 559 EXPECT_EQ(throttle2_length - throttle1_length,
570 nudge_tracker_.GetTimeUntilNextUnthrottle(t1)); 560 nudge_tracker_.GetTimeUntilNextUnthrottle(t1));
571 561
572 // Expire the second interval. 562 // Expire the second interval.
573 nudge_tracker_.UpdateTypeThrottlingState(t2); 563 nudge_tracker_.UpdateTypeThrottlingState(t2);
574 EXPECT_TRUE(nudge_tracker_.GetThrottledTypes().Empty()); 564 EXPECT_TRUE(nudge_tracker_.GetThrottledTypes().Empty());
575 } 565 }
576 566
577 TEST_F(NudgeTrackerTest, Retry) { 567 TEST_F(NudgeTrackerTest, Retry) {
578 const base::TimeTicks t0 = base::TimeTicks::FromInternalValue(12345); 568 const base::TimeTicks t0 = base::TimeTicks::FromInternalValue(12345);
(...skipping 327 matching lines...) Expand 10 before | Expand all | Expand 10 after
906 // Test dropping when the buffer overflows and subsequent drop recovery. 896 // Test dropping when the buffer overflows and subsequent drop recovery.
907 TEST_F(NudgeTrackerAckTrackingTest, OverflowAndRecover) { 897 TEST_F(NudgeTrackerAckTrackingTest, OverflowAndRecover) {
908 std::vector<int> invalidation_ids; 898 std::vector<int> invalidation_ids;
909 899
910 int inv10_id = SendInvalidation(BOOKMARKS, 10, "hint"); 900 int inv10_id = SendInvalidation(BOOKMARKS, 10, "hint");
911 for (size_t i = 1; i < GetHintBufferSize(); ++i) { 901 for (size_t i = 1; i < GetHintBufferSize(); ++i) {
912 invalidation_ids.push_back(SendInvalidation(BOOKMARKS, i + 10, "hint")); 902 invalidation_ids.push_back(SendInvalidation(BOOKMARKS, i + 10, "hint"));
913 } 903 }
914 904
915 for (std::vector<int>::iterator it = invalidation_ids.begin(); 905 for (std::vector<int>::iterator it = invalidation_ids.begin();
916 it != invalidation_ids.end(); 906 it != invalidation_ids.end(); ++it) {
917 ++it) {
918 EXPECT_TRUE(IsInvalidationUnacknowledged(*it)); 907 EXPECT_TRUE(IsInvalidationUnacknowledged(*it));
919 } 908 }
920 909
921 // This invalidation, though arriving the most recently, has the oldest 910 // This invalidation, though arriving the most recently, has the oldest
922 // version number so it should be dropped first. 911 // version number so it should be dropped first.
923 int inv5_id = SendInvalidation(BOOKMARKS, 5, "old_hint"); 912 int inv5_id = SendInvalidation(BOOKMARKS, 5, "old_hint");
924 EXPECT_TRUE(IsInvalidationDropped(inv5_id)); 913 EXPECT_TRUE(IsInvalidationDropped(inv5_id));
925 914
926 // This invalidation has a larger version number, so it will force a 915 // This invalidation has a larger version number, so it will force a
927 // previously delivered invalidation to be dropped. 916 // previously delivered invalidation to be dropped.
928 int inv100_id = SendInvalidation(BOOKMARKS, 100, "new_hint"); 917 int inv100_id = SendInvalidation(BOOKMARKS, 100, "new_hint");
929 EXPECT_TRUE(IsInvalidationDropped(inv10_id)); 918 EXPECT_TRUE(IsInvalidationDropped(inv10_id));
930 919
931 // This should recover from the drop and bring us back into sync. 920 // This should recover from the drop and bring us back into sync.
932 RecordSuccessfulSyncCycle(); 921 RecordSuccessfulSyncCycle();
933 922
934 for (std::vector<int>::iterator it = invalidation_ids.begin(); 923 for (std::vector<int>::iterator it = invalidation_ids.begin();
935 it != invalidation_ids.end(); 924 it != invalidation_ids.end(); ++it) {
936 ++it) {
937 EXPECT_TRUE(IsInvalidationAcknowledged(*it)); 925 EXPECT_TRUE(IsInvalidationAcknowledged(*it));
938 } 926 }
939 EXPECT_TRUE(IsInvalidationAcknowledged(inv100_id)); 927 EXPECT_TRUE(IsInvalidationAcknowledged(inv100_id));
940 928
941 EXPECT_TRUE(AllInvalidationsAccountedFor()); 929 EXPECT_TRUE(AllInvalidationsAccountedFor());
942 } 930 }
943 931
944 // Test receipt of an unknown version invalidation from the server. 932 // Test receipt of an unknown version invalidation from the server.
945 TEST_F(NudgeTrackerAckTrackingTest, UnknownVersionFromServer_Simple) { 933 TEST_F(NudgeTrackerAckTrackingTest, UnknownVersionFromServer_Simple) {
946 int inv_id = SendUnknownVersionInvalidation(BOOKMARKS); 934 int inv_id = SendUnknownVersionInvalidation(BOOKMARKS);
(...skipping 26 matching lines...) Expand all
973 EXPECT_TRUE(IsInvalidationAcknowledged(inv2_id)); 961 EXPECT_TRUE(IsInvalidationAcknowledged(inv2_id));
974 EXPECT_TRUE(IsInvalidationAcknowledged(inv3_id)); 962 EXPECT_TRUE(IsInvalidationAcknowledged(inv3_id));
975 EXPECT_TRUE(IsInvalidationAcknowledged(inv4_id)); 963 EXPECT_TRUE(IsInvalidationAcknowledged(inv4_id));
976 EXPECT_TRUE(IsInvalidationAcknowledged(inv5_id)); 964 EXPECT_TRUE(IsInvalidationAcknowledged(inv5_id));
977 965
978 EXPECT_TRUE(AllInvalidationsAccountedFor()); 966 EXPECT_TRUE(AllInvalidationsAccountedFor());
979 } 967 }
980 968
981 } // namespace sessions 969 } // namespace sessions
982 } // namespace syncer 970 } // namespace syncer
OLDNEW
« no previous file with comments | « components/sync/sessions_impl/nudge_tracker.cc ('k') | components/sync/sessions_impl/status_controller.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698