@Override public ConditionEvaluationResult call() throws Exception { try { return conditionEvaluator.eval(delayed); } catch (Throwable e) { if (conditionSettings.shouldExceptionBeIgnored(e)) { return new ConditionEvaluationResult(false, null, e); } return new ConditionEvaluationResult(false, e, null); } } }
/** * Instruct Awaitility to ignore exceptions that occur during evaluation and matches the supplied <code>predicate</code>. * Exceptions will be treated as evaluating to * <code>false</code>. This is useful in situations where the evaluated conditions may temporarily throw exceptions. * * @return the condition factory. */ public ConditionFactory ignoreExceptionsMatching(Predicate<? super Throwable> predicate) { return new ConditionFactory(alias, timeoutConstraint, pollInterval, pollDelay, catchUncaughtExceptions, new PredicateExceptionIgnorer(predicate), conditionEvaluationListener, executorLifecycle); }
/** * Instruct Awaitility to ignore exceptions that occur during evaluation and matches the supplied Hamcrest matcher. * Exceptions will be treated as evaluating to * <code>false</code>. This is useful in situations where the evaluated conditions may temporarily throw exceptions. * * @return the condition factory. */ public ConditionFactory ignoreExceptionsMatching(Matcher<? super Throwable> matcher) { return new ConditionFactory(alias, timeoutConstraint, pollInterval, pollDelay, catchUncaughtExceptions, new HamcrestExceptionIgnorer(matcher), conditionEvaluationListener, executorLifecycle); }
/** Returns a configured {@link ConditionFactory} that polls at a short interval. */ public static ConditionFactory await() { return Awaitility.with() .pollDelay(1, TimeUnit.MILLISECONDS).and() .pollInterval(1, TimeUnit.MILLISECONDS); } }
@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 { 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); } } };
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 void conditionEvaluated(EvaluatedCondition<CountDownBean> condition) { if (!condition.isSatisfied()) { lastMismatchMessage.value = condition.getDescription(); } } })
@Override public ExecutorService get() { return InternalExecutorServiceFactory.create(threadSupplier); } }));
@Override public ExecutorService get() { return InternalExecutorServiceFactory.sameThreadExecutorService(); } }));
/** * Handle condition evaluation results each time evaluation of a condition occurs. Works only with a Hamcrest matcher-based condition. * * @param conditionEvaluationListener the condition evaluation listener * @return the condition factory */ public ConditionFactory conditionEvaluationListener(ConditionEvaluationListener conditionEvaluationListener) { return new ConditionFactory(alias, timeoutConstraint, pollInterval, pollDelay, catchUncaughtExceptions, exceptionsIgnorer, conditionEvaluationListener, executorLifecycle); }
/** * Instruct Awaitility to ignore caught or uncaught exceptions during condition evaluation. * Exceptions will be treated as evaluating to <code>false</code>. Your test will not fail * upon an exception, unless it times out. */ public static void ignoreExceptionsByDefault() { defaultExceptionIgnorer = new PredicateExceptionIgnorer(new Predicate<Throwable>() { public boolean matches(Throwable e) { return true; } }); }
/** * <p>await.</p> * * @return a {@link java.lang.Void} object. */ public Void await() { conditionAwaiter.await(conditionEvaluationHandler); return null; } }
public Thread newThread(Runnable r) { final String threadName = generateDefaultThreadName(alias); return threadSupplier.apply(r, threadName); } });
/** * Specify the executor service whose threads will be used to evaluate the poll condition in Awaitility. * Note that the executor service must be shutdown manually! * This is an advanced feature and it should only be used sparingly. * * @param executorService The executor service that Awaitility will use when polling condition evaluations * @since 3.0.0 */ public static void pollExecutorService(final ExecutorService executorService) { defaultExecutorLifecycle = ExecutorLifecycle.withoutCleanup(executorService); }
ConditionEvaluationHandler(Matcher<? super T> matcher, ConditionSettings settings) { this.matcher = matcher; this.settings = settings; watch = new StopWatch(); }
@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 public ExecutorService get() { return InternalExecutorServiceFactory.create(new BiFunction<Runnable, String, Thread>() { @Override public Thread apply(Runnable r, String threadName) { return new Thread(Thread.currentThread().getThreadGroup(), r, threadName); } }, alias); } });
@Override public ExecutorService get() { return InternalExecutorServiceFactory.sameThreadExecutorService(); } });
/** * Instruct Awaitility to catch uncaught exceptions from other threads. This * is useful in multi-threaded systems when you want your test to fail * regardless of which thread throwing the exception. Default is * <code>true</code>. * * @return the condition factory */ public ConditionFactory catchUncaughtExceptions() { return new ConditionFactory(alias, timeoutConstraint, pollInterval, pollDelay, true, exceptionsIgnorer, conditionEvaluationListener, executorLifecycle); }