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/helium/compound_syncable.h" |
| 6 |
| 7 #include <string> |
| 8 #include <utility> |
| 9 |
| 10 #include "base/bind.h" |
| 11 #include "base/macros.h" |
| 12 #include "blimp/helium/lww_register.h" |
| 13 #include "blimp/helium/revision_generator.h" |
| 14 #include "testing/gmock/include/gmock/gmock.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" |
| 16 #include "third_party/protobuf/src/google/protobuf/io/coded_stream.h" |
| 17 #include "third_party/protobuf/src/google/protobuf/io/zero_copy_stream_impl_lite
.h" |
| 18 |
| 19 namespace blimp { |
| 20 namespace helium { |
| 21 namespace { |
| 22 |
| 23 class SampleCompoundSyncable : public CompoundSyncable { |
| 24 public: |
| 25 explicit SampleCompoundSyncable(Peer bias, Peer running_as) |
| 26 : child1_( |
| 27 CreateAndRegister<LwwRegister<int>>(bias, running_as)), |
| 28 child2_( |
| 29 CreateAndRegister<LwwRegister<int>>(bias, running_as)) {} |
| 30 |
| 31 LwwRegister<int>* mutable_child1() { return child1_.get(); } |
| 32 LwwRegister<int>* mutable_child2() { return child2_.get(); } |
| 33 |
| 34 private: |
| 35 RegisteredMember<LwwRegister<int>> child1_; |
| 36 RegisteredMember<LwwRegister<int>> child2_; |
| 37 |
| 38 DISALLOW_COPY_AND_ASSIGN(SampleCompoundSyncable); |
| 39 }; |
| 40 |
| 41 class CompoundSyncableTest : public testing::Test { |
| 42 public: |
| 43 CompoundSyncableTest() |
| 44 : last_sync_engine_(0), |
| 45 engine_(Peer::ENGINE, Peer::ENGINE), |
| 46 client_(Peer::ENGINE, Peer::CLIENT) {} |
| 47 |
| 48 ~CompoundSyncableTest() override {} |
| 49 |
| 50 protected: |
| 51 // Propagates pending changes from |engine_| to |client_|. |
| 52 void Synchronize() { |
| 53 // Create the changeset stream from |engine_|. |
| 54 std::string changeset; |
| 55 google::protobuf::io::StringOutputStream raw_output_stream(&changeset); |
| 56 google::protobuf::io::CodedOutputStream output_stream(&raw_output_stream); |
| 57 |
| 58 engine_.CreateChangesetToCurrent(last_sync_engine_, &output_stream); |
| 59 EXPECT_FALSE(changeset.empty()); |
| 60 output_stream.Trim(); |
| 61 |
| 62 // Apply the changeset stream to |client_|. |
| 63 google::protobuf::io::ArrayInputStream raw_input_stream(changeset.data(), |
| 64 changeset.size()); |
| 65 google::protobuf::io::CodedInputStream input_stream(&raw_input_stream); |
| 66 |
| 67 last_sync_engine_ = RevisionGenerator::GetInstance()->current(); |
| 68 EXPECT_EQ(Result::SUCCESS, |
| 69 client_.ApplyChangeset(last_sync_engine_, &input_stream)); |
| 70 engine_.ReleaseBefore(last_sync_engine_); |
| 71 |
| 72 // Ensure EOF. |
| 73 EXPECT_FALSE(input_stream.Skip(1)); |
| 74 } |
| 75 |
| 76 Revision last_sync_engine_ = 0; |
| 77 SampleCompoundSyncable engine_; |
| 78 SampleCompoundSyncable client_; |
| 79 |
| 80 private: |
| 81 DISALLOW_COPY_AND_ASSIGN(CompoundSyncableTest); |
| 82 }; |
| 83 |
| 84 TEST_F(CompoundSyncableTest, SequentialMutations) { |
| 85 engine_.mutable_child1()->Set(123); |
| 86 Synchronize(); |
| 87 EXPECT_EQ(123, client_.mutable_child1()->Get()); |
| 88 |
| 89 engine_.mutable_child1()->Set(456); |
| 90 Synchronize(); |
| 91 EXPECT_EQ(456, client_.mutable_child1()->Get()); |
| 92 } |
| 93 |
| 94 TEST_F(CompoundSyncableTest, MutateMultiple) { |
| 95 engine_.mutable_child1()->Set(123); |
| 96 engine_.mutable_child2()->Set(456); |
| 97 Synchronize(); |
| 98 EXPECT_EQ(123, client_.mutable_child1()->Get()); |
| 99 EXPECT_EQ(456, client_.mutable_child2()->Get()); |
| 100 } |
| 101 |
| 102 TEST_F(CompoundSyncableTest, MutateMultipleDiscrete) { |
| 103 engine_.mutable_child1()->Set(123); |
| 104 Synchronize(); |
| 105 EXPECT_EQ(123, client_.mutable_child1()->Get()); |
| 106 engine_.mutable_child2()->Set(456); |
| 107 Synchronize(); |
| 108 EXPECT_EQ(123, client_.mutable_child1()->Get()); |
| 109 EXPECT_EQ(456, client_.mutable_child2()->Get()); |
| 110 } |
| 111 |
| 112 } // namespace |
| 113 } // namespace helium |
| 114 } // namespace blimp |
OLD | NEW |