Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Respect SIGINT (Ctrl-C) for Android scenario_app. #50989

Merged
merged 3 commits into from
Feb 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 25 additions & 6 deletions testing/scenario_app/bin/run_android_tests.dart
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,14 @@ void main(List<String> args) async {
return;
}

// Capture CTRL-C.
late final StreamSubscription<void> onSigint;
runZonedGuarded(
() async {
onSigint = ProcessSignal.sigint.watch().listen((_) {
onSigint.cancel();
panic(<String>['Received SIGINT']);
});
await _run(
verbose: options.verbose,
outDir: Directory(options.outDir),
Expand All @@ -73,9 +79,11 @@ void main(List<String> args) async {
contentsGolden: options.outputContentsGolden,
ndkStack: options.ndkStack,
);
onSigint.cancel();
exit(0);
},
(Object error, StackTrace stackTrace) {
onSigint.cancel();
if (error is! Panic) {
stderr.writeln('Unhandled error: $error');
stderr.writeln(stackTrace);
Expand Down Expand Up @@ -146,8 +154,9 @@ Future<void> _run({
// for the screenshots.
// On LUCI, the host uploads the screenshots to Skia Gold.
SkiaGoldClient? skiaGoldClient;
late ServerSocket server;
late final ServerSocket server;
final List<Future<void>> pendingComparisons = <Future<void>>[];
final List<Socket> pendingConnections = <Socket>[];
await step('Starting server...', () async {
server = await ServerSocket.bind(InternetAddress.anyIPv4, _tcpPort);
if (verbose) {
Expand All @@ -157,8 +166,8 @@ Future<void> _run({
if (verbose) {
stdout.writeln('client connected ${client.remoteAddress.address}:${client.remotePort}');
}
client.transform(const ScreenshotBlobTransformer()).listen(
(Screenshot screenshot) {
pendingConnections.add(client);
client.transform(const ScreenshotBlobTransformer()).listen((Screenshot screenshot) {
final String fileName = screenshot.filename;
final Uint8List fileContent = screenshot.fileContent;
if (verbose) {
Expand All @@ -182,9 +191,9 @@ Future<void> _run({
});
pendingComparisons.add(comparison);
}
}, onError: (dynamic err) {
panic(<String>['error while receiving bytes: $err']);
}, cancelOnError: true);
}, onDone: () {
pendingConnections.remove(client);
});
});
});

Expand Down Expand Up @@ -335,6 +344,16 @@ Future<void> _run({
});
} finally {
await server.close();
for (final Socket client in pendingConnections.toList()) {
client.close();
}

await step('Killing test app and test runner...', () async {
final int exitCode = await pm.runAndForward(<String>[adb.path, 'shell', 'am', 'force-stop', 'dev.flutter.scenarios']);
if (exitCode != 0) {
panic(<String>['could not kill test app']);
}
});

await step('Killing logcat process...', () async {
final bool delivered = logcatProcess.kill(ProcessSignal.sigkill);
Expand Down