| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 // Process test program to test process communication. | 5 // Process test program to test process communication. |
| 6 // | 6 // |
| 7 // VMOptions= | 7 // VMOptions= |
| 8 // VMOptions=--short_socket_read | 8 // VMOptions=--short_socket_read |
| 9 // VMOptions=--short_socket_write | 9 // VMOptions=--short_socket_write |
| 10 // VMOptions=--short_socket_read --short_socket_write | 10 // VMOptions=--short_socket_read --short_socket_write |
| 11 | 11 |
| 12 #import('dart:io'); | 12 #import('dart:io'); |
| 13 #import('dart:math'); | 13 #import('dart:math'); |
| 14 | 14 |
| 15 #source('process_test_util.dart'); | 15 #source('process_test_util.dart'); |
| 16 | 16 |
| 17 void test(Future<Process> future, int expectedExitCode) { | 17 void test(Future<Process> future, int expectedExitCode) { |
| 18 future.then((process) { | 18 future.then((process) { |
| 19 process.onExit = (exitCode) { | 19 process.onExit = (exitCode) { |
| 20 Expect.equals(expectedExitCode, exitCode); | 20 Expect.equals(expectedExitCode, exitCode); |
| 21 }; | 21 }; |
| 22 | 22 |
| 23 List<int> data = "ABCDEFGHI\n".charCodes; | 23 List<int> data = "ABCDEFGHI\n".charCodes; |
| 24 final int dataSize = data.length; | 24 final int dataSize = data.length; |
| 25 | 25 |
| 26 InputStream input = process.stderr; | 26 InputStream err = process.stderr; |
| 27 OutputStream output = process.stdin; | |
| 28 | 27 |
| 29 int received = 0; | 28 int received = 0; |
| 30 List<int> buffer = []; | 29 List<int> buffer = []; |
| 31 | 30 |
| 32 void readData() { | 31 void readData() { |
| 33 buffer.addAll(input.read()); | 32 buffer.addAll(err.read()); |
| 34 for (int i = received; | 33 for (int i = received; |
| 35 i < min(data.length, buffer.length) - 1; | 34 i < min(data.length, buffer.length) - 1; |
| 36 i++) { | 35 i++) { |
| 37 Expect.equals(data[i], buffer[i]); | 36 Expect.equals(data[i], buffer[i]); |
| 38 } | 37 } |
| 39 received = buffer.length; | 38 received = buffer.length; |
| 40 if (received >= dataSize) { | 39 if (received >= dataSize) { |
| 41 // We expect an extra character on windows due to carriage return. | 40 // We expect an extra character on windows due to carriage return. |
| 42 if (13 === buffer[dataSize - 1] && dataSize + 1 === received) { | 41 if (13 === buffer[dataSize - 1] && dataSize + 1 === received) { |
| 43 Expect.equals(13, buffer[dataSize - 1]); | 42 Expect.equals(13, buffer[dataSize - 1]); |
| 44 Expect.equals(10, buffer[dataSize]); | 43 Expect.equals(10, buffer[dataSize]); |
| 45 buffer.removeLast(); | 44 buffer.removeLast(); |
| 46 process.close(); | |
| 47 } else if (received === dataSize) { | |
| 48 process.close(); | |
| 49 } | 45 } |
| 50 } | 46 } |
| 51 } | 47 } |
| 52 | 48 |
| 53 output.write(data); | 49 process.stdout.onData = process.stdout.read; |
| 54 output.close(); | 50 process.stdin.write(data); |
| 55 input.onData = readData; | 51 process.stdin.close(); |
| 56 | 52 err.onData = readData; |
| 57 }); | 53 }); |
| 58 } | 54 } |
| 59 | 55 |
| 60 main() { | 56 main() { |
| 61 // Run the test using the process_test binary. | 57 // Run the test using the process_test binary. |
| 62 test(Process.start(getProcessTestFileName(), | 58 test(Process.start(getProcessTestFileName(), |
| 63 const ["1", "1", "99", "0"]), 99); | 59 const ["1", "1", "99", "0"]), 99); |
| 64 | 60 |
| 65 // Run the test using the dart binary with an echo script. | 61 // Run the test using the dart binary with an echo script. |
| 66 // The test runner can be run from either the root or from runtime. | 62 // The test runner can be run from either the root or from runtime. |
| 67 var scriptFile = new File("tests/standalone/io/process_std_io_script.dart"); | 63 var scriptFile = new File("tests/standalone/io/process_std_io_script.dart"); |
| 68 if (!scriptFile.existsSync()) { | 64 if (!scriptFile.existsSync()) { |
| 69 scriptFile = new File("../tests/standalone/io/process_std_io_script.dart"); | 65 scriptFile = new File("../tests/standalone/io/process_std_io_script.dart"); |
| 70 } | 66 } |
| 71 Expect.isTrue(scriptFile.existsSync()); | 67 Expect.isTrue(scriptFile.existsSync()); |
| 72 test(Process.start(new Options().executable, [scriptFile.name, "1"]), 0); | 68 test(Process.start(new Options().executable, [scriptFile.name, "1"]), 0); |
| 73 } | 69 } |
| OLD | NEW |