Chromium Code Reviews| OLD | NEW |
|---|---|
| (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/callback.h" | |
| 9 #include "base/macros.h" | |
| 10 #include "base/memory/ptr_util.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 SyncableMember and SyncableObject | |
| 19 | |
| 20 // For simplicity of this example, the ChangeSet will be an integer. | |
| 21 class IntValueSyncable : public SyncableMember<int> { | |
| 22 public: | |
| 23 explicit IntValueSyncable(SyncableObject* parent) | |
| 24 : SyncableMember(parent), value_(0) { | |
| 25 // We initialize the last time we modified with the parent clock | |
|
Kevin M
2016/09/30 18:34:37
This is already made pretty clear by the code - re
scf
2016/09/30 22:13:24
Done.
| |
| 26 last_modified_ = parent_->get_clock(); | |
| 27 } | |
| 28 | |
| 29 bool ModifiedSince(const VectorClock& from) override { | |
| 30 // If something was modified from the last time we synchronized with | |
| 31 // my current clock. | |
|
Kevin M
2016/09/30 18:34:37
"my"?
Also, the sentence doesn't parse.
scf
2016/09/30 22:13:24
Done.
| |
| 32 return from.local_revision() < last_modified_.local_revision(); | |
| 33 } | |
| 34 | |
| 35 void CreateChangesetToCurrent( | |
| 36 const VectorClock& from, | |
| 37 const CreateChangesetCallback& callback) override { | |
| 38 std::unique_ptr<int> changeset = base::MakeUnique<int>(); | |
| 39 *changeset = value_; | |
| 40 callback.Run(std::move(changeset)); | |
| 41 } | |
| 42 | |
| 43 void ApplyChangeset(const VectorClock& from, | |
| 44 const VectorClock& to, | |
| 45 const int& changeset, | |
| 46 const base::Closure& done) override { | |
| 47 // Restore the value | |
| 48 value_ = changeset; | |
| 49 // Update our clock to the latest clock | |
|
Kevin M
2016/09/30 18:34:37
add newline above
scf
2016/09/30 22:13:24
Done.
| |
| 50 last_modified_ = to; | |
| 51 | |
| 52 done.Run(); | |
| 53 } | |
| 54 | |
| 55 void ReleaseCheckpointsBefore(const VectorClock& checkpoint) override { | |
| 56 last_modified_ = checkpoint; | |
| 57 } | |
| 58 | |
| 59 void SetValue(int value) { | |
| 60 value_ = value; | |
| 61 // Increment the parent clock and update our last_modified_ value | |
|
Kevin M
2016/09/30 18:34:37
add newline above
scf
2016/09/30 22:13:24
Done.
| |
| 62 last_modified_ = IncrementParentClock(); | |
| 63 } | |
| 64 | |
| 65 int get_value() { return value_; } | |
|
Kevin M
2016/09/30 18:34:37
value()
scf
2016/09/30 22:13:24
Done.
| |
| 66 | |
| 67 private: | |
| 68 // The last time this object was changed | |
| 69 VectorClock last_modified_; | |
| 70 int32_t value_; | |
| 71 }; | |
| 72 | |
| 73 class ParentObjectSyncable : public SyncableObject { | |
| 74 public: | |
| 75 explicit ParentObjectSyncable(VectorClock clock) | |
| 76 : SyncableObject(clock), child1_(this), child2_(this) {} | |
| 77 | |
| 78 void CreateChangesetToCurrent( | |
| 79 const VectorClock& from, | |
| 80 const CreateChangesetCallback& response_callback) override { | |
| 81 std::unique_ptr<helium::Changeset> changeset = | |
| 82 base::MakeUnique<helium::Changeset>(); | |
| 83 | |
| 84 int value1 = 0; | |
|
Kevin M
2016/09/30 18:34:37
In practice, we will only be setting values in the
scf
2016/09/30 22:13:24
Done.
| |
| 85 if (child1_.ModifiedSince(from)) { | |
| 86 IntValueSyncable::CreateChangesetCallback callback = | |
| 87 base::Bind(&ParentObjectSyncable::OnChildChangesetCreated, | |
| 88 base::Unretained(this), &value1); | |
| 89 child1_.CreateChangesetToCurrent(from, callback); | |
| 90 } | |
| 91 | |
| 92 int value2 = 0; | |
| 93 if (child2_.ModifiedSince(from)) { | |
| 94 IntValueSyncable::CreateChangesetCallback callback = | |
| 95 base::Bind(&ParentObjectSyncable::OnChildChangesetCreated, | |
| 96 base::Unretained(this), &value2); | |
| 97 child2_.CreateChangesetToCurrent(from, callback); | |
| 98 } | |
| 99 | |
| 100 // HACK2: Since we know about the internals of IntValueSyncable we can | |
| 101 // safely access value1 and value2 since the calls are blocking | |
| 102 helium::TestChangeset* bm = changeset->mutable_test(); | |
| 103 bm->set_value1(value1); | |
| 104 bm->set_value2(value2); | |
| 105 | |
| 106 response_callback.Run(std::move(changeset)); | |
| 107 } | |
| 108 | |
| 109 void OnChildChangesetCreated(int* out, std::unique_ptr<int> value) { | |
| 110 *out = *value; | |
| 111 } | |
| 112 | |
| 113 void ApplyChangeset(const VectorClock& from, | |
| 114 const VectorClock& to, | |
| 115 const helium::Changeset& content, | |
| 116 const base::Closure& done) override { | |
| 117 helium::TestChangeset bm = content.test(); | |
| 118 | |
| 119 base::Closure dummy = | |
| 120 base::Bind(&ParentObjectSyncable::OnDummy, base::Unretained(this)); | |
|
Kevin M
2016/09/30 18:34:37
base::DoNothing
scf
2016/09/30 22:13:24
Done.
| |
| 121 | |
| 122 int child1_value = bm.value1(); | |
| 123 if (child1_value != 0) { | |
| 124 child1_.ApplyChangeset(from, to, child1_value, dummy); | |
| 125 } | |
| 126 | |
| 127 int child2_value = bm.value2(); | |
| 128 if (child2_value != 0) { | |
| 129 child2_.ApplyChangeset(from, to, child2_value, dummy); | |
| 130 } | |
| 131 | |
| 132 done.Run(); | |
| 133 } | |
| 134 | |
| 135 void OnDummy() {} | |
| 136 | |
| 137 void ReleaseCheckpointsBefore(const VectorClock& checkpoint) override { | |
| 138 child1_.ReleaseCheckpointsBefore(checkpoint); | |
| 139 child2_.ReleaseCheckpointsBefore(checkpoint); | |
| 140 } | |
| 141 | |
| 142 IntValueSyncable& get_child1() { return child1_; } | |
| 143 IntValueSyncable& get_child2() { return child2_; } | |
| 144 | |
| 145 private: | |
| 146 IntValueSyncable child1_; | |
| 147 IntValueSyncable child2_; | |
|
Kevin M
2016/09/30 18:34:37
DISALLOW yada yada
scf
2016/09/30 22:13:24
Done.
| |
| 148 }; | |
| 149 | |
| 150 class SyncableTest : public testing::Test { | |
| 151 public: | |
| 152 SyncableTest() | |
| 153 : last_sync_local_(), | |
| 154 last_sync_remote_(), | |
| 155 parent_local_(last_sync_local_), | |
| 156 parent_remote_(last_sync_remote_) {} | |
|
Kevin M
2016/09/30 18:34:37
add newline below this
scf
2016/09/30 22:13:24
Done.
| |
| 157 ~SyncableTest() override {} | |
| 158 | |
| 159 void OnLocalChangesetCreated(std::unique_ptr<helium::Changeset> changeset) { | |
| 160 VectorClock local_clock = parent_local_.get_clock(); | |
| 161 VectorClock remote_clock = local_clock.Invert(); | |
| 162 base::Closure done = base::Bind(&SyncableTest::OnRemoteChangesetApplied, | |
| 163 base::Unretained(this)); | |
| 164 parent_remote_.ApplyChangeset(last_sync_remote_, remote_clock, | |
| 165 *(changeset.get()), done); | |
| 166 last_sync_local_ = local_clock; | |
| 167 parent_local_.ReleaseCheckpointsBefore(local_clock); | |
| 168 EXPECT_FALSE(parent_local_.get_child1().ModifiedSince(last_sync_local_)); | |
| 169 EXPECT_FALSE(parent_local_.get_child2().ModifiedSince(last_sync_local_)); | |
| 170 } | |
| 171 | |
| 172 void OnRemoteChangesetApplied() { | |
| 173 EXPECT_EQ(123, parent_remote_.get_child1().get_value()); | |
| 174 EXPECT_EQ(0, parent_remote_.get_child2().get_value()); | |
| 175 } | |
| 176 | |
| 177 protected: | |
| 178 VectorClock last_sync_local_; | |
| 179 VectorClock last_sync_remote_; | |
| 180 ParentObjectSyncable parent_local_; | |
| 181 ParentObjectSyncable parent_remote_; | |
| 182 | |
| 183 private: | |
| 184 DISALLOW_COPY_AND_ASSIGN(SyncableTest); | |
| 185 }; | |
| 186 | |
| 187 TEST_F(SyncableTest, DemoAPITest) { | |
| 188 // Lets modify a children object | |
| 189 parent_local_.get_child1().SetValue(123); | |
| 190 | |
| 191 // At this point |child1| and |parentObject| should have its clock incremented | |
| 192 // whereas |child2| should still be the same. | |
| 193 EXPECT_TRUE(parent_local_.get_child1().ModifiedSince(last_sync_local_)); | |
| 194 EXPECT_FALSE(parent_local_.get_child2().ModifiedSince(last_sync_local_)); | |
| 195 | |
| 196 ParentObjectSyncable::CreateChangesetCallback callback = base::Bind( | |
| 197 &SyncableTest::OnLocalChangesetCreated, base::Unretained(this)); | |
| 198 parent_local_.CreateChangesetToCurrent(last_sync_local_, callback); | |
| 199 } | |
| 200 | |
| 201 } // namespace | |
| 202 } // namespace blimp | |
| OLD | NEW |