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

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

Issue 2382533002: Helium: Initial proto and Syncable interface definition (Closed)
Patch Set: merging with master 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
« no previous file with comments | « blimp/net/helium/syncable.h ('k') | blimp/net/helium/vector_clock.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 "blimp/common/mandatory_callback.h"
11 #include "testing/gmock/include/gmock/gmock.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13
14 namespace blimp {
15 namespace {
16
17 // This is a sample implementation that demostrates the implementation
18 // of the Syncable and TwoPhaseSyncable
19
20 // For simplicity of this example, the ChangeSet will be an integer.
21 class FakeIntSyncable : public Syncable<int> {
22 public:
23 explicit FakeIntSyncable(VectorClockGenerator* clock_gen)
24 : Syncable<int>(), clock_gen_(clock_gen), value_(0) {
25 last_modified_ = clock_gen_->current();
26 }
27
28 bool ModifiedSince(const VectorClock& from) const override {
29 return from.local_revision() < last_modified_.local_revision();
30 }
31
32 std::unique_ptr<int> CreateChangesetToCurrent(
33 const VectorClock& from) override {
34 return base::MakeUnique<int>(value_);
35 }
36
37 void ApplyChangeset(const VectorClock& from,
38 const VectorClock& to,
39 std::unique_ptr<int> changeset) override {
40 // Restore the value
41 value_ = *changeset;
42
43 // Update our clock to the latest clock
44 last_modified_ = to;
45 }
46
47 void ReleaseCheckpointsBefore(const VectorClock& checkpoint) override {
48 last_modified_ = checkpoint;
49 }
50
51 void SetValue(int value) {
52 value_ = value;
53
54 // Increment the parent clock and update our last_modified_ value
55 clock_gen_->Increment();
56 last_modified_ = clock_gen_->current();
57 }
58
59 int value() { return value_; }
60
61 private:
62 // The last time this object was changed
63 VectorClockGenerator* clock_gen_;
64 VectorClock last_modified_;
65 int32_t value_;
66
67 DISALLOW_COPY_AND_ASSIGN(FakeIntSyncable);
68 };
69
70 class ParentObjectSyncable : public TwoPhaseSyncable {
71 public:
72 explicit ParentObjectSyncable(VectorClockGenerator* clock_gen)
73 : TwoPhaseSyncable(), child1_(clock_gen), child2_(clock_gen) {}
74
75 std::unique_ptr<proto::ChangesetMessage> CreateChangesetToCurrent(
76 const VectorClock& from) override {
77 std::unique_ptr<proto::ChangesetMessage> changeset =
78 base::MakeUnique<proto::ChangesetMessage>();
79
80 proto::TestChangesetMessage* bm = changeset->mutable_test();
81
82 if (child1_.ModifiedSince(from)) {
83 std::unique_ptr<int> value1 = child1_.CreateChangesetToCurrent(from);
84 bm->set_value1(*value1);
85 }
86
87 if (child2_.ModifiedSince(from)) {
88 std::unique_ptr<int> value2 = child2_.CreateChangesetToCurrent(from);
89 bm->set_value2(*value2);
90 }
91
92 return changeset;
93 }
94
95 void ApplyChangeset(
96 const VectorClock& from,
97 const VectorClock& to,
98 std::unique_ptr<proto::ChangesetMessage> changeset) override {
99 proto::TestChangesetMessage bm = changeset->test();
100
101 int child1_value = bm.value1();
102 if (child1_value != 0) {
103 child1_.ApplyChangeset(from, to, base::MakeUnique<int>(child1_value));
104 }
105
106 int child2_value = bm.value2();
107 if (child2_value != 0) {
108 child2_.ApplyChangeset(from, to, base::MakeUnique<int>(child2_value));
109 }
110 }
111
112 void ReleaseCheckpointsBefore(const VectorClock& checkpoint) override {
113 child1_.ReleaseCheckpointsBefore(checkpoint);
114 child2_.ReleaseCheckpointsBefore(checkpoint);
115 }
116
117 bool ModifiedSince(const VectorClock& from) const override {
118 return child1_.ModifiedSince(from) || child2_.ModifiedSince(from);
119 }
120
121 void PreCreateChangesetToCurrent(const VectorClock& from,
122 MandatoryClosure&& done) override {
123 done.Run();
124 }
125
126 void PostApplyChangeset(const VectorClock& from,
127 const VectorClock& to,
128 MandatoryClosure&& done) override {
129 done.Run();
130 }
131
132 FakeIntSyncable* get_mutable_child1() { return &child1_; }
133 FakeIntSyncable* get_mutable_child2() { return &child2_; }
134
135 private:
136 FakeIntSyncable child1_;
137 FakeIntSyncable child2_;
138
139 DISALLOW_COPY_AND_ASSIGN(ParentObjectSyncable);
140 };
141
142 class SyncableTest : public testing::Test {
143 public:
144 SyncableTest()
145 : clock_gen1_(base::MakeUnique<VectorClockGenerator>()),
146 clock_gen2_(base::MakeUnique<VectorClockGenerator>()),
147 last_sync_local_(clock_gen1_->current()),
148 last_sync_remote_(clock_gen2_->current()),
149 parent_local_(clock_gen1_.get()),
150 parent_remote_(clock_gen2_.get()) {}
151
152 ~SyncableTest() override {}
153
154 protected:
155 void changeChild(FakeIntSyncable* child1,
Kevin M 2016/10/06 23:26:19 This function does too much and has a name that's
156 FakeIntSyncable* child2,
157 int value1_set,
158 int value1_get,
159 int value2_get) {
160 // Lets modify a children object
161 child1->SetValue(value1_set);
162
163 // At this point |child1| and |parent_local_| should have its clock
164 // incremented whereas |child2| should still be the same.
165 EXPECT_TRUE(child1->ModifiedSince(last_sync_local_));
166 EXPECT_FALSE(child2->ModifiedSince(last_sync_local_));
167
168 std::unique_ptr<proto::ChangesetMessage> changeset =
169 parent_local_.CreateChangesetToCurrent(last_sync_local_);
170
171 VectorClock local_clock = clock_gen1_->current();
172 VectorClock remote_clock = local_clock.Invert();
173
174 parent_remote_.ApplyChangeset(last_sync_remote_, remote_clock,
175 std::move(changeset));
176 last_sync_local_ = local_clock;
177 parent_local_.ReleaseCheckpointsBefore(local_clock);
178 EXPECT_FALSE(child1->ModifiedSince(last_sync_local_));
179 EXPECT_FALSE(child2->ModifiedSince(last_sync_local_));
180
181 EXPECT_EQ(value1_get, child1->value());
182 EXPECT_EQ(value2_get, child2->value());
183 }
184
185 std::unique_ptr<VectorClockGenerator> clock_gen1_;
186 std::unique_ptr<VectorClockGenerator> clock_gen2_;
187 VectorClock last_sync_local_;
188 VectorClock last_sync_remote_;
189 ParentObjectSyncable parent_local_;
190 ParentObjectSyncable parent_remote_;
191
192 private:
193 DISALLOW_COPY_AND_ASSIGN(SyncableTest);
194 };
195
196 TEST_F(SyncableTest, CreateAndApplyChangesetTest) {
197 changeChild(parent_local_.get_mutable_child1(),
198 parent_local_.get_mutable_child2(), 123, 123, 0);
199 changeChild(parent_local_.get_mutable_child2(),
200 parent_local_.get_mutable_child1(), 456, 456, 123);
201 }
202
203 } // namespace
204 } // namespace blimp
OLDNEW
« no previous file with comments | « blimp/net/helium/syncable.h ('k') | blimp/net/helium/vector_clock.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698