Home | History | Annotate | Download | only in internal_api
      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 "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"
     13 
     14 namespace syncer {
     15 
     16 class ProtocolEventBufferTest : public ::testing::Test {
     17  public:
     18   ProtocolEventBufferTest();
     19   virtual ~ProtocolEventBufferTest();
     20 
     21   static scoped_ptr<ProtocolEvent> MakeTestEvent(int64 id);
     22   static bool HasId(const ProtocolEvent& event, int64 id);
     23 
     24  protected:
     25   ProtocolEventBuffer buffer_;
     26 };
     27 
     28 ProtocolEventBufferTest::ProtocolEventBufferTest() {}
     29 
     30 ProtocolEventBufferTest::~ProtocolEventBufferTest() {}
     31 
     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),
     37           message));
     38 }
     39 
     40 bool ProtocolEventBufferTest::HasId(const ProtocolEvent& event, int64 id) {
     41   return event.GetTimestamp() == base::Time::FromInternalValue(id);
     42 }
     43 
     44 TEST_F(ProtocolEventBufferTest, AddThenReturnEvents) {
     45   scoped_ptr<ProtocolEvent> e1(MakeTestEvent(1));
     46   scoped_ptr<ProtocolEvent> e2(MakeTestEvent(2));
     47 
     48   buffer_.RecordProtocolEvent(*e1);
     49   buffer_.RecordProtocolEvent(*e2);
     50 
     51   ScopedVector<ProtocolEvent> buffered_events(
     52       buffer_.GetBufferedProtocolEvents());
     53 
     54   ASSERT_EQ(2U, buffered_events.size());
     55   EXPECT_TRUE(HasId(*(buffered_events[0]), 1));
     56   EXPECT_TRUE(HasId(*(buffered_events[1]), 2));
     57 }
     58 
     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);
     63   }
     64 
     65   ScopedVector<ProtocolEvent> buffered_events(
     66       buffer_.GetBufferedProtocolEvents());
     67   ASSERT_EQ(ProtocolEventBuffer::kBufferSize, buffered_events.size());
     68 
     69   for (size_t i = 1; i < ProtocolEventBuffer::kBufferSize+1; ++i) {
     70     EXPECT_TRUE(
     71         HasId(*(buffered_events[i-1]), static_cast<int64>(i)));
     72   }
     73 
     74 }
     75 
     76 
     77 }  // namespace syncer
     78