1 // Copyright 2014 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 "components/invalidation/registration_manager.h"
13 #include "base/basictypes.h"
14 #include "base/message_loop/message_loop.h"
15 #include "base/stl_util.h"
16 #include "components/invalidation/invalidation_util.h"
17 #include "google/cacheinvalidation/include/invalidation-client.h"
18 #include "testing/gtest/include/gtest/gtest.h"
23 // Fake registration manager that lets you override jitter.
24 class FakeRegistrationManager
: public RegistrationManager
{
26 explicit FakeRegistrationManager(
27 invalidation::InvalidationClient
* invalidation_client
)
28 : RegistrationManager(invalidation_client
),
31 virtual ~FakeRegistrationManager() {}
33 void SetJitter(double jitter
) {
38 virtual double GetJitter() OVERRIDE
{
45 DISALLOW_COPY_AND_ASSIGN(FakeRegistrationManager
);
48 // Fake invalidation client that just stores the currently-registered
50 class FakeInvalidationClient
: public invalidation::InvalidationClient
{
52 FakeInvalidationClient() {}
54 virtual ~FakeInvalidationClient() {}
56 void LoseRegistration(const invalidation::ObjectId
& oid
) {
57 EXPECT_TRUE(ContainsKey(registered_ids_
, oid
));
58 registered_ids_
.erase(oid
);
61 void LoseAllRegistrations() {
62 registered_ids_
.clear();
65 // invalidation::InvalidationClient implementation.
67 virtual void Start() OVERRIDE
{}
68 virtual void Stop() OVERRIDE
{}
69 virtual void Acknowledge(const invalidation::AckHandle
& handle
) OVERRIDE
{}
71 virtual void Register(const invalidation::ObjectId
& oid
) OVERRIDE
{
72 EXPECT_FALSE(ContainsKey(registered_ids_
, oid
));
73 registered_ids_
.insert(oid
);
76 virtual void Register(
77 const std::vector
<invalidation::ObjectId
>& oids
) OVERRIDE
{
81 virtual void Unregister(const invalidation::ObjectId
& oid
) OVERRIDE
{
82 EXPECT_TRUE(ContainsKey(registered_ids_
, oid
));
83 registered_ids_
.erase(oid
);
86 virtual void Unregister(
87 const std::vector
<invalidation::ObjectId
>& oids
) OVERRIDE
{
91 const ObjectIdSet
& GetRegisteredIdsForTest() const {
92 return registered_ids_
;
96 ObjectIdSet registered_ids_
;
98 DISALLOW_COPY_AND_ASSIGN(FakeInvalidationClient
);
101 size_t kObjectIdsCount
= 5;
103 invalidation::ObjectId
GetIdForIndex(size_t index
) {
105 name
[0] += static_cast<char>(index
);
106 return invalidation::ObjectId(1 + index
, name
);
109 ObjectIdSet
GetSequenceOfIdsStartingAt(size_t start
, size_t count
) {
111 for (size_t i
= start
; i
< start
+ count
; ++i
)
112 ids
.insert(GetIdForIndex(i
));
116 ObjectIdSet
GetSequenceOfIds(size_t count
) {
117 return GetSequenceOfIdsStartingAt(0, count
);
120 void ExpectPendingRegistrations(
121 const ObjectIdSet
& expected_pending_ids
,
122 double expected_delay_seconds
,
123 const RegistrationManager::PendingRegistrationMap
& pending_registrations
) {
124 ObjectIdSet pending_ids
;
125 for (RegistrationManager::PendingRegistrationMap::const_iterator it
=
126 pending_registrations
.begin(); it
!= pending_registrations
.end();
128 SCOPED_TRACE(ObjectIdToString(it
->first
));
129 pending_ids
.insert(it
->first
);
130 base::TimeDelta offset
=
131 it
->second
.last_registration_request
-
132 it
->second
.registration_attempt
;
133 base::TimeDelta expected_delay
=
134 base::TimeDelta::FromSeconds(
135 static_cast<int64
>(expected_delay_seconds
)) + offset
;
136 // TODO(akalin): Add base::PrintTo() for base::Time and
138 EXPECT_EQ(expected_delay
, it
->second
.delay
)
139 << expected_delay
.InMicroseconds()
140 << ", " << it
->second
.delay
.InMicroseconds();
141 if (it
->second
.delay
<= base::TimeDelta()) {
142 EXPECT_EQ(base::TimeDelta(), it
->second
.actual_delay
);
144 EXPECT_EQ(it
->second
.actual_delay
, it
->second
.delay
);
147 EXPECT_EQ(expected_pending_ids
, pending_ids
);
150 class RegistrationManagerTest
: public testing::Test
{
152 RegistrationManagerTest()
153 : fake_registration_manager_(&fake_invalidation_client_
) {}
155 virtual ~RegistrationManagerTest() {}
157 void LoseRegistrations(const ObjectIdSet
& oids
) {
158 for (ObjectIdSet::const_iterator it
= oids
.begin(); it
!= oids
.end();
160 fake_invalidation_client_
.LoseRegistration(*it
);
161 fake_registration_manager_
.MarkRegistrationLost(*it
);
165 void DisableIds(const ObjectIdSet
& oids
) {
166 for (ObjectIdSet::const_iterator it
= oids
.begin(); it
!= oids
.end();
168 fake_invalidation_client_
.LoseRegistration(*it
);
169 fake_registration_manager_
.DisableId(*it
);
173 // Used by MarkRegistrationLostBackoff* tests.
174 void RunBackoffTest(double jitter
) {
175 fake_registration_manager_
.SetJitter(jitter
);
176 ObjectIdSet ids
= GetSequenceOfIds(kObjectIdsCount
);
177 fake_registration_manager_
.UpdateRegisteredIds(ids
);
180 ObjectIdSet lost_ids
= GetSequenceOfIds(2);
181 LoseRegistrations(lost_ids
);
182 ExpectPendingRegistrations(
184 fake_registration_manager_
.GetPendingRegistrationsForTest());
186 // Trigger another failure to start delaying.
187 fake_registration_manager_
.FirePendingRegistrationsForTest();
188 LoseRegistrations(lost_ids
);
190 double scaled_jitter
=
191 jitter
* RegistrationManager::kRegistrationDelayMaxJitter
;
193 double expected_delay
=
194 RegistrationManager::kInitialRegistrationDelaySeconds
*
195 (1.0 + scaled_jitter
);
196 expected_delay
= std::floor(expected_delay
);
197 ExpectPendingRegistrations(
198 lost_ids
, expected_delay
,
199 fake_registration_manager_
.GetPendingRegistrationsForTest());
201 // Trigger another failure.
202 fake_registration_manager_
.FirePendingRegistrationsForTest();
203 LoseRegistrations(lost_ids
);
205 RegistrationManager::kRegistrationDelayExponent
+ scaled_jitter
;
206 expected_delay
= std::floor(expected_delay
);
207 ExpectPendingRegistrations(
208 lost_ids
, expected_delay
,
209 fake_registration_manager_
.GetPendingRegistrationsForTest());
211 // Trigger enough failures to hit the ceiling.
212 while (expected_delay
< RegistrationManager::kMaxRegistrationDelaySeconds
) {
213 fake_registration_manager_
.FirePendingRegistrationsForTest();
214 LoseRegistrations(lost_ids
);
216 RegistrationManager::kRegistrationDelayExponent
+ scaled_jitter
;
217 expected_delay
= std::floor(expected_delay
);
219 ExpectPendingRegistrations(
221 RegistrationManager::kMaxRegistrationDelaySeconds
,
222 fake_registration_manager_
.GetPendingRegistrationsForTest());
225 FakeInvalidationClient fake_invalidation_client_
;
226 FakeRegistrationManager fake_registration_manager_
;
229 // Needed by timers in RegistrationManager.
230 base::MessageLoop message_loop_
;
232 DISALLOW_COPY_AND_ASSIGN(RegistrationManagerTest
);
235 // Basic test of UpdateRegisteredIds to make sure we properly register
236 // new IDs and unregister any IDs no longer in the set.
237 TEST_F(RegistrationManagerTest
, UpdateRegisteredIds
) {
238 ObjectIdSet ids
= GetSequenceOfIds(kObjectIdsCount
- 1);
240 EXPECT_TRUE(fake_registration_manager_
.GetRegisteredIdsForTest().empty());
241 EXPECT_TRUE(fake_invalidation_client_
.GetRegisteredIdsForTest().empty());
243 ObjectIdSet expected_unregistered_ids
;
245 ObjectIdSet unregistered_ids
=
246 fake_registration_manager_
.UpdateRegisteredIds(ids
);
247 EXPECT_EQ(expected_unregistered_ids
, unregistered_ids
);
248 EXPECT_EQ(ids
, fake_registration_manager_
.GetRegisteredIdsForTest());
249 EXPECT_EQ(ids
, fake_invalidation_client_
.GetRegisteredIdsForTest());
251 ids
.insert(GetIdForIndex(kObjectIdsCount
- 1));
252 ids
.erase(GetIdForIndex(kObjectIdsCount
- 2));
253 unregistered_ids
= fake_registration_manager_
.UpdateRegisteredIds(ids
);
254 expected_unregistered_ids
.insert(GetIdForIndex(kObjectIdsCount
- 2));
255 EXPECT_EQ(expected_unregistered_ids
, unregistered_ids
);
256 EXPECT_EQ(ids
, fake_registration_manager_
.GetRegisteredIdsForTest());
257 EXPECT_EQ(ids
, fake_invalidation_client_
.GetRegisteredIdsForTest());
260 int GetRoundedBackoff(double retry_interval
, double jitter
) {
261 const double kInitialRetryInterval
= 3.0;
262 const double kMinRetryInterval
= 2.0;
263 const double kMaxRetryInterval
= 20.0;
264 const double kBackoffExponent
= 2.0;
265 const double kMaxJitter
= 0.5;
267 return static_cast<int>(
268 RegistrationManager::CalculateBackoff(retry_interval
,
269 kInitialRetryInterval
,
277 TEST_F(RegistrationManagerTest
, CalculateBackoff
) {
279 EXPECT_EQ(2, GetRoundedBackoff(0.0, -1.0));
280 EXPECT_EQ(3, GetRoundedBackoff(0.0, 0.0));
281 EXPECT_EQ(4, GetRoundedBackoff(0.0, +1.0));
284 EXPECT_EQ(4, GetRoundedBackoff(3.0, -1.0));
285 EXPECT_EQ(6, GetRoundedBackoff(3.0, 0.0));
286 EXPECT_EQ(7, GetRoundedBackoff(3.0, +1.0));
288 EXPECT_EQ(7, GetRoundedBackoff(5.0, -1.0));
289 EXPECT_EQ(10, GetRoundedBackoff(5.0, 0.0));
290 EXPECT_EQ(12, GetRoundedBackoff(5.0, +1.0));
293 EXPECT_EQ(19, GetRoundedBackoff(13.0, -1.0));
294 EXPECT_EQ(20, GetRoundedBackoff(13.0, 0.0));
295 EXPECT_EQ(20, GetRoundedBackoff(13.0, +1.0));
298 // Losing a registration should queue automatic re-registration.
299 TEST_F(RegistrationManagerTest
, MarkRegistrationLost
) {
300 ObjectIdSet ids
= GetSequenceOfIds(kObjectIdsCount
);
302 fake_registration_manager_
.UpdateRegisteredIds(ids
);
304 fake_registration_manager_
.GetPendingRegistrationsForTest().empty());
307 ObjectIdSet lost_ids
= GetSequenceOfIds(3);
308 ObjectIdSet non_lost_ids
= GetSequenceOfIdsStartingAt(3, kObjectIdsCount
- 3);
309 LoseRegistrations(lost_ids
);
310 ExpectPendingRegistrations(
312 fake_registration_manager_
.GetPendingRegistrationsForTest());
313 EXPECT_EQ(non_lost_ids
, fake_registration_manager_
.GetRegisteredIdsForTest());
314 EXPECT_EQ(non_lost_ids
, fake_invalidation_client_
.GetRegisteredIdsForTest());
316 // Pretend we waited long enough to re-register.
317 fake_registration_manager_
.FirePendingRegistrationsForTest();
318 EXPECT_EQ(ids
, fake_registration_manager_
.GetRegisteredIdsForTest());
319 EXPECT_EQ(ids
, fake_invalidation_client_
.GetRegisteredIdsForTest());
322 TEST_F(RegistrationManagerTest
, MarkRegistrationLostBackoffLow
) {
323 RunBackoffTest(-1.0);
326 TEST_F(RegistrationManagerTest
, MarkRegistrationLostBackoffMid
) {
330 TEST_F(RegistrationManagerTest
, MarkRegistrationLostBackoffHigh
) {
331 RunBackoffTest(+1.0);
334 // Exponential backoff on lost registrations should be reset to zero if
335 // UpdateRegisteredIds is called.
336 TEST_F(RegistrationManagerTest
, MarkRegistrationLostBackoffReset
) {
337 ObjectIdSet ids
= GetSequenceOfIds(kObjectIdsCount
);
339 fake_registration_manager_
.UpdateRegisteredIds(ids
);
342 ObjectIdSet lost_ids
= GetSequenceOfIds(2);
343 LoseRegistrations(lost_ids
);
344 ExpectPendingRegistrations(
346 fake_registration_manager_
.GetPendingRegistrationsForTest());
348 // Trigger another failure to start delaying.
349 fake_registration_manager_
.FirePendingRegistrationsForTest();
350 LoseRegistrations(lost_ids
);
351 double expected_delay
=
352 RegistrationManager::kInitialRegistrationDelaySeconds
;
353 ExpectPendingRegistrations(
354 lost_ids
, expected_delay
,
355 fake_registration_manager_
.GetPendingRegistrationsForTest());
358 fake_registration_manager_
.UpdateRegisteredIds(ids
);
359 ExpectPendingRegistrations(
362 fake_registration_manager_
.GetPendingRegistrationsForTest());
365 TEST_F(RegistrationManagerTest
, MarkAllRegistrationsLost
) {
366 ObjectIdSet ids
= GetSequenceOfIds(kObjectIdsCount
);
368 fake_registration_manager_
.UpdateRegisteredIds(ids
);
370 fake_invalidation_client_
.LoseAllRegistrations();
371 fake_registration_manager_
.MarkAllRegistrationsLost();
373 EXPECT_TRUE(fake_registration_manager_
.GetRegisteredIdsForTest().empty());
374 EXPECT_TRUE(fake_invalidation_client_
.GetRegisteredIdsForTest().empty());
376 ExpectPendingRegistrations(
378 fake_registration_manager_
.GetPendingRegistrationsForTest());
380 // Trigger another failure to start delaying.
381 fake_registration_manager_
.FirePendingRegistrationsForTest();
382 fake_invalidation_client_
.LoseAllRegistrations();
383 fake_registration_manager_
.MarkAllRegistrationsLost();
384 double expected_delay
=
385 RegistrationManager::kInitialRegistrationDelaySeconds
;
386 ExpectPendingRegistrations(
388 fake_registration_manager_
.GetPendingRegistrationsForTest());
390 // Pretend we waited long enough to re-register.
391 fake_registration_manager_
.FirePendingRegistrationsForTest();
392 EXPECT_EQ(ids
, fake_registration_manager_
.GetRegisteredIdsForTest());
393 EXPECT_EQ(ids
, fake_invalidation_client_
.GetRegisteredIdsForTest());
396 // IDs that are disabled should not be re-registered by UpdateRegisteredIds or
397 // automatic re-registration if that registration is lost.
398 TEST_F(RegistrationManagerTest
, DisableId
) {
399 ObjectIdSet ids
= GetSequenceOfIds(kObjectIdsCount
);
401 fake_registration_manager_
.UpdateRegisteredIds(ids
);
403 fake_registration_manager_
.GetPendingRegistrationsForTest().empty());
406 ObjectIdSet disabled_ids
= GetSequenceOfIds(3);
407 ObjectIdSet enabled_ids
= GetSequenceOfIdsStartingAt(3, kObjectIdsCount
- 3);
408 DisableIds(disabled_ids
);
409 ExpectPendingRegistrations(
412 fake_registration_manager_
.GetPendingRegistrationsForTest());
413 EXPECT_EQ(enabled_ids
, fake_registration_manager_
.GetRegisteredIdsForTest());
414 EXPECT_EQ(enabled_ids
, fake_invalidation_client_
.GetRegisteredIdsForTest());
416 fake_registration_manager_
.UpdateRegisteredIds(ids
);
417 EXPECT_EQ(enabled_ids
, fake_registration_manager_
.GetRegisteredIdsForTest());
419 fake_registration_manager_
.MarkRegistrationLost(
420 *disabled_ids
.begin());
421 ExpectPendingRegistrations(
424 fake_registration_manager_
.GetPendingRegistrationsForTest());
426 fake_registration_manager_
.MarkAllRegistrationsLost();
427 ExpectPendingRegistrations(
429 fake_registration_manager_
.GetPendingRegistrationsForTest());
433 } // namespace syncer