Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 0a1464c8 authored by Treehugger Robot's avatar Treehugger Robot Committed by Gerrit Code Review
Browse files

Merge "Add timestamped circular buffer"

parents 58d8987a 383041d5
Loading
Loading
Loading
Loading
+117 −0
Original line number Diff line number Diff line
/*
 * Copyright 2020 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#pragma once

#include <stddef.h>

#include <mutex>
#include <queue>

#include "os/log.h"

namespace bluetooth {
namespace common {

template <typename T>
class CircularBuffer {
 public:
  explicit CircularBuffer(size_t size);

  void Push(T item);
  std::vector<T> Pull() const;

 private:
  const size_t size_;
  std::deque<T> queue_;
  mutable std::mutex mutex_;
};

class Timestamper {
 public:
  virtual long long GetTimestamp() const = 0;
  virtual ~Timestamper() {}
};

class TimestamperInMilliseconds : public Timestamper {
 public:
  long long GetTimestamp() const override {
    return std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch())
        .count();
  }
  virtual ~TimestamperInMilliseconds() {}
};

template <typename T>
struct TimestampedEntry {
  long long timestamp;
  T entry;
};

template <typename T>
class TimestampedCircularBuffer : public CircularBuffer<TimestampedEntry<T>> {
 public:
  explicit TimestampedCircularBuffer(
      size_t size, std::unique_ptr<Timestamper> timestamper = std::make_unique<TimestamperInMilliseconds>());

  void Push(T item);
  std::vector<TimestampedEntry<T>> Pull() const;

 private:
  std::unique_ptr<Timestamper> timestamper_{std::make_unique<TimestamperInMilliseconds>()};
};

}  // namespace common
}  // namespace bluetooth

template <typename T>
bluetooth::common::CircularBuffer<T>::CircularBuffer(size_t size) : size_(size) {}

template <typename T>
void bluetooth::common::CircularBuffer<T>::Push(const T item) {
  std::unique_lock<std::mutex> lock(mutex_);
  queue_.push_back(item);
  while (queue_.size() > size_) {
    queue_.pop_front();
  }
}

template <typename T>
std::vector<T> bluetooth::common::CircularBuffer<T>::Pull() const {
  std::unique_lock<std::mutex> lock(mutex_);
  std::vector<T> items;
  for (auto it = queue_.cbegin(); it != queue_.cend(); ++it) {
    items.push_back(*it);
  }
  return items;
}

template <typename T>
bluetooth::common::TimestampedCircularBuffer<T>::TimestampedCircularBuffer(
    size_t size, std::unique_ptr<Timestamper> timestamper)
    : CircularBuffer<TimestampedEntry<T>>(size), timestamper_(std::move(timestamper)) {}

template <typename T>
void bluetooth::common::TimestampedCircularBuffer<T>::Push(const T item) {
  TimestampedEntry<T> timestamped_entry{timestamper_->GetTimestamp(), item};
  bluetooth::common::CircularBuffer<TimestampedEntry<T>>::Push(timestamped_entry);
}

template <typename T>
std::vector<struct bluetooth::common::TimestampedEntry<T>> bluetooth::common::TimestampedCircularBuffer<T>::Pull()
    const {
  return bluetooth::common::CircularBuffer<TimestampedEntry<T>>::Pull();
}
+84 −0
Original line number Diff line number Diff line
/*
 * Copyright 2020 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <limits>
#include <string>

#include "common/circular_buffer.h"
#include "os/log.h"

namespace testing {

long long timestamp_{0};
struct TestTimestamper : public bluetooth::common::Timestamper {
  virtual long long GetTimestamp() const override {
    return timestamp_++;
  }
};

TEST(CircularBufferTest, simple) {
  bluetooth::common::TimestampedCircularBuffer<std::string> buffer(10);

  buffer.Push(std::string("One"));
  buffer.Push(std::string("Two"));
  buffer.Push(std::string("Three"));

  auto vec = buffer.Pull();

  ASSERT_STREQ("One", vec[0].entry.c_str());
  ASSERT_STREQ("Two", vec[1].entry.c_str());
  ASSERT_STREQ("Three", vec[2].entry.c_str());
}

TEST(CircularBufferTest, test_timestamps) {
  bluetooth::common::TimestampedCircularBuffer<std::string> buffer(10, std::make_unique<TestTimestamper>());

  buffer.Push(std::string("One"));
  buffer.Push(std::string("Two"));
  buffer.Push(std::string("Three"));

  auto vec = buffer.Pull();
  long long timestamp = 0;
  for (auto v : vec) {
    ASSERT_EQ(timestamp, v.timestamp);
    timestamp++;
  }
}

TEST(CircularBufferTest, max_timestamps) {
  bluetooth::common::TimestampedCircularBuffer<std::string> buffer(10);

  std::vector<std::string> test_data;
  for (int i = 0; i < 10 + 1; i++) {
    char buf[255];
    snprintf(buf, 255, "value:%d", i);
    test_data.push_back(std::string(buf));
    buffer.Push(std::string(buf));
  }

  auto vec = buffer.Pull();
  ASSERT_EQ(10, vec.size());

  int i = 0 + 1;
  for (auto v : vec) {
    ASSERT_EQ(test_data[i], v.entry.c_str());
    i++;
  }
}

}  // namespace testing