Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,10 @@ public Optional<Duration> isLimited(RateLimitState rateLimitState) {
actualState.increaseCount();
return Optional.empty();
} else {
return Optional.of(Duration.between(actualState.getLastRefreshTime(), LocalDateTime.now()));
var remaining =
Duration.between(
LocalDateTime.now(), actualState.getLastRefreshTime().plus(refreshPeriod));
return Optional.of(remaining.isNegative() ? Duration.ZERO : remaining);
Comment on lines +67 to +70
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,22 @@ void returnsMinimalDurationToAcquirePermission() {
res = rl.isLimited(state);

assertThat(res).isPresent();
assertThat(res.get()).isLessThan(REFRESH_PERIOD);
assertThat(res.get()).isLessThanOrEqualTo(REFRESH_PERIOD);
// the whole period is still ahead of us, so the reported wait must be close to it
assertThat(res.get()).isGreaterThan(REFRESH_PERIOD.dividedBy(2));
}

@Test
void reportedDurationIsTheTimeRemainingNotTheTimeElapsed() throws InterruptedException {
var rl = new LinearRateLimiter(REFRESH_PERIOD, 1);
assertThat(rl.isLimited(state)).isEmpty();

var justAfterLimit = rl.isLimited(state).orElseThrow();
Thread.sleep(REFRESH_PERIOD.toMillis() / 2);
var halfWayThroughPeriod = rl.isLimited(state).orElseThrow();

// as the refresh period elapses, less time remains until a permission is available again
assertThat(halfWayThroughPeriod).isLessThan(justAfterLimit);
Comment on lines +66 to +71
}

@Test
Expand Down
Loading