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

Commit 50bea9a1 authored by Qasim Javed's avatar Qasim Javed Committed by Gerrit Code Review
Browse files

Merge "Ensure existing unit tests pass with fake timers."

parents 5b6e9c33 39e8e4eb
Loading
Loading
Loading
Loading
+14 −1
Original line number Diff line number Diff line
@@ -237,6 +237,19 @@ cc_library {
    ],
}

cc_library {
    name: "libbluetooth_gd_unit_tests",
    defaults: [
        "libbluetooth_gd_defaults",
    ],
    srcs: [
        ":BluetoothOsSources_fake_timer",
    ],
    cflags: [
        "-DUSE_FAKE_TIMERS",
    ],
}

cc_binary {
    name: "bluetooth_stack_with_facade",
    defaults: [
@@ -389,7 +402,7 @@ cc_test {
        "libbluetooth-dumpsys-test",
        "libbluetooth-dumpsys-unittest",
        "libbluetooth-protos",
        "libbluetooth_gd",
        "libbluetooth_gd_unit_tests",
        "libc++fs",
        "libflatbuffers-cpp",
        "libgmock",
+13 −1
Original line number Diff line number Diff line
@@ -27,12 +27,16 @@
#include "common/circular_buffer.h"
#include "common/init_flags.h"
#include "common/strings.h"
#include "os/fake_timer/fake_timerfd.h"
#include "os/files.h"
#include "os/log.h"
#include "os/parameter_provider.h"
#include "os/system_properties.h"

namespace bluetooth {
#ifdef USE_FAKE_TIMERS
using os::fake_timer::fake_timerfd_get_clock;
#endif
namespace hal {

namespace {
@@ -109,13 +113,18 @@ void delete_btsnoop_files(const std::string& log_path) {
void delete_old_btsnooz_files(const std::string& log_path, const std::chrono::milliseconds log_life_time) {
  auto opt_created_ts = os::FileCreatedTime(log_path);
  if (!opt_created_ts) return;

#ifdef USE_FAKE_TIMERS
  auto diff = fake_timerfd_get_clock() - file_creation_time;
  uint64_t log_lifetime = log_life_time.count();
  if (diff >= log_lifetime) {
#else
  using namespace std::chrono;
  auto created_tp = opt_created_ts.value();
  auto current_tp = std::chrono::system_clock::now();

  auto diff = duration_cast<milliseconds>(current_tp - created_tp);
  if (diff >= log_life_time) {
#endif
    delete_btsnoop_files(log_path);
  }
}
@@ -269,6 +278,9 @@ void SnoopLogger::OpenNextSnoopLogFile() {
  mode_t prevmask = umask(0);
  // do not use std::ios::app as we want override the existing file
  btsnoop_ostream_.open(snoop_log_path_, std::ios::binary | std::ios::out);
#ifdef USE_FAKE_TIMERS
  file_creation_time = fake_timerfd_get_clock();
#endif
  if (!btsnoop_ostream_.good()) {
    LOG_ALWAYS_FATAL("Unable to open snoop log at \"%s\", error: \"%s\"", snoop_log_path_.c_str(), strerror(errno));
  }
+4 −0
Original line number Diff line number Diff line
@@ -29,6 +29,10 @@
namespace bluetooth {
namespace hal {

#ifdef USE_FAKE_TIMERS
static uint64_t file_creation_time;
#endif

class SnoopLogger : public ::bluetooth::Module {
 public:
  static const ModuleFactory Factory;
+11 −3
Original line number Diff line number Diff line
@@ -19,8 +19,13 @@
#include <gmock/gmock.h>
#include <gtest/gtest.h>

#include "os/fake_timer/fake_timerfd.h"

namespace testing {

using bluetooth::os::fake_timer::fake_timerfd_advance;
using bluetooth::os::fake_timer::fake_timerfd_reset;

namespace {
std::vector<uint8_t> kInformationRequest = {
    0xfe,
@@ -108,6 +113,7 @@ class SnoopLoggerModuleTest : public Test {
  void TearDown() override {
    DeleteSnoopLogFiles();
    delete builder_;
    fake_timerfd_reset();
  }

  void DeleteSnoopLogFiles() {
@@ -317,11 +323,13 @@ TEST_F(SnoopLoggerModuleTest, delete_old_snooz_log_files) {

  std::filesystem::create_directories(temp_snooz_log_);

  auto* handler = test_registry.GetTestModuleHandler(&SnoopLogger::Factory);
  ASSERT_TRUE(std::filesystem::exists(temp_snooz_log_));
  std::this_thread::sleep_for(10ms);
  handler->Post(bluetooth::common::BindOnce(fake_timerfd_advance, 10));
  ASSERT_TRUE(std::filesystem::exists(temp_snooz_log_));
  std::this_thread::sleep_for(15ms);
  ASSERT_FALSE(std::filesystem::exists(temp_snooz_log_));
  handler->Post(bluetooth::common::BindOnce(fake_timerfd_advance, 15));
  handler->Post(bluetooth::common::BindOnce(
      [](std::filesystem::path path) { ASSERT_FALSE(std::filesystem::exists(path)); }, temp_snooz_log_));
  test_registry.StopAll();
}

+4 −0
Original line number Diff line number Diff line
@@ -134,6 +134,10 @@ void fake_timerfd_cap_at(uint64_t ms) {
  max_clock = ms;
}

uint64_t fake_timerfd_get_clock() {
  return clock;
}

}  // namespace fake_timer
}  // namespace os
}  // namespace bluetooth
Loading