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

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

Issue 2382533002: Helium: Initial proto and Syncable interface definition (Closed)
Patch Set: remainder stufft 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 Syncable and TwoPhaseSyncable
18
19 // For simplicity of this example, the ChangeSet will be an integer.
20 class FakeIntSyncable : public Syncable<int> {
21 public:
22 explicit FakeIntSyncable(VectorClockGenerator* clock_gen)
23 : Syncable<int>(), clock_gen_(clock_gen), value_(0) {
24 last_modified_ = clock_gen_->current();
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 clock_gen_->increment();
55 last_modified_ = clock_gen_->current();
56 }
57
58 int value() { return value_; }
59
60 private:
61 // The last time this object was changed
62 VectorClockGenerator* clock_gen_;
63 VectorClock last_modified_;
64 int32_t value_;
65
66 DISALLOW_COPY_AND_ASSIGN(FakeIntSyncable);
67 };
68
69 class ParentObjectSyncable : public TwoPhaseSyncable {
70 public:
71 explicit ParentObjectSyncable(VectorClockGenerator* clock_gen)
72 : TwoPhaseSyncable(), child1_(clock_gen), child2_(clock_gen) {}
73
74 std::unique_ptr<proto::ChangesetMessage> CreateChangesetToCurrent(
75 const VectorClock& from) override {
76 std::unique_ptr<proto::ChangesetMessage> changeset =
77 base::MakeUnique<proto::ChangesetMessage>();
78
79 proto::TestChangesetMessage* bm = changeset->mutable_test();
80
81 if (child1_.ModifiedSince(from)) {
82 std::unique_ptr<int> value1 = child1_.CreateChangesetToCurrent(from);
83 bm->set_value1(*value1);
84 }
85
86 if (child2_.ModifiedSince(from)) {
87 std::unique_ptr<int> value2 = child2_.CreateChangesetToCurrent(from);
88 bm->set_value2(*value2);
89 }
90
91 return changeset;
92 }
93
94 void ApplyChangeset(
95 const VectorClock& from,
96 const VectorClock& to,
97 std::unique_ptr<proto::ChangesetMessage> changeset) override {
98 proto::TestChangesetMessage bm = changeset->test();
99
100 int child1_value = bm.value1();
101 if (child1_value != 0) {
102 child1_.ApplyChangeset(from, to, base::MakeUnique<int>(child1_value));
103 }
104
105 int child2_value = bm.value2();
106 if (child2_value != 0) {
107 child2_.ApplyChangeset(from, to, base::MakeUnique<int>(child2_value));
108 }
109 }
110
111 void ReleaseCheckpointsBefore(const VectorClock& checkpoint) override {
112 child1_.ReleaseCheckpointsBefore(checkpoint);
113 child2_.ReleaseCheckpointsBefore(checkpoint);
114 }
115
116 bool ModifiedSince(const VectorClock& from) override {
117 return child1_.ModifiedSince(from) || child2_.ModifiedSince(from);
118 }
119
120 void PreCreateChangesetToCurrent(const VectorClock& from,
121 const base::Closure& done) override {}
122
123 void PostApplyChangeset(const VectorClock& from,
124 const VectorClock& to,
125 const base::Closure& done) override {}
126
127 FakeIntSyncable* get_mutable_child1() { return &child1_; }
128 FakeIntSyncable* get_mutable_child2() { return &child2_; }
129
130 private:
131 FakeIntSyncable child1_;
132 FakeIntSyncable child2_;
133
134 DISALLOW_COPY_AND_ASSIGN(ParentObjectSyncable);
135 };
136
137 class SyncableTest : public testing::Test {
138 public:
139 SyncableTest()
140 : clock_gen1_(base::MakeUnique<VectorClockGenerator>()),
141 clock_gen2_(base::MakeUnique<VectorClockGenerator>()),
142 last_sync_local_(clock_gen1_->current()),
143 last_sync_remote_(clock_gen2_->current()),
144 parent_local_(clock_gen1_.get()),
145 parent_remote_(clock_gen2_.get()) {}
146
147 ~SyncableTest() override {}
148
149 protected:
150 void changeChild(FakeIntSyncable* child1,
Kevin M 2016/10/05 23:41:30 Is the intention to keep these tests around, or ar
scf 2016/10/06 19:47:17 I remember @bgoldman saying he liked having the te
151 FakeIntSyncable* child2,
152 int value1_set,
153 int value1_get,
154 int value2_get) {
155 // Lets modify a children object
156 child1->SetValue(value1_set);
157
158 // At this point |child1| and |parent_local_| should have its clock
159 // incremented whereas |child2| should still be the same.
160 EXPECT_TRUE(child1->ModifiedSince(last_sync_local_));
161 EXPECT_FALSE(child2->ModifiedSince(last_sync_local_));
162
163 std::unique_ptr<proto::ChangesetMessage> changeset =
164 parent_local_.CreateChangesetToCurrent(last_sync_local_);
165
166 VectorClock local_clock = clock_gen1_->current();
167 VectorClock remote_clock = local_clock.Invert();
168
169 parent_remote_.ApplyChangeset(last_sync_remote_, remote_clock,
170 std::move(changeset));
171 last_sync_local_ = local_clock;
172 parent_local_.ReleaseCheckpointsBefore(local_clock);
173 EXPECT_FALSE(child1->ModifiedSince(last_sync_local_));
174 EXPECT_FALSE(child2->ModifiedSince(last_sync_local_));
175
176 EXPECT_EQ(value1_get, child1->value());
177 EXPECT_EQ(value2_get, child2->value());
178 }
179
180 std::unique_ptr<VectorClockGenerator> clock_gen1_;
181 std::unique_ptr<VectorClockGenerator> clock_gen2_;
182 VectorClock last_sync_local_;
183 VectorClock last_sync_remote_;
184 ParentObjectSyncable parent_local_;
185 ParentObjectSyncable parent_remote_;
186
187 private:
188 DISALLOW_COPY_AND_ASSIGN(SyncableTest);
189 };
190
191 TEST_F(SyncableTest, CreateAndApplyChangesetTest) {
192 changeChild(parent_local_.get_mutable_child1(),
193 parent_local_.get_mutable_child2(), 123, 123, 0);
194 changeChild(parent_local_.get_mutable_child2(),
195 parent_local_.get_mutable_child1(), 456, 456, 123);
196 }
197
198 } // namespace
199 } // namespace blimp
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698