From 12f94cb02942a42da7c4a9d174275f1dba3e1b3e Mon Sep 17 00:00:00 2001 From: bstorozhuk Date: Fri, 24 Mar 2017 12:02:13 +0200 Subject: [PATCH] Issue #61 first cycle permissions fix --- .../ratelimiter/internal/AtomicRateLimiter.java | 2 +- .../ratelimiter/internal/AtomicRateLimiterTest.java | 11 ++++++++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/resilience4j-ratelimiter/src/main/java/io/github/resilience4j/ratelimiter/internal/AtomicRateLimiter.java b/resilience4j-ratelimiter/src/main/java/io/github/resilience4j/ratelimiter/internal/AtomicRateLimiter.java index 890ee96ca9..a085837b5e 100644 --- a/resilience4j-ratelimiter/src/main/java/io/github/resilience4j/ratelimiter/internal/AtomicRateLimiter.java +++ b/resilience4j-ratelimiter/src/main/java/io/github/resilience4j/ratelimiter/internal/AtomicRateLimiter.java @@ -66,7 +66,7 @@ public AtomicRateLimiter(String name, RateLimiterConfig rateLimiterConfig) { permissionsPerCycle = rateLimiterConfig.getLimitForPeriod(); waitingThreads = new AtomicInteger(0); - state = new AtomicReference<>(new State(0, 0, 0)); + state = new AtomicReference<>(new State(0, permissionsPerCycle, 0)); PublishProcessor publisher = PublishProcessor.create(); this.eventPublisher = publisher.toSerialized(); diff --git a/resilience4j-ratelimiter/src/test/java/io/github/resilience4j/ratelimiter/internal/AtomicRateLimiterTest.java b/resilience4j-ratelimiter/src/test/java/io/github/resilience4j/ratelimiter/internal/AtomicRateLimiterTest.java index 0110425b26..0bd283c41f 100644 --- a/resilience4j-ratelimiter/src/test/java/io/github/resilience4j/ratelimiter/internal/AtomicRateLimiterTest.java +++ b/resilience4j-ratelimiter/src/test/java/io/github/resilience4j/ratelimiter/internal/AtomicRateLimiterTest.java @@ -49,6 +49,7 @@ public class AtomicRateLimiterTest { private static final String LIMITER_NAME = "test"; private static final long CYCLE_IN_NANOS = 500_000_000L; private static final long POLL_INTERVAL_IN_NANOS = 2_000_000L; + private static final int PERMISSIONS_RER_CYCLE = 1; private RateLimiterConfig rateLimiterConfig; private AtomicRateLimiter rateLimiter; private AtomicRateLimiter.AtomicRateLimiterMetrics metrics; @@ -68,7 +69,7 @@ private void setTimeOnNanos(long nanoTime) throws Exception { @Before public void setup() { rateLimiterConfig = RateLimiterConfig.custom() - .limitForPeriod(1) + .limitForPeriod(PERMISSIONS_RER_CYCLE) .limitRefreshPeriod(Duration.ofNanos(CYCLE_IN_NANOS)) .timeoutDuration(Duration.ZERO) .build(); @@ -78,6 +79,14 @@ public void setup() { eventStream = rateLimiter.getEventStream(); } + @Test + public void permissionsInFirstCycle() throws Exception { + setTimeOnNanos(CYCLE_IN_NANOS - 10); + RateLimiter.Metrics metrics = rateLimiter.getMetrics(); + int availablePermissions = metrics.getAvailablePermissions(); + then(availablePermissions).isEqualTo(PERMISSIONS_RER_CYCLE); + } + @Test public void acquireAndRefreshWithEventPublishing() throws Exception { CompletableFuture> events = subscribeOnAllEventsDescriptions(4);