Skip to content

Commit

Permalink
Fix minor mistakes in rate limiter unit test (apache#8253)
Browse files Browse the repository at this point in the history
1、acquirePermist  -> acquirePermits
2、remove some useless 'throws Exception'
  • Loading branch information
zhanghaou authored Oct 15, 2020
1 parent 59e0cfb commit 7b8c6c7
Showing 1 changed file with 17 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,14 @@
*/
package org.apache.pulsar.common.util;

import static org.testng.Assert.fail;
import static org.testng.Assert.assertTrue;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertTrue;
import static org.testng.Assert.fail;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Supplier;

import org.testng.annotations.Test;

public class RateLimiterTest {
Expand All @@ -49,7 +48,7 @@ public void testInvalidRenewTime() {
}

@Test
public void testclose() throws Exception {
public void testClose() throws Exception {
RateLimiter rate = new RateLimiter(1, 1000, TimeUnit.MILLISECONDS);
assertFalse(rate.isClosed());
rate.close();
Expand Down Expand Up @@ -95,11 +94,11 @@ public void testAcquire() throws Exception {
public void testMultipleAcquire() throws Exception {
final long rateTimeMSec = 1000;
final int permits = 100;
final int acquirePermist = 50;
final int acquirePermits = 50;
RateLimiter rate = new RateLimiter(permits, rateTimeMSec, TimeUnit.MILLISECONDS);
long start = System.currentTimeMillis();
for (int i = 0; i < permits / acquirePermist; i++) {
rate.acquire(acquirePermist);
for (int i = 0; i < permits / acquirePermits; i++) {
rate.acquire(acquirePermits);
}
long end = System.currentTimeMillis();
assertTrue((end - start) < rateTimeMSec);
Expand All @@ -108,7 +107,7 @@ public void testMultipleAcquire() throws Exception {
}

@Test
public void testTryAcquireNoPermits() throws Exception {
public void testTryAcquireNoPermits() {
final long rateTimeMSec = 1000;
RateLimiter rate = new RateLimiter(1, rateTimeMSec, TimeUnit.MILLISECONDS);
assertTrue(rate.tryAcquire());
Expand All @@ -118,7 +117,7 @@ public void testTryAcquireNoPermits() throws Exception {
}

@Test
public void testTryAcquire() throws Exception {
public void testTryAcquire() {
final long rateTimeMSec = 1000;
final int permits = 100;
RateLimiter rate = new RateLimiter(permits, rateTimeMSec, TimeUnit.MILLISECONDS);
Expand All @@ -130,13 +129,13 @@ public void testTryAcquire() throws Exception {
}

@Test
public void testMultipleTryAcquire() throws Exception {
public void testMultipleTryAcquire() {
final long rateTimeMSec = 1000;
final int permits = 100;
final int acquirePermist = 50;
final int acquirePermits = 50;
RateLimiter rate = new RateLimiter(permits, rateTimeMSec, TimeUnit.MILLISECONDS);
for (int i = 0; i < permits / acquirePermist; i++) {
rate.tryAcquire(acquirePermist);
for (int i = 0; i < permits / acquirePermits; i++) {
rate.tryAcquire(acquirePermits);
}
assertEquals(rate.getAvailablePermits(), 0);
rate.close();
Expand Down Expand Up @@ -166,19 +165,19 @@ public void testResetRate() throws Exception {
}

@Test
public void testRateLimiterWithPermitUpdater() throws Exception{
public void testRateLimiterWithPermitUpdater() throws Exception {
long permits = 10;
long rateTime = 1;
long newUpdatedRateLimit = 100L;
Supplier<Long> permitUpdater = () -> newUpdatedRateLimit;
RateLimiter limiter = new RateLimiter(null, permits , 1, TimeUnit.SECONDS, permitUpdater);
RateLimiter limiter = new RateLimiter(null, permits, 1, TimeUnit.SECONDS, permitUpdater);
limiter.acquire();
Thread.sleep(rateTime*3*1000);
Thread.sleep(rateTime * 3 * 1000);
assertEquals(limiter.getAvailablePermits(), newUpdatedRateLimit);
}

@Test
public void testRateLimiterWithFunction()throws Exception {
public void testRateLimiterWithFunction() {
final AtomicInteger atomicInteger = new AtomicInteger(0);
long permits = 10;
long rateTime = 1;
Expand Down

0 comments on commit 7b8c6c7

Please sign in to comment.