Skip to content

Commit

Permalink
test: InterruptIssue158Test: Make delay-to-close configurable
Browse files Browse the repository at this point in the history
... and log the stacktrace for unexpected exceptions

#158
  • Loading branch information
kohlschuetter committed Jul 1, 2024
1 parent 0bed969 commit 3f1c393
Showing 1 changed file with 24 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ public abstract class InterruptIssue158Test<A extends SocketAddress> extends Soc
private static boolean DEBUG_VERBOSE = (System.getProperty("com.kohlschutter.selftest") == null)
&& SystemPropertyUtil.getBooleanSystemProperty("selftest.issue.158.debug.verbose", true);

private static boolean DELAY_CLOSE = SystemPropertyUtil.getBooleanSystemProperty(
"selftest.issue.158.delay-close", false);

private final A address;
private TestInfo testInfo;

Expand Down Expand Up @@ -198,19 +201,28 @@ private void withServer(boolean acceptConnections, ThrowingRunnable func) throws
} finally {
if (socket != null) {
final SocketChannel socketToClose = socket;
CompletableFuture.runAsync(() -> {
try {
done.tryAcquire(1, TimeUnit.SECONDS);
} catch (InterruptedException e) {
// ignore
}
if (DELAY_CLOSE) {
CompletableFuture.runAsync(() -> {
try {
done.tryAcquire(1, TimeUnit.SECONDS);
} catch (InterruptedException e) {
// ignore
}
try {
socketToClose.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
});
} else {
try {
socketToClose.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
});
}
}
}
}
Expand Down Expand Up @@ -252,6 +264,11 @@ <T extends AutoCloseable> Throwable runOperation(CountDownLatch ready, IOSupplie
boolean interruptStateOK = Thread.interrupted() || (ClosedChannelException.class
.isAssignableFrom(expectedException) && !(e instanceof ClosedByInterruptException));

if (!expectedException.isAssignableFrom(e.getClass())) {
// log unexpected exception stack trace
e.printStackTrace();
}

assertAll(() -> assertInstanceOf(expectedException, e, "Socket exception"),
() -> assertTrue(ignoreInterruptState || interruptStateOK, "Thread interrupted"),
() -> assertTrue(closeCheck.test(sock), "Socket closed"));
Expand Down

0 comments on commit 3f1c393

Please sign in to comment.