Skip to content

Commit 176df83

Browse files
authored
[tool] Let the OS pick a port for chromedriver and wait for exit (#13355)
Part of flutter/flutter#194313 `DriveExamplesCommand` starts `chromedriver --port=4444` inside the per-example loop without waiting for it to bind, and stops it with `chromedriver.kill()` without awaiting `chromedriver.exitCode`. When a package has multiple web examples (e.g. `google_maps_flutter_web`'s `example/latest` and `example/3-64`), the dying `chromedriver` from the first example still holds port `4444` when the next example spawns a new `chromedriver --port=4444`, so the second `chromedriver` fails to bind and exits immediately: ``` Unable to start a WebDriver session for web testing. Make sure you have the correct WebDriver server (e.g. chromedriver) running at 4444. ``` Example CI failures: - https://ci.chromium.org/b/8667405073082275745 - https://ci.chromium.org/b/8667406160854692097 - https://ci.chromium.org/b/8667756860257169345 This PR: - Launches `chromedriver --port=0` and waits for `started successfully on port (\d+)` on stdout before running `flutter drive --driver-port=<port>`. - Waits for `chromedriver.exitCode` after `chromedriver.kill()` in a `finally` block.
1 parent 35a1e60 commit 176df83

3 files changed

Lines changed: 395 additions & 21 deletions

File tree

‎script/tool/CHANGELOG.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
* Exempts `test_apps/` from requiring version and changelog changes.
44
* Fixes `dart-test --package-tags` not matching any packages when the
55
repository is checked out into a directory named `packages`, as it is in CI.
6+
* Runs `chromedriver` for `drive-examples --run-chromedriver` on an
7+
OS-assigned port, and waits for it to exit before starting the next example.
68

79
## 0.14.4+4
810

‎script/tool/lib/src/drive_examples_command.dart‎

Lines changed: 175 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,6 @@ import 'common/repository_package.dart';
1818
const int _exitInvalidArgs = 2;
1919
const int _exitNoAvailableDevice = 3;
2020

21-
// From https://flutter.dev/to/integration-test-on-web
22-
const int _chromeDriverPort = 4444;
23-
2421
/// A command to run the integration tests for a package's example applications.
2522
class DriveExamplesCommand extends PackageLoopingCommand {
2623
/// Creates an instance of the drive command.
@@ -236,28 +233,22 @@ class DriveExamplesCommand extends PackageLoopingCommand {
236233

237234
testsRan = true;
238235
if (useFlutterDrive) {
239-
Process? chromedriver;
240-
if (getBoolArg(_chromeDriverFlag)) {
241-
print('Starting chromedriver on port $_chromeDriverPort');
242-
chromedriver = await processRunner.start('chromedriver', <String>[
243-
'--port=$_chromeDriverPort',
244-
]);
245-
}
246-
for (final driver in drivers) {
247-
final List<File> failingTargets = await _driveTests(
236+
final List<File> failingTargets;
237+
try {
238+
failingTargets = await _driveExample(
248239
example,
249-
driver,
240+
drivers,
250241
testTargets,
251242
deviceFlags: deviceFlags,
252243
exampleName: exampleName,
253244
);
254-
for (final failingTarget in failingTargets) {
255-
errors.add(getRelativePosixPath(failingTarget, from: package.directory));
256-
}
245+
} on _ChromeDriverStartException catch (e) {
246+
printError(e.message);
247+
errors.add('chromedriver failed to start for $exampleName');
248+
continue;
257249
}
258-
if (chromedriver != null) {
259-
print('Stopping chromedriver');
260-
chromedriver.kill();
250+
for (final failingTarget in failingTargets) {
251+
errors.add(getRelativePosixPath(failingTarget, from: package.directory));
261252
}
262253
} else {
263254
if (!await _runTests(example, deviceFlags: deviceFlags, testFiles: testTargets)) {
@@ -377,6 +368,79 @@ class DriveExamplesCommand extends PackageLoopingCommand {
377368
return true;
378369
}
379370

371+
/// Drives [testTargets] for [example] across all [drivers], starting and
372+
/// stopping `chromedriver` when `--run-chromedriver` is enabled.
373+
Future<List<File>> _driveExample(
374+
RepositoryPackage example,
375+
List<File> drivers,
376+
List<File> testTargets, {
377+
required List<String> deviceFlags,
378+
required String exampleName,
379+
}) async {
380+
if (!getBoolArg(_chromeDriverFlag)) {
381+
return _driveTestsForDrivers(
382+
example,
383+
drivers,
384+
testTargets,
385+
deviceFlags: deviceFlags,
386+
exampleName: exampleName,
387+
);
388+
}
389+
390+
final _ChromeDriver chromedriver = await _startChromeDriver();
391+
try {
392+
return await _driveTestsForDrivers(
393+
example,
394+
drivers,
395+
testTargets,
396+
deviceFlags: <String>[...deviceFlags, '--driver-port=${await chromedriver.port}'],
397+
exampleName: exampleName,
398+
);
399+
} finally {
400+
print('Stopping chromedriver');
401+
await chromedriver.stop();
402+
}
403+
}
404+
405+
/// Starts `chromedriver --port=0` and waits for it to report the bound port
406+
/// on stdout.
407+
Future<_ChromeDriver> _startChromeDriver() async {
408+
print('Starting chromedriver...');
409+
final chromedriver = _ChromeDriver(
410+
await processRunner.start('chromedriver', <String>['--port=0']),
411+
);
412+
try {
413+
final int port = await chromedriver.port;
414+
print('Started chromedriver on port $port');
415+
return chromedriver;
416+
} on Object {
417+
await chromedriver.stop();
418+
rethrow;
419+
}
420+
}
421+
422+
Future<List<File>> _driveTestsForDrivers(
423+
RepositoryPackage example,
424+
List<File> drivers,
425+
List<File> testTargets, {
426+
required List<String> deviceFlags,
427+
required String exampleName,
428+
}) async {
429+
final failingTargets = <File>[];
430+
for (final driver in drivers) {
431+
failingTargets.addAll(
432+
await _driveTests(
433+
example,
434+
driver,
435+
testTargets,
436+
deviceFlags: deviceFlags,
437+
exampleName: exampleName,
438+
),
439+
);
440+
}
441+
return failingTargets;
442+
}
443+
380444
/// For each file in [targets], uses
381445
/// `flutter drive --driver [driver] --target <target>`
382446
/// to drive [example], returning a list of any failing test targets.
@@ -472,3 +536,95 @@ class DriveExamplesCommand extends PackageLoopingCommand {
472536
return passed;
473537
}
474538
}
539+
540+
/// A running `chromedriver` process started with `--port=0`.
541+
class _ChromeDriver {
542+
_ChromeDriver(this._process) {
543+
_stderrSubscription = _process.stderr
544+
.transform(utf8.decoder)
545+
.transform(const LineSplitter())
546+
.listen(
547+
(String line) {
548+
if (!_portCompleter.isCompleted) {
549+
_stderrBuffer.writeln(line);
550+
}
551+
},
552+
onError: (Object _) {
553+
if (!_stderrDone.isCompleted) {
554+
_stderrDone.complete();
555+
}
556+
},
557+
onDone: () {
558+
if (!_stderrDone.isCompleted) {
559+
_stderrDone.complete();
560+
}
561+
},
562+
);
563+
_stdoutSubscription = _process.stdout
564+
.transform(utf8.decoder)
565+
.transform(const LineSplitter())
566+
.listen(
567+
(String line) {
568+
if (_portCompleter.isCompleted) {
569+
return;
570+
}
571+
_stdoutBuffer.writeln(line);
572+
if (_portPattern.firstMatch(line) case final RegExpMatch match) {
573+
_portCompleter.complete(int.parse(match.group(1)!));
574+
}
575+
},
576+
onError: (Object error, StackTrace stackTrace) {
577+
if (!_portCompleter.isCompleted) {
578+
_portCompleter.completeError(error, stackTrace);
579+
}
580+
},
581+
onDone: () async {
582+
if (!_portCompleter.isCompleted) {
583+
await _stderrDone.future;
584+
if (!_portCompleter.isCompleted) {
585+
_portCompleter.completeError(
586+
_ChromeDriverStartException(
587+
'chromedriver exited before reporting its port.\n'
588+
'stdout:\n$_stdoutBuffer'
589+
'stderr:\n$_stderrBuffer',
590+
),
591+
);
592+
}
593+
}
594+
},
595+
);
596+
}
597+
598+
static final RegExp _portPattern = RegExp(r'started successfully on port (\d+)');
599+
600+
final Process _process;
601+
final Completer<int> _portCompleter = Completer<int>();
602+
final Completer<void> _stderrDone = Completer<void>();
603+
final StringBuffer _stdoutBuffer = StringBuffer();
604+
final StringBuffer _stderrBuffer = StringBuffer();
605+
606+
// Both subscriptions stay active until the process exits, since
607+
// `Process.start` requires reading all of its output.
608+
late final StreamSubscription<String> _stdoutSubscription;
609+
late final StreamSubscription<String> _stderrSubscription;
610+
611+
/// The port that `chromedriver` reports on stdout once it is listening.
612+
Future<int> get port => _portCompleter.future;
613+
614+
/// Kills `chromedriver`, waits for it to exit, and stops reading its output.
615+
Future<void> stop() async {
616+
_process.kill();
617+
await _process.exitCode;
618+
await Future.wait(<Future<void>>[_stdoutSubscription.cancel(), _stderrSubscription.cancel()]);
619+
}
620+
}
621+
622+
/// Thrown when `chromedriver` exits before reporting the port it listens on.
623+
class _ChromeDriverStartException implements Exception {
624+
_ChromeDriverStartException(this.message);
625+
626+
final String message;
627+
628+
@override
629+
String toString() => message;
630+
}

0 commit comments

Comments
 (0)