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