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

Commit eeafe9a5 authored by Jakub Tyszkowski's avatar Jakub Tyszkowski
Browse files

LeAudio: Fix flaky unit tests

Since the audio context notifications on the other thread invalidate the
existing audio set configurations. We should wait of rthe main loop to
finish the job before we start to use these configurations.

We are not consistent in every test with the synchronisation after the
injected notification. This patch moves the synchronisation to a
notification sendind helper function, eliminating the need for new tests
to call the synchronisation routine manually.

Bug: 302775890
Test: atest --host UnicastTest#StartStream_AvailableContextTypeNotifiedLater --rerun-until-failure=1000
Change-Id: I38a892c829779616ac20b0dbb283dcef5204c4e0
parent 94882978
Loading
Loading
Loading
Loading
+6 −9
Original line number Diff line number Diff line
@@ -108,12 +108,16 @@ extern "C" const char* __asan_default_options() {
}

std::atomic<int> num_async_tasks;
static base::MessageLoop* message_loop_;
bluetooth::common::MessageLoopThread message_loop_thread("test message loop");
bluetooth::common::MessageLoopThread* get_main_thread() {
  return &message_loop_thread;
}

bt_status_t do_in_main_thread(const base::Location& from_here,
                              base::OnceClosure task) {
  if (!message_loop_) return BT_STATUS_FAIL;

  // Wrap the task with task counter so we could later know if there are
  // any callbacks scheduled and we should wait before performing some actions
  if (!message_loop_thread.DoInThread(
@@ -138,7 +142,6 @@ bt_status_t do_in_main_thread_delayed(const base::Location& from_here,
  return do_in_main_thread(from_here, std::move(task));
}

static base::MessageLoop* message_loop_;
base::MessageLoop* get_main_message_loop() { return message_loop_; }

static void init_message_loop_thread() {
@@ -527,6 +530,7 @@ class UnicastTestNoInit : public Test {
                                   AudioContexts source_ctxs) {
    /* 0x0077 pacs->supp_contexts_char + 1 */
    InjectContextTypes(test_address, conn_id, 0x0077, sink_ctxs, source_ctxs);
    SyncOnMainLoop();
  }

  void InjectAvailableContextTypes(const RawAddress& test_address,
@@ -534,6 +538,7 @@ class UnicastTestNoInit : public Test {
                                   AudioContexts source_ctxs) {
    /* 0x0074 is pacs->avail_contexts_char + 1 */
    InjectContextTypes(test_address, conn_id, 0x0074, sink_ctxs, source_ctxs);
    SyncOnMainLoop();
  }

  void SetUpMockGatt() {
@@ -3917,7 +3922,6 @@ TEST_F(UnicastTest, HandleResumeWithoutMetadataUpdateOnLocalSink) {
  auto source_available_context = types::kLeAudioContextAllRemoteSource;
  InjectAvailableContextTypes(test_address0, 1, sink_available_context,
                              source_available_context);
  SyncOnMainLoop();

  StopStreaming(group_id, true);
  SyncOnMainLoop();
@@ -5588,7 +5592,6 @@ TEST_F(UnicastTest, TwoEarbuds2ndReleaseAseRemoveAvailableContextAndBack) {
  /* Simulate available context type being cleared */
  InjectAvailableContextTypes(device->address_, device->conn_id_,
                              types::AudioContexts(0), types::AudioContexts(0));
  SyncOnMainLoop();

  /* Simulate ASE releasing and CIS Disconnection */
  for (auto& ase : device->ases_) {
@@ -5615,7 +5618,6 @@ TEST_F(UnicastTest, TwoEarbuds2ndReleaseAseRemoveAvailableContextAndBack) {
  InjectAvailableContextTypes(device->address_, device->conn_id_,
                              types::kLeAudioContextAllTypes,
                              types::kLeAudioContextAllTypes);
  SyncOnMainLoop();

  /* Check both devices are streaming */
  cis_count_out = 2;
@@ -5683,10 +5685,8 @@ TEST_F(UnicastTest, StartStream_AvailableContextTypeNotifiedLater) {
      .Times(1);

  StartStreaming(AUDIO_USAGE_MEDIA, AUDIO_CONTENT_TYPE_MUSIC, group_id);

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

  // Expect two iso channel to be fed with data
  uint8_t cis_count_out = 2;
@@ -5779,7 +5779,6 @@ TEST_F(UnicastTest, ModifyContextTypeOnDeviceA_WhileDeviceB_IsDisconnected) {
                              source_supported_context);
  InjectAvailableContextTypes(test_address0, 1, sink_supported_context,
                              source_supported_context);
  SyncOnMainLoop();

  /* Android starts stream. */
  EXPECT_CALL(mock_state_machine_, StartStream(_, _, _, _)).Times(0);
@@ -6063,8 +6062,6 @@ TEST_F(UnicastTest, StartStreamToSupportedContextTypeThenMixUnavailable) {
  InjectAvailableContextTypes(test_address1, 2, sink_supported_context,
                              source_supported_context);

  SyncOnMainLoop();

  // Verify cache has been removed due to available context change
  ASSERT_FALSE(group
                   ->GetCachedCodecConfigurationByDirection(