summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJesse Wilson <jessewilson@google.com>2009-10-09 21:53:23 -0700
committerJesse Wilson <jessewilson@google.com>2009-10-09 22:10:06 -0700
commit9c048fb7738ee3ac3677ba2170e360de8d70dcab (patch)
tree5fcfdbcebfc3105ccee59ffcb31b54f723be31f4
parent2cb12f590e440b9060e47f16ab7c98d4e5ea174f (diff)
downloadlibcore-9c048fb7738ee3ac3677ba2170e360de8d70dcab.zip
libcore-9c048fb7738ee3ac3677ba2170e360de8d70dcab.tar.gz
libcore-9c048fb7738ee3ac3677ba2170e360de8d70dcab.tar.bz2
Adding a timeout for standard out as well as standard error.
-rw-r--r--support/src/test/java/tests/support/Support_Exec.java51
1 files changed, 27 insertions, 24 deletions
diff --git a/support/src/test/java/tests/support/Support_Exec.java b/support/src/test/java/tests/support/Support_Exec.java
index d058927..7b92d0d 100644
--- a/support/src/test/java/tests/support/Support_Exec.java
+++ b/support/src/test/java/tests/support/Support_Exec.java
@@ -73,32 +73,31 @@ public class Support_Exec extends TestCase {
}
/**
- * Starts the specified process, collects its output from standard out, and
- * returns. If the stream emits anything to standard err, an
- * AssertionFailedError will be thrown.
+ * Starts the specified process, collects its output from standard out and
+ * standard err, and returns. If the stream emits anything to standard err,
+ * an AssertionFailedError will be thrown.
*
* <p>This method assumes the target process will complete within ten
* seconds. If it does not, an AssertionFailedError will be thrown.
*/
public static String execAndGetOutput(ProcessBuilder builder)
throws IOException {
- final Process process = builder.start();
- ExecutorService executorService = Executors.newFixedThreadPool(1);
+ Process process = builder.start();
+ ExecutorService executorService = Executors.newFixedThreadPool(2);
try {
- Future<Throwable> future = executorService.submit(new Callable<Throwable>() {
- public Throwable call() throws Exception {
- String err = streamToString(process.getErrorStream());
- return err.length() > 0
- ? new AssertionFailedError("Unexpected err stream data:\n" + err)
- : null;
- }
- });
-
- String out = streamToString(process.getInputStream());
+ Future<String> errFuture = executorService.submit(
+ streamToStringCallable(process.getErrorStream()));
+ Future<String> outFuture = executorService.submit(
+ streamToStringCallable(process.getInputStream()));
Throwable failure;
+ String out = "";
try {
- failure = future.get(10, TimeUnit.SECONDS);
+ out = outFuture.get(10, TimeUnit.SECONDS);
+ String err = errFuture.get(10, TimeUnit.SECONDS);
+ failure = err.length() > 0
+ ? new AssertionFailedError("Unexpected err stream data:\n" + err)
+ : null;
} catch (Exception e) {
failure = e;
}
@@ -116,13 +115,17 @@ public class Support_Exec extends TestCase {
}
}
- private static String streamToString(InputStream in) throws IOException {
- StringWriter writer = new StringWriter();
- Reader reader = new InputStreamReader(in);
- int c;
- while ((c = reader.read()) != -1) {
- writer.write(c);
- }
- return writer.toString();
+ private static Callable<String> streamToStringCallable(final InputStream in) {
+ return new Callable<String>() {
+ public String call() throws Exception {
+ StringWriter writer = new StringWriter();
+ Reader reader = new InputStreamReader(in);
+ int c;
+ while ((c = reader.read()) != -1) {
+ writer.write(c);
+ }
+ return writer.toString();
+ }
+ };
}
}