Skip to content
This repository has been archived by the owner on Nov 10, 2023. It is now read-only.

Commit

Permalink
Easy: Fix XctoolOutputParsing exception on empty JSON
Browse files Browse the repository at this point in the history
Summary:
If xctool is in a bad state, it can close stdout before printing
anything (facebookarchive/xctool#656)

That makes the JSON parser in `XctoolOutputParsing` fail:

  com.google.gson.JsonIOException: java.io.EOFException: End of input at line 1 column 1
                                   at com.google.gson.JsonStreamParser.hasNext(JsonStreamParser.java:109)
                                   at com.facebook.buck.apple.XctoolOutputParsing.streamOutputFromReader(XctoolOutputParsing.java:147)
                                   at com.facebook.buck.apple.AppleTest$AppleTestXctoolStdoutReader.readStdout(AppleTest.java:138)
                                   at com.facebook.buck.apple.XctoolRunTestsStep.execute(XctoolRunTestsStep.java:292)
                                   at com.facebook.buck.step.DefaultStepRunner.runStepForBuildTarget(DefaultStepRunner.java:63)
                                   at com.facebook.buck.step.DefaultStepRunner$1.call(DefaultStepRunner.java:92)
                                   at java.util.concurrent.FutureTask.run(FutureTask.java:266)
                                   at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
                                   at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
                                   at java.lang.Thread.run(Thread.java:745)
  Caused by: java.io.EOFException: End of input at line 1 column 1
         at com.google.gson.stream.JsonReader.nextNonWhitespace(JsonReader.java:1377)
         at com.google.gson.stream.JsonReader.consumeNonExecutePrefix(JsonReader.java:1514)
         at com.google.gson.stream.JsonReader.doPeek(JsonReader.java:523)
         at com.google.gson.stream.JsonReader.peek(JsonReader.java:414)
         at com.google.gson.JsonStreamParser.hasNext(JsonStreamParser.java:105)
         ... 9 more

This fixes the issue by cleanly handling exceptions from `hasNext()` and just
ignoring the rest of the output.

Test Plan: Unit tests updated. Confirmed test failed before fix and passed after fix.

Reviewed By: ryu2

fb-gh-sync-id: 3d3a2e0
  • Loading branch information
bhamiltoncx authored and facebook-github-bot-6 committed Jan 15, 2016
1 parent 46f05a9 commit 3e714fd
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/com/facebook/buck/apple/XctoolOutputParsing.java
Original file line number Diff line number Diff line change
Expand Up @@ -144,12 +144,12 @@ public static void streamOutputFromReader(
XctoolEventCallback eventCallback) {
Gson gson = new Gson();
JsonStreamParser streamParser = new JsonStreamParser(reader);
while (streamParser.hasNext()) {
try {
try {
while (streamParser.hasNext()) {
dispatchEventCallback(gson, streamParser.next(), eventCallback);
} catch (JsonParseException e) {
LOG.warn(e, "Couldn't parse xctool JSON stream");
}
} catch (JsonParseException e) {
LOG.warn(e, "Couldn't parse xctool JSON stream");
}
}

Expand Down
10 changes: 10 additions & 0 deletions test/com/facebook/buck/apple/XctoolOutputParsingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

import org.junit.Test;

import java.io.StringReader;
import java.io.Reader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
Expand Down Expand Up @@ -268,6 +269,15 @@ public void streamingSimpleFailure() throws Exception {
assertThat(endOcunitEvent.succeeded, is(false));
}

@Test
public void streamingEmptyReaderDoesNotCauseFailure() {
final List<Object> streamedObjects = new ArrayList<>();
XctoolOutputParsing.streamOutputFromReader(
new StringReader(""),
eventCallbackAddingEventsToList(streamedObjects));
assertThat(streamedObjects, is(empty()));
}

@Test
public void validEventParsesToStatusMessage() {
XctoolOutputParsing.StatusEvent statusEvent = new XctoolOutputParsing.StatusEvent();
Expand Down

0 comments on commit 3e714fd

Please sign in to comment.