| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "sync/internal_api/protocol_event_buffer.h" |
| 6 |
| 5 #include <stddef.h> | 7 #include <stddef.h> |
| 6 #include <stdint.h> | 8 #include <stdint.h> |
| 7 | 9 |
| 8 #include "base/memory/scoped_ptr.h" | 10 #include <memory> |
| 11 |
| 9 #include "base/memory/scoped_vector.h" | 12 #include "base/memory/scoped_vector.h" |
| 10 #include "base/time/time.h" | 13 #include "base/time/time.h" |
| 11 #include "sync/internal_api/protocol_event_buffer.h" | |
| 12 #include "sync/internal_api/public/events/poll_get_updates_request_event.h" | 14 #include "sync/internal_api/public/events/poll_get_updates_request_event.h" |
| 13 #include "sync/internal_api/public/events/protocol_event.h" | 15 #include "sync/internal_api/public/events/protocol_event.h" |
| 14 #include "sync/protocol/sync.pb.h" | 16 #include "sync/protocol/sync.pb.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
| 16 | 18 |
| 17 namespace syncer { | 19 namespace syncer { |
| 18 | 20 |
| 19 class ProtocolEventBufferTest : public ::testing::Test { | 21 class ProtocolEventBufferTest : public ::testing::Test { |
| 20 public: | 22 public: |
| 21 ProtocolEventBufferTest(); | 23 ProtocolEventBufferTest(); |
| 22 ~ProtocolEventBufferTest() override; | 24 ~ProtocolEventBufferTest() override; |
| 23 | 25 |
| 24 static scoped_ptr<ProtocolEvent> MakeTestEvent(int64_t id); | 26 static std::unique_ptr<ProtocolEvent> MakeTestEvent(int64_t id); |
| 25 static bool HasId(const ProtocolEvent& event, int64_t id); | 27 static bool HasId(const ProtocolEvent& event, int64_t id); |
| 26 | 28 |
| 27 protected: | 29 protected: |
| 28 ProtocolEventBuffer buffer_; | 30 ProtocolEventBuffer buffer_; |
| 29 }; | 31 }; |
| 30 | 32 |
| 31 ProtocolEventBufferTest::ProtocolEventBufferTest() {} | 33 ProtocolEventBufferTest::ProtocolEventBufferTest() {} |
| 32 | 34 |
| 33 ProtocolEventBufferTest::~ProtocolEventBufferTest() {} | 35 ProtocolEventBufferTest::~ProtocolEventBufferTest() {} |
| 34 | 36 |
| 35 scoped_ptr<ProtocolEvent> ProtocolEventBufferTest::MakeTestEvent(int64_t id) { | 37 std::unique_ptr<ProtocolEvent> ProtocolEventBufferTest::MakeTestEvent( |
| 38 int64_t id) { |
| 36 sync_pb::ClientToServerMessage message; | 39 sync_pb::ClientToServerMessage message; |
| 37 return scoped_ptr<ProtocolEvent>( | 40 return std::unique_ptr<ProtocolEvent>(new PollGetUpdatesRequestEvent( |
| 38 new PollGetUpdatesRequestEvent( | 41 base::Time::FromInternalValue(id), message)); |
| 39 base::Time::FromInternalValue(id), | |
| 40 message)); | |
| 41 } | 42 } |
| 42 | 43 |
| 43 bool ProtocolEventBufferTest::HasId(const ProtocolEvent& event, int64_t id) { | 44 bool ProtocolEventBufferTest::HasId(const ProtocolEvent& event, int64_t id) { |
| 44 return event.GetTimestamp() == base::Time::FromInternalValue(id); | 45 return event.GetTimestamp() == base::Time::FromInternalValue(id); |
| 45 } | 46 } |
| 46 | 47 |
| 47 TEST_F(ProtocolEventBufferTest, AddThenReturnEvents) { | 48 TEST_F(ProtocolEventBufferTest, AddThenReturnEvents) { |
| 48 scoped_ptr<ProtocolEvent> e1(MakeTestEvent(1)); | 49 std::unique_ptr<ProtocolEvent> e1(MakeTestEvent(1)); |
| 49 scoped_ptr<ProtocolEvent> e2(MakeTestEvent(2)); | 50 std::unique_ptr<ProtocolEvent> e2(MakeTestEvent(2)); |
| 50 | 51 |
| 51 buffer_.RecordProtocolEvent(*e1); | 52 buffer_.RecordProtocolEvent(*e1); |
| 52 buffer_.RecordProtocolEvent(*e2); | 53 buffer_.RecordProtocolEvent(*e2); |
| 53 | 54 |
| 54 ScopedVector<ProtocolEvent> buffered_events( | 55 ScopedVector<ProtocolEvent> buffered_events( |
| 55 buffer_.GetBufferedProtocolEvents()); | 56 buffer_.GetBufferedProtocolEvents()); |
| 56 | 57 |
| 57 ASSERT_EQ(2U, buffered_events.size()); | 58 ASSERT_EQ(2U, buffered_events.size()); |
| 58 EXPECT_TRUE(HasId(*(buffered_events[0]), 1)); | 59 EXPECT_TRUE(HasId(*(buffered_events[0]), 1)); |
| 59 EXPECT_TRUE(HasId(*(buffered_events[1]), 2)); | 60 EXPECT_TRUE(HasId(*(buffered_events[1]), 2)); |
| 60 } | 61 } |
| 61 | 62 |
| 62 TEST_F(ProtocolEventBufferTest, AddThenOverflowThenReturnEvents) { | 63 TEST_F(ProtocolEventBufferTest, AddThenOverflowThenReturnEvents) { |
| 63 for (size_t i = 0; i < ProtocolEventBuffer::kBufferSize+1; ++i) { | 64 for (size_t i = 0; i < ProtocolEventBuffer::kBufferSize+1; ++i) { |
| 64 scoped_ptr<ProtocolEvent> e(MakeTestEvent(static_cast<int64_t>(i))); | 65 std::unique_ptr<ProtocolEvent> e(MakeTestEvent(static_cast<int64_t>(i))); |
| 65 buffer_.RecordProtocolEvent(*e); | 66 buffer_.RecordProtocolEvent(*e); |
| 66 } | 67 } |
| 67 | 68 |
| 68 ScopedVector<ProtocolEvent> buffered_events( | 69 ScopedVector<ProtocolEvent> buffered_events( |
| 69 buffer_.GetBufferedProtocolEvents()); | 70 buffer_.GetBufferedProtocolEvents()); |
| 70 ASSERT_EQ(ProtocolEventBuffer::kBufferSize, buffered_events.size()); | 71 ASSERT_EQ(ProtocolEventBuffer::kBufferSize, buffered_events.size()); |
| 71 | 72 |
| 72 for (size_t i = 1; i < ProtocolEventBuffer::kBufferSize+1; ++i) { | 73 for (size_t i = 1; i < ProtocolEventBuffer::kBufferSize+1; ++i) { |
| 73 EXPECT_TRUE(HasId(*(buffered_events[i - 1]), static_cast<int64_t>(i))); | 74 EXPECT_TRUE(HasId(*(buffered_events[i - 1]), static_cast<int64_t>(i))); |
| 74 } | 75 } |
| 75 } | 76 } |
| 76 | 77 |
| 77 | 78 |
| 78 } // namespace syncer | 79 } // namespace syncer |
| OLD | NEW |