@SuppressWarnings("unchecked") void handleConditionResultMatch(String matchMessage, T currentConditionValue, Duration pollInterval) { ConditionEvaluationListener<T> listener = settings.getConditionEvaluationListener(); if (listener == null) { return; } long elapsedTimeInMS = watch.getElapsedTimeInMS(); long remainingTimeInMS = getRemainingTimeInMS(elapsedTimeInMS, settings.getMaxWaitTime()); try { listener.conditionEvaluated(new EvaluatedCondition<T>(matchMessage, matcher, currentConditionValue, elapsedTimeInMS, remainingTimeInMS, true, settings.getAlias(), pollInterval)); } catch (ClassCastException e) { throwClassCastExceptionBecauseConditionEvaluationListenerCouldntBeApplied(e, listener); } }
/** * {@inheritDoc} */ @Override public void uncaughtException(Thread thread, Throwable throwable) { if (!conditionSettings.shouldExceptionBeIgnored(throwable)) { uncaughtThrowable.set(throwable); // We shutdown the executor "now" in order to fail the test immediately conditionSettings.getExecutorLifecycle().executeUnexpectedCleanupBehavior(executor); } }
/** * <p>Constructor for ConditionAwaiter.</p> * * @param conditionEvaluator a {@link ConditionEvaluator} object. * @param conditionSettings a {@link org.awaitility.core.ConditionSettings} object. */ ConditionAwaiter(final ConditionEvaluator conditionEvaluator, final ConditionSettings conditionSettings) { if (conditionEvaluator == null) { throw new IllegalArgumentException("You must specify a condition (was null)."); } if (conditionSettings == null) { throw new IllegalArgumentException("You must specify the condition settings (was null)."); } if (conditionSettings.shouldCatchUncaughtExceptions()) { Thread.setDefaultUncaughtExceptionHandler(this); } this.conditionSettings = conditionSettings; this.conditionEvaluator = conditionEvaluator; this.executor = conditionSettings.getExecutorLifecycle().supplyExecutorService(); this.uncaughtThrowable = new AtomicReference<Throwable>(); }
final Duration pollDelay = conditionSettings.getPollDelay(); final Duration maxWaitTime = conditionSettings.getMaxWaitTime(); final Duration minWaitTime = conditionSettings.getMinWaitTime(); break; pollInterval = conditionSettings.getPollInterval().next(pollCount, pollInterval); Thread.sleep(pollInterval.getValueInMS()); evaluationDuration = calculateConditionEvaluationDuration(pollDelay, pollingStartedNanos); final String message; String timeoutMessage = getTimeoutMessage(); if (conditionSettings.hasAlias()) { message = String.format("Condition with alias '%s' didn't complete within %s %s because %s.", conditionSettings.getAlias(), maxTimeout, maxWaitTimeLowerCase, decapitalize(timeoutMessage)); } else { message = String.format("%s within %s %s.", timeoutMessage, maxTimeout, maxWaitTimeLowerCase); } finally { uncaughtThrowable.set(null); conditionSettings.getExecutorLifecycle().executeNormalCleanupBehavior(executor);
@Override protected String getTimeoutMessage() { return getMismatchMessage(supplier, lastExceptionMessage, settings.getAlias(), false); } };
return new ConditionSettings(alias, catchUncaughtExceptions, timeoutConstraint, pollInterval, actualPollDelay, conditionEvaluationListener, exceptionsIgnorer, executorLifecycle);
final Duration pollDelay = conditionSettings.getPollDelay(); final Duration maxWaitTime = conditionSettings.getMaxWaitTime(); final Duration minWaitTime = conditionSettings.getMinWaitTime(); break; pollInterval = conditionSettings.getPollInterval().next(pollCount, pollInterval); Thread.sleep(pollInterval.getValueInMS()); evaluationDuration = calculateConditionEvaluationDuration(pollDelay, pollingStartedNanos); final String message; String timeoutMessage = getTimeoutMessage(); if (conditionSettings.hasAlias()) { message = String.format("Condition with alias '%s' didn't complete within %s %s because %s.", conditionSettings.getAlias(), maxTimeout, maxWaitTimeLowerCase, decapitalize(timeoutMessage)); } else { message = String.format("%s within %s %s.", timeoutMessage, maxTimeout, maxWaitTimeLowerCase); } finally { uncaughtThrowable.set(null); conditionSettings.getExecutorLifecycle().executeNormalCleanupBehavior(executor);
public ConditionEvaluationResult eval(Duration pollInterval) throws Exception { boolean conditionFulfilled = matcher.call(); if (conditionFulfilled) { conditionEvaluationHandler.handleConditionResultMatch(getMatchMessage(matcher, settings.getAlias()), true, pollInterval); } else { conditionEvaluationHandler.handleConditionResultMismatch(getMismatchMessage(matcher, settings.getAlias()), false, pollInterval); } return new ConditionEvaluationResult(conditionFulfilled); }
return new ConditionSettings(alias, catchUncaughtExceptions, timeoutConstraint, pollInterval, actualPollDelay, conditionEvaluationListener, exceptionsIgnorer, executorLifecycle);
@SuppressWarnings("unchecked") void handleConditionResultMismatch(String mismatchMessage, T currentConditionValue, Duration pollInterval) { ConditionEvaluationListener<T> listener = settings.getConditionEvaluationListener(); if (listener == null) { return; } long elapsedTimeInMS = watch.getElapsedTimeInMS(); long remainingTimeInMS = getRemainingTimeInMS(elapsedTimeInMS, settings.getMaxWaitTime()); try { listener.conditionEvaluated(new EvaluatedCondition<T>(mismatchMessage, matcher, currentConditionValue, elapsedTimeInMS, remainingTimeInMS, false, settings.getAlias(), pollInterval)); } catch (ClassCastException e) { throwClassCastExceptionBecauseConditionEvaluationListenerCouldntBeApplied(e, listener); } }
/** * {@inheritDoc} */ @Override public void uncaughtException(Thread thread, Throwable throwable) { if (!conditionSettings.shouldExceptionBeIgnored(throwable)) { uncaughtThrowable.set(throwable); // We shutdown the executor "now" in order to fail the test immediately conditionSettings.getExecutorLifecycle().executeUnexpectedCleanupBehavior(executor); } }
/** * <p>Constructor for ConditionAwaiter.</p> * * @param conditionEvaluator a {@link ConditionEvaluator} object. * @param conditionSettings a {@link org.awaitility.core.ConditionSettings} object. */ ConditionAwaiter(final ConditionEvaluator conditionEvaluator, final ConditionSettings conditionSettings) { if (conditionEvaluator == null) { throw new IllegalArgumentException("You must specify a condition (was null)."); } if (conditionSettings == null) { throw new IllegalArgumentException("You must specify the condition settings (was null)."); } if (conditionSettings.shouldCatchUncaughtExceptions()) { Thread.setDefaultUncaughtExceptionHandler(this); } this.conditionSettings = conditionSettings; this.conditionEvaluator = conditionEvaluator; this.executor = conditionSettings.getExecutorLifecycle().supplyExecutorService(); this.uncaughtThrowable = new AtomicReference<Throwable>(); }
public ConditionEvaluationResult eval(Duration pollInterval) throws Exception { try { supplier.run(); conditionEvaluationHandler.handleConditionResultMatch(getMatchMessage(supplier, settings.getAlias()), null, pollInterval); return new ConditionEvaluationResult(true); } catch (AssertionError e) { lastExceptionMessage = e.getMessage(); conditionEvaluationHandler.handleConditionResultMismatch(getMismatchMessage(supplier, lastExceptionMessage, settings.getAlias(), true), null, pollInterval); return new ConditionEvaluationResult(false, null, e); } catch (Throwable throwable) { return CheckedExceptionRethrower.safeRethrow(throwable); } } };
@SuppressWarnings("unchecked") void handleConditionResultMismatch(String mismatchMessage, T currentConditionValue, Duration pollInterval) { ConditionEvaluationListener<T> listener = settings.getConditionEvaluationListener(); if (listener == null) { return; } long elapsedTimeInMS = watch.getElapsedTimeInMS(); long remainingTimeInMS = getRemainingTimeInMS(elapsedTimeInMS, settings.getMaxWaitTime()); try { listener.conditionEvaluated(new EvaluatedCondition<T>(mismatchMessage, matcher, currentConditionValue, elapsedTimeInMS, remainingTimeInMS, false, settings.getAlias(), pollInterval)); } catch (ClassCastException e) { throwClassCastExceptionBecauseConditionEvaluationListenerCouldntBeApplied(e, listener); } }
@Override protected String getTimeoutMessage() { return getMismatchMessage(supplier, lastExceptionMessage, settings.getAlias(), false); } };
@SuppressWarnings("unchecked") void handleConditionResultMatch(String matchMessage, T currentConditionValue, Duration pollInterval) { ConditionEvaluationListener<T> listener = settings.getConditionEvaluationListener(); if (listener == null) { return; } long elapsedTimeInMS = watch.getElapsedTimeInMS(); long remainingTimeInMS = getRemainingTimeInMS(elapsedTimeInMS, settings.getMaxWaitTime()); try { listener.conditionEvaluated(new EvaluatedCondition<T>(matchMessage, matcher, currentConditionValue, elapsedTimeInMS, remainingTimeInMS, true, settings.getAlias(), pollInterval)); } catch (ClassCastException e) { throwClassCastExceptionBecauseConditionEvaluationListenerCouldntBeApplied(e, listener); } }
public ConditionEvaluationResult eval(Duration pollInterval) throws Exception { boolean conditionFulfilled = matcher.call(); if (conditionFulfilled) { conditionEvaluationHandler.handleConditionResultMatch(getMatchMessage(matcher, settings.getAlias()), true, pollInterval); } else { conditionEvaluationHandler.handleConditionResultMismatch(getMismatchMessage(matcher, settings.getAlias()), false, pollInterval); } return new ConditionEvaluationResult(conditionFulfilled); }
public ConditionEvaluationResult eval(Duration pollInterval) throws Exception { try { supplier.run(); conditionEvaluationHandler.handleConditionResultMatch(getMatchMessage(supplier, settings.getAlias()), null, pollInterval); return new ConditionEvaluationResult(true); } catch (AssertionError e) { lastExceptionMessage = e.getMessage(); conditionEvaluationHandler.handleConditionResultMismatch(getMismatchMessage(supplier, lastExceptionMessage, settings.getAlias(), true), null, pollInterval); return new ConditionEvaluationResult(false, null, e); } catch (Throwable throwable) { return CheckedExceptionRethrower.safeRethrow(throwable); } } };