| 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/helium_test.h" | |
| 13 #include "blimp/helium/lww_register.h" | |
| 14 #include "blimp/helium/revision_generator.h" | |
| 15 #include "testing/gmock/include/gmock/gmock.h" | |
| 16 #include "testing/gtest/include/gtest/gtest.h" | |
| 17 #include "third_party/protobuf/src/google/protobuf/io/coded_stream.h" | |
| 18 #include "third_party/protobuf/src/google/protobuf/io/zero_copy_stream_impl_lite
.h" | |
| 19 | |
| 20 namespace blimp { | |
| 21 namespace helium { | |
| 22 namespace { | |
| 23 | |
| 24 struct SampleCompoundSyncable : public CompoundSyncable { | |
| 25 explicit SampleCompoundSyncable(Peer bias, Peer running_as) | |
| 26 : child1(CreateAndRegister<LwwRegister<int>>(bias, running_as)), | |
| 27 child2(CreateAndRegister<LwwRegister<int>>(bias, running_as)) {} | |
| 28 | |
| 29 RegisteredSyncable<LwwRegister<int>> child1; | |
| 30 RegisteredSyncable<LwwRegister<int>> child2; | |
| 31 | |
| 32 private: | |
| 33 DISALLOW_COPY_AND_ASSIGN(SampleCompoundSyncable); | |
| 34 }; | |
| 35 | |
| 36 class CompoundSyncableTest : public HeliumTest { | |
| 37 public: | |
| 38 CompoundSyncableTest() | |
| 39 : last_sync_engine_(0), | |
| 40 engine_(Peer::ENGINE, Peer::ENGINE), | |
| 41 client_(Peer::ENGINE, Peer::CLIENT) { | |
| 42 client_.SetLocalUpdateCallback(base::Bind( | |
| 43 &CompoundSyncableTest::OnClientCallbackCalled, base::Unretained(this))); | |
| 44 engine_.SetLocalUpdateCallback(base::Bind( | |
| 45 &CompoundSyncableTest::OnEngineCallbackCalled, base::Unretained(this))); | |
| 46 } | |
| 47 | |
| 48 ~CompoundSyncableTest() override {} | |
| 49 | |
| 50 public: | |
| 51 MOCK_METHOD0(OnEngineCallbackCalled, void()); | |
| 52 MOCK_METHOD0(OnClientCallbackCalled, void()); | |
| 53 | |
| 54 protected: | |
| 55 // Propagates pending changes between |engine_| and |client_|. | |
| 56 void Synchronize() { | |
| 57 ASSERT_TRUE( | |
| 58 client_.ValidateChangeset(*engine_.CreateChangeset(last_sync_client_))); | |
| 59 ASSERT_TRUE( | |
| 60 engine_.ValidateChangeset(*client_.CreateChangeset(last_sync_engine_))); | |
| 61 | |
| 62 client_.ApplyChangeset(*engine_.CreateChangeset(last_sync_client_)); | |
| 63 engine_.ApplyChangeset(*client_.CreateChangeset(last_sync_engine_)); | |
| 64 | |
| 65 last_sync_engine_ = RevisionGenerator::GetInstance()->current(); | |
| 66 last_sync_client_ = RevisionGenerator::GetInstance()->current(); | |
| 67 } | |
| 68 | |
| 69 Revision last_sync_engine_ = 0; | |
| 70 Revision last_sync_client_ = 0; | |
| 71 SampleCompoundSyncable engine_; | |
| 72 SampleCompoundSyncable client_; | |
| 73 | |
| 74 private: | |
| 75 DISALLOW_COPY_AND_ASSIGN(CompoundSyncableTest); | |
| 76 }; | |
| 77 | |
| 78 TEST_F(CompoundSyncableTest, SequentialMutations) { | |
| 79 EXPECT_CALL(*this, OnClientCallbackCalled()).Times(0); | |
| 80 EXPECT_CALL(*this, OnEngineCallbackCalled()).Times(2); | |
| 81 | |
| 82 engine_.child1->Set(123); | |
| 83 Synchronize(); | |
| 84 EXPECT_EQ(123, client_.child1->Get()); | |
| 85 | |
| 86 engine_.child1->Set(456); | |
| 87 Synchronize(); | |
| 88 EXPECT_EQ(456, client_.child1->Get()); | |
| 89 } | |
| 90 | |
| 91 TEST_F(CompoundSyncableTest, NoOp) { | |
| 92 Synchronize(); | |
| 93 | |
| 94 EXPECT_CALL(*this, OnClientCallbackCalled()).Times(0); | |
| 95 EXPECT_CALL(*this, OnEngineCallbackCalled()).Times(1); | |
| 96 | |
| 97 engine_.child1->Set(123); | |
| 98 Synchronize(); | |
| 99 EXPECT_EQ(123, client_.child1->Get()); | |
| 100 | |
| 101 Synchronize(); | |
| 102 } | |
| 103 | |
| 104 TEST_F(CompoundSyncableTest, MutateMultiple) { | |
| 105 EXPECT_CALL(*this, OnClientCallbackCalled()).Times(0); | |
| 106 EXPECT_CALL(*this, OnEngineCallbackCalled()).Times(2); | |
| 107 | |
| 108 engine_.child1->Set(123); | |
| 109 engine_.child2->Set(456); | |
| 110 Synchronize(); | |
| 111 EXPECT_EQ(123, client_.child1->Get()); | |
| 112 EXPECT_EQ(456, client_.child2->Get()); | |
| 113 } | |
| 114 | |
| 115 TEST_F(CompoundSyncableTest, MutateMultipleDiscrete) { | |
| 116 EXPECT_CALL(*this, OnClientCallbackCalled()).Times(2); | |
| 117 EXPECT_CALL(*this, OnEngineCallbackCalled()).Times(0); | |
| 118 | |
| 119 client_.child1->Set(123); | |
| 120 Synchronize(); | |
| 121 EXPECT_EQ(123, client_.child1->Get()); | |
| 122 client_.child2->Set(456); | |
| 123 Synchronize(); | |
| 124 EXPECT_EQ(123, engine_.child1->Get()); | |
| 125 EXPECT_EQ(456, engine_.child2->Get()); | |
| 126 } | |
| 127 | |
| 128 } // namespace | |
| 129 } // namespace helium | |
| 130 } // namespace blimp | |
| OLD | NEW |