|
8 | 8 | import org.slf4j.Logger;
|
9 | 9 | import org.slf4j.LoggerFactory;
|
10 | 10 |
|
11 |
| -import java.io.InterruptedIOException; |
12 |
| -import java.net.SocketException; |
| 11 | +import java.io.IOException; |
13 | 12 |
|
14 | 13 | public class IOExceptionTestExecutionListener implements TestExecutionExceptionHandler {
|
15 | 14 |
|
16 | 15 | private static final Logger logger = LoggerFactory.getLogger(IOExceptionTestExecutionListener.class);
|
17 | 16 |
|
18 |
| - // Telling Sonar not to complain about the intentional sleep. |
19 |
| - @SuppressWarnings("java:S2925") |
20 | 17 | @Override
|
21 | 18 | 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()); |
30 | 45 | }
|
| 46 | + return false; |
31 | 47 | }
|
32 | 48 | }
|
0 commit comments