Repository: flutter
Issue: [Regression, Linux]: Tests need pump to succeed
Labels: a: tests,c: regression,framework,platform-linux,a: desktop,f: integration_test,has reproducible steps,P2,found in release: 3.16,found in release: 3.18,team-linux,triaged-linux

Ref: https://github.com/bluefireteam/audioplayers/issues/1556

### Steps to reproduce

1. Update Flutter to `5def6f2` or to `master`: git checkout `5def6f2`, then `flutter doctor -v`
1. Prepare example:
    - Download example code from [audioplayers](https://github.com/bluefireteam/audioplayers/tree/87f3cb7e47e2103d2079a3dfe6aebe80c8a76c3d/packages/audioplayers/example) & Update to the latest dependencies with `melos bs`
    - OR use below code sample.
1. Execute this integration test on linux: On [online example](https://github.com/bluefireteam/audioplayers/blob/fd33b1d073280797cdd88fb6324cc1906bfd5957/packages/audioplayers/example/integration_test/platform_test.dart#L36) via `flutter test integration_test/platform_test.dart -d linux  --plain-name 'Throw PlatformException, when loading invalid file'
` or use provided snipped in a Linux integration environment.

Unfortunately I haven't had the time to determine the source, but this bug happened from time to time before on other places, but not at this place. But now it is consistently reproducible since this change: https://github.com/flutter/flutter/commit/5def6f2bab65ab05f772a8e55ec2fff71822090d
 

### Expected results

Test should succeed without needing a pump, see https://github.com/bluefireteam/audioplayers/issues/1556.


### Actual results

Test fail due to a missing pump. This pump in only needed on Linux, not on other platforms. Audioplayers also isn't dependent on any frame, just on the native platform channel, so it should not need any pump statement to be able to work properly.
See: https://github.com/bluefireteam/audioplayers/blob/fd33b1d073280797cdd88fb6324cc1906bfd5957/packages/audioplayers/example/integration_test/lib/lib_test_utils.dart#L7

Worked without pump: ffecedb
Now needs a pump after: 5def6f2

### Code sample

<details><summary>Code sample</summary>

```dart
import 'dart:async';

import 'package:audioplayers/audioplayers.dart';
import 'package:audioplayers_platform_interface/audioplayers_platform_interface.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:integration_test/integration_test.dart';

void main() async {
  IntegrationTestWidgetsFlutterBinding.ensureInitialized();

  group('Platform method channel', () {
    late AudioplayersPlatformInterface platform;
    late String playerId;

    testWidgets(
      'Throw PlatformException, when loading invalid file',
      (tester) async {
        platform = AudioplayersPlatformInterface.instance;
        playerId = 'somePlayerId';
        await platform.create(playerId);
        try {
          // Throws PlatformException via MethodChannel:
          await tester.prepareSource(
            playerId: playerId,
            platform: platform,
            source: AssetSource('invalid.txt'),
          );
          fail('PlatformException not thrown');
        } on PlatformException catch (e) {
          expect(e.message, startsWith('Failed to set source.'));
        }
        await tester.pump();
        await platform.dispose(playerId);
      },
    );
  });
}

extension on WidgetTester {
  Future<void> prepareSource({
    required String playerId,
    required AudioplayersPlatformInterface platform,
    required Source source,
  }) async {
    final eventStream = platform.getEventStream(playerId);
    final preparedCompleter = Completer<void>();
    late StreamSubscription<bool> onPreparedSub;
    onPreparedSub = eventStream
        .where((event) => event.eventType == AudioEventType.prepared)
        .map((event) => event.isPrepared!)
        .listen(
      (isPrepared) async {
        if (isPrepared) {
          preparedCompleter.complete();
          await onPreparedSub.cancel();
        }
      },
      onError: (Object e, [StackTrace? st]) async {
        if (!preparedCompleter.isCompleted) {
          preparedCompleter.completeError(e, st);
          await onPreparedSub.cancel();
        }
      },
    );
    await pump();
    if (source is UrlSource) {
      await platform.setSourceUrl(playerId, source.url);
    } else if (source is AssetSource) {
      final cachePath = await AudioCache.instance.loadPath(source.path);
      await platform.setSourceUrl(playerId, cachePath, isLocal: true);
    } else if (source is BytesSource) {
      await platform.setSourceBytes(playerId, source.bytes);
    }
    // FIXME: Introduced in Flutter 3.16.0-0.0.pre (5def6f2), still worked without pump in ffecedb
    // await pump();
    await preparedCompleter.future.timeout(const Duration(seconds: 30));
  }
}

```

</details>


### Logs

<details><summary>Logs</summary>

```console
 flutter test integration_test/platform_example_test.dart -d linux
00:00 +0: loading /mnt/d/Tmp/Coding/git/bluefireteam/audioplayers/packages/audioplayers/example/integration_test/platform_example_test.dart                       B00:40 +0: loading /mnt/d/Tmp/Coding/git/bluefireteam/audioplayers/packages/audioplayers/example/integration_test/platform_example_test.dart                       
01:19 +0: Platform method channel Throw PlatformException, when loading invalid file                                                                              
══╡ EXCEPTION CAUGHT BY FLUTTER TEST FRAMEWORK ╞════════════════════════════════════════════════════
The following TimeoutException was thrown running a test:
TimeoutException after 0:00:30.000000: Future not completed

When the exception was thrown, this was the stack

The test description was:
  Throw PlatformException, when loading invalid file
════════════════════════════════════════════════════════════════════════════════════════════════════
01:19 +0 -1: Platform method channel Throw PlatformException, when loading invalid file [E]                                                                       
  Test failed. See exception logs above.
  The test description was: Throw PlatformException, when loading invalid file


To run this test again: /opt/flutter/bin/cache/dart-sdk/bin/dart test /mnt/d/Tmp/Coding/git/bluefireteam/audioplayers/packages/audioplayers/example/integration_test/platform_example_test.dart -p vm --plain-name 'Platform method channel Throw PlatformException, when loading invalid file'
01:19 +0 -1: Some tests failed.             
```

</details>


### Flutter Doctor output

<details><summary>Doctor output</summary>

```console
[✓] Flutter (Channel master, 3.17.0-18.0.pre.61, on Ubuntu 22.04.3 LTS 5.15.133.1-microsoft-standard-WSL2, locale C.UTF-8)
    • Flutter version 3.17.0-18.0.pre.61 on channel master at /opt/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 918e336014 (73 minutes ago), 2023-11-30 06:47:12 -0800
    • Engine revision 35939ca853
    • Dart version 3.3.0 (build 3.3.0-170.0.dev)
    • DevTools version 2.30.0-dev.4

[✓] Android toolchain - develop for Android devices (Android SDK version 31.0.0)
    • Android SDK at /usr/lib/android-sdk
    • Platform android-31, build-tools 31.0.0
    • ANDROID_SDK_ROOT = /usr/lib/android-sdk
    • Java binary at: /usr/bin/java
    • Java version OpenJDK Runtime Environment (build 11.0.20.1+1-post-Ubuntu-0ubuntu122.04)
    • All Android licenses accepted.

[✓] Chrome - develop for the web
    • Chrome at google-chrome

[✓] Linux toolchain - develop for Linux desktop
    • Ubuntu clang version 14.0.0-1ubuntu1.1
    • cmake version 3.22.1
    • ninja version 1.10.1
    • pkg-config version 0.29.2

[!] Android Studio (not installed)
    • Android Studio not found; download from https://developer.android.com/studio/index.html
      (or visit https://flutter.dev/docs/get-started/install/linux#android-setup for detailed instructions).

[✓] IntelliJ IDEA Ultimate Edition (version 2023.2)
    • IntelliJ at /home/gustl/.cache/JetBrains/RemoteDev/dist/88bee55d9974b_ideaIU-2023.2
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart

[✓] Connected device (2 available)
    • Linux (desktop) • linux  • linux-x64      • Ubuntu 22.04.3 LTS 5.15.133.1-microsoft-standard-WSL2
    • Chrome (web)    • chrome • web-javascript • Google Chrome 104.0.5112.79

[✓] Network resources
    • All expected network resources are available.

! Doctor found issues in 1 category.

```

</details>