Skip to content

Commit 3be95c1

Browse files
committed
Trying a better error handler for JUnit
1 parent 9763a07 commit 3be95c1

File tree

1 file changed

+28
-12
lines changed

1 file changed

+28
-12
lines changed

src/test/java/com/marklogic/spark/IOExceptionTestExecutionListener.java

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,41 @@
88
import org.slf4j.Logger;
99
import org.slf4j.LoggerFactory;
1010

11-
import java.io.InterruptedIOException;
12-
import java.net.SocketException;
11+
import java.io.IOException;
1312

1413
public class IOExceptionTestExecutionListener implements TestExecutionExceptionHandler {
1514

1615
private static final Logger logger = LoggerFactory.getLogger(IOExceptionTestExecutionListener.class);
1716

18-
// Telling Sonar not to complain about the intentional sleep.
19-
@SuppressWarnings("java:S2925")
2017
@Override
2118
public void handleTestExecutionException(ExtensionContext context, Throwable throwable) throws Throwable {
22-
logger.error("Handling error; class: {}; message: {}", throwable.getClass(), throwable.getMessage());
23-
Throwable cause = throwable.getCause();
24-
if (cause instanceof InterruptedIOException || cause instanceof SocketException) {
25-
logger.error("Cause class: {}; message: {}; assuming MarkLogic is being restarted on Docker and not throwing this error.",
26-
cause.getClass(), cause.getMessage());
27-
Thread.sleep(1000);
28-
} else {
29-
logger.error("Unexpected error, swallowing for now");
19+
// Valid test failures should still be thrown. We're just looking to catch intermittent IO exceptions.
20+
if (throwable instanceof AssertionError) {
21+
throw throwable;
22+
}
23+
24+
if (!logIfIOException(throwable)) {
25+
throw throwable;
26+
}
27+
}
28+
29+
// Telling Sonar not to complain about the intentional sleep.
30+
@SuppressWarnings("java:S2925")
31+
private boolean logIfIOException(Throwable throwable) {
32+
if (throwable instanceof IOException) {
33+
logger.error("Cause is IOException: {}; assuming this is due to a temporary and intermittent connection " +
34+
"issue due to MarkLogic running on Docker, so will not fail test.", throwable.getMessage());
35+
36+
// Sleep for a short duration in case MarkLogic was in fact restarted by Docker.
37+
try {
38+
Thread.sleep(1000);
39+
} catch (InterruptedException e) {
40+
// Ignore.
41+
}
42+
return true;
43+
} else if (throwable.getCause() != null) {
44+
return logIfIOException(throwable.getCause());
3045
}
46+
return false;
3147
}
3248
}

0 commit comments

Comments
 (0)