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

Side by Side Diff: blimp/net/helium/syncable_unittest.cc

Issue 2382533002: Helium: Initial proto and Syncable interface definition (Closed)
Patch Set: changes as discussed offline with @wez and @kevin Created 4 years, 2 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
(Empty)
1 // Copyright 2016 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.
4
5 #include "blimp/net/helium/syncable.h"
6
7 #include "base/bind.h"
8 #include "base/macros.h"
9 #include "base/memory/ptr_util.h"
10 #include "testing/gmock/include/gmock/gmock.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12
13 namespace blimp {
14 namespace {
15
16 // This is a sample implementation that demostrates the implementation
17 // of the SyncableMember and SyncableObject
18
19 // For simplicity of this example, the ChangeSet will be an integer.
20 class FakeIntSyncable : public SyncableMember<int> {
21 public:
22 explicit FakeIntSyncable(SyncableObject* parent)
23 : SyncableMember(parent), value_(0) {
24 last_modified_ = parent_clock();
25 }
26
27 bool ModifiedSince(const VectorClock& from) override {
28 return from.local_revision() < last_modified_.local_revision();
29 }
30
31 std::unique_ptr<int> CreateChangesetToCurrent(
32 const VectorClock& from) override {
33 return base::MakeUnique<int>(value_);
34 }
35
36 void ApplyChangeset(const VectorClock& from,
37 const VectorClock& to,
38 std::unique_ptr<int> changeset) override {
39 // Restore the value
40 value_ = *changeset;
41
42 // Update our clock to the latest clock
43 last_modified_ = to;
44 }
45
46 void ReleaseCheckpointsBefore(const VectorClock& checkpoint) override {
47 last_modified_ = checkpoint;
48 }
49
50 void SetValue(int value) {
51 value_ = value;
52
53 // Increment the parent clock and update our last_modified_ value
54 last_modified_ = IncrementParentClock();
55 }
56
57 int value() { return value_; }
58
59 private:
60 // The last time this object was changed
61 VectorClock last_modified_;
steimel 2016/10/05 00:57:38 Should we make this a protected member at the Sync
scf 2016/10/05 16:45:10 each class might implement this differently. hard
steimel 2016/10/05 16:57:58 Gotcha. Makes sense. I guess in my head I figured
62 int32_t value_;
63
64 DISALLOW_COPY_AND_ASSIGN(FakeIntSyncable);
65 };
66
67 class ParentObjectSyncable : public SyncableObject {
68 public:
69 explicit ParentObjectSyncable(VectorClock clock)
70 : SyncableObject(clock), child1_(this), child2_(this) {}
71
72 std::unique_ptr<proto::ChangesetMessage> CreateChangesetToCurrent(
73 const VectorClock& from) override {
74 std::unique_ptr<proto::ChangesetMessage> changeset =
75 base::MakeUnique<proto::ChangesetMessage>();
76
77 proto::TestChangesetMessage* bm = changeset->mutable_test();
78
79 if (child1_.ModifiedSince(from)) {
80 std::unique_ptr<int> value1 = child1_.CreateChangesetToCurrent(from);
81 bm->set_value1(*value1);
82 }
83
84 if (child2_.ModifiedSince(from)) {
85 std::unique_ptr<int> value2 = child2_.CreateChangesetToCurrent(from);
86 bm->set_value2(*value2);
87 }
88
89 return changeset;
90 }
91
92 void ApplyChangeset(
93 const VectorClock& from,
94 const VectorClock& to,
95 std::unique_ptr<proto::ChangesetMessage> changeset) override {
96 proto::TestChangesetMessage bm = changeset->test();
97
98 int child1_value = bm.value1();
99 if (child1_value != 0) {
100 child1_.ApplyChangeset(from, to, base::MakeUnique<int>(child1_value));
101 }
102
103 int child2_value = bm.value2();
104 if (child2_value != 0) {
105 child2_.ApplyChangeset(from, to, base::MakeUnique<int>(child2_value));
106 }
107 }
108
109 void ReleaseCheckpointsBefore(const VectorClock& checkpoint) override {
110 child1_.ReleaseCheckpointsBefore(checkpoint);
111 child2_.ReleaseCheckpointsBefore(checkpoint);
112 }
113
114 FakeIntSyncable& get_child1() { return child1_; }
115 FakeIntSyncable& get_child2() { return child2_; }
116
117 private:
118 FakeIntSyncable child1_;
119 FakeIntSyncable child2_;
120
121 DISALLOW_COPY_AND_ASSIGN(ParentObjectSyncable);
122 };
123
124 class SyncableTest : public testing::Test {
125 public:
126 SyncableTest()
127 : last_sync_local_(),
128 last_sync_remote_(),
129 parent_local_(VectorClock()),
130 parent_remote_(VectorClock()) {}
131
132 ~SyncableTest() override {}
133
134 void OnChangesetCreated(std::unique_ptr<proto::ChangesetMessage> changeset) {
135 VectorClock local_clock = parent_local_.clock();
136 VectorClock remote_clock = local_clock.Invert();
137
138 base::Closure done_cb =
139 base::Bind(&SyncableTest::OnChangesetApplied, base::Unretained(this));
140
141 parent_remote_.ApplyChangesetAsync(last_sync_remote_, remote_clock,
142 std::move(changeset), done_cb);
143 }
144
145 void OnChangesetApplied() {
146 EXPECT_EQ(123, parent_remote_.get_child1().value());
147 EXPECT_EQ(0, parent_remote_.get_child2().value());
148 }
149
150 protected:
151 VectorClock last_sync_local_;
152 VectorClock last_sync_remote_;
153 ParentObjectSyncable parent_local_;
154 ParentObjectSyncable parent_remote_;
155
156 private:
157 DISALLOW_COPY_AND_ASSIGN(SyncableTest);
158 };
159
160 TEST_F(SyncableTest, CreateAndApplyChangesetTestSync) {
161 // Lets modify a children object
162 parent_local_.get_child1().SetValue(123);
163
164 // At this point |child1| and |parentObject| should have its clock incremented
165 // whereas |child2| should still be the same.
166 EXPECT_TRUE(parent_local_.get_child1().ModifiedSince(last_sync_local_));
167 EXPECT_FALSE(parent_local_.get_child2().ModifiedSince(last_sync_local_));
168
169 std::unique_ptr<proto::ChangesetMessage> changeset =
170 parent_local_.CreateChangesetToCurrent(last_sync_local_);
171
172 VectorClock local_clock = parent_local_.clock();
173 VectorClock remote_clock = local_clock.Invert();
174
175 parent_remote_.ApplyChangeset(last_sync_remote_, remote_clock,
176 std::move(changeset));
177 last_sync_local_ = local_clock;
178 parent_local_.ReleaseCheckpointsBefore(local_clock);
179 EXPECT_FALSE(parent_local_.get_child1().ModifiedSince(last_sync_local_));
180 EXPECT_FALSE(parent_local_.get_child2().ModifiedSince(last_sync_local_));
181
182 EXPECT_EQ(123, parent_remote_.get_child1().value());
183 EXPECT_EQ(0, parent_remote_.get_child2().value());
184 }
185
186 TEST_F(SyncableTest, CreateAndApplyChangesetTestAsync) {
187 // Lets modify a children object
188 parent_local_.get_child1().SetValue(123);
189
190 ParentObjectSyncable::CreateChangesetCallback changeset_created_cb =
191 base::Bind(&SyncableTest::OnChangesetCreated, base::Unretained(this));
192
193 parent_local_.CreateChangesetToCurrentAsync(last_sync_local_,
194 changeset_created_cb);
195 }
196
197 } // namespace
198 } // namespace blimp
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698