|
| 1 | +// Copyright (c) 2020, the Dart project authors. Please see the AUTHORS file |
| 2 | +// for details. All rights reserved. Use of this source code is governed by a |
| 3 | +// BSD-style license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +import 'dart:async'; |
| 6 | +import 'dart:convert'; |
| 7 | +import 'dart:io'; |
| 8 | + |
| 9 | +import 'package:expect/expect.dart'; |
| 10 | + |
| 11 | +late Process process; |
| 12 | +bool lastKill = false; |
| 13 | + |
| 14 | +Future<void> sigint(int iterations) async { |
| 15 | + for (int i = 0; i < iterations; ++i) { |
| 16 | + if (i + 1 == iterations) { |
| 17 | + lastKill = true; |
| 18 | + } |
| 19 | + process.kill(ProcessSignal.sigint); |
| 20 | + // Yield to the event loop to allow for the signal to be sent. |
| 21 | + await Future.value(null); |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +Future<void> main() async { |
| 26 | + process = await Process.start( |
| 27 | + Platform.resolvedExecutable, |
| 28 | + [ |
| 29 | + Platform.script.resolve('regress_42092_script.dart').toString(), |
| 30 | + ], |
| 31 | + ); |
| 32 | + final startCompleter = Completer<void>(); |
| 33 | + late StreamSubscription sub; |
| 34 | + sub = process.stdout.transform(Utf8Decoder()).listen((event) { |
| 35 | + if (event.contains('Waiting...')) { |
| 36 | + startCompleter.complete(); |
| 37 | + sub.cancel(); |
| 38 | + } |
| 39 | + }); |
| 40 | + |
| 41 | + // Wait for target script to setup its signal handling. |
| 42 | + await startCompleter.future; |
| 43 | + |
| 44 | + final exitCompleter = Completer<void>(); |
| 45 | + process.exitCode.then((code) { |
| 46 | + Expect.isTrue(lastKill); |
| 47 | + exitCompleter.complete(); |
| 48 | + }); |
| 49 | + await sigint(3); |
| 50 | + await exitCompleter.future; |
| 51 | +} |
0 commit comments