/** * Create a new instance of the {@link FixedPollInterval}. * * @param pollInterval The poll interval amount * @param unit The time unit */ public FixedPollInterval(long pollInterval, TimeUnit unit) { this(new Duration(pollInterval, unit)); }
public final Duration apply(Duration lhs, Duration rhs) { if (lhs == null || rhs == null) { throw new IllegalArgumentException("Duration cannot be null"); } Duration specialDuration = handleSpecialCases(lhs, rhs); if (specialDuration != null) { return specialDuration; } final Duration newDuration; if (lhs.getTimeUnit().ordinal() > rhs.getTimeUnit().ordinal()) { long lhsConverted = rhs.getTimeUnit().convert(lhs.getValue(), lhs.getTimeUnit()); newDuration = new Duration(apply(lhsConverted, rhs.getValue()), rhs.getTimeUnit()); } else if (lhs.getTimeUnit().ordinal() < rhs.getTimeUnit().ordinal()) { long rhsConverted = lhs.getTimeUnit().convert(rhs.getValue(), rhs.getTimeUnit()); newDuration = new Duration(apply(lhs.getValue(), rhsConverted), lhs.getTimeUnit()); } else { // Same time unit newDuration = new Duration(apply(lhs.getValue(), rhs.getValue()), lhs.getTimeUnit()); } return newDuration; }
@Test public void duration_is_equal_to_other_duration_if_number_of_milliseconds_are_the_same() { // Given final Duration duration1 = new Duration(1000, MILLISECONDS); final Duration duration2 = new Duration(1, SECONDS); // When final boolean equals = duration1.equals(duration2); // Then assertThat(equals, is(true)); } }
private long getRemainingTimeInMS(long elapsedTimeInMS, Duration maxWaitTime) { return maxWaitTime.equals(Duration.FOREVER) ? Long.MAX_VALUE : maxWaitTime.getValueInMS() - elapsedTimeInMS; }
static Duration calculateConditionEvaluationDuration(Duration pollDelay, long pollingStarted) { return new Duration(System.nanoTime() - pollingStarted - pollDelay.getValueInNS(), NANOSECONDS); } }
@Test public void minus_can_subtract_amount_from_greater_time_unit_with_amount_with_lower_time_unit() { // When Duration sum = new Duration(2, SECONDS).minus(4, MILLISECONDS); // Then assertThat(sum.getValue(), is(1996L)); assertThat(sum.getTimeUnit(), is(MILLISECONDS)); }
@Test public void plus_can_add_duration() { // When Duration sum = new Duration(4, SECONDS).plus(Duration.FIVE_HUNDRED_MILLISECONDS); // Then assertThat(sum.getValue(), is(4500L)); assertThat(sum.getTimeUnit(), is(MILLISECONDS)); }
final long maxTimeout = maxWaitTime.getValue(); long pollingStartedNanos = System.nanoTime() - pollDelay.getValueInNS(); Duration evaluationDuration = new Duration(0, MILLISECONDS); Future<ConditionEvaluationResult> currentConditionEvaluation = null; try { if (!pollDelay.isZero()) { Thread.sleep(pollDelay.getValueInMS()); while (maxWaitTime.compareTo(evaluationDuration) > 0) { pollCount = pollCount + 1; Duration maxWaitTimeForThisCondition = maxWaitTime.minus(evaluationDuration); currentConditionEvaluation = executor.submit(new ConditionPoller(pollInterval)); lastResult = currentConditionEvaluation.get(maxWaitTimeForThisCondition.getValue(), maxWaitTimeForThisCondition.getTimeUnit()); if (lastResult.isSuccessful() || lastResult.hasThrowable()) { break; Thread.sleep(pollInterval.getValueInMS()); evaluationDuration = calculateConditionEvaluationDuration(pollDelay, pollingStartedNanos); succeededBeforeTimeout = maxWaitTime.compareTo(evaluationDuration) > 0; } catch (TimeoutException e) { lastResult = new ConditionEvaluationResult(false, null, e); throw lastResult.getThrowable(); } else if (!succeededBeforeTimeout) {
@Test public void divide_can_divide_amount() { // When Duration sum = new Duration(4, SECONDS).divide(2); // Then assertThat(sum.getValue(), is(2L)); assertThat(sum.getTimeUnit(), is(SECONDS)); }
@Test public void multiply_can_multiply_amount() { // When Duration sum = new Duration(4, SECONDS).multiply(2); // Then assertThat(sum.getValue(), is(8L)); assertThat(sum.getTimeUnit(), is(SECONDS)); }
/** * Gets the timeout value as a {@link Duration}. * * <p> * One use of this is with {@link Mockito#timeout(long)}: * * <pre> * import static org.apache.geode.test.awaitility.GeodeAwaitility.getTimeout; * * private static final long TIMEOUT = getTimeout().getValueInMS(); * * {@literal @}Test * public void test() { * ... * ArgumentCaptor<AlertDetails> alertDetailsCaptor = ArgumentCaptor.forClass(AlertDetails.class); * verify(messageListener, timeout(TIMEOUT)).created(alertDetailsCaptor.capture()); * } * * <pre> * * @return the current timeout value as a {@code Duration} */ public static Duration getTimeout() { return new Duration(getLong(TIMEOUT_SECONDS_PROPERTY, DEFAULT_TIMEOUT.getValue()), SECONDS); }
private ConditionSettings generateConditionSettings() { Duration actualPollDelay = definePollDelay(pollDelay, pollInterval); if (actualPollDelay.isForever()) { throw new IllegalArgumentException("Cannot delay polling forever"); final long timeoutInMS = timeout.getValueInMS(); if (!timeout.isForever() && timeoutInMS <= actualPollDelay.getValueInMS()) { throw new IllegalStateException(String.format("Timeout (%s %s) must be greater than the poll delay (%s %s).", timeout.getValue(), timeout.getTimeUnitAsString(), actualPollDelay.getValue(), actualPollDelay.getTimeUnitAsString())); } else if ((!actualPollDelay.isForever() && !timeout.isForever()) && timeoutInMS <= actualPollDelay.getValueInMS()) { throw new IllegalStateException(String.format("Timeout (%s %s) must be greater than the poll delay (%s %s).", timeout.getValue(), timeout.getTimeUnitAsString(), actualPollDelay.getValue(), actualPollDelay.getTimeUnitAsString()));
public void conditionEvaluated(EvaluatedCondition<Object> condition) { String description = condition.getDescription(); long elapsedTime = timeUnit.convert(condition.getElapsedTimeInMS(), MILLISECONDS); long remainingTime = timeUnit.convert(condition.getRemainingTimeInMS(), MILLISECONDS); String timeUnitAsString = timeUnit.toString().toLowerCase(); if (condition.isSatisfied()) { System.out.printf("%s after %d %s (remaining time %d %s, last poll interval was %d %s)%n", description, elapsedTime, timeUnitAsString, remainingTime, timeUnitAsString, condition.getPollInterval().getValue(), condition.getPollInterval().getTimeUnitAsString()); } else { System.out.printf("%s (elapsed time %d %s, remaining time %d %s (last poll interval was %d %s))%n", description, elapsedTime, timeUnitAsString, remainingTime, timeUnitAsString, condition.getPollInterval().getValue(), condition.getPollInterval().getTimeUnitAsString()); } } }
/** * <p>isZero</p> * * @return a boolean. */ public boolean isZero() { return this.equals(ZERO); }
@Test public void getTimeoutIsOverriddenWithSystemProperty() { System.setProperty(TIMEOUT_SECONDS_PROPERTY, valueOf(ONE_MINUTE.getValue())); assertThat(getTimeout()).isEqualTo(ONE_MINUTE); }