Skip to content

Commit

Permalink
Make timeout failures privileged if they need to be. This resolves #91.
Browse files Browse the repository at this point in the history
  • Loading branch information
MaisiKoleni committed Mar 19, 2021
1 parent 8559d4d commit f1ab632
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/main/java/de/tum/in/test/api/internal/TimeoutUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.junit.platform.commons.support.AnnotationSupport;
import org.opentest4j.AssertionFailedError;

import de.tum.in.test.api.PrivilegedExceptionsOnly;
import de.tum.in.test.api.StrictTimeout;
import de.tum.in.test.api.security.ArtemisSecurityManager;

Expand All @@ -46,7 +47,7 @@ public static <T> T performTimeoutExecution(ThrowingSupplier<T> execution, TestC
var timeout = findTimeout(context);
if (timeout.isEmpty())
return execution.get();
return executeWithTimeout(timeout.get(), () -> rethrowThrowableSafe(execution));
return executeWithTimeout(timeout.get(), () -> rethrowThrowableSafe(execution), context);
}

private static <T> T rethrowThrowableSafe(ThrowingSupplier<T> execution) throws Exception {
Expand All @@ -63,7 +64,8 @@ private static <T> T rethrowThrowableSafe(ThrowingSupplier<T> execution) throws
}
}

private static <T> T executeWithTimeout(Duration timeout, Callable<T> action) throws Throwable {
private static <T> T executeWithTimeout(Duration timeout, Callable<T> action, TestContext context)
throws Throwable {
ArtemisSecurityManager.revokeThreadWhitelisting();
ExecutorService executorService = Executors.newSingleThreadExecutor(new WhitelistedThreadFactory());
try {
Expand All @@ -75,12 +77,19 @@ private static <T> T executeWithTimeout(Duration timeout, Callable<T> action) th
throw ex.getCause().getCause();
throw ex.getCause();
} catch (@SuppressWarnings("unused") TimeoutException ex) {
throw new AssertionFailedError("execution timed out after " + formatDuration(timeout));
throw generateTimeoutFailure(timeout, context);
} finally {
executorService.shutdownNow();
}
}

private static AssertionFailedError generateTimeoutFailure(Duration timeout, TestContext context) {
var failure = new AssertionFailedError("execution timed out after " + formatDuration(timeout));
if (TestContextUtils.findAnnotationIn(context, PrivilegedExceptionsOnly.class).isPresent())
throw new PrivilegedException(failure);
return failure;
}

private static String formatDuration(Duration duration) {
List<String> parts = new ArrayList<>();
long h = duration.toHours();
Expand Down
7 changes: 7 additions & 0 deletions src/test/java/de/tum/in/test/api/PrivilegedExceptionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class PrivilegedExceptionTest {
private final String nonprivilegedExceptionTry = "nonprivilegedExceptionTry";
private final String privilegedExceptionFail = "privilegedExceptionFail";
private final String privilegedExceptionNormal = "privilegedExceptionNormal";
private final String privilegedTimeout = "privilegedTimeout";

@TestTest
void test_nonprivilegedExceptionExtern() {
Expand Down Expand Up @@ -48,4 +49,10 @@ void test_privilegedExceptionNormal() {
tests.assertThatEvents().haveExactly(1,
testFailedWith(privilegedExceptionNormal, NullPointerException.class, "xyz"));
}

@TestTest
void test_privilegedTimeout() {
tests.assertThatEvents().haveExactly(1,
testFailedWith(privilegedTimeout, AssertionError.class, "execution timed out after 300 ms"));
}
}
6 changes: 6 additions & 0 deletions src/test/java/de/tum/in/testuser/PrivilegedExceptionUser.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,10 @@ void privilegedExceptionNormal() throws Exception {
throw new NullPointerException("xyz");
});
}

@PrivilegedExceptionsOnly("ABC")
@PublicTest
void privilegedTimeout() throws InterruptedException {
Thread.sleep(1000);
}
}

0 comments on commit f1ab632

Please sign in to comment.