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

Commit b337f2b3 authored by Chienyuan's avatar Chienyuan
Browse files

gd: Fix flaky tests of LeAddressManager

Bug: 174541433
Tag: #gd-refactor
Test: atest bluetooth_test_gd
Change-Id: Ia3af25d2d4c3574c926e18b3d5d5c838316908b7
parent fc6592ba
Loading
Loading
Loading
Loading
+5 −5
Original line number Diff line number Diff line
@@ -1259,16 +1259,16 @@ TEST_F(AclManagerWithResolvableAddressTest, create_connection_cancel_fail) {
  test_hci_layer_->SetCommandFuture();
  acl_manager_->CreateLeConnection(remote_with_type_);

  // Add device to connect list
  test_hci_layer_->GetLastCommandPacket(OpCode::LE_ADD_DEVICE_TO_CONNECT_LIST);
  test_hci_layer_->SetCommandFuture();
  test_hci_layer_->IncomingEvent(LeAddDeviceToConnectListCompleteBuilder::Create(0x01, ErrorCode::SUCCESS));

  // Set random address
  test_hci_layer_->GetLastCommandPacket(OpCode::LE_SET_RANDOM_ADDRESS);
  test_hci_layer_->SetCommandFuture();
  test_hci_layer_->IncomingEvent(LeSetRandomAddressCompleteBuilder::Create(0x01, ErrorCode::SUCCESS));

  // Add device to connect list
  test_hci_layer_->GetLastCommandPacket(OpCode::LE_ADD_DEVICE_TO_CONNECT_LIST);
  test_hci_layer_->SetCommandFuture();
  test_hci_layer_->IncomingEvent(LeAddDeviceToConnectListCompleteBuilder::Create(0x01, ErrorCode::SUCCESS));

  // send create connection command
  test_hci_layer_->GetLastCommandPacket(OpCode::LE_CREATE_CONNECTION);
  test_hci_layer_->IncomingEvent(LeCreateConnectionStatusBuilder::Create(ErrorCode::SUCCESS, 0x01));
+11 −12
Original line number Diff line number Diff line
@@ -199,6 +199,11 @@ void LeAddressManager::pause_registered_clients() {
  }
}

void LeAddressManager::push_command(Command command) {
  pause_registered_clients();
  cached_commands_.push(std::move(command));
}

void LeAddressManager::ack_pause(LeAddressManagerCallback* callback) {
  ASSERT(registered_clients_.find(callback) != registered_clients_.end());
  registered_clients_.find(callback)->second = ClientState::PAUSED;
@@ -364,8 +369,7 @@ void LeAddressManager::AddDeviceToConnectList(
    ConnectListAddressType connect_list_address_type, bluetooth::hci::Address address) {
  auto packet_builder = hci::LeAddDeviceToConnectListBuilder::Create(connect_list_address_type, address);
  Command command = {CommandType::ADD_DEVICE_TO_CONNECT_LIST, std::move(packet_builder)};
  handler_->BindOnceOn(this, &LeAddressManager::pause_registered_clients).Invoke();
  cached_commands_.push(std::move(command));
  handler_->BindOnceOn(this, &LeAddressManager::push_command, std::move(command)).Invoke();
}

void LeAddressManager::AddDeviceToResolvingList(
@@ -376,16 +380,14 @@ void LeAddressManager::AddDeviceToResolvingList(
  auto packet_builder = hci::LeAddDeviceToResolvingListBuilder::Create(
      peer_identity_address_type, peer_identity_address, peer_irk, local_irk);
  Command command = {CommandType::ADD_DEVICE_TO_RESOLVING_LIST, std::move(packet_builder)};
  handler_->BindOnceOn(this, &LeAddressManager::pause_registered_clients).Invoke();
  cached_commands_.push(std::move(command));
  handler_->BindOnceOn(this, &LeAddressManager::push_command, std::move(command)).Invoke();
}

void LeAddressManager::RemoveDeviceFromConnectList(
    ConnectListAddressType connect_list_address_type, bluetooth::hci::Address address) {
  auto packet_builder = hci::LeRemoveDeviceFromConnectListBuilder::Create(connect_list_address_type, address);
  Command command = {CommandType::REMOVE_DEVICE_FROM_CONNECT_LIST, std::move(packet_builder)};
  handler_->BindOnceOn(this, &LeAddressManager::pause_registered_clients).Invoke();
  cached_commands_.push(std::move(command));
  handler_->BindOnceOn(this, &LeAddressManager::push_command, std::move(command)).Invoke();
}

void LeAddressManager::RemoveDeviceFromResolvingList(
@@ -393,22 +395,19 @@ void LeAddressManager::RemoveDeviceFromResolvingList(
  auto packet_builder =
      hci::LeRemoveDeviceFromResolvingListBuilder::Create(peer_identity_address_type, peer_identity_address);
  Command command = {CommandType::REMOVE_DEVICE_FROM_RESOLVING_LIST, std::move(packet_builder)};
  handler_->BindOnceOn(this, &LeAddressManager::pause_registered_clients).Invoke();
  cached_commands_.push(std::move(command));
  handler_->BindOnceOn(this, &LeAddressManager::push_command, std::move(command)).Invoke();
}

void LeAddressManager::ClearConnectList() {
  auto packet_builder = hci::LeClearConnectListBuilder::Create();
  Command command = {CommandType::CLEAR_CONNECT_LIST, std::move(packet_builder)};
  handler_->BindOnceOn(this, &LeAddressManager::pause_registered_clients).Invoke();
  cached_commands_.push(std::move(command));
  handler_->BindOnceOn(this, &LeAddressManager::push_command, std::move(command)).Invoke();
}

void LeAddressManager::ClearResolvingList() {
  auto packet_builder = hci::LeClearResolvingListBuilder::Create();
  Command command = {CommandType::CLEAR_RESOLVING_LIST, std::move(packet_builder)};
  handler_->BindOnceOn(this, &LeAddressManager::pause_registered_clients).Invoke();
  cached_commands_.push(std::move(command));
  handler_->BindOnceOn(this, &LeAddressManager::push_command, std::move(command)).Invoke();
}

void LeAddressManager::OnCommandComplete(bluetooth::hci::CommandCompleteView view) {
+16 −15
Original line number Diff line number Diff line
@@ -88,21 +88,6 @@ class LeAddressManager {
  void OnCommandComplete(CommandCompleteView view);

 private:
  void pause_registered_clients();
  void ack_pause(LeAddressManagerCallback* callback);
  void resume_registered_clients();
  void ack_resume(LeAddressManagerCallback* callback);
  void register_client(LeAddressManagerCallback* callback);
  void unregister_client(LeAddressManagerCallback* callback);
  void prepare_to_rotate();
  void rotate_random_address();
  void schedule_rotate_random_address();
  void set_random_address();
  hci::Address generate_rpa();
  hci::Address generate_nrpa();
  std::chrono::milliseconds get_next_private_address_interval_ms();
  void handle_next_command();

  enum ClientState {
    WAITING_FOR_PAUSE,
    PAUSED,
@@ -125,6 +110,22 @@ class LeAddressManager {
    std::unique_ptr<CommandPacketBuilder> command_packet;
  };

  void pause_registered_clients();
  void push_command(Command command);
  void ack_pause(LeAddressManagerCallback* callback);
  void resume_registered_clients();
  void ack_resume(LeAddressManagerCallback* callback);
  void register_client(LeAddressManagerCallback* callback);
  void unregister_client(LeAddressManagerCallback* callback);
  void prepare_to_rotate();
  void rotate_random_address();
  void schedule_rotate_random_address();
  void set_random_address();
  hci::Address generate_rpa();
  hci::Address generate_nrpa();
  std::chrono::milliseconds get_next_private_address_interval_ms();
  void handle_next_command();

  common::Callback<void(std::unique_ptr<CommandPacketBuilder>)> enqueue_command_;
  os::Handler* handler_;
  std::map<LeAddressManagerCallback*, ClientState> registered_clients_;
+9 −3
Original line number Diff line number Diff line
@@ -46,6 +46,7 @@ class TestHciLayer : public HciLayer {
  void EnqueueCommand(
      std::unique_ptr<CommandPacketBuilder> command,
      common::ContextualOnceCallback<void(CommandCompleteView)> on_complete) override {
    std::lock_guard<std::mutex> lock(mutex_);
    command_queue_.push(std::move(command));
    command_complete_callbacks.push_front(std::move(on_complete));
    if (command_promise_ != nullptr) {
@@ -70,13 +71,17 @@ class TestHciLayer : public HciLayer {
  }

  CommandPacketView GetCommandPacket(OpCode op_code) {
    if (!command_queue_.empty() && command_future_ != nullptr) {
      command_promise_.reset();
    if (!command_queue_.empty()) {
      std::lock_guard<std::mutex> lock(mutex_);
      if (command_future_ != nullptr) {
        command_future_.reset();
        command_promise_.reset();
      }
    } else if (command_future_ != nullptr) {
      auto result = command_future_->wait_for(std::chrono::milliseconds(1000));
      EXPECT_NE(std::future_status::timeout, result);
    }
    std::lock_guard<std::mutex> lock(mutex_);
    ASSERT_LOG(
        !command_queue_.empty(), "Expecting command %s but command queue was empty", OpCodeText(op_code).c_str());
    CommandPacketView command_packet_view = GetLastCommand();
@@ -108,6 +113,7 @@ class TestHciLayer : public HciLayer {
  std::queue<std::unique_ptr<CommandPacketBuilder>> command_queue_;
  std::unique_ptr<std::promise<void>> command_promise_;
  std::unique_ptr<std::future<void>> command_future_;
  mutable std::mutex mutex_;
};

class RotatorClient : public LeAddressManagerCallback {