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

Side by Side Diff: components/invalidation/invalidator_test_template.h

Issue 1146533005: [Sync] InvalidationService shouldn't CHECK when registering ObjectId for more than one handler (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressed comments from Bartosz. Created 5 years, 7 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 2014 The Chromium Authors. All rights reserved. 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 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 // This class defines tests that implementations of Invalidator should pass in 5 // This class defines tests that implementations of Invalidator should pass in
6 // order to be conformant. Here's how you use it to test your implementation. 6 // order to be conformant. Here's how you use it to test your implementation.
7 // 7 //
8 // Say your class is called MyInvalidator. Then you need to define a class 8 // Say your class is called MyInvalidator. Then you need to define a class
9 // called MyInvalidatorTestDelegate in my_sync_notifier_unittest.cc like this: 9 // called MyInvalidatorTestDelegate in my_sync_notifier_unittest.cc like this:
10 // 10 //
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after
139 invalidation_map.Insert(Invalidation::Init(this->id2, 2, "2")); 139 invalidation_map.Insert(Invalidation::Init(this->id2, 2, "2"));
140 invalidation_map.Insert(Invalidation::Init(this->id3, 3, "3")); 140 invalidation_map.Insert(Invalidation::Init(this->id3, 3, "3"));
141 141
142 // Should be ignored since no IDs are registered to |handler|. 142 // Should be ignored since no IDs are registered to |handler|.
143 this->delegate_.TriggerOnIncomingInvalidation(invalidation_map); 143 this->delegate_.TriggerOnIncomingInvalidation(invalidation_map);
144 EXPECT_EQ(0, handler.GetInvalidationCount()); 144 EXPECT_EQ(0, handler.GetInvalidationCount());
145 145
146 ObjectIdSet ids; 146 ObjectIdSet ids;
147 ids.insert(this->id1); 147 ids.insert(this->id1);
148 ids.insert(this->id2); 148 ids.insert(this->id2);
149 invalidator->UpdateRegisteredIds(&handler, ids); 149 EXPECT_TRUE(invalidator->UpdateRegisteredIds(&handler, ids));
150 150
151 this->delegate_.TriggerOnInvalidatorStateChange(INVALIDATIONS_ENABLED); 151 this->delegate_.TriggerOnInvalidatorStateChange(INVALIDATIONS_ENABLED);
152 EXPECT_EQ(INVALIDATIONS_ENABLED, handler.GetInvalidatorState()); 152 EXPECT_EQ(INVALIDATIONS_ENABLED, handler.GetInvalidatorState());
153 153
154 ObjectIdInvalidationMap expected_invalidations; 154 ObjectIdInvalidationMap expected_invalidations;
155 expected_invalidations.Insert(Invalidation::Init(this->id1, 1, "1")); 155 expected_invalidations.Insert(Invalidation::Init(this->id1, 1, "1"));
156 expected_invalidations.Insert(Invalidation::Init(this->id2, 2, "2")); 156 expected_invalidations.Insert(Invalidation::Init(this->id2, 2, "2"));
157 157
158 this->delegate_.TriggerOnIncomingInvalidation(invalidation_map); 158 this->delegate_.TriggerOnIncomingInvalidation(invalidation_map);
159 EXPECT_EQ(1, handler.GetInvalidationCount()); 159 EXPECT_EQ(1, handler.GetInvalidationCount());
160 EXPECT_THAT(expected_invalidations, Eq(handler.GetLastInvalidationMap())); 160 EXPECT_THAT(expected_invalidations, Eq(handler.GetLastInvalidationMap()));
161 161
162 ids.erase(this->id1); 162 ids.erase(this->id1);
163 ids.insert(this->id3); 163 ids.insert(this->id3);
164 invalidator->UpdateRegisteredIds(&handler, ids); 164 EXPECT_TRUE(invalidator->UpdateRegisteredIds(&handler, ids));
165 165
166 expected_invalidations = ObjectIdInvalidationMap(); 166 expected_invalidations = ObjectIdInvalidationMap();
167 expected_invalidations.Insert(Invalidation::Init(this->id2, 2, "2")); 167 expected_invalidations.Insert(Invalidation::Init(this->id2, 2, "2"));
168 expected_invalidations.Insert(Invalidation::Init(this->id3, 3, "3")); 168 expected_invalidations.Insert(Invalidation::Init(this->id3, 3, "3"));
169 169
170 // Removed object IDs should not be notified, newly-added ones should. 170 // Removed object IDs should not be notified, newly-added ones should.
171 this->delegate_.TriggerOnIncomingInvalidation(invalidation_map); 171 this->delegate_.TriggerOnIncomingInvalidation(invalidation_map);
172 EXPECT_EQ(2, handler.GetInvalidationCount()); 172 EXPECT_EQ(2, handler.GetInvalidationCount());
173 EXPECT_THAT(expected_invalidations, Eq(handler.GetLastInvalidationMap())); 173 EXPECT_THAT(expected_invalidations, Eq(handler.GetLastInvalidationMap()));
174 174
(...skipping 28 matching lines...) Expand all
203 203
204 invalidator->RegisterHandler(&handler1); 204 invalidator->RegisterHandler(&handler1);
205 invalidator->RegisterHandler(&handler2); 205 invalidator->RegisterHandler(&handler2);
206 invalidator->RegisterHandler(&handler3); 206 invalidator->RegisterHandler(&handler3);
207 invalidator->RegisterHandler(&handler4); 207 invalidator->RegisterHandler(&handler4);
208 208
209 { 209 {
210 ObjectIdSet ids; 210 ObjectIdSet ids;
211 ids.insert(this->id1); 211 ids.insert(this->id1);
212 ids.insert(this->id2); 212 ids.insert(this->id2);
213 invalidator->UpdateRegisteredIds(&handler1, ids); 213 EXPECT_TRUE(invalidator->UpdateRegisteredIds(&handler1, ids));
214 } 214 }
215 215
216 { 216 {
217 ObjectIdSet ids; 217 ObjectIdSet ids;
218 ids.insert(this->id3); 218 ids.insert(this->id3);
219 invalidator->UpdateRegisteredIds(&handler2, ids); 219 EXPECT_TRUE(invalidator->UpdateRegisteredIds(&handler2, ids));
220 } 220 }
221 221
222 // Don't register any IDs for handler3. 222 // Don't register any IDs for handler3.
223 223
224 { 224 {
225 ObjectIdSet ids; 225 ObjectIdSet ids;
226 ids.insert(this->id4); 226 ids.insert(this->id4);
227 invalidator->UpdateRegisteredIds(&handler4, ids); 227 EXPECT_TRUE(invalidator->UpdateRegisteredIds(&handler4, ids));
228 } 228 }
229 229
230 invalidator->UnregisterHandler(&handler4); 230 invalidator->UnregisterHandler(&handler4);
231 231
232 this->delegate_.TriggerOnInvalidatorStateChange(INVALIDATIONS_ENABLED); 232 this->delegate_.TriggerOnInvalidatorStateChange(INVALIDATIONS_ENABLED);
233 EXPECT_EQ(INVALIDATIONS_ENABLED, handler1.GetInvalidatorState()); 233 EXPECT_EQ(INVALIDATIONS_ENABLED, handler1.GetInvalidatorState());
234 EXPECT_EQ(INVALIDATIONS_ENABLED, handler2.GetInvalidatorState()); 234 EXPECT_EQ(INVALIDATIONS_ENABLED, handler2.GetInvalidatorState());
235 EXPECT_EQ(INVALIDATIONS_ENABLED, handler3.GetInvalidatorState()); 235 EXPECT_EQ(INVALIDATIONS_ENABLED, handler3.GetInvalidatorState());
236 EXPECT_EQ(TRANSIENT_INVALIDATION_ERROR, handler4.GetInvalidatorState()); 236 EXPECT_EQ(TRANSIENT_INVALIDATION_ERROR, handler4.GetInvalidatorState());
237 237
(...skipping 27 matching lines...) Expand all
265 EXPECT_EQ(TRANSIENT_INVALIDATION_ERROR, handler1.GetInvalidatorState()); 265 EXPECT_EQ(TRANSIENT_INVALIDATION_ERROR, handler1.GetInvalidatorState());
266 EXPECT_EQ(TRANSIENT_INVALIDATION_ERROR, handler2.GetInvalidatorState()); 266 EXPECT_EQ(TRANSIENT_INVALIDATION_ERROR, handler2.GetInvalidatorState());
267 EXPECT_EQ(TRANSIENT_INVALIDATION_ERROR, handler3.GetInvalidatorState()); 267 EXPECT_EQ(TRANSIENT_INVALIDATION_ERROR, handler3.GetInvalidatorState());
268 EXPECT_EQ(TRANSIENT_INVALIDATION_ERROR, handler4.GetInvalidatorState()); 268 EXPECT_EQ(TRANSIENT_INVALIDATION_ERROR, handler4.GetInvalidatorState());
269 269
270 invalidator->UnregisterHandler(&handler3); 270 invalidator->UnregisterHandler(&handler3);
271 invalidator->UnregisterHandler(&handler2); 271 invalidator->UnregisterHandler(&handler2);
272 invalidator->UnregisterHandler(&handler1); 272 invalidator->UnregisterHandler(&handler1);
273 } 273 }
274 274
275 // Multiple registrations by different handlers on the same object ID should
276 // return false.
277 TYPED_TEST_P(InvalidatorTest, MultipleRegistrations) {
278 Invalidator* const invalidator = this->CreateAndInitializeInvalidator();
279
280 FakeInvalidationHandler handler1;
281 FakeInvalidationHandler handler2;
282
283 invalidator->RegisterHandler(&handler1);
284 invalidator->RegisterHandler(&handler2);
285
286 // Registering both handlers for the same ObjectId. First call should succeed,
287 // second should fail.
288 ObjectIdSet ids;
289 ids.insert(this->id1);
290 EXPECT_TRUE(invalidator->UpdateRegisteredIds(&handler1, ids));
291 EXPECT_FALSE(invalidator->UpdateRegisteredIds(&handler2, ids));
292
293 invalidator->UnregisterHandler(&handler2);
294 invalidator->UnregisterHandler(&handler1);
295 }
296
275 // Make sure that passing an empty set to UpdateRegisteredIds clears the 297 // Make sure that passing an empty set to UpdateRegisteredIds clears the
276 // corresponding entries for the handler. 298 // corresponding entries for the handler.
277 TYPED_TEST_P(InvalidatorTest, EmptySetUnregisters) { 299 TYPED_TEST_P(InvalidatorTest, EmptySetUnregisters) {
278 Invalidator* const invalidator = this->CreateAndInitializeInvalidator(); 300 Invalidator* const invalidator = this->CreateAndInitializeInvalidator();
279 301
280 FakeInvalidationHandler handler1; 302 FakeInvalidationHandler handler1;
281 303
282 // Control observer. 304 // Control observer.
283 FakeInvalidationHandler handler2; 305 FakeInvalidationHandler handler2;
284 306
285 invalidator->RegisterHandler(&handler1); 307 invalidator->RegisterHandler(&handler1);
286 invalidator->RegisterHandler(&handler2); 308 invalidator->RegisterHandler(&handler2);
287 309
288 { 310 {
289 ObjectIdSet ids; 311 ObjectIdSet ids;
290 ids.insert(this->id1); 312 ids.insert(this->id1);
291 ids.insert(this->id2); 313 ids.insert(this->id2);
292 invalidator->UpdateRegisteredIds(&handler1, ids); 314 EXPECT_TRUE(invalidator->UpdateRegisteredIds(&handler1, ids));
293 } 315 }
294 316
295 { 317 {
296 ObjectIdSet ids; 318 ObjectIdSet ids;
297 ids.insert(this->id3); 319 ids.insert(this->id3);
298 invalidator->UpdateRegisteredIds(&handler2, ids); 320 EXPECT_TRUE(invalidator->UpdateRegisteredIds(&handler2, ids));
299 } 321 }
300 322
301 // Unregister the IDs for the first observer. It should not receive any 323 // Unregister the IDs for the first observer. It should not receive any
302 // further invalidations. 324 // further invalidations.
303 invalidator->UpdateRegisteredIds(&handler1, ObjectIdSet()); 325 EXPECT_TRUE(invalidator->UpdateRegisteredIds(&handler1, ObjectIdSet()));
304 326
305 this->delegate_.TriggerOnInvalidatorStateChange(INVALIDATIONS_ENABLED); 327 this->delegate_.TriggerOnInvalidatorStateChange(INVALIDATIONS_ENABLED);
306 EXPECT_EQ(INVALIDATIONS_ENABLED, handler1.GetInvalidatorState()); 328 EXPECT_EQ(INVALIDATIONS_ENABLED, handler1.GetInvalidatorState());
307 EXPECT_EQ(INVALIDATIONS_ENABLED, handler2.GetInvalidatorState()); 329 EXPECT_EQ(INVALIDATIONS_ENABLED, handler2.GetInvalidatorState());
308 330
309 { 331 {
310 ObjectIdInvalidationMap invalidation_map; 332 ObjectIdInvalidationMap invalidation_map;
311 invalidation_map.Insert(Invalidation::Init(this->id1, 1, "1")); 333 invalidation_map.Insert(Invalidation::Init(this->id1, 1, "1"));
312 invalidation_map.Insert(Invalidation::Init(this->id2, 2, "2")); 334 invalidation_map.Insert(Invalidation::Init(this->id2, 2, "2"));
313 invalidation_map.Insert(Invalidation::Init(this->id3, 3, "3")); 335 invalidation_map.Insert(Invalidation::Init(this->id3, 3, "3"));
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
362 EXPECT_EQ(INVALIDATIONS_ENABLED, handler.GetLastRetrievedState()); 384 EXPECT_EQ(INVALIDATIONS_ENABLED, handler.GetLastRetrievedState());
363 385
364 this->delegate_.TriggerOnInvalidatorStateChange(TRANSIENT_INVALIDATION_ERROR); 386 this->delegate_.TriggerOnInvalidatorStateChange(TRANSIENT_INVALIDATION_ERROR);
365 EXPECT_EQ(TRANSIENT_INVALIDATION_ERROR, handler.GetInvalidatorState()); 387 EXPECT_EQ(TRANSIENT_INVALIDATION_ERROR, handler.GetInvalidatorState());
366 EXPECT_EQ(TRANSIENT_INVALIDATION_ERROR, handler.GetLastRetrievedState()); 388 EXPECT_EQ(TRANSIENT_INVALIDATION_ERROR, handler.GetLastRetrievedState());
367 389
368 invalidator->UnregisterHandler(&handler); 390 invalidator->UnregisterHandler(&handler);
369 } 391 }
370 392
371 REGISTER_TYPED_TEST_CASE_P(InvalidatorTest, 393 REGISTER_TYPED_TEST_CASE_P(InvalidatorTest,
372 Basic, MultipleHandlers, EmptySetUnregisters, 394 Basic,
395 MultipleHandlers,
396 MultipleRegistrations,
397 EmptySetUnregisters,
373 GetInvalidatorStateAlwaysCurrent); 398 GetInvalidatorStateAlwaysCurrent);
374 399
375 } // namespace syncer 400 } // namespace syncer
376 401
377 #endif // COMPONENTS_INVALIDATION_INVALIDATOR_TEST_TEMPLATE_H_ 402 #endif // COMPONENTS_INVALIDATION_INVALIDATOR_TEST_TEMPLATE_H_
OLDNEW
« no previous file with comments | « components/invalidation/invalidator_registrar_unittest.cc ('k') | components/invalidation/non_blocking_invalidator.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698