| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 library scheduled_test.scheduled_process; | 5 library scheduled_test.scheduled_process; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 import 'dart:io'; | 8 import 'dart:io'; |
| 9 | 9 |
| 10 import 'scheduled_test.dart'; | 10 import 'scheduled_test.dart'; |
| (...skipping 21 matching lines...) Expand all Loading... |
| 32 final Encoding _encoding; | 32 final Encoding _encoding; |
| 33 | 33 |
| 34 /// The process that's scheduled to run. | 34 /// The process that's scheduled to run. |
| 35 ValueFuture<Process> _process; | 35 ValueFuture<Process> _process; |
| 36 | 36 |
| 37 /// A fork of [_stdout] that records the standard output of the process. Used | 37 /// A fork of [_stdout] that records the standard output of the process. Used |
| 38 /// for debugging information. | 38 /// for debugging information. |
| 39 Stream<String> _stdoutLog; | 39 Stream<String> _stdoutLog; |
| 40 | 40 |
| 41 /// A line-by-line view of the standard output stream of the process. | 41 /// A line-by-line view of the standard output stream of the process. |
| 42 Stream<String> _stdout; | 42 StreamIterator<String> _stdout; |
| 43 | 43 |
| 44 /// A canceller that controls both [_stdout] and [_stdoutLog]. | 44 /// A canceller that controls both [_stdout] and [_stdoutLog]. |
| 45 StreamCanceller _stdoutCanceller; | 45 StreamCanceller _stdoutCanceller; |
| 46 | 46 |
| 47 /// A fork of [_stderr] that records the standard error of the process. Used | 47 /// A fork of [_stderr] that records the standard error of the process. Used |
| 48 /// for debugging information. | 48 /// for debugging information. |
| 49 Stream<String> _stderrLog; | 49 Stream<String> _stderrLog; |
| 50 | 50 |
| 51 /// A line-by-line view of the standard error stream of the process. | 51 /// A line-by-line view of the standard error stream of the process. |
| 52 Stream<String> _stderr; | 52 StreamIterator<String> _stderr; |
| 53 | 53 |
| 54 /// A canceller that controls both [_stderr] and [_stderrLog]. | 54 /// A canceller that controls both [_stderr] and [_stderrLog]. |
| 55 StreamCanceller _stderrCanceller; | 55 StreamCanceller _stderrCanceller; |
| 56 | 56 |
| 57 /// The exit code of the process that's scheduled to run. This will naturally | 57 /// The exit code of the process that's scheduled to run. This will naturally |
| 58 /// only complete once the process has terminated. | 58 /// only complete once the process has terminated. |
| 59 ValueFuture<int> _exitCode; | 59 ValueFuture<int> _exitCode; |
| 60 | 60 |
| 61 /// Whether the user has scheduled the end of this process by calling either | 61 /// Whether the user has scheduled the end of this process by calling either |
| 62 /// [shouldExit] or [kill]. | 62 /// [shouldExit] or [kill]. |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 95 var stdoutWithCanceller = _lineStreamWithCanceller( | 95 var stdoutWithCanceller = _lineStreamWithCanceller( |
| 96 _process.then((p) => p.stdout)); | 96 _process.then((p) => p.stdout)); |
| 97 _stdoutCanceller = stdoutWithCanceller.last; | 97 _stdoutCanceller = stdoutWithCanceller.last; |
| 98 _stdoutLog = stdoutWithCanceller.first; | 98 _stdoutLog = stdoutWithCanceller.first; |
| 99 | 99 |
| 100 var stderrWithCanceller = _lineStreamWithCanceller( | 100 var stderrWithCanceller = _lineStreamWithCanceller( |
| 101 _process.then((p) => p.stderr)); | 101 _process.then((p) => p.stderr)); |
| 102 _stderrCanceller = stderrWithCanceller.last; | 102 _stderrCanceller = stderrWithCanceller.last; |
| 103 _stderrLog = stderrWithCanceller.first; | 103 _stderrLog = stderrWithCanceller.first; |
| 104 | 104 |
| 105 _stdout = stdoutStream(); | 105 _stdout = new StreamIterator<String>(stdoutStream()); |
| 106 _stderr = stderrStream(); | 106 _stderr = new StreamIterator<String>(stderrStream()); |
| 107 } | 107 } |
| 108 | 108 |
| 109 /// Updates [_description] to reflect [executable] and [arguments], which are | 109 /// Updates [_description] to reflect [executable] and [arguments], which are |
| 110 /// the same values as in [start]. | 110 /// the same values as in [start]. |
| 111 void _updateDescription(executable, arguments) { | 111 void _updateDescription(executable, arguments) { |
| 112 if (_explicitDescription) return; | 112 if (_explicitDescription) return; |
| 113 if (executable is Future) { | 113 if (executable is Future) { |
| 114 _description = "future process"; | 114 _description = "future process"; |
| 115 } else if (arguments is Future || arguments.any((e) => e is Future)) { | 115 } else if (arguments is Future || arguments.any((e) => e is Future)) { |
| 116 _description = executable; | 116 _description = executable; |
| (...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 233 "$exitDescription\n" | 233 "$exitDescription\n" |
| 234 "Standard output:\n" | 234 "Standard output:\n" |
| 235 "${prefixLines(stdout)}\n" | 235 "${prefixLines(stdout)}\n" |
| 236 "Standard error:\n" | 236 "Standard error:\n" |
| 237 "${prefixLines(stderr)}"); | 237 "${prefixLines(stderr)}"); |
| 238 }); | 238 }); |
| 239 }, "cleaning up process '$description'"); | 239 }, "cleaning up process '$description'"); |
| 240 } | 240 } |
| 241 | 241 |
| 242 /// Reads the next line of stdout from the process. | 242 /// Reads the next line of stdout from the process. |
| 243 Future<String> nextLine() => schedule(() => streamFirst(_stdout), | 243 Future<String> nextLine() => schedule(() => streamIteratorFirst(_stdout), |
| 244 "reading the next stdout line from process '$description'"); | 244 "reading the next stdout line from process '$description'"); |
| 245 | 245 |
| 246 /// Reads the next line of stderr from the process. | 246 /// Reads the next line of stderr from the process. |
| 247 Future<String> nextErrLine() => schedule(() => streamFirst(_stderr), | 247 Future<String> nextErrLine() => schedule(() => streamIteratorFirst(_stderr), |
| 248 "reading the next stderr line from process '$description'"); | 248 "reading the next stderr line from process '$description'"); |
| 249 | 249 |
| 250 /// Reads the remaining stdout from the process. This should only be called | 250 /// Reads the remaining stdout from the process. This should only be called |
| 251 /// after kill() or shouldExit(). | 251 /// after kill() or shouldExit(). |
| 252 Future<String> remainingStdout() { | 252 Future<String> remainingStdout() { |
| 253 if (!_endScheduled) { | 253 if (!_endScheduled) { |
| 254 throw new StateError("remainingStdout() should only be called after " | 254 throw new StateError("remainingStdout() should only be called after " |
| 255 "kill() or shouldExit()."); | 255 "kill() or shouldExit()."); |
| 256 } | 256 } |
| 257 | 257 return schedule(() => concatRest(_stdout), |
| 258 return schedule(() => _stdout.toList().then((lines) => lines.join("\n")), | |
| 259 "reading the remaining stdout from process '$description'"); | 258 "reading the remaining stdout from process '$description'"); |
| 260 } | 259 } |
| 261 | 260 |
| 262 /// Reads the remaining stderr from the process. This should only be called | 261 /// Reads the remaining stderr from the process. This should only be called |
| 263 /// after kill() or shouldExit(). | 262 /// after kill() or shouldExit(). |
| 264 Future<String> remainingStderr() { | 263 Future<String> remainingStderr() { |
| 265 if (!_endScheduled) { | 264 if (!_endScheduled) { |
| 266 throw new StateError("remainingStderr() should only be called after " | 265 throw new StateError("remainingStderr() should only be called after " |
| 267 "kill() or shouldExit()."); | 266 "kill() or shouldExit()."); |
| 268 } | 267 } |
| 269 | 268 |
| 270 return schedule(() => _stderr.toList().then((lines) => lines.join("\n")), | 269 return schedule(() => concatRest(_stderr), |
| 271 "reading the remaining stderr from process '$description'"); | 270 "reading the remaining stderr from process '$description'"); |
| 272 } | 271 } |
| 273 | 272 |
| 274 /// Returns a stream that will emit anything the process emits via the | 273 /// Returns a stream that will emit anything the process emits via the |
| 275 /// process's standard output from now on. | 274 /// process's standard output from now on. |
| 276 /// | 275 /// |
| 277 /// This stream will be independent from any other methods that deal with | 276 /// This stream will be independent from any other methods that deal with |
| 278 /// standard output, including other calls to [stdoutStream]. | 277 /// standard output, including other calls to [stdoutStream]. |
| 279 /// | 278 /// |
| 280 /// This can be overridden by subclasses to return a derived standard output | 279 /// This can be overridden by subclasses to return a derived standard output |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 336 schedule(() { | 335 schedule(() { |
| 337 _endExpected = true; | 336 _endExpected = true; |
| 338 return _exitCode.then((exitCode) { | 337 return _exitCode.then((exitCode) { |
| 339 if (expectedExitCode != null) { | 338 if (expectedExitCode != null) { |
| 340 expect(exitCode, equals(expectedExitCode)); | 339 expect(exitCode, equals(expectedExitCode)); |
| 341 } | 340 } |
| 342 }); | 341 }); |
| 343 }, "waiting for process '$description' to exit"); | 342 }, "waiting for process '$description' to exit"); |
| 344 } | 343 } |
| 345 } | 344 } |
| OLD | NEW |