| OLD | NEW |
| 1 | 1 |
| 2 // Copyright 2014 The Chromium Authors. All rights reserved. | 2 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 3 // Use of this source code is governed by a BSD-style license that can be | 3 // Use of this source code is governed by a BSD-style license that can be |
| 4 // found in the LICENSE file. | 4 // found in the LICENSE file. |
| 5 | 5 |
| 6 #include "sync/engine/sync_thread_sync_entity.h" | 6 #include "sync/engine/entity_tracker.h" |
| 7 | 7 |
| 8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
| 9 #include "base/time/time.h" | 9 #include "base/time/time.h" |
| 10 #include "sync/internal_api/public/base/model_type.h" | 10 #include "sync/internal_api/public/base/model_type.h" |
| 11 #include "sync/syncable/syncable_util.h" | 11 #include "sync/syncable/syncable_util.h" |
| 12 #include "sync/util/time.h" | 12 #include "sync/util/time.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 | 14 |
| 15 namespace syncer { | 15 namespace syncer { |
| 16 | 16 |
| 17 // Some simple tests for the SyncThreadSyncEntity. | 17 // Some simple tests for the EntityTracker. |
| 18 // | 18 // |
| 19 // The SyncThreadSyncEntity is an implementation detail of the | 19 // The EntityTracker is an implementation detail of the ModelTypeSyncWorker. |
| 20 // NonBlockingTypeProcessorCore. As such, it doesn't make much sense to test | 20 // As such, it doesn't make much sense to test it exhaustively, since it |
| 21 // it exhaustively, since it already gets a lot of test coverage from the | 21 // already gets a lot of test coverage from the ModelTypeSyncWorker unit tests. |
| 22 // NonBlockingTypeProcessorCore unit tests. | |
| 23 // | 22 // |
| 24 // These tests are intended as a basic sanity check. Anything more complicated | 23 // These tests are intended as a basic sanity check. Anything more complicated |
| 25 // would be redundant. | 24 // would be redundant. |
| 26 class SyncThreadSyncEntityTest : public ::testing::Test { | 25 class EntityTrackerTest : public ::testing::Test { |
| 27 public: | 26 public: |
| 28 SyncThreadSyncEntityTest() | 27 EntityTrackerTest() |
| 29 : kServerId("ServerID"), | 28 : kServerId("ServerID"), |
| 30 kClientTag("some.sample.tag"), | 29 kClientTag("some.sample.tag"), |
| 31 kClientTagHash(syncable::GenerateSyncableHash(PREFERENCES, kClientTag)), | 30 kClientTagHash(syncable::GenerateSyncableHash(PREFERENCES, kClientTag)), |
| 32 kCtime(base::Time::UnixEpoch() + base::TimeDelta::FromDays(10)), | 31 kCtime(base::Time::UnixEpoch() + base::TimeDelta::FromDays(10)), |
| 33 kMtime(base::Time::UnixEpoch() + base::TimeDelta::FromDays(20)) { | 32 kMtime(base::Time::UnixEpoch() + base::TimeDelta::FromDays(20)) { |
| 34 specifics.mutable_preference()->set_name(kClientTag); | 33 specifics.mutable_preference()->set_name(kClientTag); |
| 35 specifics.mutable_preference()->set_value("pref.value"); | 34 specifics.mutable_preference()->set_value("pref.value"); |
| 36 } | 35 } |
| 37 | 36 |
| 38 virtual ~SyncThreadSyncEntityTest() {} | 37 virtual ~EntityTrackerTest() {} |
| 39 | 38 |
| 40 const std::string kServerId; | 39 const std::string kServerId; |
| 41 const std::string kClientTag; | 40 const std::string kClientTag; |
| 42 const std::string kClientTagHash; | 41 const std::string kClientTagHash; |
| 43 const base::Time kCtime; | 42 const base::Time kCtime; |
| 44 const base::Time kMtime; | 43 const base::Time kMtime; |
| 45 sync_pb::EntitySpecifics specifics; | 44 sync_pb::EntitySpecifics specifics; |
| 46 }; | 45 }; |
| 47 | 46 |
| 48 // Construct a new entity from a server update. Then receive another update. | 47 // Construct a new entity from a server update. Then receive another update. |
| 49 TEST_F(SyncThreadSyncEntityTest, FromServerUpdate) { | 48 TEST_F(EntityTrackerTest, FromServerUpdate) { |
| 50 scoped_ptr<SyncThreadSyncEntity> entity( | 49 scoped_ptr<EntityTracker> entity( |
| 51 SyncThreadSyncEntity::FromServerUpdate(kServerId, kClientTagHash, 10)); | 50 EntityTracker::FromServerUpdate(kServerId, kClientTagHash, 10)); |
| 52 EXPECT_FALSE(entity->IsCommitPending()); | 51 EXPECT_FALSE(entity->IsCommitPending()); |
| 53 | 52 |
| 54 entity->ReceiveUpdate(20); | 53 entity->ReceiveUpdate(20); |
| 55 EXPECT_FALSE(entity->IsCommitPending()); | 54 EXPECT_FALSE(entity->IsCommitPending()); |
| 56 } | 55 } |
| 57 | 56 |
| 58 // Construct a new entity from a commit request. Then serialize it. | 57 // Construct a new entity from a commit request. Then serialize it. |
| 59 TEST_F(SyncThreadSyncEntityTest, FromCommitRequest) { | 58 TEST_F(EntityTrackerTest, FromCommitRequest) { |
| 60 scoped_ptr<SyncThreadSyncEntity> entity( | 59 scoped_ptr<EntityTracker> entity( |
| 61 SyncThreadSyncEntity::FromCommitRequest(kServerId, | 60 EntityTracker::FromCommitRequest(kServerId, |
| 62 kClientTagHash, | 61 kClientTagHash, |
| 63 22, | 62 22, |
| 64 33, | 63 33, |
| 65 kCtime, | 64 kCtime, |
| 66 kMtime, | 65 kMtime, |
| 67 kClientTag, | 66 kClientTag, |
| 68 false, | 67 false, |
| 69 specifics)); | 68 specifics)); |
| 70 | 69 |
| 71 ASSERT_TRUE(entity->IsCommitPending()); | 70 ASSERT_TRUE(entity->IsCommitPending()); |
| 72 sync_pb::SyncEntity pb_entity; | 71 sync_pb::SyncEntity pb_entity; |
| 73 int64 sequence_number = 0; | 72 int64 sequence_number = 0; |
| 74 entity->PrepareCommitProto(&pb_entity, &sequence_number); | 73 entity->PrepareCommitProto(&pb_entity, &sequence_number); |
| 75 EXPECT_EQ(22, sequence_number); | 74 EXPECT_EQ(22, sequence_number); |
| 76 EXPECT_EQ(kServerId, pb_entity.id_string()); | 75 EXPECT_EQ(kServerId, pb_entity.id_string()); |
| 77 EXPECT_EQ(kClientTagHash, pb_entity.client_defined_unique_tag()); | 76 EXPECT_EQ(kClientTagHash, pb_entity.client_defined_unique_tag()); |
| 78 EXPECT_EQ(33, pb_entity.version()); | 77 EXPECT_EQ(33, pb_entity.version()); |
| 79 EXPECT_EQ(kCtime, ProtoTimeToTime(pb_entity.ctime())); | 78 EXPECT_EQ(kCtime, ProtoTimeToTime(pb_entity.ctime())); |
| 80 EXPECT_EQ(kMtime, ProtoTimeToTime(pb_entity.mtime())); | 79 EXPECT_EQ(kMtime, ProtoTimeToTime(pb_entity.mtime())); |
| 81 EXPECT_FALSE(pb_entity.deleted()); | 80 EXPECT_FALSE(pb_entity.deleted()); |
| 82 EXPECT_EQ(specifics.preference().name(), | 81 EXPECT_EQ(specifics.preference().name(), |
| 83 pb_entity.specifics().preference().name()); | 82 pb_entity.specifics().preference().name()); |
| 84 EXPECT_EQ(specifics.preference().value(), | 83 EXPECT_EQ(specifics.preference().value(), |
| 85 pb_entity.specifics().preference().value()); | 84 pb_entity.specifics().preference().value()); |
| 86 } | 85 } |
| 87 | 86 |
| 88 // Start with a server initiated entity. Commit over top of it. | 87 // Start with a server initiated entity. Commit over top of it. |
| 89 TEST_F(SyncThreadSyncEntityTest, RequestCommit) { | 88 TEST_F(EntityTrackerTest, RequestCommit) { |
| 90 scoped_ptr<SyncThreadSyncEntity> entity( | 89 scoped_ptr<EntityTracker> entity( |
| 91 SyncThreadSyncEntity::FromServerUpdate(kServerId, kClientTagHash, 10)); | 90 EntityTracker::FromServerUpdate(kServerId, kClientTagHash, 10)); |
| 92 | 91 |
| 93 entity->RequestCommit(kServerId, | 92 entity->RequestCommit(kServerId, |
| 94 kClientTagHash, | 93 kClientTagHash, |
| 95 1, | 94 1, |
| 96 10, | 95 10, |
| 97 kCtime, | 96 kCtime, |
| 98 kMtime, | 97 kMtime, |
| 99 kClientTag, | 98 kClientTag, |
| 100 false, | 99 false, |
| 101 specifics); | 100 specifics); |
| 102 | 101 |
| 103 EXPECT_TRUE(entity->IsCommitPending()); | 102 EXPECT_TRUE(entity->IsCommitPending()); |
| 104 } | 103 } |
| 105 | 104 |
| 106 // Start with a server initiated entity. Fail to request a commit because of | 105 // Start with a server initiated entity. Fail to request a commit because of |
| 107 // an out of date base version. | 106 // an out of date base version. |
| 108 TEST_F(SyncThreadSyncEntityTest, RequestCommitFailure) { | 107 TEST_F(EntityTrackerTest, RequestCommitFailure) { |
| 109 scoped_ptr<SyncThreadSyncEntity> entity( | 108 scoped_ptr<EntityTracker> entity( |
| 110 SyncThreadSyncEntity::FromServerUpdate(kServerId, kClientTagHash, 10)); | 109 EntityTracker::FromServerUpdate(kServerId, kClientTagHash, 10)); |
| 111 EXPECT_FALSE(entity->IsCommitPending()); | 110 EXPECT_FALSE(entity->IsCommitPending()); |
| 112 | 111 |
| 113 entity->RequestCommit(kServerId, | 112 entity->RequestCommit(kServerId, |
| 114 kClientTagHash, | 113 kClientTagHash, |
| 115 23, | 114 23, |
| 116 5, // Version 5 < 10 | 115 5, // Version 5 < 10 |
| 117 kCtime, | 116 kCtime, |
| 118 kMtime, | 117 kMtime, |
| 119 kClientTag, | 118 kClientTag, |
| 120 false, | 119 false, |
| 121 specifics); | 120 specifics); |
| 122 EXPECT_FALSE(entity->IsCommitPending()); | 121 EXPECT_FALSE(entity->IsCommitPending()); |
| 123 } | 122 } |
| 124 | 123 |
| 125 // Start with a pending commit. Clobber it with an incoming update. | 124 // Start with a pending commit. Clobber it with an incoming update. |
| 126 TEST_F(SyncThreadSyncEntityTest, UpdateClobbersCommit) { | 125 TEST_F(EntityTrackerTest, UpdateClobbersCommit) { |
| 127 scoped_ptr<SyncThreadSyncEntity> entity( | 126 scoped_ptr<EntityTracker> entity( |
| 128 SyncThreadSyncEntity::FromCommitRequest(kServerId, | 127 EntityTracker::FromCommitRequest(kServerId, |
| 129 kClientTagHash, | 128 kClientTagHash, |
| 130 22, | 129 22, |
| 131 33, | 130 33, |
| 132 kCtime, | 131 kCtime, |
| 133 kMtime, | 132 kMtime, |
| 134 kClientTag, | 133 kClientTag, |
| 135 false, | 134 false, |
| 136 specifics)); | 135 specifics)); |
| 137 | 136 |
| 138 EXPECT_TRUE(entity->IsCommitPending()); | 137 EXPECT_TRUE(entity->IsCommitPending()); |
| 139 | 138 |
| 140 entity->ReceiveUpdate(400); // Version 400 > 33. | 139 entity->ReceiveUpdate(400); // Version 400 > 33. |
| 141 EXPECT_FALSE(entity->IsCommitPending()); | 140 EXPECT_FALSE(entity->IsCommitPending()); |
| 142 } | 141 } |
| 143 | 142 |
| 144 // Start with a pending commit. Send it a reflected update that | 143 // Start with a pending commit. Send it a reflected update that |
| 145 // will not override the in-progress commit. | 144 // will not override the in-progress commit. |
| 146 TEST_F(SyncThreadSyncEntityTest, ReflectedUpdateDoesntClobberCommit) { | 145 TEST_F(EntityTrackerTest, ReflectedUpdateDoesntClobberCommit) { |
| 147 scoped_ptr<SyncThreadSyncEntity> entity( | 146 scoped_ptr<EntityTracker> entity( |
| 148 SyncThreadSyncEntity::FromCommitRequest(kServerId, | 147 EntityTracker::FromCommitRequest(kServerId, |
| 149 kClientTagHash, | 148 kClientTagHash, |
| 150 22, | 149 22, |
| 151 33, | 150 33, |
| 152 kCtime, | 151 kCtime, |
| 153 kMtime, | 152 kMtime, |
| 154 kClientTag, | 153 kClientTag, |
| 155 false, | 154 false, |
| 156 specifics)); | 155 specifics)); |
| 157 | 156 |
| 158 EXPECT_TRUE(entity->IsCommitPending()); | 157 EXPECT_TRUE(entity->IsCommitPending()); |
| 159 | 158 |
| 160 entity->ReceiveUpdate(33); // Version 33 == 33. | 159 entity->ReceiveUpdate(33); // Version 33 == 33. |
| 161 EXPECT_TRUE(entity->IsCommitPending()); | 160 EXPECT_TRUE(entity->IsCommitPending()); |
| 162 } | 161 } |
| 163 | 162 |
| 164 } // namespace syncer | 163 } // namespace syncer |
| OLD | NEW |