Chromium Code Reviews| 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..d18208cdcbea9880a0d1c088e3bd82f3532dbf74 |
| --- /dev/null |
| +++ b/blimp/net/helium/syncable_unittest.cc |
| @@ -0,0 +1,204 @@ |
| +// 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/macros.h" |
| +#include "base/memory/ptr_util.h" |
| +#include "blimp/common/mandatory_callback.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 Syncable and TwoPhaseSyncable |
| + |
| +// For simplicity of this example, the ChangeSet will be an integer. |
| +class FakeIntSyncable : public Syncable<int> { |
| + public: |
| + explicit FakeIntSyncable(VectorClockGenerator* clock_gen) |
| + : Syncable<int>(), clock_gen_(clock_gen), value_(0) { |
| + last_modified_ = clock_gen_->current(); |
| + } |
| + |
| + bool ModifiedSince(const VectorClock& from) const override { |
| + return from.local_revision() < last_modified_.local_revision(); |
| + } |
| + |
| + std::unique_ptr<int> CreateChangesetToCurrent( |
| + const VectorClock& from) override { |
| + return base::MakeUnique<int>(value_); |
| + } |
| + |
| + void ApplyChangeset(const VectorClock& from, |
| + const VectorClock& to, |
| + std::unique_ptr<int> changeset) override { |
| + // Restore the value |
| + value_ = *changeset; |
| + |
| + // Update our clock to the latest clock |
| + last_modified_ = to; |
| + } |
| + |
| + 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 |
| + clock_gen_->Increment(); |
| + last_modified_ = clock_gen_->current(); |
| + } |
| + |
| + int value() { return value_; } |
| + |
| + private: |
| + // The last time this object was changed |
| + VectorClockGenerator* clock_gen_; |
| + VectorClock last_modified_; |
| + int32_t value_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(FakeIntSyncable); |
| +}; |
| + |
| +class ParentObjectSyncable : public TwoPhaseSyncable { |
| + public: |
| + explicit ParentObjectSyncable(VectorClockGenerator* clock_gen) |
| + : TwoPhaseSyncable(), child1_(clock_gen), child2_(clock_gen) {} |
| + |
| + std::unique_ptr<proto::ChangesetMessage> CreateChangesetToCurrent( |
| + const VectorClock& from) override { |
| + std::unique_ptr<proto::ChangesetMessage> changeset = |
| + base::MakeUnique<proto::ChangesetMessage>(); |
| + |
| + proto::TestChangesetMessage* bm = changeset->mutable_test(); |
| + |
| + if (child1_.ModifiedSince(from)) { |
| + std::unique_ptr<int> value1 = child1_.CreateChangesetToCurrent(from); |
| + bm->set_value1(*value1); |
| + } |
| + |
| + if (child2_.ModifiedSince(from)) { |
| + std::unique_ptr<int> value2 = child2_.CreateChangesetToCurrent(from); |
| + bm->set_value2(*value2); |
| + } |
| + |
| + return changeset; |
| + } |
| + |
| + void ApplyChangeset( |
| + const VectorClock& from, |
| + const VectorClock& to, |
| + std::unique_ptr<proto::ChangesetMessage> changeset) override { |
| + proto::TestChangesetMessage bm = changeset->test(); |
| + |
| + int child1_value = bm.value1(); |
| + if (child1_value != 0) { |
| + child1_.ApplyChangeset(from, to, base::MakeUnique<int>(child1_value)); |
| + } |
| + |
| + int child2_value = bm.value2(); |
| + if (child2_value != 0) { |
| + child2_.ApplyChangeset(from, to, base::MakeUnique<int>(child2_value)); |
| + } |
| + } |
| + |
| + void ReleaseCheckpointsBefore(const VectorClock& checkpoint) override { |
| + child1_.ReleaseCheckpointsBefore(checkpoint); |
| + child2_.ReleaseCheckpointsBefore(checkpoint); |
| + } |
| + |
| + bool ModifiedSince(const VectorClock& from) const override { |
| + return child1_.ModifiedSince(from) || child2_.ModifiedSince(from); |
| + } |
| + |
| + void PreCreateChangesetToCurrent(const VectorClock& from, |
| + MandatoryClosure&& done) override { |
| + done.Run(); |
| + } |
| + |
| + void PostApplyChangeset(const VectorClock& from, |
| + const VectorClock& to, |
| + MandatoryClosure&& done) override { |
| + done.Run(); |
| + } |
| + |
| + FakeIntSyncable* get_mutable_child1() { return &child1_; } |
| + FakeIntSyncable* get_mutable_child2() { return &child2_; } |
| + |
| + private: |
| + FakeIntSyncable child1_; |
| + FakeIntSyncable child2_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(ParentObjectSyncable); |
| +}; |
| + |
| +class SyncableTest : public testing::Test { |
| + public: |
| + SyncableTest() |
| + : clock_gen1_(base::MakeUnique<VectorClockGenerator>()), |
| + clock_gen2_(base::MakeUnique<VectorClockGenerator>()), |
| + last_sync_local_(clock_gen1_->current()), |
| + last_sync_remote_(clock_gen2_->current()), |
| + parent_local_(clock_gen1_.get()), |
| + parent_remote_(clock_gen2_.get()) {} |
| + |
| + ~SyncableTest() override {} |
| + |
| + protected: |
| + void changeChild(FakeIntSyncable* child1, |
|
Kevin M
2016/10/06 23:26:19
This function does too much and has a name that's
|
| + FakeIntSyncable* child2, |
| + int value1_set, |
| + int value1_get, |
| + int value2_get) { |
| + // Lets modify a children object |
| + child1->SetValue(value1_set); |
| + |
| + // At this point |child1| and |parent_local_| should have its clock |
| + // incremented whereas |child2| should still be the same. |
| + EXPECT_TRUE(child1->ModifiedSince(last_sync_local_)); |
| + EXPECT_FALSE(child2->ModifiedSince(last_sync_local_)); |
| + |
| + std::unique_ptr<proto::ChangesetMessage> changeset = |
| + parent_local_.CreateChangesetToCurrent(last_sync_local_); |
| + |
| + VectorClock local_clock = clock_gen1_->current(); |
| + VectorClock remote_clock = local_clock.Invert(); |
| + |
| + parent_remote_.ApplyChangeset(last_sync_remote_, remote_clock, |
| + std::move(changeset)); |
| + last_sync_local_ = local_clock; |
| + parent_local_.ReleaseCheckpointsBefore(local_clock); |
| + EXPECT_FALSE(child1->ModifiedSince(last_sync_local_)); |
| + EXPECT_FALSE(child2->ModifiedSince(last_sync_local_)); |
| + |
| + EXPECT_EQ(value1_get, child1->value()); |
| + EXPECT_EQ(value2_get, child2->value()); |
| + } |
| + |
| + std::unique_ptr<VectorClockGenerator> clock_gen1_; |
| + std::unique_ptr<VectorClockGenerator> clock_gen2_; |
| + VectorClock last_sync_local_; |
| + VectorClock last_sync_remote_; |
| + ParentObjectSyncable parent_local_; |
| + ParentObjectSyncable parent_remote_; |
| + |
| + private: |
| + DISALLOW_COPY_AND_ASSIGN(SyncableTest); |
| +}; |
| + |
| +TEST_F(SyncableTest, CreateAndApplyChangesetTest) { |
| + changeChild(parent_local_.get_mutable_child1(), |
| + parent_local_.get_mutable_child2(), 123, 123, 0); |
| + changeChild(parent_local_.get_mutable_child2(), |
| + parent_local_.get_mutable_child1(), 456, 456, 123); |
| +} |
| + |
| +} // namespace |
| +} // namespace blimp |