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

Commit 052a965d authored by Michał Narajowski's avatar Michał Narajowski
Browse files

le_audio/client: Fix RemoveWhileStreaming test

The order of operations was getting randomly mixed up
due to race condition between tester thread and main loop.

To fix this we put the tested command on the main loop as well.

Bug: 266872728
Test: atest bluetooth_le_audio_client_test
Tag: #feature

Change-Id: I4f9af01a411b3a0914145e7ade54d8fef039cc48
parent b4707b67
Loading
Loading
Loading
Loading
+3 −1
Original line number Original line Diff line number Diff line
@@ -1415,7 +1415,9 @@ class LeAudioClientImpl : public LeAudioClient {
      return;
      return;
    }
    }


    if (leAudioDevice->GetConnectionState() != DeviceConnectState::REMOVING) {
      leAudioDevice->SetConnectionState(DeviceConnectState::DISCONNECTING);
      leAudioDevice->SetConnectionState(DeviceConnectState::DISCONNECTING);
    }


    BtaGattQueue::Clean(leAudioDevice->conn_id_);
    BtaGattQueue::Clean(leAudioDevice->conn_id_);
    BTA_GATTC_Close(leAudioDevice->conn_id_);
    BTA_GATTC_Close(leAudioDevice->conn_id_);
+70 −8
Original line number Original line Diff line number Diff line
@@ -1253,8 +1253,15 @@ class UnicastTestNoInit : public Test {
          group->SetState(group->GetTargetState());
          group->SetState(group->GetTargetState());
          state_machine_callbacks_->StatusReportCb(
          state_machine_callbacks_->StatusReportCb(
              group->group_id_, GroupStreamStatus::RELEASING);
              group->group_id_, GroupStreamStatus::RELEASING);
          state_machine_callbacks_->StatusReportCb(group->group_id_,

                                                   GroupStreamStatus::IDLE);
          do_in_main_thread(
              FROM_HERE,
              base::BindOnce(
                  [](le_audio::LeAudioGroupStateMachine::Callbacks* cb,
                     int group_id) {
                    cb->StatusReportCb(group_id, GroupStreamStatus::IDLE);
                  },
                  state_machine_callbacks_, group->group_id_));
        });
        });
  }
  }


@@ -3195,7 +3202,17 @@ TEST_F(UnicastTest, RemoveDeviceWhenConnected) {
  EXPECT_CALL(mock_gatt_queue_, Clean(conn_id)).Times(AtLeast(1));
  EXPECT_CALL(mock_gatt_queue_, Clean(conn_id)).Times(AtLeast(1));
  EXPECT_CALL(mock_gatt_interface_, Close(conn_id)).Times(1);
  EXPECT_CALL(mock_gatt_interface_, Close(conn_id)).Times(1);


  LeAudioClient::Get()->RemoveDevice(test_address0);
  /*
   * StopStream will put calls on main_loop so to keep the correct order
   * of operations and to avoid races we put the test command on main_loop as
   * well.
   */
  do_in_main_thread(FROM_HERE, base::BindOnce(
                                   [](LeAudioClient* client,
                                      const RawAddress& test_address0) {
                                     client->RemoveDevice(test_address0);
                                   },
                                   LeAudioClient::Get(), test_address0));
  SyncOnMainLoop();
  SyncOnMainLoop();


  Mock::VerifyAndClearExpectations(&mock_btif_storage_);
  Mock::VerifyAndClearExpectations(&mock_btif_storage_);
@@ -3231,7 +3248,18 @@ TEST_F(UnicastTest, RemoveDeviceWhenConnecting) {
  EXPECT_CALL(mock_gatt_interface_, CancelOpen(gatt_if, test_address0, false))
  EXPECT_CALL(mock_gatt_interface_, CancelOpen(gatt_if, test_address0, false))
      .Times(1);
      .Times(1);


  LeAudioClient::Get()->RemoveDevice(test_address0);
  /*
   * StopStream will put calls on main_loop so to keep the correct order
   * of operations and to avoid races we put the test command on main_loop as
   * well.
   */
  do_in_main_thread(FROM_HERE, base::BindOnce(
                                   [](LeAudioClient* client,
                                      const RawAddress& test_address0) {
                                     client->RemoveDevice(test_address0);
                                   },
                                   LeAudioClient::Get(), test_address0));

  SyncOnMainLoop();
  SyncOnMainLoop();


  Mock::VerifyAndClearExpectations(&mock_gatt_interface_);
  Mock::VerifyAndClearExpectations(&mock_gatt_interface_);
@@ -3264,7 +3292,18 @@ TEST_F(UnicastTest, RemoveDeviceWhenGettingConnectionReady) {
  EXPECT_CALL(mock_gatt_interface_, CancelOpen(gatt_if, test_address0, false))
  EXPECT_CALL(mock_gatt_interface_, CancelOpen(gatt_if, test_address0, false))
      .Times(1);
      .Times(1);


  LeAudioClient::Get()->RemoveDevice(test_address0);
  /*
   * StopStream will put calls on main_loop so to keep the correct order
   * of operations and to avoid races we put the test command on main_loop as
   * well.
   */
  do_in_main_thread(FROM_HERE, base::BindOnce(
                                   [](LeAudioClient* client,
                                      const RawAddress& test_address0) {
                                     client->RemoveDevice(test_address0);
                                   },
                                   LeAudioClient::Get(), test_address0));

  SyncOnMainLoop();
  SyncOnMainLoop();


  Mock::VerifyAndClearExpectations(&mock_btif_storage_);
  Mock::VerifyAndClearExpectations(&mock_btif_storage_);
@@ -3439,7 +3478,17 @@ TEST_F(UnicastTest, RemoveWhileStreaming) {
              OnConnectionState(ConnectionState::DISCONNECTED, test_address0))
              OnConnectionState(ConnectionState::DISCONNECTED, test_address0))
      .Times(1);
      .Times(1);


  LeAudioClient::Get()->RemoveDevice(test_address0);
  /*
   * StopStream will put calls on main_loop so to keep the correct order
   * of operations and to avoid races we put the test command on main_loop as
   * well.
   */
  do_in_main_thread(FROM_HERE, base::BindOnce(
                                   [](LeAudioClient* client,
                                      const RawAddress& test_address0) {
                                     client->RemoveDevice(test_address0);
                                   },
                                   LeAudioClient::Get(), test_address0));


  SyncOnMainLoop();
  SyncOnMainLoop();
  Mock::VerifyAndClearExpectations(&mock_groups_module_);
  Mock::VerifyAndClearExpectations(&mock_groups_module_);
@@ -4394,8 +4443,14 @@ TEST_F(UnicastTest, NotifyAboutGroupTunrnedIdleEnabled) {
  EXPECT_CALL(*mock_le_audio_source_hal_client_, OnDestroyed()).Times(1);
  EXPECT_CALL(*mock_le_audio_source_hal_client_, OnDestroyed()).Times(1);
  EXPECT_CALL(*mock_le_audio_sink_hal_client_, OnDestroyed()).Times(1);
  EXPECT_CALL(*mock_le_audio_sink_hal_client_, OnDestroyed()).Times(1);


  LeAudioClient::Get()->GroupSetActive(bluetooth::groups::kGroupUnknown);
  do_in_main_thread(
      FROM_HERE, base::BindOnce(
                     [](LeAudioClient* client) {
                       client->GroupSetActive(bluetooth::groups::kGroupUnknown);
                     },
                     LeAudioClient::Get()));


  SyncOnMainLoop();
  Mock::VerifyAndClearExpectations(&mock_le_audio_source_hal_client_);
  Mock::VerifyAndClearExpectations(&mock_le_audio_source_hal_client_);


  LeAudioClient::Get()->SetInCall(false);
  LeAudioClient::Get()->SetInCall(false);
@@ -4455,8 +4510,15 @@ TEST_F(UnicastTest, NotifyAboutGroupTunrnedIdleDisabled) {
  EXPECT_CALL(*mock_le_audio_source_hal_client_, Stop()).Times(1);
  EXPECT_CALL(*mock_le_audio_source_hal_client_, Stop()).Times(1);
  EXPECT_CALL(*mock_le_audio_source_hal_client_, OnDestroyed()).Times(1);
  EXPECT_CALL(*mock_le_audio_source_hal_client_, OnDestroyed()).Times(1);
  EXPECT_CALL(*mock_le_audio_sink_hal_client_, OnDestroyed()).Times(1);
  EXPECT_CALL(*mock_le_audio_sink_hal_client_, OnDestroyed()).Times(1);
  LeAudioClient::Get()->GroupSetActive(bluetooth::groups::kGroupUnknown);


  do_in_main_thread(
      FROM_HERE, base::BindOnce(
                     [](LeAudioClient* client) {
                       client->GroupSetActive(bluetooth::groups::kGroupUnknown);
                     },
                     LeAudioClient::Get()));

  SyncOnMainLoop();
  Mock::VerifyAndClearExpectations(&mock_le_audio_source_hal_client_);
  Mock::VerifyAndClearExpectations(&mock_le_audio_source_hal_client_);


  LeAudioClient::Get()->SetInCall(false);
  LeAudioClient::Get()->SetInCall(false);