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.
5 #include "base/memory/scoped_ptr.h"
6 #include "base/memory/scoped_vector.h"
7 #include "base/time/time.h"
8 #include "sync/internal_api/protocol_event_buffer.h"
9 #include "sync/internal_api/public/events/poll_get_updates_request_event.h"
10 #include "sync/internal_api/public/events/protocol_event.h"
11 #include "sync/protocol/sync.pb.h"
12 #include "testing/gtest/include/gtest/gtest.h"
16 class ProtocolEventBufferTest
: public ::testing::Test
{
18 ProtocolEventBufferTest();
19 virtual ~ProtocolEventBufferTest();
21 static scoped_ptr
<ProtocolEvent
> MakeTestEvent(int64 id
);
22 static bool HasId(const ProtocolEvent
& event
, int64 id
);
25 ProtocolEventBuffer buffer_
;
28 ProtocolEventBufferTest::ProtocolEventBufferTest() {}
30 ProtocolEventBufferTest::~ProtocolEventBufferTest() {}
32 scoped_ptr
<ProtocolEvent
> ProtocolEventBufferTest::MakeTestEvent(int64 id
) {
33 sync_pb::ClientToServerMessage message
;
34 return scoped_ptr
<ProtocolEvent
>(
35 new PollGetUpdatesRequestEvent(
36 base::Time::FromInternalValue(id
),
40 bool ProtocolEventBufferTest::HasId(const ProtocolEvent
& event
, int64 id
) {
41 return event
.GetTimestamp() == base::Time::FromInternalValue(id
);
44 TEST_F(ProtocolEventBufferTest
, AddThenReturnEvents
) {
45 scoped_ptr
<ProtocolEvent
> e1(MakeTestEvent(1));
46 scoped_ptr
<ProtocolEvent
> e2(MakeTestEvent(2));
48 buffer_
.RecordProtocolEvent(*e1
);
49 buffer_
.RecordProtocolEvent(*e2
);
51 ScopedVector
<ProtocolEvent
> buffered_events(
52 buffer_
.GetBufferedProtocolEvents());
54 ASSERT_EQ(2U, buffered_events
.size());
55 EXPECT_TRUE(HasId(*(buffered_events
[0]), 1));
56 EXPECT_TRUE(HasId(*(buffered_events
[1]), 2));
59 TEST_F(ProtocolEventBufferTest
, AddThenOverflowThenReturnEvents
) {
60 for (size_t i
= 0; i
< ProtocolEventBuffer::kBufferSize
+1; ++i
) {
61 scoped_ptr
<ProtocolEvent
> e(MakeTestEvent(static_cast<int64
>(i
)));
62 buffer_
.RecordProtocolEvent(*e
);
65 ScopedVector
<ProtocolEvent
> buffered_events(
66 buffer_
.GetBufferedProtocolEvents());
67 ASSERT_EQ(ProtocolEventBuffer::kBufferSize
, buffered_events
.size());
69 for (size_t i
= 1; i
< ProtocolEventBuffer::kBufferSize
+1; ++i
) {
71 HasId(*(buffered_events
[i
-1]), static_cast<int64
>(i
)));