Skip to content

Commit

Permalink
Fix #224 - handle immidiate success scenario (#228)
Browse files Browse the repository at this point in the history
* Add test to verify behaviour

* Initial ugly implementation

Co-authored-by: Robby Decosemaeker <robby.decosemaeker@infosupport.com>
  • Loading branch information
Joengenduvel and Joengenduvel committed Mar 4, 2022
1 parent 3c9f345 commit 9e40d37
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
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);
}
}

0 comments on commit 9e40d37

Please sign in to comment.