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

Commit df236fc7 authored by Jack He's avatar Jack He
Browse files

Test: Further stability improvements to ACL tests

* erase items before calling std::map::emplace in le_acl_facade
* add more logging to LE and classic ACL facades
* reduce GrpcEventQueue's wait time from 500ms to 100ms so that
  it can be terminated faster
* force a wait of 100ms after closing gRPC streams in Python

Bug: 228619929
Bug: 235872679
Bug: 232048916
Test: gd/cer/run --sl4a
Ignore-AOSP-First: cherry-pick

Merged-In: Idbb12b55db96b00bb695c54af1fd0efcc1f65841
Change-Id: Idbb12b55db96b00bb695c54af1fd0efcc1f65841
parent 438d0afe
Loading
Loading
Loading
Loading
+3 −1
Original line number Diff line number Diff line
@@ -13,7 +13,7 @@
#   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.

import time
from abc import ABC, abstractmethod


@@ -37,3 +37,5 @@ class Closable(ABC):
def safeClose(closable):
    if closable is not None:
        closable.close()
        # sleep for 100ms because GrpcEventQueue takes at most 100 ms to close
        time.sleep(0.1)
+2 −2
Original line number Diff line number Diff line
@@ -51,8 +51,8 @@ class GrpcEventQueue {
    using namespace std::chrono_literals;
    LOG_INFO("%s: Entering Loop", log_name_.c_str());
    while (!context->IsCancelled()) {
      // Wait for 500 ms so that cancellation can be caught in amortized 250 ms latency
      if (pending_events_.wait_to_take(500ms)) {
      // Wait for 100 ms so that cancellation can be caught in amortized 50 ms latency
      if (pending_events_.wait_to_take(100ms)) {
        LOG_INFO("%s: Got event from queue", log_name_.c_str());
        writer->Write(pending_events_.take());
      }
+13 −1
Original line number Diff line number Diff line
@@ -64,6 +64,7 @@ class AclManagerFacadeService : public AclManagerFacade::Service, public Connect
      ::grpc::ServerContext* context,
      const ConnectionMsg* request,
      ::grpc::ServerWriter<ConnectionEvent>* writer) override {
    LOG_INFO("peer=%s", request->address().c_str());
    Address peer;
    ASSERT(Address::FromString(request->address(), peer));
    acl_manager_->CreateConnection(peer);
@@ -77,6 +78,7 @@ class AclManagerFacadeService : public AclManagerFacade::Service, public Connect

  ::grpc::Status Disconnect(
      ::grpc::ServerContext* context, const HandleMsg* request, ::google::protobuf::Empty* response) override {
    LOG_INFO("handle=%d", request->handle());
    std::unique_lock<std::mutex> lock(acl_connections_mutex_);
    auto connection = acl_connections_.find(request->handle());
    if (connection == acl_connections_.end()) {
@@ -90,6 +92,7 @@ class AclManagerFacadeService : public AclManagerFacade::Service, public Connect

  ::grpc::Status AuthenticationRequested(
      ::grpc::ServerContext* context, const HandleMsg* request, ::google::protobuf::Empty* response) override {
    LOG_INFO("handle=%d", request->handle());
    std::unique_lock<std::mutex> lock(acl_connections_mutex_);
    auto connection = acl_connections_.find(request->handle());
    if (connection == acl_connections_.end()) {
@@ -118,12 +121,14 @@ class AclManagerFacadeService : public AclManagerFacade::Service, public Connect
      ::grpc::ServerContext* context,
      const ConnectionCommandMsg* request,
      ::google::protobuf::Empty* response) override {
    LOG_INFO("size=%zu", request->packet().size());
    auto command_view =
        ConnectionManagementCommandView::Create(AclCommandView::Create(CommandView::Create(PacketView<kLittleEndian>(
            std::make_shared<std::vector<uint8_t>>(request->packet().begin(), request->packet().end())))));
    if (!command_view.IsValid()) {
      return ::grpc::Status(::grpc::StatusCode::INVALID_ARGUMENT, "Invalid command packet");
    }
    LOG_INFO("opcode=%s", OpCodeText(command_view.GetOpCode()).c_str());
    switch (command_view.GetOpCode()) {
      case OpCode::AUTHENTICATION_REQUESTED: {
        GET_CONNECTION(AuthenticationRequestedView::Create(command_view));
@@ -266,6 +271,7 @@ class AclManagerFacadeService : public AclManagerFacade::Service, public Connect
      ::grpc::ServerContext* context,
      const google::protobuf::Empty* request,
      ::grpc::ServerWriter<ConnectionEvent>* writer) override {
    LOG_INFO("wait for one incoming connection");
    if (per_connection_events_.size() > current_connection_request_) {
      return ::grpc::Status(::grpc::StatusCode::RESOURCE_EXHAUSTED, "Only one outstanding connection is supported");
    }
@@ -276,6 +282,7 @@ class AclManagerFacadeService : public AclManagerFacade::Service, public Connect

  ::grpc::Status SendAclData(
      ::grpc::ServerContext* context, const AclData* request, ::google::protobuf::Empty* response) override {
    LOG_INFO("handle=%d, size=%zu", request->handle(), request->payload().size());
    std::promise<void> promise;
    auto future = promise.get_future();
    {
@@ -312,6 +319,7 @@ class AclManagerFacadeService : public AclManagerFacade::Service, public Connect

  ::grpc::Status FetchAclData(
      ::grpc::ServerContext* context, const HandleMsg* request, ::grpc::ServerWriter<AclData>* writer) override {
    LOG_INFO("handle=%d", request->handle());
    auto connection = acl_connections_.find(request->handle());
    if (connection == acl_connections_.end()) {
      return ::grpc::Status(::grpc::StatusCode::INVALID_ARGUMENT, "Invalid handle");
@@ -331,16 +339,19 @@ class AclManagerFacadeService : public AclManagerFacade::Service, public Connect
  }

  void on_incoming_acl(std::shared_ptr<ClassicAclConnection> connection, uint16_t handle) {
    LOG_INFO("handle=%d, addr=%s", connection->GetHandle(), connection->GetAddress().ToString().c_str());
    auto packet = connection->GetAclQueueEnd()->TryDequeue();
    auto connection_tracker = acl_connections_.find(handle);
    ASSERT_LOG(connection_tracker != acl_connections_.end(), "handle %d", handle);
    AclData acl_data;
    acl_data.set_handle(handle);
    acl_data.set_payload(std::string(packet->begin(), packet->end()));
    LOG_INFO("length=%zu", acl_data.payload().size());
    connection_tracker->second.pending_acl_data_.OnIncomingEvent(acl_data);
  }

  void OnConnectSuccess(std::unique_ptr<ClassicAclConnection> connection) override {
    LOG_INFO("handle=%d, addr=%s", connection->GetHandle(), connection->GetAddress().ToString().c_str());
    std::unique_lock<std::mutex> lock(acl_connections_mutex_);
    std::shared_ptr<ClassicAclConnection> shared_connection = std::move(connection);
    uint16_t handle = to_handle(current_connection_request_);
@@ -363,6 +374,7 @@ class AclManagerFacadeService : public AclManagerFacade::Service, public Connect
  }

  void OnConnectFail(Address address, ErrorCode reason) override {
    LOG_INFO("addr=%s, reason=%s", address.ToString().c_str(), ErrorCodeText(reason).c_str());
    std::unique_ptr<BasePacketBuilder> builder =
        ConnectionCompleteBuilder::Create(reason, 0, address, LinkType::ACL, Enable::DISABLED);
    ConnectionEvent fail;
@@ -516,7 +528,7 @@ class AclManagerFacadeService : public AclManagerFacade::Service, public Connect
    }

    void OnDisconnection(ErrorCode reason) override {
      LOG_INFO("OnDisconnection reason: %s", ErrorCodeText(reason).c_str());
      LOG_INFO("reason: %s", ErrorCodeText(reason).c_str());
      std::unique_ptr<BasePacketBuilder> builder =
          DisconnectionCompleteBuilder::Create(ErrorCode::SUCCESS, handle_, reason);
      ConnectionEvent disconnection;
+18 −2
Original line number Diff line number Diff line
@@ -65,6 +65,11 @@ class LeAclManagerFacadeService : public LeAclManagerFacade::Service, public LeC
      ::grpc::ServerContext* context,
      const CreateConnectionMsg* request,
      ::grpc::ServerWriter<LeConnectionEvent>* writer) override {
    LOG_INFO(
        "peer=%s, type=%d, id_direct=%d",
        request->peer_address().address().address().c_str(),
        request->peer_address().type(),
        request->is_direct());
    Address peer_address;
    ASSERT(Address::FromString(request->peer_address().address().address(), peer_address));
    AddressWithType peer(peer_address, static_cast<AddressType>(request->peer_address().type()));
@@ -92,6 +97,7 @@ class LeAclManagerFacadeService : public LeAclManagerFacade::Service, public LeC
      ::grpc::ServerContext* context,
      const ::blueberry::facade::BluetoothAddressWithType* request,
      google::protobuf::Empty* response) override {
    LOG_INFO("peer=%s, type=%d", request->address().address().c_str(), request->type());
    Address peer_address;
    ASSERT(Address::FromString(request->address().address(), peer_address));
    AddressWithType peer(peer_address, static_cast<AddressType>(request->type()));
@@ -109,6 +115,7 @@ class LeAclManagerFacadeService : public LeAclManagerFacade::Service, public LeC

  ::grpc::Status Disconnect(::grpc::ServerContext* context, const LeHandleMsg* request,
                            ::google::protobuf::Empty* response) override {
    LOG_INFO("handle=%d", request->handle());
    std::unique_lock<std::mutex> lock(acl_connections_mutex_);
    auto connection = acl_connections_.find(request->handle());
    if (connection == acl_connections_.end()) {
@@ -137,12 +144,14 @@ class LeAclManagerFacadeService : public LeAclManagerFacade::Service, public LeC
      ::grpc::ServerContext* context,
      const LeConnectionCommandMsg* request,
      ::google::protobuf::Empty* response) override {
    LOG_INFO("size=%zu", request->packet().size());
    auto command_view =
        ConnectionManagementCommandView::Create(AclCommandView::Create(CommandView::Create(PacketView<kLittleEndian>(
            std::make_shared<std::vector<uint8_t>>(request->packet().begin(), request->packet().end())))));
    if (!command_view.IsValid()) {
      return ::grpc::Status(::grpc::StatusCode::INVALID_ARGUMENT, "Invalid command packet");
    }
    LOG_INFO("opcode=%s", OpCodeText(command_view.GetOpCode()).c_str());
    switch (command_view.GetOpCode()) {
      case OpCode::DISCONNECT: {
        auto view = DisconnectView::Create(command_view);
@@ -160,6 +169,7 @@ class LeAclManagerFacadeService : public LeAclManagerFacade::Service, public LeC
      ::grpc::ServerContext* context,
      const google::protobuf::Empty* request,
      ::grpc::ServerWriter<LeConnectionEvent>* writer) override {
    LOG_INFO("wait for one incoming connection");
    if (incoming_connection_events_ != nullptr) {
      return ::grpc::Status(
          ::grpc::StatusCode::RESOURCE_EXHAUSTED, "Only one outstanding incoming connection is supported");
@@ -171,6 +181,7 @@ class LeAclManagerFacadeService : public LeAclManagerFacade::Service, public LeC

  ::grpc::Status AddDeviceToResolvingList(
      ::grpc::ServerContext* context, const IrkMsg* request, ::google::protobuf::Empty* response) override {
    LOG_INFO("peer=%s, type=%d", request->peer().address().address().c_str(), request->peer().type());
    Address peer_address;
    ASSERT(Address::FromString(request->peer().address().address(), peer_address));
    AddressWithType peer(peer_address, static_cast<AddressType>(request->peer().type()));
@@ -201,6 +212,7 @@ class LeAclManagerFacadeService : public LeAclManagerFacade::Service, public LeC

  ::grpc::Status SendAclData(
      ::grpc::ServerContext* context, const LeAclData* request, ::google::protobuf::Empty* response) override {
    LOG_INFO("handle=%d, size=%zu", request->handle(), request->payload().size());
    std::promise<void> promise;
    auto future = promise.get_future();
    {
@@ -236,6 +248,7 @@ class LeAclManagerFacadeService : public LeAclManagerFacade::Service, public LeC

  ::grpc::Status FetchAclData(
      ::grpc::ServerContext* context, const LeHandleMsg* request, ::grpc::ServerWriter<LeAclData>* writer) override {
    LOG_INFO("handle=%d", request->handle());
    auto connection = acl_connections_.find(request->handle());
    if (connection == acl_connections_.end()) {
      return ::grpc::Status(::grpc::StatusCode::INVALID_ARGUMENT, "Invalid handle");
@@ -251,6 +264,7 @@ class LeAclManagerFacadeService : public LeAclManagerFacade::Service, public LeC
  }

  void on_incoming_acl(std::shared_ptr<LeAclConnection> connection, uint16_t handle) {
    LOG_INFO("handle=%d, addr=%s", connection->GetHandle(), connection->GetRemoteAddress().ToString().c_str());
    auto packet = connection->GetAclQueueEnd()->TryDequeue();
    auto connection_tracker = acl_connections_.find(handle);
    ASSERT_LOG(connection_tracker != acl_connections_.end(), "handle %d", handle);
@@ -261,8 +275,7 @@ class LeAclManagerFacadeService : public LeAclManagerFacade::Service, public LeC
  }

  void OnLeConnectSuccess(AddressWithType peer, std::unique_ptr<LeAclConnection> connection) override {
    LOG_INFO("%s", peer.ToString().c_str());

    LOG_INFO("handle=%d, addr=%s", connection->GetHandle(), peer.ToString().c_str());
    std::unique_lock<std::mutex> lock(acl_connections_mutex_);
    std::shared_ptr<LeAclConnection> shared_connection = std::move(connection);
    uint16_t handle = shared_connection->GetHandle();
@@ -282,6 +295,7 @@ class LeAclManagerFacadeService : public LeAclManagerFacade::Service, public LeC
    } else {
      ASSERT_LOG(per_connection_events_.count(peer) > 0, "No connection request for %s", peer.ToString().c_str());
    }
    acl_connections_.erase(handle);
    acl_connections_.emplace(
        std::piecewise_construct,
        std::forward_as_tuple(handle),
@@ -301,6 +315,7 @@ class LeAclManagerFacadeService : public LeAclManagerFacade::Service, public LeC
  }

  void OnLeConnectFail(AddressWithType address, ErrorCode reason) override {
    LOG_INFO("addr=%s, reason=%s", address.ToString().c_str(), ErrorCodeText(reason).c_str());
    std::unique_ptr<BasePacketBuilder> builder = LeConnectionCompleteBuilder::Create(
        reason, 0, Role::CENTRAL, address.GetAddressType(), address.GetAddress(), 0, 0, 0, ClockAccuracy::PPM_20);
    LeConnectionEvent fail;
@@ -340,6 +355,7 @@ class LeAclManagerFacadeService : public LeAclManagerFacade::Service, public LeC
    void OnPhyUpdate(hci::ErrorCode hci_status, uint8_t tx_phy, uint8_t rx_phy) override {}
    void OnLocalAddressUpdate(AddressWithType address_with_type) override {}
    void OnDisconnection(ErrorCode reason) override {
      LOG_INFO("reason: %s", ErrorCodeText(reason).c_str());
      std::unique_ptr<BasePacketBuilder> builder =
          DisconnectionCompleteBuilder::Create(ErrorCode::SUCCESS, handle_, reason);
      LeConnectionEvent disconnection;