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

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

Issue 2382533002: Helium: Initial proto and Syncable interface definition (Closed)
Patch Set: Initial proto and Syncable interface definition 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 // TODO(scf): Will delete this before checkin in. This is just to
17 // ilustrate a sample implementation. Will replace with mocks.
Brian Goldman 2016/09/29 19:44:23 Actually I favor keeping IntValueSyncable around f
18
19 // For simplicity of this example, the ChangeSet will be an integer.
20 class IntValueSyncable : public Syncable<int> {
21 public:
22 explicit IntValueSyncable(VectorClock* parent_clock)
23 : Syncable(parent_clock), value_(0) {
24 // We initialize the last time we modified with the parent clock
25 last_modified_ = Syncable::parent_clock();
26 }
27
28 bool ModifiedSince(const VectorClock& from) override {
29 // If something was modified from the last time we synchronized with
30 // my current clock.
31 return from.local_revision() < last_modified_.local_revision();
32 }
33
34 void CreateChangesetToCurrent(const VectorClock& from,
35 int* changeset) override {
36 *changeset = value_;
37 }
38
39 void ApplyChangeset(const VectorClock& from,
40 const VectorClock& to,
41 const int& changeset) override {
42 // Restore the value
43 value_ = changeset;
44 // Update our clock to the latest clock
45 last_modified_ = to;
46 }
47
48 void ReleaseCheckpointsBefore(const VectorClock& checkpoint) override {
49 last_modified_ = checkpoint;
50 }
51
52 void SetValue(int value) {
53 value_ = value;
54 // Increment the parent clock and update our last_modified_ value
55 last_modified_ = IncrementParentClock();
56 }
57
58 int get_value() { return value_; }
59
60 private:
61 // The last time this object was changed
62 VectorClock last_modified_;
63 int32_t value_;
64 };
65
66 class ParentObjectSyncable : public SyncableObject {
67 public:
68 explicit ParentObjectSyncable(VectorClock clock)
69 : SyncableObject(clock), child1_(&clock_), child2_(&clock_) {}
70
71 void CreateChangesetToCurrent(
72 const VectorClock& from,
73 const CreateChangesetCallback& response_callback) override {
74 std::unique_ptr<helium::Changeset> changeset =
75 base::MakeUnique<helium::Changeset>();
76
77 int child1_value = 0;
78 if (child1_.ModifiedSince(from)) {
79 child1_.CreateChangesetToCurrent(from, &child1_value);
80 }
81
82 int child2_value = 0;
83 if (child2_.ModifiedSince(from)) {
84 child2_.CreateChangesetToCurrent(from, &child2_value);
85 }
86
87 // HACK: For simplicity of this example I will encode the first
88 // value in |session_id| and the second in |message_id|
89 BlimpMessage* bm = changeset->mutable_legacy()->add_messages();
90 bm->set_session_id(child1_value);
91 bm->set_message_id(child2_value);
92
93 response_callback.Run(std::move(changeset));
94 }
95
96 void ApplyChangeset(const VectorClock& from,
97 const VectorClock& to,
98 const helium::Changeset& content,
99 const base::Closure& done) override {
100 BlimpMessage bm = content.legacy().messages(0);
101
102 // HACK: For simplicity of this example I will encode the first
103 // value in |session_id| and the second in |message_id|
104
105 int child1_value = bm.session_id();
106 if (child1_value != 0) {
107 child1_.ApplyChangeset(from, to, child1_value);
108 }
109
110 int child2_value = bm.message_id();
111 if (child2_value != 0) {
112 child2_.ApplyChangeset(from, to, child2_value);
113 }
114
115 done.Run();
116 }
117
118 void ReleaseCheckpointsBefore(const VectorClock& checkpoint) override {
119 child1_.ReleaseCheckpointsBefore(checkpoint);
120 child2_.ReleaseCheckpointsBefore(checkpoint);
121 }
122
123 IntValueSyncable& get_child1() { return child1_; }
124
125 IntValueSyncable& get_child2() { return child2_; }
126
127 private:
128 IntValueSyncable child1_;
129 IntValueSyncable child2_;
130 };
131
132 class SyncableTest : public testing::Test {
133 public:
134 SyncableTest()
135 : last_sync_local_(),
136 last_sync_remote_(),
137 parent_local_(last_sync_local_),
138 parent_remote_(last_sync_remote_) {}
139 ~SyncableTest() override {}
140
141 void OnLocalChangesetCreated(std::unique_ptr<helium::Changeset> changeset) {
142 VectorClock local_clock = parent_local_.get_clock();
143 VectorClock remote_clock = local_clock.Invert();
144 base::Closure done = base::Bind(&SyncableTest::OnRemoteChangesetApplied,
145 base::Unretained(this));
146 parent_remote_.ApplyChangeset(last_sync_remote_, remote_clock,
147 *(changeset.get()), done);
148 last_sync_local_ = local_clock;
149 parent_local_.ReleaseCheckpointsBefore(local_clock);
150 EXPECT_FALSE(parent_local_.get_child1().ModifiedSince(last_sync_local_));
151 EXPECT_FALSE(parent_local_.get_child2().ModifiedSince(last_sync_local_));
152 }
153
154 void OnRemoteChangesetApplied() {
155 EXPECT_EQ(123, parent_remote_.get_child1().get_value());
156 EXPECT_EQ(0, parent_remote_.get_child2().get_value());
157 }
158
159 protected:
160 VectorClock last_sync_local_;
161 VectorClock last_sync_remote_;
scf 2016/09/29 18:26:53 One thought also is have this bit of information i
162 ParentObjectSyncable parent_local_;
163 ParentObjectSyncable parent_remote_;
164
165 private:
166 DISALLOW_COPY_AND_ASSIGN(SyncableTest);
167 };
168
169 TEST_F(SyncableTest, Test1) {
CJ 2016/09/29 20:36:55 Usually we want to give our Test names a little mo
scf 2016/09/29 23:13:39 Good catch. I'm not really testing much here other
170 // Lets modify a children object
171 parent_local_.get_child1().SetValue(123);
172
173 // At this point |child1| and |parentObject| should have its clock incremented
174 // whereas |child2| should still be the same.
175 EXPECT_TRUE(parent_local_.get_child1().ModifiedSince(last_sync_local_));
176 EXPECT_FALSE(parent_local_.get_child2().ModifiedSince(last_sync_local_));
177
178 ParentObjectSyncable::CreateChangesetCallback callback = base::Bind(
179 &SyncableTest::OnLocalChangesetCreated, base::Unretained(this));
180 parent_local_.CreateChangesetToCurrent(last_sync_local_, callback);
181 }
182
183 } // namespace
184 } // namespace blimp
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698