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