1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/bind.h"
#include "base/callback.h"
#include "base/compiler_specific.h"
#include "base/memory/weak_ptr.h"
#include "base/message_loop.h"
#include "base/test/test_timeouts.h"
#include "sync/engine/backoff_delay_provider.h"
#include "sync/engine/sync_scheduler_impl.h"
#include "sync/engine/syncer.h"
#include "sync/internal_api/public/base/model_type_invalidation_map_test_util.h"
#include "sync/sessions/test_util.h"
#include "sync/test/callback_counter.h"
#include "sync/test/engine/fake_model_worker.h"
#include "sync/test/engine/mock_connection_manager.h"
#include "sync/test/engine/test_directory_setter_upper.h"
#include "sync/test/fake_extensions_activity_monitor.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
using base::TimeDelta;
using base::TimeTicks;
using testing::_;
using testing::AtLeast;
using testing::DoAll;
using testing::Eq;
using testing::Invoke;
using testing::Mock;
using testing::Not;
using testing::Return;
using testing::WithArg;
using testing::WithArgs;
namespace syncer {
using sessions::SyncSession;
using sessions::SyncSessionContext;
using sessions::SyncSessionSnapshot;
using sync_pb::GetUpdatesCallerInfo;
class MockSyncer : public Syncer {
public:
MOCK_METHOD3(NormalSyncShare, bool(ModelTypeSet,
const sessions::NudgeTracker&,
sessions::SyncSession*));
MOCK_METHOD2(ConfigureSyncShare, bool(ModelTypeSet, sessions::SyncSession*));
MOCK_METHOD2(PollSyncShare, bool(ModelTypeSet, sessions::SyncSession*));
};
// Used when tests want to record syncing activity to examine later.
struct SyncShareRecords {
std::vector<TimeTicks> times;
std::vector<SyncSessionSnapshot> snapshots;
};
void QuitLoopNow() {
// We use QuitNow() instead of Quit() as the latter may get stalled
// indefinitely in the presence of repeated timers with low delays
// and a slow test (e.g., ThrottlingDoesThrottle [which has a poll
// delay of 5ms] run under TSAN on the trybots).
base::MessageLoop::current()->QuitNow();
}
void RunLoop() {
base::MessageLoop::current()->Run();
}
void PumpLoop() {
// Do it this way instead of RunAllPending to pump loop exactly once
// (necessary in the presence of timers; see comment in
// QuitLoopNow).
base::MessageLoop::current()->PostTask(FROM_HERE, base::Bind(&QuitLoopNow));
RunLoop();
}
void PumpLoopFor(base::TimeDelta time) {
// Allow the loop to run for the specified amount of time.
base::MessageLoop::current()->PostDelayedTask(
FROM_HERE, base::Bind(&QuitLoopNow), time);
RunLoop();
}
ModelSafeRoutingInfo TypesToRoutingInfo(ModelTypeSet types) {
ModelSafeRoutingInfo routes;
for (ModelTypeSet::Iterator iter = types.First(); iter.Good(); iter.Inc()) {
routes[iter.Get()] = GROUP_PASSIVE;
}
return routes;
}
// Compare a ModelTypeSet to a ModelTypeInvalidationMap, ignoring
// state values.
testing::AssertionResult ModelTypeSetMatchesInvalidationMap(
ModelTypeSet lhs, const ModelTypeInvalidationMap& rhs) {
ModelTypeSet rhs_set = ModelTypeInvalidationMapToSet(rhs);
if (!rhs_set.Equals(rhs_set)) {
return testing::AssertionFailure()
<< "ModelTypeSet: " << ModelTypeSetToString(lhs)
<< " does not match ModelTypeInvalidationMap: "
<< ModelTypeSetToString(rhs_set);
} else {
return testing::AssertionSuccess();
}
}
// Convenient to use in tests wishing to analyze SyncShare calls over time.
static const size_t kMinNumSamples = 5;
class SyncSchedulerTest : public testing::Test {
public:
SyncSchedulerTest() : weak_ptr_factory_(this), syncer_(NULL), delay_(NULL) {}
class MockDelayProvider : public BackoffDelayProvider {
public:
MockDelayProvider() : BackoffDelayProvider(
TimeDelta::FromSeconds(kInitialBackoffRetrySeconds),
TimeDelta::FromSeconds(kInitialBackoffImmediateRetrySeconds)) {
}
MOCK_METHOD1(GetDelay, TimeDelta(const TimeDelta&));
};
virtual void SetUp() {
dir_maker_.SetUp();
syncer_ = new MockSyncer();
delay_ = NULL;
routing_info_[BOOKMARKS] = GROUP_UI;
routing_info_[AUTOFILL] = GROUP_DB;
routing_info_[THEMES] = GROUP_UI;
routing_info_[NIGORI] = GROUP_PASSIVE;
workers_.clear();
workers_.push_back(make_scoped_refptr(new FakeModelWorker(GROUP_UI)));
workers_.push_back(make_scoped_refptr(new FakeModelWorker(GROUP_DB)));
workers_.push_back(make_scoped_refptr(new FakeModelWorker(GROUP_PASSIVE)));
std::vector<ModelSafeWorker*> workers;
for (std::vector<scoped_refptr<FakeModelWorker> >::iterator it =
workers_.begin(); it != workers_.end(); ++it) {
workers.push_back(it->get());
}
connection_.reset(new MockConnectionManager(directory()));
connection_->SetServerReachable();
context_.reset(new SyncSessionContext(
connection_.get(), directory(), workers,
&extensions_activity_monitor_,
std::vector<SyncEngineEventListener*>(), NULL, NULL,
true, // enable keystore encryption
"fake_invalidator_client_id"));
context_->set_routing_info(routing_info_);
context_->set_notifications_enabled(true);
context_->set_account_name("Test");
scheduler_.reset(
new SyncSchedulerImpl("TestSyncScheduler",
BackoffDelayProvider::FromDefaults(),
context(),
syncer_));
}
SyncSchedulerImpl* scheduler() { return scheduler_.get(); }
const ModelSafeRoutingInfo& routing_info() { return routing_info_; }
MockSyncer* syncer() { return syncer_; }
MockDelayProvider* delay() { return delay_; }
MockConnectionManager* connection() { return connection_.get(); }
TimeDelta zero() { return TimeDelta::FromSeconds(0); }
TimeDelta timeout() {
return TestTimeouts::action_timeout();
}
virtual void TearDown() {
PumpLoop();
scheduler_.reset();
PumpLoop();
dir_maker_.TearDown();
}
void AnalyzePollRun(const SyncShareRecords& records, size_t min_num_samples,
const TimeTicks& optimal_start, const TimeDelta& poll_interval) {
const std::vector<TimeTicks>& data(records.times);
EXPECT_GE(data.size(), min_num_samples);
for (size_t i = 0; i < data.size(); i++) {
SCOPED_TRACE(testing::Message() << "SyncShare # (" << i << ")");
TimeTicks optimal_next_sync = optimal_start + poll_interval * i;
EXPECT_GE(data[i], optimal_next_sync);
EXPECT_EQ(GetUpdatesCallerInfo::PERIODIC,
records.snapshots[i].source().updates_source);
}
}
void DoQuitLoopNow() {
QuitLoopNow();
}
void StartSyncScheduler(SyncScheduler::Mode mode) {
scheduler()->Start(mode);
}
// This stops the scheduler synchronously.
void StopSyncScheduler() {
base::MessageLoop::current()->PostTask(
FROM_HERE,
base::Bind(&SyncSchedulerTest::DoQuitLoopNow,
weak_ptr_factory_.GetWeakPtr()));
RunLoop();
}
bool RunAndGetBackoff() {
ModelTypeSet nudge_types(BOOKMARKS);
StartSyncScheduler(SyncScheduler::NORMAL_MODE);
scheduler()->ScheduleLocalNudge(zero(), nudge_types, FROM_HERE);
RunLoop();
return scheduler()->IsBackingOff();
}
void UseMockDelayProvider() {
delay_ = new MockDelayProvider();
scheduler_->delay_provider_.reset(delay_);
}
SyncSessionContext* context() { return context_.get(); }
ModelTypeSet GetThrottledTypes() {
return scheduler_->nudge_tracker_.GetThrottledTypes();
}
private:
syncable::Directory* directory() {
return dir_maker_.directory();
}
base::MessageLoop loop_;
base::WeakPtrFactory<SyncSchedulerTest> weak_ptr_factory_;
TestDirectorySetterUpper dir_maker_;
scoped_ptr<MockConnectionManager> connection_;
scoped_ptr<SyncSessionContext> context_;
scoped_ptr<SyncSchedulerImpl> scheduler_;
MockSyncer* syncer_;
MockDelayProvider* delay_;
std::vector<scoped_refptr<FakeModelWorker> > workers_;
FakeExtensionsActivityMonitor extensions_activity_monitor_;
ModelSafeRoutingInfo routing_info_;
};
void RecordSyncShareImpl(SyncSession* s, SyncShareRecords* record) {
record->times.push_back(TimeTicks::Now());
record->snapshots.push_back(s->TakeSnapshot());
}
ACTION_P(RecordSyncShare, record) {
RecordSyncShareImpl(arg0, record);
if (base::MessageLoop::current()->is_running())
QuitLoopNow();
return true;
}
ACTION_P2(RecordSyncShareMultiple, record, quit_after) {
RecordSyncShareImpl(arg0, record);
EXPECT_LE(record->times.size(), quit_after);
if (record->times.size() >= quit_after &&
base::MessageLoop::current()->is_running()) {
QuitLoopNow();
}
return true;
}
ACTION(AddFailureAndQuitLoopNow) {
ADD_FAILURE();
QuitLoopNow();
return true;
}
ACTION(QuitLoopNowAction) {
QuitLoopNow();
return true;
}
// Test nudge scheduling.
TEST_F(SyncSchedulerTest, Nudge) {
SyncShareRecords records;
ModelTypeSet model_types(BOOKMARKS);
EXPECT_CALL(*syncer(), NormalSyncShare(_,_,_))
.WillOnce(DoAll(Invoke(sessions::test_util::SimulateNormalSuccess),
WithArg<2>(RecordSyncShare(&records))))
.RetiresOnSaturation();
StartSyncScheduler(SyncScheduler::NORMAL_MODE);
scheduler()->ScheduleLocalNudge(zero(), model_types, FROM_HERE);
RunLoop();
ASSERT_EQ(1U, records.snapshots.size());
EXPECT_TRUE(ModelTypeSetMatchesInvalidationMap(
model_types,
records.snapshots[0].source().types));
EXPECT_EQ(GetUpdatesCallerInfo::LOCAL,
records.snapshots[0].source().updates_source);
Mock::VerifyAndClearExpectations(syncer());
// Make sure a second, later, nudge is unaffected by first (no coalescing).
SyncShareRecords records2;
model_types.Remove(BOOKMARKS);
model_types.Put(AUTOFILL);
EXPECT_CALL(*syncer(), NormalSyncShare(_,_,_))
.WillOnce(DoAll(Invoke(sessions::test_util::SimulateNormalSuccess),
WithArg<2>(RecordSyncShare(&records2))));
scheduler()->ScheduleLocalNudge(zero(), model_types, FROM_HERE);
RunLoop();
ASSERT_EQ(1U, records2.snapshots.size());
EXPECT_TRUE(ModelTypeSetMatchesInvalidationMap(
model_types,
records2.snapshots[0].source().types));
EXPECT_EQ(GetUpdatesCallerInfo::LOCAL,
records2.snapshots[0].source().updates_source);
}
// Make sure a regular config command is scheduled fine in the absence of any
// errors.
TEST_F(SyncSchedulerTest, Config) {
SyncShareRecords records;
const ModelTypeSet model_types(BOOKMARKS);
EXPECT_CALL(*syncer(), ConfigureSyncShare(_,_))
.WillOnce(DoAll(Invoke(sessions::test_util::SimulateConfigureSuccess),
WithArg<1>(RecordSyncShare(&records))));
StartSyncScheduler(SyncScheduler::CONFIGURATION_MODE);
CallbackCounter counter;
ConfigurationParams params(
GetUpdatesCallerInfo::RECONFIGURATION,
model_types,
TypesToRoutingInfo(model_types),
base::Bind(&CallbackCounter::Callback, base::Unretained(&counter)));
ASSERT_TRUE(scheduler()->ScheduleConfiguration(params));
ASSERT_EQ(1, counter.times_called());
ASSERT_EQ(1U, records.snapshots.size());
EXPECT_TRUE(ModelTypeSetMatchesInvalidationMap(
model_types,
records.snapshots[0].source().types));
EXPECT_EQ(GetUpdatesCallerInfo::RECONFIGURATION,
records.snapshots[0].source().updates_source);
}
// Simulate a failure and make sure the config request is retried.
TEST_F(SyncSchedulerTest, ConfigWithBackingOff) {
UseMockDelayProvider();
EXPECT_CALL(*delay(), GetDelay(_))
.WillRepeatedly(Return(TimeDelta::FromMilliseconds(1)));
SyncShareRecords records;
const ModelTypeSet model_types(BOOKMARKS);
EXPECT_CALL(*syncer(), ConfigureSyncShare(_,_))
.WillOnce(DoAll(Invoke(sessions::test_util::SimulateConfigureFailed),
WithArg<1>(RecordSyncShare(&records))))
.WillOnce(DoAll(Invoke(sessions::test_util::SimulateConfigureSuccess),
WithArg<1>(RecordSyncShare(&records))));
StartSyncScheduler(SyncScheduler::CONFIGURATION_MODE);
ASSERT_EQ(0U, records.snapshots.size());
CallbackCounter counter;
ConfigurationParams params(
GetUpdatesCallerInfo::RECONFIGURATION,
model_types,
TypesToRoutingInfo(model_types),
base::Bind(&CallbackCounter::Callback, base::Unretained(&counter)));
ASSERT_FALSE(scheduler()->ScheduleConfiguration(params));
ASSERT_EQ(0, counter.times_called());
ASSERT_EQ(1U, records.snapshots.size());
RunLoop();
ASSERT_EQ(2U, records.snapshots.size());
ASSERT_EQ(1, counter.times_called());
EXPECT_TRUE(ModelTypeSetMatchesInvalidationMap(
model_types,
records.snapshots[1].source().types));
EXPECT_EQ(GetUpdatesCallerInfo::RECONFIGURATION,
records.snapshots[1].source().updates_source);
}
// Issue a nudge when the config has failed. Make sure both the config and
// nudge are executed.
TEST_F(SyncSchedulerTest, NudgeWithConfigWithBackingOff) {
const ModelTypeSet model_types(BOOKMARKS);
UseMockDelayProvider();
EXPECT_CALL(*delay(), GetDelay(_))
.WillRepeatedly(Return(TimeDelta::FromMilliseconds(50)));
SyncShareRecords records;
EXPECT_CALL(*syncer(), ConfigureSyncShare(_,_))
.WillOnce(DoAll(Invoke(sessions::test_util::SimulateConfigureFailed),
WithArg<1>(RecordSyncShare(&records))))
.WillOnce(DoAll(Invoke(sessions::test_util::SimulateConfigureFailed),
WithArg<1>(RecordSyncShare(&records))))
.WillOnce(DoAll(Invoke(sessions::test_util::SimulateConfigureSuccess),
WithArg<1>(RecordSyncShare(&records))));
EXPECT_CALL(*syncer(), NormalSyncShare(_,_,_))
.WillOnce(DoAll(Invoke(sessions::test_util::SimulateNormalSuccess),
WithArg<2>(RecordSyncShare(&records))));
StartSyncScheduler(SyncScheduler::CONFIGURATION_MODE);
ASSERT_EQ(0U, records.snapshots.size());
CallbackCounter counter;
ConfigurationParams params(
GetUpdatesCallerInfo::RECONFIGURATION,
model_types,
TypesToRoutingInfo(model_types),
base::Bind(&CallbackCounter::Callback, base::Unretained(&counter)));
ASSERT_FALSE(scheduler()->ScheduleConfiguration(params));
ASSERT_EQ(0, counter.times_called());
ASSERT_EQ(1U, records.snapshots.size());
scheduler()->ScheduleLocalNudge(zero(), model_types, FROM_HERE);
RunLoop();
// Note that we're not RunLoop()ing for the NUDGE we just scheduled, but
// for the first retry attempt from the config job (after
// waiting ~+/- 50ms).
ASSERT_EQ(2U, records.snapshots.size());
ASSERT_EQ(0, counter.times_called());
EXPECT_EQ(GetUpdatesCallerInfo::RECONFIGURATION,
records.snapshots[1].source().updates_source);
RunLoop();
// This is the 3rd attempt, which we've set up to SimulateSuccess.
ASSERT_EQ(3U, records.snapshots.size());
ASSERT_EQ(1, counter.times_called());
// Now change the mode so nudge can execute.
StartSyncScheduler(SyncScheduler::NORMAL_MODE);
ASSERT_EQ(4U, records.snapshots.size());
EXPECT_TRUE(ModelTypeSetMatchesInvalidationMap(
model_types,
records.snapshots[2].source().types));
EXPECT_EQ(GetUpdatesCallerInfo::RECONFIGURATION,
records.snapshots[2].source().updates_source);
EXPECT_TRUE(ModelTypeSetMatchesInvalidationMap(
model_types,
records.snapshots[3].source().types));
EXPECT_EQ(GetUpdatesCallerInfo::LOCAL,
records.snapshots[3].source().updates_source);
}
// Test that nudges are coalesced.
TEST_F(SyncSchedulerTest, NudgeCoalescing) {
StartSyncScheduler(SyncScheduler::NORMAL_MODE);
SyncShareRecords r;
EXPECT_CALL(*syncer(), NormalSyncShare(_,_,_))
.WillOnce(DoAll(Invoke(sessions::test_util::SimulateNormalSuccess),
WithArg<2>(RecordSyncShare(&r))));
const ModelTypeSet types1(BOOKMARKS), types2(AUTOFILL), types3(THEMES);
TimeDelta delay = zero();
TimeTicks optimal_time = TimeTicks::Now() + delay;
scheduler()->ScheduleLocalNudge(delay, types1, FROM_HERE);
scheduler()->ScheduleLocalNudge(zero(), types2, FROM_HERE);
RunLoop();
ASSERT_EQ(1U, r.snapshots.size());
EXPECT_GE(r.times[0], optimal_time);
EXPECT_TRUE(ModelTypeSetMatchesInvalidationMap(
Union(types1, types2),
r.snapshots[0].source().types));
EXPECT_EQ(GetUpdatesCallerInfo::LOCAL,
r.snapshots[0].source().updates_source);
Mock::VerifyAndClearExpectations(syncer());
SyncShareRecords r2;
EXPECT_CALL(*syncer(), NormalSyncShare(_,_,_))
.WillOnce(DoAll(Invoke(sessions::test_util::SimulateNormalSuccess),
WithArg<2>(RecordSyncShare(&r2))));
scheduler()->ScheduleLocalNudge(zero(), types3, FROM_HERE);
RunLoop();
ASSERT_EQ(1U, r2.snapshots.size());
EXPECT_TRUE(ModelTypeSetMatchesInvalidationMap(
types3,
r2.snapshots[0].source().types));
EXPECT_EQ(GetUpdatesCallerInfo::LOCAL,
r2.snapshots[0].source().updates_source);
}
// Test that nudges are coalesced.
TEST_F(SyncSchedulerTest, NudgeCoalescingWithDifferentTimings) {
StartSyncScheduler(SyncScheduler::NORMAL_MODE);
SyncShareRecords r;
EXPECT_CALL(*syncer(), NormalSyncShare(_,_,_))
.WillOnce(DoAll(Invoke(sessions::test_util::SimulateNormalSuccess),
WithArg<2>(RecordSyncShare(&r))));
ModelTypeSet types1(BOOKMARKS), types2(AUTOFILL), types3;
// Create a huge time delay.
TimeDelta delay = TimeDelta::FromDays(1);
scheduler()->ScheduleLocalNudge(delay, types1, FROM_HERE);
scheduler()->ScheduleLocalNudge(zero(), types2, FROM_HERE);
TimeTicks min_time = TimeTicks::Now();
TimeTicks max_time = TimeTicks::Now() + delay;
RunLoop();
// Make sure the sync has happened.
ASSERT_EQ(1U, r.snapshots.size());
EXPECT_TRUE(ModelTypeSetMatchesInvalidationMap(
Union(types1, types2),
r.snapshots[0].source().types));
// Make sure the sync happened at the right time.
EXPECT_GE(r.times[0], min_time);
EXPECT_LE(r.times[0], max_time);
}
// Test nudge scheduling.
TEST_F(SyncSchedulerTest, NudgeWithStates) {
StartSyncScheduler(SyncScheduler::NORMAL_MODE);
SyncShareRecords records;
const ModelTypeSet types(BOOKMARKS);
ModelTypeInvalidationMap invalidation_map =
ModelTypeSetToInvalidationMap(types, "test");
EXPECT_CALL(*syncer(), NormalSyncShare(_,_,_))
.WillOnce(DoAll(Invoke(sessions::test_util::SimulateNormalSuccess),
WithArg<2>(RecordSyncShare(&records))))
.RetiresOnSaturation();
scheduler()->ScheduleInvalidationNudge(zero(), invalidation_map, FROM_HERE);
RunLoop();
ASSERT_EQ(1U, records.snapshots.size());
EXPECT_THAT(invalidation_map, Eq(records.snapshots[0].source().types));
EXPECT_EQ(GetUpdatesCallerInfo::NOTIFICATION,
records.snapshots[0].source().updates_source);
Mock::VerifyAndClearExpectations(syncer());
// Make sure a second, later, nudge is unaffected by first (no coalescing).
SyncShareRecords records2;
invalidation_map.erase(BOOKMARKS);
invalidation_map[AUTOFILL].payload = "test2";
EXPECT_CALL(*syncer(), NormalSyncShare(_,_,_))
.WillOnce(DoAll(Invoke(sessions::test_util::SimulateNormalSuccess),
WithArg<2>(RecordSyncShare(&records2))));
scheduler()->ScheduleInvalidationNudge(zero(), invalidation_map, FROM_HERE);
RunLoop();
ASSERT_EQ(1U, records2.snapshots.size());
EXPECT_THAT(invalidation_map, Eq(records2.snapshots[0].source().types));
EXPECT_EQ(GetUpdatesCallerInfo::NOTIFICATION,
records2.snapshots[0].source().updates_source);
}
// Test that polling works as expected.
TEST_F(SyncSchedulerTest, Polling) {
SyncShareRecords records;
TimeDelta poll_interval(TimeDelta::FromMilliseconds(30));
EXPECT_CALL(*syncer(), PollSyncShare(_,_)).Times(AtLeast(kMinNumSamples))
.WillRepeatedly(DoAll(Invoke(sessions::test_util::SimulatePollSuccess),
WithArg<1>(RecordSyncShareMultiple(&records, kMinNumSamples))));
scheduler()->OnReceivedLongPollIntervalUpdate(poll_interval);
TimeTicks optimal_start = TimeTicks::Now() + poll_interval;
StartSyncScheduler(SyncScheduler::NORMAL_MODE);
// Run again to wait for polling.
RunLoop();
StopSyncScheduler();
AnalyzePollRun(records, kMinNumSamples, optimal_start, poll_interval);
}
// Test that the short poll interval is used.
TEST_F(SyncSchedulerTest, PollNotificationsDisabled) {
SyncShareRecords records;
TimeDelta poll_interval(TimeDelta::FromMilliseconds(30));
EXPECT_CALL(*syncer(), PollSyncShare(_,_)).Times(AtLeast(kMinNumSamples))
.WillRepeatedly(DoAll(Invoke(sessions::test_util::SimulatePollSuccess),
WithArg<1>(RecordSyncShareMultiple(&records, kMinNumSamples))));
scheduler()->OnReceivedShortPollIntervalUpdate(poll_interval);
scheduler()->SetNotificationsEnabled(false);
TimeTicks optimal_start = TimeTicks::Now() + poll_interval;
StartSyncScheduler(SyncScheduler::NORMAL_MODE);
// Run again to wait for polling.
RunLoop();
StopSyncScheduler();
AnalyzePollRun(records, kMinNumSamples, optimal_start, poll_interval);
}
// Test that polling intervals are updated when needed.
TEST_F(SyncSchedulerTest, PollIntervalUpdate) {
SyncShareRecords records;
TimeDelta poll1(TimeDelta::FromMilliseconds(120));
TimeDelta poll2(TimeDelta::FromMilliseconds(30));
scheduler()->OnReceivedLongPollIntervalUpdate(poll1);
EXPECT_CALL(*syncer(), PollSyncShare(_,_)).Times(AtLeast(kMinNumSamples))
.WillOnce(DoAll(
WithArgs<0,1>(
sessions::test_util::SimulatePollIntervalUpdate(poll2)),
Return(true)))
.WillRepeatedly(
DoAll(Invoke(sessions::test_util::SimulatePollSuccess),
WithArg<1>(
RecordSyncShareMultiple(&records, kMinNumSamples))));
TimeTicks optimal_start = TimeTicks::Now() + poll1 + poll2;
StartSyncScheduler(SyncScheduler::NORMAL_MODE);
// Run again to wait for polling.
RunLoop();
StopSyncScheduler();
AnalyzePollRun(records, kMinNumSamples, optimal_start, poll2);
}
// Test that the sessions commit delay is updated when needed.
TEST_F(SyncSchedulerTest, SessionsCommitDelay) {
SyncShareRecords records;
TimeDelta delay1(TimeDelta::FromMilliseconds(120));
TimeDelta delay2(TimeDelta::FromMilliseconds(30));
scheduler()->OnReceivedSessionsCommitDelay(delay1);
EXPECT_CALL(*syncer(), NormalSyncShare(_,_,_))
.WillOnce(
DoAll(
WithArgs<0,1,2>(
sessions::test_util::SimulateSessionsCommitDelayUpdate(
delay2)),
Invoke(sessions::test_util::SimulateNormalSuccess),
QuitLoopNowAction()));
EXPECT_EQ(delay1, scheduler()->GetSessionsCommitDelay());
StartSyncScheduler(SyncScheduler::NORMAL_MODE);
EXPECT_EQ(delay1, scheduler()->GetSessionsCommitDelay());
const ModelTypeSet model_types(BOOKMARKS);
scheduler()->ScheduleLocalNudge(zero(), model_types, FROM_HERE);
RunLoop();
EXPECT_EQ(delay2, scheduler()->GetSessionsCommitDelay());
StopSyncScheduler();
}
// Test that no syncing occurs when throttled.
TEST_F(SyncSchedulerTest, ThrottlingDoesThrottle) {
const ModelTypeSet types(BOOKMARKS);
TimeDelta poll(TimeDelta::FromMilliseconds(5));
TimeDelta throttle(TimeDelta::FromMinutes(10));
scheduler()->OnReceivedLongPollIntervalUpdate(poll);
EXPECT_CALL(*syncer(), ConfigureSyncShare(_,_))
.WillOnce(DoAll(
WithArg<1>(sessions::test_util::SimulateThrottled(throttle)),
Return(true)))
.WillRepeatedly(AddFailureAndQuitLoopNow());
StartSyncScheduler(SyncScheduler::NORMAL_MODE);
scheduler()->ScheduleLocalNudge(
TimeDelta::FromMicroseconds(1), types, FROM_HERE);
PumpLoop();
StartSyncScheduler(SyncScheduler::CONFIGURATION_MODE);
CallbackCounter counter;
ConfigurationParams params(
GetUpdatesCallerInfo::RECONFIGURATION,
types,
TypesToRoutingInfo(types),
base::Bind(&CallbackCounter::Callback, base::Unretained(&counter)));
ASSERT_FALSE(scheduler()->ScheduleConfiguration(params));
ASSERT_EQ(0, counter.times_called());
}
TEST_F(SyncSchedulerTest, ThrottlingExpiresFromPoll) {
SyncShareRecords records;
TimeDelta poll(TimeDelta::FromMilliseconds(15));
TimeDelta throttle1(TimeDelta::FromMilliseconds(150));
scheduler()->OnReceivedLongPollIntervalUpdate(poll);
::testing::InSequence seq;
EXPECT_CALL(*syncer(), PollSyncShare(_,_))
.WillOnce(DoAll(
WithArg<1>(sessions::test_util::SimulateThrottled(throttle1)),
Return(true)))
.RetiresOnSaturation();
EXPECT_CALL(*syncer(), PollSyncShare(_,_))
.WillRepeatedly(DoAll(Invoke(sessions::test_util::SimulatePollSuccess),
WithArg<1>(RecordSyncShareMultiple(&records, kMinNumSamples))));
TimeTicks optimal_start = TimeTicks::Now() + poll + throttle1;
StartSyncScheduler(SyncScheduler::NORMAL_MODE);
// Run again to wait for polling.
RunLoop();
StopSyncScheduler();
AnalyzePollRun(records, kMinNumSamples, optimal_start, poll);
}
TEST_F(SyncSchedulerTest, ThrottlingExpiresFromNudge) {
SyncShareRecords records;
TimeDelta poll(TimeDelta::FromDays(1));
TimeDelta throttle1(TimeDelta::FromMilliseconds(150));
scheduler()->OnReceivedLongPollIntervalUpdate(poll);
::testing::InSequence seq;
EXPECT_CALL(*syncer(), NormalSyncShare(_,_,_))
.WillOnce(DoAll(
WithArg<2>(sessions::test_util::SimulateThrottled(throttle1)),
Return(true)))
.RetiresOnSaturation();
EXPECT_CALL(*syncer(), NormalSyncShare(_,_,_))
.WillOnce(DoAll(Invoke(sessions::test_util::SimulateNormalSuccess),
QuitLoopNowAction()));
const ModelTypeSet types(BOOKMARKS);
StartSyncScheduler(SyncScheduler::NORMAL_MODE);
scheduler()->ScheduleLocalNudge(zero(), types, FROM_HERE);
PumpLoop();
EXPECT_TRUE(scheduler()->IsCurrentlyThrottled());
RunLoop();
EXPECT_FALSE(scheduler()->IsCurrentlyThrottled());
StopSyncScheduler();
}
TEST_F(SyncSchedulerTest, ThrottlingExpiresFromConfigure) {
SyncShareRecords records;
TimeDelta poll(TimeDelta::FromDays(1));
TimeDelta throttle1(TimeDelta::FromMilliseconds(150));
scheduler()->OnReceivedLongPollIntervalUpdate(poll);
::testing::InSequence seq;
EXPECT_CALL(*syncer(), ConfigureSyncShare(_,_))
.WillOnce(DoAll(
WithArg<1>(sessions::test_util::SimulateThrottled(throttle1)),
Return(true)))
.RetiresOnSaturation();
EXPECT_CALL(*syncer(), ConfigureSyncShare(_,_))
.WillOnce(DoAll(Invoke(sessions::test_util::SimulateConfigureSuccess),
QuitLoopNowAction()));
const ModelTypeSet types(BOOKMARKS);
StartSyncScheduler(SyncScheduler::CONFIGURATION_MODE);
CallbackCounter counter;
ConfigurationParams params(
GetUpdatesCallerInfo::RECONFIGURATION,
types,
TypesToRoutingInfo(types),
base::Bind(&CallbackCounter::Callback, base::Unretained(&counter)));
EXPECT_FALSE(scheduler()->ScheduleConfiguration(params));
EXPECT_EQ(0, counter.times_called());
EXPECT_TRUE(scheduler()->IsCurrentlyThrottled());
RunLoop();
EXPECT_FALSE(scheduler()->IsCurrentlyThrottled());
StopSyncScheduler();
}
TEST_F(SyncSchedulerTest, TypeThrottlingBlocksNudge) {
UseMockDelayProvider();
EXPECT_CALL(*delay(), GetDelay(_))
.WillRepeatedly(Return(zero()));
TimeDelta poll(TimeDelta::FromDays(1));
TimeDelta throttle1(TimeDelta::FromSeconds(60));
scheduler()->OnReceivedLongPollIntervalUpdate(poll);
const ModelTypeSet types(BOOKMARKS);
::testing::InSequence seq;
EXPECT_CALL(*syncer(), NormalSyncShare(_,_,_))
.WillOnce(DoAll(
WithArg<2>(
sessions::test_util::SimulateTypesThrottled(types, throttle1)),
Return(true)))
.RetiresOnSaturation();
StartSyncScheduler(SyncScheduler::NORMAL_MODE);
scheduler()->ScheduleLocalNudge(zero(), types, FROM_HERE);
PumpLoop();
EXPECT_TRUE(GetThrottledTypes().HasAll(types));
// This won't cause a sync cycle because the types are throttled.
scheduler()->ScheduleLocalNudge(zero(), types, FROM_HERE);
PumpLoop();
StopSyncScheduler();
}
TEST_F(SyncSchedulerTest, TypeThrottlingDoesBlockOtherSources) {
UseMockDelayProvider();
EXPECT_CALL(*delay(), GetDelay(_))
.WillRepeatedly(Return(zero()));
SyncShareRecords records;
TimeDelta poll(TimeDelta::FromDays(1));
TimeDelta throttle1(TimeDelta::FromSeconds(60));
scheduler()->OnReceivedLongPollIntervalUpdate(poll);
const ModelTypeSet throttled_types(BOOKMARKS);
const ModelTypeSet unthrottled_types(PREFERENCES);
::testing::InSequence seq;
EXPECT_CALL(*syncer(), NormalSyncShare(_,_,_))
.WillOnce(DoAll(
WithArg<2>(
sessions::test_util::SimulateTypesThrottled(
throttled_types, throttle1)),
Return(true)))
.RetiresOnSaturation();
EXPECT_CALL(*syncer(), NormalSyncShare(_,_,_))
.WillRepeatedly(DoAll(Invoke(sessions::test_util::SimulateNormalSuccess),
WithArg<2>(RecordSyncShare(&records))));
StartSyncScheduler(SyncScheduler::NORMAL_MODE);
scheduler()->ScheduleLocalNudge(zero(), throttled_types, FROM_HERE);
PumpLoop();
EXPECT_EQ(0U, records.snapshots.size());
EXPECT_TRUE(GetThrottledTypes().HasAll(throttled_types));
// Ignore invalidations for throttled types.
ModelTypeInvalidationMap invalidation_map =
ModelTypeSetToInvalidationMap(throttled_types, "test");
scheduler()->ScheduleInvalidationNudge(zero(), invalidation_map, FROM_HERE);
PumpLoop();
EXPECT_EQ(0U, records.snapshots.size());
// Ignore refresh requests for throttled types.
scheduler()->ScheduleLocalRefreshRequest(zero(), throttled_types, FROM_HERE);
PumpLoop();
EXPECT_EQ(0U, records.snapshots.size());
// Local nudges for non-throttled types will trigger a sync.
scheduler()->ScheduleLocalNudge(zero(), unthrottled_types, FROM_HERE);
RunLoop();
EXPECT_EQ(1U, records.snapshots.size());
StopSyncScheduler();
}
// Test nudges / polls don't run in config mode and config tasks do.
TEST_F(SyncSchedulerTest, ConfigurationMode) {
TimeDelta poll(TimeDelta::FromMilliseconds(15));
SyncShareRecords records;
scheduler()->OnReceivedLongPollIntervalUpdate(poll);
EXPECT_CALL(*syncer(), ConfigureSyncShare(_,_))
.WillOnce(DoAll(Invoke(sessions::test_util::SimulateConfigureSuccess),
WithArg<1>(RecordSyncShare(&records))))
.RetiresOnSaturation();
StartSyncScheduler(SyncScheduler::CONFIGURATION_MODE);
const ModelTypeSet nudge_types(AUTOFILL);
scheduler()->ScheduleLocalNudge(zero(), nudge_types, FROM_HERE);
scheduler()->ScheduleLocalNudge(zero(), nudge_types, FROM_HERE);
const ModelTypeSet config_types(BOOKMARKS);
CallbackCounter counter;
ConfigurationParams params(
GetUpdatesCallerInfo::RECONFIGURATION,
config_types,
TypesToRoutingInfo(config_types),
base::Bind(&CallbackCounter::Callback, base::Unretained(&counter)));
ASSERT_TRUE(scheduler()->ScheduleConfiguration(params));
ASSERT_EQ(1, counter.times_called());
ASSERT_EQ(1U, records.snapshots.size());
EXPECT_TRUE(ModelTypeSetMatchesInvalidationMap(
config_types,
records.snapshots[0].source().types));
// Switch to NORMAL_MODE to ensure NUDGES were properly saved and run.
// SyncSchedulerWhiteboxTest also provides coverage for this, but much
// more targeted ('whitebox' style).
scheduler()->OnReceivedLongPollIntervalUpdate(TimeDelta::FromDays(1));
SyncShareRecords records2;
EXPECT_CALL(*syncer(), NormalSyncShare(_,_,_))
.WillOnce(DoAll(Invoke(sessions::test_util::SimulateNormalSuccess),
WithArg<2>(RecordSyncShare(&records2))));
// TODO(tim): Figure out how to remove this dangerous need to reset
// routing info between mode switches.
context()->set_routing_info(routing_info());
StartSyncScheduler(SyncScheduler::NORMAL_MODE);
ASSERT_EQ(1U, records2.snapshots.size());
EXPECT_EQ(GetUpdatesCallerInfo::LOCAL,
records2.snapshots[0].source().updates_source);
EXPECT_TRUE(ModelTypeSetMatchesInvalidationMap(
nudge_types,
records2.snapshots[0].source().types));
PumpLoop();
}
class BackoffTriggersSyncSchedulerTest : public SyncSchedulerTest {
virtual void SetUp() {
SyncSchedulerTest::SetUp();
UseMockDelayProvider();
EXPECT_CALL(*delay(), GetDelay(_))
.WillRepeatedly(Return(TimeDelta::FromMilliseconds(1)));
}
virtual void TearDown() {
StopSyncScheduler();
SyncSchedulerTest::TearDown();
}
};
// Have the sycner fail during commit. Expect that the scheduler enters
// backoff.
TEST_F(BackoffTriggersSyncSchedulerTest, FailCommitOnce) {
EXPECT_CALL(*syncer(), NormalSyncShare(_,_,_))
.WillOnce(DoAll(Invoke(sessions::test_util::SimulateCommitFailed),
QuitLoopNowAction()));
EXPECT_TRUE(RunAndGetBackoff());
}
// Have the syncer fail during download updates and succeed on the first
// retry. Expect that this clears the backoff state.
TEST_F(BackoffTriggersSyncSchedulerTest, FailDownloadOnceThenSucceed) {
EXPECT_CALL(*syncer(), NormalSyncShare(_,_,_))
.WillOnce(DoAll(
Invoke(sessions::test_util::SimulateDownloadUpdatesFailed),
Return(true)))
.WillOnce(DoAll(Invoke(sessions::test_util::SimulateNormalSuccess),
QuitLoopNowAction()));
EXPECT_FALSE(RunAndGetBackoff());
}
// Have the syncer fail during commit and succeed on the first retry. Expect
// that this clears the backoff state.
TEST_F(BackoffTriggersSyncSchedulerTest, FailCommitOnceThenSucceed) {
EXPECT_CALL(*syncer(), NormalSyncShare(_,_,_))
.WillOnce(DoAll(
Invoke(sessions::test_util::SimulateCommitFailed),
Return(true)))
.WillOnce(DoAll(Invoke(sessions::test_util::SimulateNormalSuccess),
QuitLoopNowAction()));
EXPECT_FALSE(RunAndGetBackoff());
}
// Have the syncer fail to download updates and fail again on the retry.
// Expect this will leave the scheduler in backoff.
TEST_F(BackoffTriggersSyncSchedulerTest, FailDownloadTwice) {
EXPECT_CALL(*syncer(), NormalSyncShare(_,_,_))
.WillOnce(DoAll(
Invoke(sessions::test_util::SimulateDownloadUpdatesFailed),
Return(true)))
.WillRepeatedly(DoAll(
Invoke(sessions::test_util::SimulateDownloadUpdatesFailed),
QuitLoopNowAction()));
EXPECT_TRUE(RunAndGetBackoff());
}
// Have the syncer fail to get the encryption key yet succeed in downloading
// updates. Expect this will leave the scheduler in backoff.
TEST_F(BackoffTriggersSyncSchedulerTest, FailGetEncryptionKey) {
EXPECT_CALL(*syncer(), ConfigureSyncShare(_,_))
.WillOnce(DoAll(
Invoke(sessions::test_util::SimulateGetEncryptionKeyFailed),
Return(true)))
.WillRepeatedly(DoAll(
Invoke(sessions::test_util::SimulateGetEncryptionKeyFailed),
QuitLoopNowAction()));
StartSyncScheduler(SyncScheduler::CONFIGURATION_MODE);
ModelTypeSet types(BOOKMARKS);
CallbackCounter counter;
ConfigurationParams params(
GetUpdatesCallerInfo::RECONFIGURATION,
types,
TypesToRoutingInfo(types),
base::Bind(&CallbackCounter::Callback, base::Unretained(&counter)));
scheduler()->ScheduleConfiguration(params);
RunLoop();
EXPECT_TRUE(scheduler()->IsBackingOff());
}
// Test that no polls or extraneous nudges occur when in backoff.
TEST_F(SyncSchedulerTest, BackoffDropsJobs) {
SyncShareRecords r;
TimeDelta poll(TimeDelta::FromMilliseconds(5));
const ModelTypeSet types(BOOKMARKS);
scheduler()->OnReceivedLongPollIntervalUpdate(poll);
UseMockDelayProvider();
EXPECT_CALL(*syncer(), NormalSyncShare(_,_,_))
.WillRepeatedly(DoAll(Invoke(sessions::test_util::SimulateCommitFailed),
WithArg<2>(RecordSyncShareMultiple(&r, 1U))));
EXPECT_CALL(*delay(), GetDelay(_)).
WillRepeatedly(Return(TimeDelta::FromDays(1)));
StartSyncScheduler(SyncScheduler::NORMAL_MODE);
// This nudge should fail and put us into backoff. Thanks to our mock
// GetDelay() setup above, this will be a long backoff.
scheduler()->ScheduleLocalNudge(zero(), types, FROM_HERE);
RunLoop();
Mock::VerifyAndClearExpectations(syncer());
ASSERT_EQ(1U, r.snapshots.size());
EXPECT_EQ(GetUpdatesCallerInfo::LOCAL,
r.snapshots[0].source().updates_source);
// Wait a while (10x poll interval) so a few poll jobs will be attempted.
PumpLoopFor(poll * 10);
// Try (and fail) to schedule a nudge.
scheduler()->ScheduleLocalNudge(
base::TimeDelta::FromMilliseconds(1),
types,
FROM_HERE);
Mock::VerifyAndClearExpectations(syncer());
Mock::VerifyAndClearExpectations(delay());
ASSERT_EQ(1U, r.snapshots.size());
EXPECT_CALL(*delay(), GetDelay(_)).Times(0);
StartSyncScheduler(SyncScheduler::CONFIGURATION_MODE);
CallbackCounter counter;
ConfigurationParams params(
GetUpdatesCallerInfo::RECONFIGURATION,
types,
TypesToRoutingInfo(types),
base::Bind(&CallbackCounter::Callback, base::Unretained(&counter)));
ASSERT_FALSE(scheduler()->ScheduleConfiguration(params));
ASSERT_EQ(0, counter.times_called());
}
// Test that backoff is shaping traffic properly with consecutive errors.
TEST_F(SyncSchedulerTest, BackoffElevation) {
SyncShareRecords r;
UseMockDelayProvider();
EXPECT_CALL(*syncer(), NormalSyncShare(_,_,_)).Times(kMinNumSamples)
.WillRepeatedly(DoAll(Invoke(sessions::test_util::SimulateCommitFailed),
WithArg<2>(RecordSyncShareMultiple(&r, kMinNumSamples))));
const TimeDelta first = TimeDelta::FromSeconds(kInitialBackoffRetrySeconds);
const TimeDelta second = TimeDelta::FromMilliseconds(2);
const TimeDelta third = TimeDelta::FromMilliseconds(3);
const TimeDelta fourth = TimeDelta::FromMilliseconds(4);
const TimeDelta fifth = TimeDelta::FromMilliseconds(5);
const TimeDelta sixth = TimeDelta::FromDays(1);
EXPECT_CALL(*delay(), GetDelay(first)).WillOnce(Return(second))
.RetiresOnSaturation();
EXPECT_CALL(*delay(), GetDelay(second)).WillOnce(Return(third))
.RetiresOnSaturation();
EXPECT_CALL(*delay(), GetDelay(third)).WillOnce(Return(fourth))
.RetiresOnSaturation();
EXPECT_CALL(*delay(), GetDelay(fourth)).WillOnce(Return(fifth))
.RetiresOnSaturation();
EXPECT_CALL(*delay(), GetDelay(fifth)).WillOnce(Return(sixth));
StartSyncScheduler(SyncScheduler::NORMAL_MODE);
// Run again with a nudge.
scheduler()->ScheduleLocalNudge(zero(), ModelTypeSet(BOOKMARKS), FROM_HERE);
RunLoop();
ASSERT_EQ(kMinNumSamples, r.snapshots.size());
EXPECT_GE(r.times[1] - r.times[0], second);
EXPECT_GE(r.times[2] - r.times[1], third);
EXPECT_GE(r.times[3] - r.times[2], fourth);
EXPECT_GE(r.times[4] - r.times[3], fifth);
}
// Test that things go back to normal once a retry makes forward progress.
TEST_F(SyncSchedulerTest, BackoffRelief) {
SyncShareRecords r;
const TimeDelta poll(TimeDelta::FromMilliseconds(10));
scheduler()->OnReceivedLongPollIntervalUpdate(poll);
UseMockDelayProvider();
const TimeDelta backoff = TimeDelta::FromMilliseconds(5);
EXPECT_CALL(*syncer(), NormalSyncShare(_,_,_))
.WillOnce(DoAll(Invoke(sessions::test_util::SimulateCommitFailed),
WithArg<2>(RecordSyncShareMultiple(&r, kMinNumSamples))))
.WillRepeatedly(DoAll(
Invoke(sessions::test_util::SimulateNormalSuccess),
WithArg<2>(RecordSyncShareMultiple(&r, kMinNumSamples))));
EXPECT_CALL(*syncer(), PollSyncShare(_,_))
.WillRepeatedly(DoAll(
Invoke(sessions::test_util::SimulatePollSuccess),
WithArg<1>(RecordSyncShareMultiple(&r, kMinNumSamples))));
EXPECT_CALL(*delay(), GetDelay(_)).WillOnce(Return(backoff));
// Optimal start for the post-backoff poll party.
TimeTicks optimal_start = TimeTicks::Now();
StartSyncScheduler(SyncScheduler::NORMAL_MODE);
// Run again to wait for polling.
scheduler()->ScheduleLocalNudge(zero(), ModelTypeSet(BOOKMARKS), FROM_HERE);
RunLoop();
StopSyncScheduler();
EXPECT_EQ(kMinNumSamples, r.times.size());
// The first nudge ran as soon as possible. It failed.
TimeTicks optimal_job_time = optimal_start;
EXPECT_GE(r.times[0], optimal_job_time);
EXPECT_EQ(GetUpdatesCallerInfo::LOCAL,
r.snapshots[0].source().updates_source);
// It was followed by a successful retry nudge shortly afterward.
optimal_job_time = optimal_job_time + backoff;
EXPECT_GE(r.times[1], optimal_job_time);
EXPECT_EQ(GetUpdatesCallerInfo::LOCAL,
r.snapshots[1].source().updates_source);
// After that, we went back to polling.
for (size_t i = 2; i < r.snapshots.size(); i++) {
optimal_job_time = optimal_job_time + poll;
SCOPED_TRACE(testing::Message() << "SyncShare # (" << i << ")");
EXPECT_GE(r.times[i], optimal_job_time);
EXPECT_EQ(GetUpdatesCallerInfo::PERIODIC,
r.snapshots[i].source().updates_source);
}
}
// Test that poll failures are ignored. They should have no effect on
// subsequent poll attempts, nor should they trigger a backoff/retry.
TEST_F(SyncSchedulerTest, TransientPollFailure) {
SyncShareRecords r;
const TimeDelta poll_interval(TimeDelta::FromMilliseconds(1));
scheduler()->OnReceivedLongPollIntervalUpdate(poll_interval);
UseMockDelayProvider(); // Will cause test failure if backoff is initiated.
EXPECT_CALL(*syncer(), PollSyncShare(_,_))
.WillOnce(DoAll(Invoke(sessions::test_util::SimulatePollFailed),
WithArg<1>(RecordSyncShare(&r))))
.WillOnce(DoAll(Invoke(sessions::test_util::SimulatePollSuccess),
WithArg<1>(RecordSyncShare(&r))));
StartSyncScheduler(SyncScheduler::NORMAL_MODE);
// Run the unsucessful poll. The failed poll should not trigger backoff.
RunLoop();
EXPECT_FALSE(scheduler()->IsBackingOff());
// Run the successful poll.
RunLoop();
EXPECT_FALSE(scheduler()->IsBackingOff());
}
// Test that starting the syncer thread without a valid connection doesn't
// break things when a connection is detected.
TEST_F(SyncSchedulerTest, StartWhenNotConnected) {
connection()->SetServerNotReachable();
connection()->UpdateConnectionStatus();
EXPECT_CALL(*syncer(), NormalSyncShare(_,_,_))
.WillOnce(DoAll(Invoke(sessions::test_util::SimulateConnectionFailure),
Return(true)))
.WillOnce(DoAll(Invoke(sessions::test_util::SimulateNormalSuccess),
Return(true)));
StartSyncScheduler(SyncScheduler::NORMAL_MODE);
scheduler()->ScheduleLocalNudge(zero(), ModelTypeSet(BOOKMARKS), FROM_HERE);
// Should save the nudge for until after the server is reachable.
base::MessageLoop::current()->RunUntilIdle();
scheduler()->OnConnectionStatusChange();
connection()->SetServerReachable();
connection()->UpdateConnectionStatus();
base::MessageLoop::current()->RunUntilIdle();
}
TEST_F(SyncSchedulerTest, ServerConnectionChangeDuringBackoff) {
UseMockDelayProvider();
EXPECT_CALL(*delay(), GetDelay(_))
.WillRepeatedly(Return(TimeDelta::FromMilliseconds(0)));
StartSyncScheduler(SyncScheduler::NORMAL_MODE);
connection()->SetServerNotReachable();
connection()->UpdateConnectionStatus();
EXPECT_CALL(*syncer(), NormalSyncShare(_,_,_))
.WillOnce(DoAll(Invoke(sessions::test_util::SimulateConnectionFailure),
Return(true)))
.WillOnce(DoAll(Invoke(sessions::test_util::SimulateNormalSuccess),
Return(true)));
scheduler()->ScheduleLocalNudge(zero(), ModelTypeSet(BOOKMARKS), FROM_HERE);
PumpLoop(); // Run the nudge, that will fail and schedule a quick retry.
ASSERT_TRUE(scheduler()->IsBackingOff());
// Before we run the scheduled canary, trigger a server connection change.
scheduler()->OnConnectionStatusChange();
connection()->SetServerReachable();
connection()->UpdateConnectionStatus();
base::MessageLoop::current()->RunUntilIdle();
}
// This was supposed to test the scenario where we receive a nudge while a
// connection change canary is scheduled, but has not run yet. Since we've made
// the connection change canary synchronous, this is no longer possible.
TEST_F(SyncSchedulerTest, ConnectionChangeCanaryPreemptedByNudge) {
UseMockDelayProvider();
EXPECT_CALL(*delay(), GetDelay(_))
.WillRepeatedly(Return(TimeDelta::FromMilliseconds(0)));
StartSyncScheduler(SyncScheduler::NORMAL_MODE);
connection()->SetServerNotReachable();
connection()->UpdateConnectionStatus();
EXPECT_CALL(*syncer(), NormalSyncShare(_,_,_))
.WillOnce(DoAll(Invoke(sessions::test_util::SimulateConnectionFailure),
Return(true)))
.WillOnce(DoAll(Invoke(sessions::test_util::SimulateNormalSuccess),
Return(true)))
.WillOnce(DoAll(Invoke(sessions::test_util::SimulateNormalSuccess),
QuitLoopNowAction()));
scheduler()->ScheduleLocalNudge(zero(), ModelTypeSet(BOOKMARKS), FROM_HERE);
PumpLoop(); // Run the nudge, that will fail and schedule a quick retry.
ASSERT_TRUE(scheduler()->IsBackingOff());
// Before we run the scheduled canary, trigger a server connection change.
scheduler()->OnConnectionStatusChange();
connection()->SetServerReachable();
connection()->UpdateConnectionStatus();
scheduler()->ScheduleLocalNudge(zero(), ModelTypeSet(BOOKMARKS), FROM_HERE);
base::MessageLoop::current()->RunUntilIdle();
}
// Tests that we don't crash trying to run two canaries at once if we receive
// extra connection status change notifications. See crbug.com/190085.
TEST_F(SyncSchedulerTest, DoubleCanaryInConfigure) {
EXPECT_CALL(*syncer(), ConfigureSyncShare(_,_))
.WillRepeatedly(DoAll(
Invoke(sessions::test_util::SimulateConfigureConnectionFailure),
Return(true)));
StartSyncScheduler(SyncScheduler::CONFIGURATION_MODE);
connection()->SetServerNotReachable();
connection()->UpdateConnectionStatus();
ModelTypeSet model_types(BOOKMARKS);
CallbackCounter counter;
ConfigurationParams params(
GetUpdatesCallerInfo::RECONFIGURATION,
model_types,
TypesToRoutingInfo(model_types),
base::Bind(&CallbackCounter::Callback, base::Unretained(&counter)));
scheduler()->ScheduleConfiguration(params);
scheduler()->OnConnectionStatusChange();
scheduler()->OnConnectionStatusChange();
PumpLoop(); // Run the nudge, that will fail and schedule a quick retry.
}
} // namespace syncer
|