/** * Reset(nullify) the authorization context on the host. * * Mainly used paired with {@link #setSystemAuthorizationContext}. * For setting system auth context for lambda code block, see {@link #executeWithSystemAuthContext}. * * @param host a host to reset auth context * @see #setSystemAuthorizationContext * @see #executeWithSystemAuthContext */ public static void resetAuthorizationContext(ServiceHost host) { ExceptionTestUtils.executeSafely(() -> { SET_SYSTEM_AUTH_CONTEXT_METHOD.invoke(host, new Object[]{null}); }); }
/** * Throw any {@link Throwable} as unchecked exception. * This does not wrap the {@link Throwable}, rather uses java trick to throw it as is. * TODO: more documentation */ public static RuntimeException throwAsUnchecked(Throwable throwable) { throwsUnchecked(throwable); // allowing this method to be used with return statement. this line will never be executed. return null; }
public static void waitFor(WaitConfig waitConfig, WaitHandler wh, Supplier<String> timeoutMessageSupplier) { if (waitConfig.duration == null) { throw new RuntimeException("duration must be specified"); } TestContext waitContext = new TestContext(1, waitConfig.duration); waitContext.setCheckInterval(waitConfig.interval); try { waitContext.await(() -> { if (wh.isReady()) { waitContext.complete(); } }); } catch (Exception ex) { Exception exceptionToThrow; if (ex instanceof TimeoutException) { // add original exception as suppressed exception to provide more context String timeoutMessage = timeoutMessageSupplier.get(); exceptionToThrow = new TimeoutException(timeoutMessage); } else { String msg = "waitFor check logic raised exception"; exceptionToThrow = new IllegalStateException(msg); } exceptionToThrow.addSuppressed(ex); throw ExceptionTestUtils.throwAsUnchecked(exceptionToThrow); } }
public static void waitFor(WaitConfig waitConfig, WaitHandler wh, Supplier<String> timeoutMessageSupplier) { if (waitConfig.duration == null) { throw new RuntimeException("duration must be specified"); } TestContext waitContext = new TestContext(1, waitConfig.duration); waitContext.setCheckInterval(waitConfig.interval); try { waitContext.await(() -> { if (wh.isReady()) { waitContext.complete(); } }); } catch (Exception ex) { Exception exceptionToThrow; if (ex instanceof TimeoutException) { // add original exception as suppressed exception to provide more context String timeoutMessage = timeoutMessageSupplier.get(); exceptionToThrow = new TimeoutException(timeoutMessage); } else { String msg = "waitFor check logic raised exception"; exceptionToThrow = new IllegalStateException(msg); } exceptionToThrow.addSuppressed(ex); throw ExceptionTestUtils.throwAsUnchecked(exceptionToThrow); } }
/** * Reset(nullify) the authorization context on the host. * * Mainly used paired with {@link #setSystemAuthorizationContext}. * For setting system auth context for lambda code block, see {@link #executeWithSystemAuthContext}. * * @param host a host to reset auth context * @see #setSystemAuthorizationContext * @see #executeWithSystemAuthContext */ public static void resetAuthorizationContext(ServiceHost host) { ExceptionTestUtils.executeSafely(() -> { SET_SYSTEM_AUTH_CONTEXT_METHOD.invoke(host, new Object[]{null}); }); }
/** * Throw any {@link Throwable} as unchecked exception. * This does not wrap the {@link Throwable}, rather uses java trick to throw it as is. * TODO: more documentation */ public static RuntimeException throwAsUnchecked(Throwable throwable) { throwsUnchecked(throwable); // allowing this method to be used with return statement. this line will never be executed. return null; }
} catch (RuntimeException e) { throw ExceptionTestUtils.throwAsUnchecked(e.getSuppressed()[0]);
/** * Set system auth context to the host. * * This method is used descriptively paired with {@link #resetAuthorizationContext}. * Please see {@link #executeWithSystemAuthContext} for functional way using lambda. * * @param host a host to set system auth context * @return system auth context * @see #resetAuthorizationContext * @see #executeWithSystemAuthContext */ public static AuthorizationContext setSystemAuthorizationContext(ServiceHost host) { // use reflection to set auth context AuthorizationContext[] result = new AuthorizationContext[]{null}; ExceptionTestUtils.executeSafely(() -> { AuthorizationContext systemAuthContext = (AuthorizationContext) GET_SYSTEM_AUTH_CONTEXT_METHOD.invoke(host); SET_SYSTEM_AUTH_CONTEXT_METHOD.invoke(host, systemAuthContext); result[0] = systemAuthContext; }); return result[0]; }
public static void executeSafely(ExecutableBlock block) { try { block.execute(); } catch (Throwable throwable) { throwsUnchecked(throwable); } } }
} catch (RuntimeException e) { throw ExceptionTestUtils.throwAsUnchecked(e.getSuppressed()[0]);
/** * Set system auth context to the host. * * This method is used descriptively paired with {@link #resetAuthorizationContext}. * Please see {@link #executeWithSystemAuthContext} for functional way using lambda. * * @param host a host to set system auth context * @return system auth context * @see #resetAuthorizationContext * @see #executeWithSystemAuthContext */ public static AuthorizationContext setSystemAuthorizationContext(ServiceHost host) { // use reflection to set auth context AuthorizationContext[] result = new AuthorizationContext[]{null}; ExceptionTestUtils.executeSafely(() -> { AuthorizationContext systemAuthContext = (AuthorizationContext) GET_SYSTEM_AUTH_CONTEXT_METHOD.invoke(host); SET_SYSTEM_AUTH_CONTEXT_METHOD.invoke(host, systemAuthContext); result[0] = systemAuthContext; }); return result[0]; }
public static void executeSafely(ExecutableBlock block) { try { block.execute(); } catch (Throwable throwable) { throwsUnchecked(throwable); } } }
public static void executeWithSystemAuthContext(Collection<ServiceHost> hosts, ExecutableBlock block) { // currently this method does not keep track of existing auth context. // If we need to recover existing auth context, retrieve and set them back in this method. Set<ServiceHost> authEnabledHosts = hosts.stream() .filter(ServiceHost::isAuthorizationEnabled) .collect(toSet()); authEnabledHosts.forEach(AuthTestUtils::setSystemAuthorizationContext); ExceptionTestUtils.executeSafely(block); authEnabledHosts.forEach(AuthTestUtils::resetAuthorizationContext); } }
public static void executeWithSystemAuthContext(Collection<ServiceHost> hosts, ExecutableBlock block) { // currently this method does not keep track of existing auth context. // If we need to recover existing auth context, retrieve and set them back in this method. Set<ServiceHost> authEnabledHosts = hosts.stream() .filter(ServiceHost::isAuthorizationEnabled) .collect(toSet()); authEnabledHosts.forEach(AuthTestUtils::setSystemAuthorizationContext); ExceptionTestUtils.executeSafely(block); authEnabledHosts.forEach(AuthTestUtils::resetAuthorizationContext); } }
public void waitFor(String timeoutMsg, WaitHandler wh) { ExceptionTestUtils.executeSafely(() -> { Date exp = getTestExpiration(); while (new Date().before(exp)) { if (wh.isReady()) { return; } // sleep for a tenth of the maintenance interval Thread.sleep(TimeUnit.MICROSECONDS.toMillis(getMaintenanceIntervalMicros()) / 10); } throw new TimeoutException(timeoutMsg); }); }
public void waitFor(String timeoutMsg, WaitHandler wh) { ExceptionTestUtils.executeSafely(() -> { Date exp = getTestExpiration(); while (new Date().before(exp)) { if (wh.isReady()) { return; } // sleep for a tenth of the maintenance interval Thread.sleep(TimeUnit.MICROSECONDS.toMillis(getMaintenanceIntervalMicros()) / 10); } throw new TimeoutException(timeoutMsg); }); }
public void await(ExecutableBlock beforeCheck) { ExceptionTestUtils.executeSafely(() -> {
public void await(ExecutableBlock beforeCheck) { ExceptionTestUtils.executeSafely(() -> {