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