Skip to content

Commit

Permalink
Trim stdio even on failing tests, just to a 100× larger cutoff.
Browse files Browse the repository at this point in the history
  • Loading branch information
jglick committed Feb 16, 2015
1 parent e064fa9 commit c8fd11f
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 15 deletions.
32 changes: 17 additions & 15 deletions src/main/java/hudson/tasks/junit/CaseResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -143,39 +143,40 @@ private static float parseTime(Element testCase) {
stderr = possiblyTrimStdio(_this, keepLongStdio, testCase.elementText("system-err"));
}

private static final int HALF_MAX_SIZE = 500;
static String possiblyTrimStdio(Collection<CaseResult> results, boolean keepLongStdio, String stdio) { // HUDSON-6516
if (stdio == null) {
return null;
}
if (!isTrimming(results, keepLongStdio)) {
if (keepLongStdio) {
return stdio;
}
int len = stdio.length();
int middle = len - HALF_MAX_SIZE * 2;
int halfMaxSize = halfMaxSize(results);
int middle = len - halfMaxSize * 2;
if (middle <= 0) {
return stdio;
}
return stdio.subSequence(0, HALF_MAX_SIZE) + "\n...[truncated " + middle + " chars]...\n" + stdio.subSequence(len - HALF_MAX_SIZE, len);
return stdio.subSequence(0, halfMaxSize) + "\n...[truncated " + middle + " chars]...\n" + stdio.subSequence(len - halfMaxSize, len);
}

/**
* Flavor of {@link #possiblyTrimStdio(Collection, boolean, String)} that doesn't try to read the whole thing into memory.
*/
static String possiblyTrimStdio(Collection<CaseResult> results, boolean keepLongStdio, File stdio) throws IOException {
if (!isTrimming(results, keepLongStdio) && stdio.length()<1024*1024) {
long len = stdio.length();
if (keepLongStdio && len < 1024 * 1024) {
return FileUtils.readFileToString(stdio);
}
int halfMaxSize = halfMaxSize(results);

long len = stdio.length();
long middle = len - HALF_MAX_SIZE * 2;
long middle = len - halfMaxSize * 2;
if (middle <= 0) {
return FileUtils.readFileToString(stdio);
}

TextFile tx = new TextFile(stdio);
String head = tx.head(HALF_MAX_SIZE);
String tail = tx.fastTail(HALF_MAX_SIZE);
String head = tx.head(halfMaxSize);
String tail = tx.fastTail(halfMaxSize);

int headBytes = head.getBytes().length;
int tailBytes = tail.getBytes().length;
Expand All @@ -189,14 +190,15 @@ static String possiblyTrimStdio(Collection<CaseResult> results, boolean keepLong
return head + "\n...[truncated " + middle + " bytes]...\n" + tail;
}

private static boolean isTrimming(Collection<CaseResult> results, boolean keepLongStdio) {
if (keepLongStdio) return false;
private static final int HALF_MAX_SIZE = 500;
private static final int HALF_MAX_FAILING_SIZE = 50000;
private static int halfMaxSize(Collection<CaseResult> results) {
for (CaseResult result : results) {
// if there's a failure, do not trim and keep the whole thing
if (result.errorStackTrace != null)
return false;
if (result.errorStackTrace != null) {
return HALF_MAX_FAILING_SIZE;
}
}
return true;
return HALF_MAX_SIZE;
}


Expand Down
70 changes: 70 additions & 0 deletions src/test/java/hudson/tasks/junit/SuiteResultTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,36 @@ public void testSuiteStdioTrimming() throws Exception {
}
}

public void testSuiteStdioTrimmingOnFail() throws Exception {
File data = File.createTempFile("testSuiteStdioTrimming", ".xml");
try {
Writer w = new FileWriter(data);
try {
PrintWriter pw = new PrintWriter(w);
pw.println("<testsuites name='x'>");
pw.println("<testsuite failures='1' errors='0' tests='1' name='x'>");
pw.println("<testcase name='x' classname='x'><error>oops</error></testcase>");
pw.println("<system-out/>");
pw.print("<system-err><![CDATA[");
pw.println("First line is intact.");
for (int i = 0; i < 10000; i++) {
pw.println("Line #" + i + " might be elided.");
}
pw.println("Last line is intact.");
pw.println("]]></system-err>");
pw.println("</testsuite>");
pw.println("</testsuites>");
pw.flush();
} finally {
w.close();
}
SuiteResult sr = parseOne(data);
assertEquals(sr.getStderr(), 100032, sr.getStderr().length());
} finally {
data.delete();
}
}

@SuppressWarnings({"RV_RETURN_VALUE_IGNORED_BAD_PRACTICE", "DM_DEFAULT_ENCODING", "OS_OPEN_STREAM"})
public void testSuiteStdioTrimmingSurefire() throws Exception {
File data = File.createTempFile("TEST-", ".xml");
Expand Down Expand Up @@ -218,6 +248,46 @@ public void testSuiteStdioTrimmingSurefire() throws Exception {
}
}

@SuppressWarnings({"RV_RETURN_VALUE_IGNORED_BAD_PRACTICE", "DM_DEFAULT_ENCODING", "OS_OPEN_STREAM"})
public void testSuiteStdioTrimmingSurefireOnFail() throws Exception {
File data = File.createTempFile("TEST-", ".xml");
try {
Writer w = new FileWriter(data);
try {
PrintWriter pw = new PrintWriter(w);
pw.println("<testsuites name='x'>");
pw.println("<testsuite failures='1' errors='0' tests='1' name='x'>");
pw.println("<testcase name='x' classname='x'><error>oops</error></testcase>");
pw.println("</testsuite>");
pw.println("</testsuites>");
pw.flush();
} finally {
w.close();
}
File data2 = new File(data.getParentFile(), data.getName().replaceFirst("^TEST-(.+)[.]xml$", "$1-output.txt"));
try {
w = new FileWriter(data2);
try {
PrintWriter pw = new PrintWriter(w);
pw.println("First line is intact.");
for (int i = 0; i < 10000; i++) {
pw.println("Line #" + i + " might be elided.");
}
pw.println("Last line is intact.");
pw.flush();
} finally {
w.close();
}
SuiteResult sr = parseOne(data);
assertEquals(sr.getStdout(), 100032, sr.getStdout().length());
} finally {
data2.delete();
}
} finally {
data.delete();
}
}

/**
* When the testcase fails to initialize (exception in constructor or @Before)
* there is no 'testcase' element at all.
Expand Down

0 comments on commit c8fd11f

Please sign in to comment.