| OLD | NEW |
| 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 #include <stddef.h> |
| 6 #include <stdint.h> |
| 7 |
| 5 #include <cstddef> | 8 #include <cstddef> |
| 6 #include <map> | 9 #include <map> |
| 7 #include <set> | 10 #include <set> |
| 8 #include <string> | 11 #include <string> |
| 9 #include <vector> | 12 #include <vector> |
| 10 | 13 |
| 11 #include "base/compiler_specific.h" | 14 #include "base/compiler_specific.h" |
| 12 #include "base/single_thread_task_runner.h" | 15 #include "base/single_thread_task_runner.h" |
| 13 #include "base/stl_util.h" | 16 #include "base/stl_util.h" |
| 14 #include "base/thread_task_runner_handle.h" | 17 #include "base/thread_task_runner_handle.h" |
| (...skipping 17 matching lines...) Expand all Loading... |
| 32 | 35 |
| 33 const char kClientId[] = "client_id"; | 36 const char kClientId[] = "client_id"; |
| 34 const char kClientInfo[] = "client_info"; | 37 const char kClientInfo[] = "client_info"; |
| 35 | 38 |
| 36 const char kState[] = "state"; | 39 const char kState[] = "state"; |
| 37 const char kNewState[] = "new_state"; | 40 const char kNewState[] = "new_state"; |
| 38 | 41 |
| 39 const char kPayload1[] = "payload1"; | 42 const char kPayload1[] = "payload1"; |
| 40 const char kPayload2[] = "payload2"; | 43 const char kPayload2[] = "payload2"; |
| 41 | 44 |
| 42 const int64 kVersion1 = 1LL; | 45 const int64_t kVersion1 = 1LL; |
| 43 const int64 kVersion2 = 2LL; | 46 const int64_t kVersion2 = 2LL; |
| 44 | 47 |
| 45 const int kChromeSyncSourceId = 1004; | 48 const int kChromeSyncSourceId = 1004; |
| 46 | 49 |
| 47 struct AckHandleLessThan { | 50 struct AckHandleLessThan { |
| 48 bool operator()(const AckHandle& lhs, const AckHandle& rhs) const { | 51 bool operator()(const AckHandle& lhs, const AckHandle& rhs) const { |
| 49 return lhs.handle_data() < rhs.handle_data(); | 52 return lhs.handle_data() < rhs.handle_data(); |
| 50 } | 53 } |
| 51 }; | 54 }; |
| 52 | 55 |
| 53 typedef std::set<AckHandle, AckHandleLessThan> AckHandleSet; | 56 typedef std::set<AckHandle, AckHandleLessThan> AckHandleSet; |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 136 | 139 |
| 137 size_t GetInvalidationCount(const ObjectId& id) const { | 140 size_t GetInvalidationCount(const ObjectId& id) const { |
| 138 Map::const_iterator it = invalidations_.find(id); | 141 Map::const_iterator it = invalidations_.find(id); |
| 139 if (it == invalidations_.end()) { | 142 if (it == invalidations_.end()) { |
| 140 return 0; | 143 return 0; |
| 141 } else { | 144 } else { |
| 142 return it->second.size(); | 145 return it->second.size(); |
| 143 } | 146 } |
| 144 } | 147 } |
| 145 | 148 |
| 146 int64 GetVersion(const ObjectId& id) const { | 149 int64_t GetVersion(const ObjectId& id) const { |
| 147 Map::const_iterator it = invalidations_.find(id); | 150 Map::const_iterator it = invalidations_.find(id); |
| 148 if (it == invalidations_.end()) { | 151 if (it == invalidations_.end()) { |
| 149 ADD_FAILURE() << "No invalidations for ID " << ObjectIdToString(id); | 152 ADD_FAILURE() << "No invalidations for ID " << ObjectIdToString(id); |
| 150 return 0; | 153 return 0; |
| 151 } else { | 154 } else { |
| 152 return it->second.back().version(); | 155 return it->second.back().version(); |
| 153 } | 156 } |
| 154 } | 157 } |
| 155 | 158 |
| 156 std::string GetPayload(const ObjectId& id) const { | 159 std::string GetPayload(const ObjectId& id) const { |
| (...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 300 // drain the task queue before calling it. | 303 // drain the task queue before calling it. |
| 301 FlushPendingWrites(); | 304 FlushPendingWrites(); |
| 302 fake_invalidation_client_ = NULL; | 305 fake_invalidation_client_ = NULL; |
| 303 listener_.StopForTest(); | 306 listener_.StopForTest(); |
| 304 } | 307 } |
| 305 | 308 |
| 306 size_t GetInvalidationCount(const ObjectId& id) const { | 309 size_t GetInvalidationCount(const ObjectId& id) const { |
| 307 return fake_delegate_.GetInvalidationCount(id); | 310 return fake_delegate_.GetInvalidationCount(id); |
| 308 } | 311 } |
| 309 | 312 |
| 310 int64 GetVersion(const ObjectId& id) const { | 313 int64_t GetVersion(const ObjectId& id) const { |
| 311 return fake_delegate_.GetVersion(id); | 314 return fake_delegate_.GetVersion(id); |
| 312 } | 315 } |
| 313 | 316 |
| 314 std::string GetPayload(const ObjectId& id) const { | 317 std::string GetPayload(const ObjectId& id) const { |
| 315 return fake_delegate_.GetPayload(id); | 318 return fake_delegate_.GetPayload(id); |
| 316 } | 319 } |
| 317 | 320 |
| 318 bool IsUnknownVersion(const ObjectId& id) const { | 321 bool IsUnknownVersion(const ObjectId& id) const { |
| 319 return fake_delegate_.IsUnknownVersion(id); | 322 return fake_delegate_.IsUnknownVersion(id); |
| 320 } | 323 } |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 376 return map.ForObject(id); | 379 return map.ForObject(id); |
| 377 } | 380 } |
| 378 } | 381 } |
| 379 | 382 |
| 380 ObjectIdSet GetRegisteredIds() const { | 383 ObjectIdSet GetRegisteredIds() const { |
| 381 return fake_invalidation_client_->GetRegisteredIds(); | 384 return fake_invalidation_client_->GetRegisteredIds(); |
| 382 } | 385 } |
| 383 | 386 |
| 384 // |payload| can be NULL. | 387 // |payload| can be NULL. |
| 385 void FireInvalidate(const ObjectId& object_id, | 388 void FireInvalidate(const ObjectId& object_id, |
| 386 int64 version, const char* payload) { | 389 int64_t version, |
| 390 const char* payload) { |
| 387 invalidation::Invalidation inv; | 391 invalidation::Invalidation inv; |
| 388 if (payload) { | 392 if (payload) { |
| 389 inv = invalidation::Invalidation(object_id, version, payload); | 393 inv = invalidation::Invalidation(object_id, version, payload); |
| 390 } else { | 394 } else { |
| 391 inv = invalidation::Invalidation(object_id, version); | 395 inv = invalidation::Invalidation(object_id, version); |
| 392 } | 396 } |
| 393 const AckHandle ack_handle("fakedata"); | 397 const AckHandle ack_handle("fakedata"); |
| 394 fake_invalidation_client_->ClearAckedHandles(); | 398 fake_invalidation_client_->ClearAckedHandles(); |
| 395 listener_.Invalidate(fake_invalidation_client_, inv, ack_handle); | 399 listener_.Invalidate(fake_invalidation_client_, inv, ack_handle); |
| 396 EXPECT_TRUE(fake_invalidation_client_->IsAckedHandle(ack_handle)); | 400 EXPECT_TRUE(fake_invalidation_client_->IsAckedHandle(ack_handle)); |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 503 ASSERT_EQ(1U, GetInvalidationCount(id)); | 507 ASSERT_EQ(1U, GetInvalidationCount(id)); |
| 504 ASSERT_FALSE(IsUnknownVersion(id)); | 508 ASSERT_FALSE(IsUnknownVersion(id)); |
| 505 EXPECT_EQ(kVersion1, GetVersion(id)); | 509 EXPECT_EQ(kVersion1, GetVersion(id)); |
| 506 EXPECT_EQ(kPayload1, GetPayload(id)); | 510 EXPECT_EQ(kPayload1, GetPayload(id)); |
| 507 } | 511 } |
| 508 | 512 |
| 509 // Fire ten invalidations in a row. All should be received. | 513 // Fire ten invalidations in a row. All should be received. |
| 510 TEST_F(SyncInvalidationListenerTest, ManyInvalidations_NoDrop) { | 514 TEST_F(SyncInvalidationListenerTest, ManyInvalidations_NoDrop) { |
| 511 const int kRepeatCount = 10; | 515 const int kRepeatCount = 10; |
| 512 const ObjectId& id = kPreferencesId_; | 516 const ObjectId& id = kPreferencesId_; |
| 513 int64 initial_version = kVersion1; | 517 int64_t initial_version = kVersion1; |
| 514 for (int64 i = initial_version; i < initial_version + kRepeatCount; ++i) { | 518 for (int64_t i = initial_version; i < initial_version + kRepeatCount; ++i) { |
| 515 FireInvalidate(id, i, kPayload1); | 519 FireInvalidate(id, i, kPayload1); |
| 516 } | 520 } |
| 517 ASSERT_EQ(static_cast<size_t>(kRepeatCount), GetInvalidationCount(id)); | 521 ASSERT_EQ(static_cast<size_t>(kRepeatCount), GetInvalidationCount(id)); |
| 518 ASSERT_FALSE(IsUnknownVersion(id)); | 522 ASSERT_FALSE(IsUnknownVersion(id)); |
| 519 EXPECT_EQ(kPayload1, GetPayload(id)); | 523 EXPECT_EQ(kPayload1, GetPayload(id)); |
| 520 EXPECT_EQ(initial_version + kRepeatCount - 1, GetVersion(id)); | 524 EXPECT_EQ(initial_version + kRepeatCount - 1, GetVersion(id)); |
| 521 } | 525 } |
| 522 | 526 |
| 523 // Fire an invalidation for an unregistered object ID with a payload. It should | 527 // Fire an invalidation for an unregistered object ID with a payload. It should |
| 524 // still be processed, and both the payload and the version should be updated. | 528 // still be processed, and both the payload and the version should be updated. |
| (...skipping 24 matching lines...) Expand all Loading... |
| 549 TEST_F(SyncInvalidationListenerTest, InvalidateBeforeRegistration_Drop) { | 553 TEST_F(SyncInvalidationListenerTest, InvalidateBeforeRegistration_Drop) { |
| 550 const int kRepeatCount = | 554 const int kRepeatCount = |
| 551 UnackedInvalidationSet::kMaxBufferedInvalidations + 1; | 555 UnackedInvalidationSet::kMaxBufferedInvalidations + 1; |
| 552 const ObjectId kUnregisteredId(kChromeSyncSourceId, "unregistered"); | 556 const ObjectId kUnregisteredId(kChromeSyncSourceId, "unregistered"); |
| 553 const ObjectId& id = kUnregisteredId; | 557 const ObjectId& id = kUnregisteredId; |
| 554 ObjectIdSet ids; | 558 ObjectIdSet ids; |
| 555 ids.insert(id); | 559 ids.insert(id); |
| 556 | 560 |
| 557 EXPECT_EQ(0U, GetInvalidationCount(id)); | 561 EXPECT_EQ(0U, GetInvalidationCount(id)); |
| 558 | 562 |
| 559 int64 initial_version = kVersion1; | 563 int64_t initial_version = kVersion1; |
| 560 for (int64 i = initial_version; i < initial_version + kRepeatCount; ++i) { | 564 for (int64_t i = initial_version; i < initial_version + kRepeatCount; ++i) { |
| 561 FireInvalidate(id, i, kPayload1); | 565 FireInvalidate(id, i, kPayload1); |
| 562 } | 566 } |
| 563 | 567 |
| 564 EnableNotifications(); | 568 EnableNotifications(); |
| 565 listener_.Ready(fake_invalidation_client_); | 569 listener_.Ready(fake_invalidation_client_); |
| 566 listener_.UpdateRegisteredIds(ids); | 570 listener_.UpdateRegisteredIds(ids); |
| 567 | 571 |
| 568 ASSERT_EQ(UnackedInvalidationSet::kMaxBufferedInvalidations, | 572 ASSERT_EQ(UnackedInvalidationSet::kMaxBufferedInvalidations, |
| 569 GetInvalidationCount(id)); | 573 GetInvalidationCount(id)); |
| 570 ASSERT_FALSE(IsUnknownVersion(id)); | 574 ASSERT_FALSE(IsUnknownVersion(id)); |
| (...skipping 530 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1101 ids.insert(kExtensionsId_); | 1105 ids.insert(kExtensionsId_); |
| 1102 listener_.UpdateRegisteredIds(ids); | 1106 listener_.UpdateRegisteredIds(ids); |
| 1103 | 1107 |
| 1104 ASSERT_EQ(3U, GetInvalidationCount(kExtensionsId_)); | 1108 ASSERT_EQ(3U, GetInvalidationCount(kExtensionsId_)); |
| 1105 EXPECT_EQ(30, GetVersion(kExtensionsId_)); | 1109 EXPECT_EQ(30, GetVersion(kExtensionsId_)); |
| 1106 } | 1110 } |
| 1107 | 1111 |
| 1108 } // namespace | 1112 } // namespace |
| 1109 | 1113 |
| 1110 } // namespace syncer | 1114 } // namespace syncer |
| OLD | NEW |