diff --git a/packages/flutter_tools/lib/src/commands/drive.dart b/packages/flutter_tools/lib/src/commands/drive.dart index 0d59fcb2fe..dacbd0212d 100644 --- a/packages/flutter_tools/lib/src/commands/drive.dart +++ b/packages/flutter_tools/lib/src/commands/drive.dart @@ -246,7 +246,19 @@ class DriveCommand extends RunCommandBase { webUri = residentRunner.uri; } - final LaunchResult result = await appStarter(this, webUri, package, applicationBinary != null); + // Attempt to launch the application up to 3 times, to validate whether it + // is possible to reduce flakiness by hardnening the launch code. + int attempt = 0; + LaunchResult result; + while (attempt < 3) { + // On attempts past 1, assume the application is built correctly and re-use it. + result = await appStarter(this, webUri, package, applicationBinary != null || attempt > 0); + if (result != null) { + break; + } + attempt += 1; + globals.printError('Application failed to start on attempt: $attempt'); + } if (result == null) { throwToolExit('Application failed to start. Will not run test. Quitting.', exitCode: 1); } diff --git a/packages/flutter_tools/test/commands.shard/hermetic/drive_test.dart b/packages/flutter_tools/test/commands.shard/hermetic/drive_test.dart index 0ed3aa0fbf..3e1ff4ea2d 100644 --- a/packages/flutter_tools/test/commands.shard/hermetic/drive_test.dart +++ b/packages/flutter_tools/test/commands.shard/hermetic/drive_test.dart @@ -99,7 +99,7 @@ void main() { testUsingContext('returns 1 when app fails to run', () async { testDeviceManager.addDevice(MockDevice()); - appStarter = expectAsync4((DriveCommand command, Uri webUri, ApplicationPackage package, bool prebuiltApplication) async => null); + appStarter = expectAsync4((DriveCommand command, Uri webUri, ApplicationPackage package, bool prebuiltApplication) async => null, count: 3); final String testApp = globals.fs.path.join(tempDir.path, 'test_driver', 'e2e.dart'); final String testFile = globals.fs.path.join(tempDir.path, 'test_driver', 'e2e_test.dart');