Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #224 - handle immidiate success scenario #228

Merged
merged 2 commits into from Mar 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -112,9 +112,9 @@ public <T> void await(final ConditionEvaluationHandler<T> conditionEvaluationHan

pollInterval = conditionSettings.getPollInterval().next(pollCount, pollInterval);
sleepUninterruptibly(pollInterval.toNanos(), NANOSECONDS);
evaluationDuration = calculateConditionEvaluationDuration(pollDelay, pollingStartedNanos);
evaluationDuration = calculateConditionEvaluationDuration(pollDelay, pollingStartedNanos, firstSucceedSinceStarted, minWaitTime, holdPredicateWaitTime);
}
evaluationDuration = calculateConditionEvaluationDuration(pollDelay, pollingStartedNanos);
evaluationDuration = calculateConditionEvaluationDuration(pollDelay, pollingStartedNanos, firstSucceedSinceStarted, minWaitTime, holdPredicateWaitTime);
succeededBeforeTimeout = maxWaitTime.compareTo(evaluationDuration) > 0;
} catch (TimeoutException e) {
lastResult = new ConditionEvaluationResult(false, null, e);
Expand Down Expand Up @@ -239,8 +239,12 @@ public ConditionEvaluationResult call() {
}
}

static Duration calculateConditionEvaluationDuration(Duration pollDelay, long pollingStarted) {
final long calculatedDuration = System.nanoTime() - pollingStarted - pollDelay.toNanos();
static Duration calculateConditionEvaluationDuration(Duration pollDelay, long pollingStarted, long firstSucceedSinceStarted, Duration minWaitTime, Duration holdPredicateWaitTime) {
final long now = System.nanoTime();
long calculatedDuration = now - pollingStarted - pollDelay.toNanos();
if(firstSucceedSinceStarted > 0 && minWaitTime.isZero() && holdPredicateWaitTime.isZero()){
calculatedDuration = now - firstSucceedSinceStarted;
}
final long potentiallyCompensatedDuration = Math.max(calculatedDuration, 1L);
return Duration.of(potentiallyCompensatedDuration, NANOS);
}
Expand Down
Expand Up @@ -18,7 +18,7 @@ public class ConditionAwaiterTest {
*/
@Test public void
calculates_a_duration_of_1_nano_when_system_nano_time_is_skewed() {
Duration duration = ConditionAwaiter.calculateConditionEvaluationDuration(Duration.ofNanos(10000000L), System.nanoTime());
Duration duration = ConditionAwaiter.calculateConditionEvaluationDuration(Duration.ofNanos(10000000L), System.nanoTime(), 0, Duration.ofNanos(0), Duration.ofNanos(0));

assertThat(duration.toNanos(), is(1L));
}
Expand Down Expand Up @@ -49,4 +49,9 @@ public class ConditionAwaiterTest {

assertThat(Thread.getDefaultUncaughtExceptionHandler(), is(originalUncaughtExceptionHandler));
}

@Test
public void shouldHandleImmediateResultWithAtMost(){
await().atMost(Duration.ofMillis(10)).pollInterval(Duration.ofMillis(5)).until(() -> true);
}
}