add RateLimiter

This commit is contained in:
Looly 2024-08-19 22:44:33 +08:00
parent 0ad439fc7f
commit ec5fcaa76b

View File

@ -0,0 +1,27 @@
package org.dromara.hutool.core.thread.ratelimiter;
import org.dromara.hutool.core.thread.ThreadUtil;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
public class SemaphoreRateLimiterTest {
@Test
void test() {
final RateLimiterConfig rateLimiterConfig = RateLimiterConfig.of(5000, 300, 5);
final RateLimiter rateLimiter = new SemaphoreRateLimiter(rateLimiterConfig);
final boolean b = rateLimiter.tryAcquire(5);
Assertions.assertTrue(b);
// 超过数量
final boolean b1 = rateLimiter.tryAcquire(1);
Assertions.assertFalse(b1);
ThreadUtil.sleep(310);
// 填充新的许可
final boolean b2 = rateLimiter.tryAcquire(5);
Assertions.assertTrue(b2);
// 超过数量
final boolean b3 = rateLimiter.tryAcquire(1);
Assertions.assertFalse(b3);
}
}