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

Commit ece7c9e8 authored by Hansong Zhang's avatar Hansong Zhang
Browse files

Fix flaky gd unit tests

Test: $ANDROID_HOST_OUT/nativetest64/bluetooth_test_gd/bluetooth_test_gd
Bug: 147765784
Bug: 146363673
Bug: 146363318
Bug: 146363313
Change-Id: I382ccfd2b561c2154d2bef6c6b87db4e82ae7acb
parent ed916606
Loading
Loading
Loading
Loading
+9 −1
Original line number Diff line number Diff line
@@ -397,6 +397,14 @@ class AclManagerWithConnectionTest : public AclManagerTest {
    connection_->RegisterCallbacks(&mock_connection_management_callbacks_, client_handler_);
  }

  void sync_client_handler() {
    std::promise<void> promise;
    auto future = promise.get_future();
    client_handler_->Post(common::BindOnce(&std::promise<void>::set_value, common::Unretained(&promise)));
    auto future_status = future.wait_for(std::chrono::seconds(1));
    EXPECT_EQ(future_status, std::future_status::ready);
  }

  uint16_t handle_;
  std::shared_ptr<AclConnection> connection_;

@@ -1092,7 +1100,7 @@ TEST_F(AclManagerWithConnectionTest, send_read_rssi) {
  auto packet = test_hci_layer_->GetCommandPacket(OpCode::READ_RSSI);
  auto command_view = ReadRssiView::Create(packet);
  ASSERT(command_view.IsValid());

  sync_client_handler();
  EXPECT_CALL(mock_connection_management_callbacks_, OnReadRssiComplete(0x00));
  uint8_t num_packets = 1;
  test_hci_layer_->IncomingEvent(ReadRssiCompleteBuilder::Create(num_packets, ErrorCode::SUCCESS, handle_, 0x00));
+4 −1
Original line number Diff line number Diff line
@@ -30,6 +30,7 @@ namespace bluetooth {
namespace l2cap {
namespace classic {
namespace internal {
namespace {

class L2capDynamicServiceManagerTest : public ::testing::Test {
 public:
@@ -62,7 +63,8 @@ class L2capDynamicServiceManagerTest : public ::testing::Test {
    std::promise<void> promise;
    auto future = promise.get_future();
    user_handler_->Post(common::BindOnce(&std::promise<void>::set_value, common::Unretained(&promise)));
    future.wait_for(std::chrono::milliseconds(3));
    auto future_status = future.wait_for(std::chrono::seconds(1));
    EXPECT_EQ(future_status, std::future_status::ready);
  }

  DynamicChannelServiceManagerImpl* manager_ = nullptr;
@@ -101,6 +103,7 @@ TEST_F(L2capDynamicServiceManagerTest, register_classic_dynamic_channel_bad_cid)
  EXPECT_FALSE(service_registered_);
}

}  // namespace
}  // namespace internal
}  // namespace classic
}  // namespace l2cap
+32 −4
Original line number Diff line number Diff line
@@ -39,7 +39,7 @@ class L2capClassicFixedChannelImplTest : public ::testing::Test {
    std::promise<void> promise;
    auto future = promise.get_future();
    handler->Post(common::BindOnce(&std::promise<void>::set_value, common::Unretained(&promise)));
    future.wait_for(std::chrono::milliseconds(3));
    future.wait_for(std::chrono::seconds(1));
  }

 protected:
@@ -60,6 +60,8 @@ class L2capClassicFixedChannelImplTest : public ::testing::Test {

TEST_F(L2capClassicFixedChannelImplTest, get_device) {
  MockParameterProvider mock_parameter_provider;
  EXPECT_CALL(mock_parameter_provider, GetClassicLinkIdleDisconnectTimeout())
      .WillRepeatedly(Return(std::chrono::seconds(5)));
  MockLink mock_classic_link(l2cap_handler_, &mock_parameter_provider);
  hci::AddressWithType device{hci::Address{{0x01, 0x02, 0x03, 0x04, 0x05, 0x06}},
                              hci::AddressType::PUBLIC_IDENTITY_ADDRESS};
@@ -70,6 +72,8 @@ TEST_F(L2capClassicFixedChannelImplTest, get_device) {

TEST_F(L2capClassicFixedChannelImplTest, close_triggers_callback) {
  MockParameterProvider mock_parameter_provider;
  EXPECT_CALL(mock_parameter_provider, GetClassicLinkIdleDisconnectTimeout())
      .WillRepeatedly(Return(std::chrono::seconds(5)));
  MockLink mock_classic_link(l2cap_handler_, &mock_parameter_provider);
  hci::AddressWithType device{hci::Address{{0x01, 0x02, 0x03, 0x04, 0x05, 0x06}},
                              hci::AddressType::PUBLIC_IDENTITY_ADDRESS};
@@ -92,6 +96,8 @@ TEST_F(L2capClassicFixedChannelImplTest, close_triggers_callback) {

TEST_F(L2capClassicFixedChannelImplTest, register_callback_after_close_should_call_immediately) {
  MockParameterProvider mock_parameter_provider;
  EXPECT_CALL(mock_parameter_provider, GetClassicLinkIdleDisconnectTimeout())
      .WillRepeatedly(Return(std::chrono::seconds(5)));
  MockLink mock_classic_link(l2cap_handler_, &mock_parameter_provider);
  hci::AddressWithType device{hci::Address{{0x01, 0x02, 0x03, 0x04, 0x05, 0x06}},
                              hci::AddressType::PUBLIC_IDENTITY_ADDRESS};
@@ -114,6 +120,8 @@ TEST_F(L2capClassicFixedChannelImplTest, register_callback_after_close_should_ca

TEST_F(L2capClassicFixedChannelImplTest, close_twice_should_fail) {
  MockParameterProvider mock_parameter_provider;
  EXPECT_CALL(mock_parameter_provider, GetClassicLinkIdleDisconnectTimeout())
      .WillRepeatedly(Return(std::chrono::seconds(5)));
  MockLink mock_classic_link(l2cap_handler_, &mock_parameter_provider);
  hci::AddressWithType device{hci::Address{{0x01, 0x02, 0x03, 0x04, 0x05, 0x06}},
                              hci::AddressType::PUBLIC_IDENTITY_ADDRESS};
@@ -139,6 +147,8 @@ TEST_F(L2capClassicFixedChannelImplTest, close_twice_should_fail) {

TEST_F(L2capClassicFixedChannelImplTest, multiple_registeration_should_fail) {
  MockParameterProvider mock_parameter_provider;
  EXPECT_CALL(mock_parameter_provider, GetClassicLinkIdleDisconnectTimeout())
      .WillRepeatedly(Return(std::chrono::seconds(5)));
  MockLink mock_classic_link(l2cap_handler_, &mock_parameter_provider);
  hci::AddressWithType device{hci::Address{{0x01, 0x02, 0x03, 0x04, 0x05, 0x06}},
                              hci::AddressType::PUBLIC_IDENTITY_ADDRESS};
@@ -160,6 +170,8 @@ TEST_F(L2capClassicFixedChannelImplTest, multiple_registeration_should_fail) {

TEST_F(L2capClassicFixedChannelImplTest, call_acquire_before_registeration_should_fail) {
  MockParameterProvider mock_parameter_provider;
  EXPECT_CALL(mock_parameter_provider, GetClassicLinkIdleDisconnectTimeout())
      .WillRepeatedly(Return(std::chrono::seconds(5)));
  MockLink mock_classic_link(l2cap_handler_, &mock_parameter_provider);
  hci::AddressWithType device{hci::Address{{0x01, 0x02, 0x03, 0x04, 0x05, 0x06}},
                              hci::AddressType::PUBLIC_IDENTITY_ADDRESS};
@@ -170,6 +182,8 @@ TEST_F(L2capClassicFixedChannelImplTest, call_acquire_before_registeration_shoul

TEST_F(L2capClassicFixedChannelImplTest, call_release_before_registeration_should_fail) {
  MockParameterProvider mock_parameter_provider;
  EXPECT_CALL(mock_parameter_provider, GetClassicLinkIdleDisconnectTimeout())
      .WillRepeatedly(Return(std::chrono::seconds(5)));
  MockLink mock_classic_link(l2cap_handler_, &mock_parameter_provider);
  hci::AddressWithType device{hci::Address{{0x01, 0x02, 0x03, 0x04, 0x05, 0x06}},
                              hci::AddressType::PUBLIC_IDENTITY_ADDRESS};
@@ -180,6 +194,10 @@ TEST_F(L2capClassicFixedChannelImplTest, call_release_before_registeration_shoul

TEST_F(L2capClassicFixedChannelImplTest, test_acquire_release_channel) {
  MockParameterProvider mock_parameter_provider;
  EXPECT_CALL(mock_parameter_provider, GetClassicLinkIdleDisconnectTimeout())
      .WillRepeatedly(Return(std::chrono::seconds(5)));
  EXPECT_CALL(mock_parameter_provider, GetClassicLinkIdleDisconnectTimeout())
      .WillRepeatedly(Return(std::chrono::seconds(5)));
  MockLink mock_classic_link(l2cap_handler_, &mock_parameter_provider);
  hci::AddressWithType device{hci::Address{{0x01, 0x02, 0x03, 0x04, 0x05, 0x06}},
                              hci::AddressType::PUBLIC_IDENTITY_ADDRESS};
@@ -190,7 +208,8 @@ TEST_F(L2capClassicFixedChannelImplTest, test_acquire_release_channel) {
  auto user_handler = std::make_unique<os::Handler>(thread_);
  hci::ErrorCode my_status = hci::ErrorCode::SUCCESS;
  fixed_channel_impl.RegisterOnCloseCallback(
      user_handler.get(), common::testing::BindLambdaForTesting([&](hci::ErrorCode status) { my_status = status; }));
      user_handler.get(),
      common::testing::BindLambdaForTesting([&my_status](hci::ErrorCode status) { my_status = status; }));

  // Default should be false
  EXPECT_FALSE(fixed_channel_impl.IsAcquired());
@@ -209,6 +228,8 @@ TEST_F(L2capClassicFixedChannelImplTest, test_acquire_release_channel) {

TEST_F(L2capClassicFixedChannelImplTest, test_acquire_after_close) {
  MockParameterProvider mock_parameter_provider;
  EXPECT_CALL(mock_parameter_provider, GetClassicLinkIdleDisconnectTimeout())
      .WillRepeatedly(Return(std::chrono::seconds(5)));
  MockLink mock_classic_link(l2cap_handler_, &mock_parameter_provider);
  hci::AddressWithType device{hci::Address{{0x01, 0x02, 0x03, 0x04, 0x05, 0x06}},
                              hci::AddressType::PUBLIC_IDENTITY_ADDRESS};
@@ -218,12 +239,19 @@ TEST_F(L2capClassicFixedChannelImplTest, test_acquire_after_close) {
  // Register on close callback
  auto user_handler = std::make_unique<os::Handler>(thread_);
  hci::ErrorCode my_status = hci::ErrorCode::SUCCESS;
  fixed_channel_impl.RegisterOnCloseCallback(
      user_handler.get(), common::testing::BindLambdaForTesting([&](hci::ErrorCode status) { my_status = status; }));
  std::promise<void> promise;
  auto future = promise.get_future();
  fixed_channel_impl.RegisterOnCloseCallback(user_handler.get(),
                                             common::testing::BindLambdaForTesting([&](hci::ErrorCode status) {
                                               my_status = status;
                                               promise.set_value();
                                             }));

  // Channel closure should trigger such callback
  fixed_channel_impl.OnClosed(hci::ErrorCode::REMOTE_USER_TERMINATED_CONNECTION);
  SyncHandler(user_handler.get());
  auto future_status = future.wait_for(std::chrono::seconds(1));
  EXPECT_EQ(future_status, std::future_status::ready);
  EXPECT_EQ(hci::ErrorCode::REMOTE_USER_TERMINATED_CONNECTION, my_status);

  // Release or Acquire after closing should crash
+71 −25
Original line number Diff line number Diff line
@@ -39,6 +39,7 @@ namespace bluetooth {
namespace l2cap {
namespace classic {
namespace internal {
namespace {

using hci::testing::MockAclConnection;
using hci::testing::MockAclManager;
@@ -53,7 +54,7 @@ using ::testing::Return;
using ::testing::SaveArg;

constexpr static auto kTestIdleDisconnectTimeoutLong = std::chrono::milliseconds(1000);
constexpr static auto kTestIdleDisconnectTimeoutShort = std::chrono::milliseconds(30);
constexpr static auto kTestIdleDisconnectTimeoutShort = std::chrono::milliseconds(1000);

class L2capClassicLinkManagerTest : public ::testing::Test {
 public:
@@ -122,16 +123,27 @@ TEST_F(L2capClassicLinkManagerTest, connect_fixed_channel_service_without_acl) {
  EXPECT_CALL(*acl_connection, RegisterDisconnectCallback(_, l2cap_handler_)).Times(1);
  EXPECT_CALL(*acl_connection, GetAddress()).WillRepeatedly(Return(device));
  std::unique_ptr<FixedChannel> channel_1, channel_2;
  EXPECT_CALL(mock_service_1, NotifyChannelCreation(_)).WillOnce([&channel_1](std::unique_ptr<FixedChannel> channel) {
  std::promise<void> promise_1, promise_2;
  auto future_1 = promise_1.get_future();
  auto future_2 = promise_2.get_future();
  EXPECT_CALL(mock_service_1, NotifyChannelCreation(_))
      .WillOnce([&channel_1, &promise_1](std::unique_ptr<FixedChannel> channel) {
        channel_1 = std::move(channel);
        promise_1.set_value();
      });
  EXPECT_CALL(mock_service_2, NotifyChannelCreation(_)).WillOnce([&channel_2](std::unique_ptr<FixedChannel> channel) {
  EXPECT_CALL(mock_service_2, NotifyChannelCreation(_))
      .WillOnce([&channel_2, &promise_2](std::unique_ptr<FixedChannel> channel) {
        channel_2 = std::move(channel);
        promise_2.set_value();
      });
  hci_callback_handler->Post(common::BindOnce(&hci::ConnectionCallbacks::OnConnectSuccess,
                                              common::Unretained(hci_connection_callbacks), std::move(acl_connection)));
  SyncHandler(hci_callback_handler);
  auto future_1_status = future_1.wait_for(kTestIdleDisconnectTimeoutShort);
  EXPECT_EQ(future_1_status, std::future_status::ready);
  EXPECT_NE(channel_1, nullptr);
  auto future_2_status = future_2.wait_for(kTestIdleDisconnectTimeoutShort);
  EXPECT_EQ(future_2_status, std::future_status::ready);
  EXPECT_NE(channel_2, nullptr);

  // Step 4: Calling ConnectServices() to the same device will no trigger another connection attempt
@@ -279,16 +291,27 @@ TEST_F(L2capClassicLinkManagerTest, not_acquiring_channels_should_disconnect_acl
  std::unique_ptr<MockAclConnection> acl_connection(raw_acl_connection);
  EXPECT_CALL(*acl_connection, GetAddress()).WillRepeatedly(Return(device));
  std::unique_ptr<FixedChannel> channel_1, channel_2;
  EXPECT_CALL(mock_service_1, NotifyChannelCreation(_)).WillOnce([&channel_1](std::unique_ptr<FixedChannel> channel) {
  std::promise<void> promise_1, promise_2;
  auto future_1 = promise_1.get_future();
  auto future_2 = promise_2.get_future();
  EXPECT_CALL(mock_service_1, NotifyChannelCreation(_))
      .WillOnce([&channel_1, &promise_1](std::unique_ptr<FixedChannel> channel) {
        channel_1 = std::move(channel);
        promise_1.set_value();
      });
  EXPECT_CALL(mock_service_2, NotifyChannelCreation(_)).WillOnce([&channel_2](std::unique_ptr<FixedChannel> channel) {
  EXPECT_CALL(mock_service_2, NotifyChannelCreation(_))
      .WillOnce([&channel_2, &promise_2](std::unique_ptr<FixedChannel> channel) {
        channel_2 = std::move(channel);
        promise_2.set_value();
      });
  hci_callback_handler->Post(common::BindOnce(&hci::ConnectionCallbacks::OnConnectSuccess,
                                              common::Unretained(hci_connection_callbacks), std::move(acl_connection)));
  SyncHandler(hci_callback_handler);
  auto future_1_status = future_1.wait_for(kTestIdleDisconnectTimeoutShort);
  EXPECT_EQ(future_1_status, std::future_status::ready);
  EXPECT_NE(channel_1, nullptr);
  auto future_2_status = future_2.wait_for(kTestIdleDisconnectTimeoutShort);
  EXPECT_EQ(future_2_status, std::future_status::ready);
  EXPECT_NE(channel_2, nullptr);
  hci::ErrorCode status_1 = hci::ErrorCode::SUCCESS;
  channel_1->RegisterOnCloseCallback(
@@ -347,16 +370,27 @@ TEST_F(L2capClassicLinkManagerTest, acquiring_channels_should_not_disconnect_acl
  std::unique_ptr<MockAclConnection> acl_connection(raw_acl_connection);
  EXPECT_CALL(*acl_connection, GetAddress()).WillRepeatedly(Return(device));
  std::unique_ptr<FixedChannel> channel_1, channel_2;
  EXPECT_CALL(mock_service_1, NotifyChannelCreation(_)).WillOnce([&channel_1](std::unique_ptr<FixedChannel> channel) {
  std::promise<void> promise_1, promise_2;
  auto future_1 = promise_1.get_future();
  auto future_2 = promise_2.get_future();
  EXPECT_CALL(mock_service_1, NotifyChannelCreation(_))
      .WillOnce([&channel_1, &promise_1](std::unique_ptr<FixedChannel> channel) {
        channel_1 = std::move(channel);
        promise_1.set_value();
      });
  EXPECT_CALL(mock_service_2, NotifyChannelCreation(_)).WillOnce([&channel_2](std::unique_ptr<FixedChannel> channel) {
  EXPECT_CALL(mock_service_2, NotifyChannelCreation(_))
      .WillOnce([&channel_2, &promise_2](std::unique_ptr<FixedChannel> channel) {
        channel_2 = std::move(channel);
        promise_2.set_value();
      });
  hci_callback_handler->Post(common::BindOnce(&hci::ConnectionCallbacks::OnConnectSuccess,
                                              common::Unretained(hci_connection_callbacks), std::move(acl_connection)));
  SyncHandler(hci_callback_handler);
  auto future_1_status = future_1.wait_for(kTestIdleDisconnectTimeoutShort);
  EXPECT_EQ(future_1_status, std::future_status::ready);
  EXPECT_NE(channel_1, nullptr);
  auto future_2_status = future_2.wait_for(kTestIdleDisconnectTimeoutShort);
  EXPECT_EQ(future_2_status, std::future_status::ready);
  EXPECT_NE(channel_2, nullptr);
  hci::ErrorCode status_1 = hci::ErrorCode::SUCCESS;
  channel_1->RegisterOnCloseCallback(
@@ -417,16 +451,27 @@ TEST_F(L2capClassicLinkManagerTest, acquiring_and_releasing_channels_should_even
  std::unique_ptr<MockAclConnection> acl_connection(raw_acl_connection);
  EXPECT_CALL(*acl_connection, GetAddress()).WillRepeatedly(Return(device));
  std::unique_ptr<FixedChannel> channel_1, channel_2;
  EXPECT_CALL(mock_service_1, NotifyChannelCreation(_)).WillOnce([&channel_1](std::unique_ptr<FixedChannel> channel) {
  std::promise<void> promise_1, promise_2;
  auto future_1 = promise_1.get_future();
  auto future_2 = promise_2.get_future();
  EXPECT_CALL(mock_service_1, NotifyChannelCreation(_))
      .WillOnce([&channel_1, &promise_1](std::unique_ptr<FixedChannel> channel) {
        channel_1 = std::move(channel);
        promise_1.set_value();
      });
  EXPECT_CALL(mock_service_2, NotifyChannelCreation(_)).WillOnce([&channel_2](std::unique_ptr<FixedChannel> channel) {
  EXPECT_CALL(mock_service_2, NotifyChannelCreation(_))
      .WillOnce([&channel_2, &promise_2](std::unique_ptr<FixedChannel> channel) {
        channel_2 = std::move(channel);
        promise_2.set_value();
      });
  hci_callback_handler->Post(common::BindOnce(&hci::ConnectionCallbacks::OnConnectSuccess,
                                              common::Unretained(hci_connection_callbacks), std::move(acl_connection)));
  SyncHandler(hci_callback_handler);
  auto future_1_status = future_1.wait_for(kTestIdleDisconnectTimeoutShort);
  EXPECT_EQ(future_1_status, std::future_status::ready);
  EXPECT_NE(channel_1, nullptr);
  auto future_2_status = future_2.wait_for(kTestIdleDisconnectTimeoutShort);
  EXPECT_EQ(future_2_status, std::future_status::ready);
  EXPECT_NE(channel_2, nullptr);
  hci::ErrorCode status_1 = hci::ErrorCode::SUCCESS;
  channel_1->RegisterOnCloseCallback(
@@ -455,6 +500,7 @@ TEST_F(L2capClassicLinkManagerTest, acquiring_and_releasing_channels_should_even
  user_handler->Clear();
}

}  // namespace
}  // namespace internal
}  // namespace classic
}  // namespace l2cap
+86 −34

File changed.

Preview size limit exceeded, changes collapsed.