mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
add RateLimiter
This commit is contained in:
parent
0ad439fc7f
commit
ec5fcaa76b
@ -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);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user