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

Unified Diff: blimp/net/helium/syncable_unittest.cc

Issue 2382533002: Helium: Initial proto and Syncable interface definition (Closed)
Patch Set: Couple minor tweaks Created 4 years, 3 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 side-by-side diff with in-line comments
Download patch
Index: blimp/net/helium/syncable_unittest.cc
diff --git a/blimp/net/helium/syncable_unittest.cc b/blimp/net/helium/syncable_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..297f908db273fbc44402ec30634ec31558d34a08
--- /dev/null
+++ b/blimp/net/helium/syncable_unittest.cc
@@ -0,0 +1,202 @@
+// Copyright 2016 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "blimp/net/helium/syncable.h"
+
+#include "base/bind.h"
+#include "base/callback.h"
+#include "base/macros.h"
+#include "base/memory/ptr_util.h"
+#include "testing/gmock/include/gmock/gmock.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace blimp {
+namespace {
+
+// This is a sample implementation that demostrates the implementation
+// of the SyncableMember and SyncableObject
+
+// For simplicity of this example, the ChangeSet will be an integer.
+class IntValueSyncable : public SyncableMember<int> {
+ public:
+ explicit IntValueSyncable(SyncableObject* parent)
+ : SyncableMember(parent), value_(0) {
+ // 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.
+ last_modified_ = parent_->get_clock();
+ }
+
+ bool ModifiedSince(const VectorClock& from) override {
+ // If something was modified from the last time we synchronized with
+ // 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.
+ return from.local_revision() < last_modified_.local_revision();
+ }
+
+ void CreateChangesetToCurrent(
+ const VectorClock& from,
+ const CreateChangesetCallback& callback) override {
+ std::unique_ptr<int> changeset = base::MakeUnique<int>();
+ *changeset = value_;
+ callback.Run(std::move(changeset));
+ }
+
+ void ApplyChangeset(const VectorClock& from,
+ const VectorClock& to,
+ const int& changeset,
+ const base::Closure& done) override {
+ // Restore the value
+ value_ = changeset;
+ // 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.
+ last_modified_ = to;
+
+ done.Run();
+ }
+
+ void ReleaseCheckpointsBefore(const VectorClock& checkpoint) override {
+ last_modified_ = checkpoint;
+ }
+
+ void SetValue(int value) {
+ value_ = value;
+ // 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.
+ last_modified_ = IncrementParentClock();
+ }
+
+ int get_value() { return value_; }
Kevin M 2016/09/30 18:34:37 value()
scf 2016/09/30 22:13:24 Done.
+
+ private:
+ // The last time this object was changed
+ VectorClock last_modified_;
+ int32_t value_;
+};
+
+class ParentObjectSyncable : public SyncableObject {
+ public:
+ explicit ParentObjectSyncable(VectorClock clock)
+ : SyncableObject(clock), child1_(this), child2_(this) {}
+
+ void CreateChangesetToCurrent(
+ const VectorClock& from,
+ const CreateChangesetCallback& response_callback) override {
+ std::unique_ptr<helium::Changeset> changeset =
+ base::MakeUnique<helium::Changeset>();
+
+ 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.
+ if (child1_.ModifiedSince(from)) {
+ IntValueSyncable::CreateChangesetCallback callback =
+ base::Bind(&ParentObjectSyncable::OnChildChangesetCreated,
+ base::Unretained(this), &value1);
+ child1_.CreateChangesetToCurrent(from, callback);
+ }
+
+ int value2 = 0;
+ if (child2_.ModifiedSince(from)) {
+ IntValueSyncable::CreateChangesetCallback callback =
+ base::Bind(&ParentObjectSyncable::OnChildChangesetCreated,
+ base::Unretained(this), &value2);
+ child2_.CreateChangesetToCurrent(from, callback);
+ }
+
+ // HACK2: Since we know about the internals of IntValueSyncable we can
+ // safely access value1 and value2 since the calls are blocking
+ helium::TestChangeset* bm = changeset->mutable_test();
+ bm->set_value1(value1);
+ bm->set_value2(value2);
+
+ response_callback.Run(std::move(changeset));
+ }
+
+ void OnChildChangesetCreated(int* out, std::unique_ptr<int> value) {
+ *out = *value;
+ }
+
+ void ApplyChangeset(const VectorClock& from,
+ const VectorClock& to,
+ const helium::Changeset& content,
+ const base::Closure& done) override {
+ helium::TestChangeset bm = content.test();
+
+ base::Closure dummy =
+ 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.
+
+ int child1_value = bm.value1();
+ if (child1_value != 0) {
+ child1_.ApplyChangeset(from, to, child1_value, dummy);
+ }
+
+ int child2_value = bm.value2();
+ if (child2_value != 0) {
+ child2_.ApplyChangeset(from, to, child2_value, dummy);
+ }
+
+ done.Run();
+ }
+
+ void OnDummy() {}
+
+ void ReleaseCheckpointsBefore(const VectorClock& checkpoint) override {
+ child1_.ReleaseCheckpointsBefore(checkpoint);
+ child2_.ReleaseCheckpointsBefore(checkpoint);
+ }
+
+ IntValueSyncable& get_child1() { return child1_; }
+ IntValueSyncable& get_child2() { return child2_; }
+
+ private:
+ IntValueSyncable child1_;
+ IntValueSyncable child2_;
Kevin M 2016/09/30 18:34:37 DISALLOW yada yada
scf 2016/09/30 22:13:24 Done.
+};
+
+class SyncableTest : public testing::Test {
+ public:
+ SyncableTest()
+ : last_sync_local_(),
+ last_sync_remote_(),
+ parent_local_(last_sync_local_),
+ 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.
+ ~SyncableTest() override {}
+
+ void OnLocalChangesetCreated(std::unique_ptr<helium::Changeset> changeset) {
+ VectorClock local_clock = parent_local_.get_clock();
+ VectorClock remote_clock = local_clock.Invert();
+ base::Closure done = base::Bind(&SyncableTest::OnRemoteChangesetApplied,
+ base::Unretained(this));
+ parent_remote_.ApplyChangeset(last_sync_remote_, remote_clock,
+ *(changeset.get()), done);
+ last_sync_local_ = local_clock;
+ parent_local_.ReleaseCheckpointsBefore(local_clock);
+ EXPECT_FALSE(parent_local_.get_child1().ModifiedSince(last_sync_local_));
+ EXPECT_FALSE(parent_local_.get_child2().ModifiedSince(last_sync_local_));
+ }
+
+ void OnRemoteChangesetApplied() {
+ EXPECT_EQ(123, parent_remote_.get_child1().get_value());
+ EXPECT_EQ(0, parent_remote_.get_child2().get_value());
+ }
+
+ protected:
+ VectorClock last_sync_local_;
+ VectorClock last_sync_remote_;
+ ParentObjectSyncable parent_local_;
+ ParentObjectSyncable parent_remote_;
+
+ private:
+ DISALLOW_COPY_AND_ASSIGN(SyncableTest);
+};
+
+TEST_F(SyncableTest, DemoAPITest) {
+ // Lets modify a children object
+ parent_local_.get_child1().SetValue(123);
+
+ // At this point |child1| and |parentObject| should have its clock incremented
+ // whereas |child2| should still be the same.
+ EXPECT_TRUE(parent_local_.get_child1().ModifiedSince(last_sync_local_));
+ EXPECT_FALSE(parent_local_.get_child2().ModifiedSince(last_sync_local_));
+
+ ParentObjectSyncable::CreateChangesetCallback callback = base::Bind(
+ &SyncableTest::OnLocalChangesetCreated, base::Unretained(this));
+ parent_local_.CreateChangesetToCurrent(last_sync_local_, callback);
+}
+
+} // namespace
+} // namespace blimp

Powered by Google App Engine
This is Rietveld 408576698