Repository: flutter
Issue: [flutter_tools] make integration test device connection timeout configurable
Labels: c: new feature,tool,f: integration_test,P3,team-tool,triaged-tool

I have been constructing the automated CI for the Flutter android apps using the Github actions. The error saying "Connecting to the VM Service timed out" occurred before running the integration test. When I removed the timeout code of connecting to the VM service in the flutter/packages/flutter_tools/lib/src/test/integration_test_device.dart, the integration test was worked well and it took about 20s for connecting to the VM service. Is there a solution like passing the argument of the timeout or starting the VM service before connecting?

# Details

Full code for an example occurring this error can be found in [this repository](https://github.com/nullist0/integration_test_example).

## flutter doctor -v
```text
[✓] Flutter (Channel stable, 2.8.1, on Ubuntu 20.04.3 LTS 5.11.0-1022-azure, locale C.UTF-8)
    • Flutter version 2.8.1 at /opt/hostedtoolcache/flutter/2.8.1-stable/x64
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 77d935af4d (10 days ago), 2021-12-16 08:37:33 -0800
    • Engine revision 890a5fca2e
    • Dart version 2.15.1

[✓] Android toolchain - develop for Android devices (Android SDK version 32.0.0)
    • Android SDK at /usr/local/lib/android/sdk
    • Platform android-32, build-tools 32.0.0
    • ANDROID_HOME = /usr/local/lib/android/sdk
    • ANDROID_SDK_ROOT = /usr/local/lib/android/sdk
    • Java binary at: /opt/hostedtoolcache/Java_Adopt_jdk/11.0.11-9/x64/bin/java
    • Java version OpenJDK Runtime Environment AdoptOpenJDK-11.0.11+9 (build 11.0.11+9)
    • All Android licenses accepted.

[✓] Chrome - develop for the web
    • Chrome at google-chrome

[!] 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).

[✓] Connected device (2 available)
    • Android SDK built for x86 (mobile) • emulator-5554 • android-x86    • Android 9 (API 28) (emulator)
    • Chrome (web)                       • chrome        • web-javascript • Google Chrome 96.0.4664.110
```

## Github workflow
```yml
name: build
on:
  push:
    branches:
      - master
  pull_request:
    branches:
      - master

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-java@v2
        with:
          distribution: 'adopt'
          java-version: '11'
      - name: Cache android SDK
        uses: actions/cache@v2
        id: android-sdk
        with:
          key: android-sdk
          path: |
            /usr/local/lib/android/adk
            ~/.android
      - name: Setup Android SDK
        if: steps.android-sdk.outputs.cache-hit != 'true'
        uses: android-actions/setup-android@v2
      - name: Setup Flutter SDK
        uses: subosito/flutter-action@v1
        with:
          channel: 'stable'
      - name: Run integration tests
        uses: reactivecircus/android-emulator-runner@v2
        with:
          api-level: 28
          script: |
            flutter doctor -v
            flutter test integration_test --verbose
```

## Error Message
```text
04:25 +0: loading /home/runner/work/integration_test_example/integration_test_example/integration_test/app_test.dart                                                                                   [+25080 ms] Observatory URL on device: http://127.0.0.1:45266/VC-ETjqMSiQ=/
[   +2 ms] executing: /usr/local/lib/android/sdk/platform-tools/adb -s emulator-5554 forward tcp:0 tcp:45266
[   +8 ms] 45937
[        ] Forwarded host port 45937 to device port 45266 for Observatory
[   +6 ms] test 0: Connecting to vm service
[  +19 ms] test 0: Observatory uri is available at http://127.0.0.1:45937/VC-ETjqMSiQ=/

04:26 +0: loading /home/runner/work/integration_test_example/integration_test_example/integration_test/app_test.dart                                                                                   
04:27 +0: loading /home/runner/work/integration_test_example/integration_test_example/integration_test/app_test.dart                                                                                   
04:28 +0: loading /home/runner/work/integration_test_example/integration_test_example/integration_test/app_test.dart                                                                                   
04:29 +0: loading /home/runner/work/integration_test_example/integration_test_example/integration_test/app_test.dart                                                                                   
04:30 +0: loading /home/runner/work/integration_test_example/integration_test_example/integration_test/app_test.dart                                                                                   [+5003 ms] test 0: error caught during test; sending to test framework
[   +2 ms] test 0: cleaning up...
[        ] test 0: ensuring test device is terminated.
[   +1 ms] executing: /usr/local/lib/android/sdk/platform-tools/adb -s emulator-5554 shell am force-stop com.example.flutter.integration.integration_test_example

04:30 +0 -1: loading /home/runner/work/integration_test_example/integration_test_example/integration_test/app_test.dart [E]                                                                            
  Failed to load "/home/runner/work/integration_test_example/integration_test_example/integration_test/app_test.dart": Connecting to the VM Service timed out.
  package:flutter_tools/src/test/integration_test_device.dart 75:24  IntegrationTestTestDevice.start.<fn>
  dart:async/zone.dart 1420:47                                       _rootRun
  dart:async/zone.dart 1328:19                                       _CustomZone.run
  dart:async/future_impl.dart 865:34                                 Future.timeout.<fn>
  dart:async/zone.dart 1420:47                                       _rootRun
  dart:async/zone.dart 1328:19                                       _CustomZone.run
  dart:async/zone.dart 1236:7                                        _CustomZone.runGuarded
  dart:async/zone.dart 1276:23                                       _CustomZone.bindCallbackGuarded.<fn>
  dart:async/zone.dart 1428:13                                       _rootRun
  dart:async/zone.dart 1328:19                                       _CustomZone.run
  dart:async/zone.dart 1260:23                                       _CustomZone.bindCallback.<fn>
  dart:async-patch/timer_patch.dart 18:15                            Timer._createTimer.<fn>
  dart:isolate-patch/timer_impl.dart 395:19                          _Timer._runTimers
  dart:isolate-patch/timer_impl.dart 426:5                           _Timer._handleMessage
  dart:isolate-patch/isolate_patch.dart 192:12                       _RawReceivePortImpl._handleMessage
```