Skip to content

Commit

Permalink
[fix] avoided thread.sleep usage and added semaphore as replacement
Browse files Browse the repository at this point in the history
Committer: Vipin Sharma <sharmavipin1310@gmail.com>
  • Loading branch information
Vipin Sharma authored and Vipin Sharma committed Oct 23, 2021
1 parent 4fe3108 commit 7d13b01
Showing 1 changed file with 7 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,14 @@
import static org.junit.Assert.assertTrue;

import java.time.Duration;
import java.util.concurrent.TimeUnit;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

import org.apache.commons.lang3.concurrent.TimedSemaphore;

public class BucketRateLimiterTest {
@Rule public ExpectedException thrown = ExpectedException.none();

Expand All @@ -46,15 +50,16 @@ public void testBucketRateLimiterBasic() {
@Test
public void testBucketRateLimiterTokenAdded() throws InterruptedException {
RateLimiter<String> rateLimiter = new BucketRateLimiter<>(2, 1, Duration.ofSeconds(2));
TimedSemaphore timedSemaphore = new TimedSemaphore(4, TimeUnit.SECONDS, 1);

timedSemaphore.acquire();
assertEquals(Duration.ZERO, rateLimiter.when("one"));
assertEquals(Duration.ZERO, rateLimiter.when("one"));

Duration waitDuration = rateLimiter.when("one");
assertTrue(waitDuration.getSeconds() > 0);

Thread.sleep(4000);

timedSemaphore.acquire();
assertEquals(Duration.ZERO, rateLimiter.when("two"));

waitDuration = rateLimiter.when("two");
Expand Down

0 comments on commit 7d13b01

Please sign in to comment.