-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
74 additions
and
1 deletion.
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
...limiter-core/src/test/java/com/wunong/smart/rate/limiter/core/limiter/AllLimitedTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package com.wunong.smart.rate.limiter.core.limiter; | ||
|
||
import com.wunong.smart.rate.limiter.core.factory.RateLimiterFactory; | ||
import com.wunong.smart.rate.limiter.core.factory.impl.DefaultRateLimiterFactory; | ||
import com.wunong.smart.rate.limiter.core.support.GuavaRateLimiter; | ||
|
||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
|
||
/** | ||
* @author created by zealot.zt | ||
*/ | ||
public class AllLimitedTest { | ||
|
||
private static final int POOL_SIZE = 2; | ||
private static ExecutorService service = Executors.newFixedThreadPool(POOL_SIZE); | ||
private static RateLimiterFactory factory = new DefaultRateLimiterFactory(); | ||
|
||
public static final int qps = 0; | ||
public static final String TEST_PREFIX_KEY = "test_"; | ||
private static final int KEY_SIZE = 4; | ||
|
||
static { | ||
for (int i = 0; i < KEY_SIZE; i++) { | ||
factory.register(getKey(i), GuavaRateLimiter.create(qps)); | ||
} | ||
} | ||
|
||
private static String getKey(int i) { | ||
return TEST_PREFIX_KEY + i; | ||
} | ||
|
||
public static void main(String[] args) { | ||
for (int i = 0; i < POOL_SIZE; i++) { | ||
service.submit(new LimierTask(factory, getKey(i % KEY_SIZE))); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
...core/src/test/java/com/wunong/smart/rate/limiter/core/support/KeyResolverFactoryTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package com.wunong.smart.rate.limiter.core.support; | ||
|
||
import com.wunong.smart.rate.limiter.core.api.KeyResolver; | ||
import com.wunong.smart.rate.limiter.core.factory.KeyResolverFactory; | ||
import com.wunong.smart.rate.limiter.core.factory.impl.DefaultKeyResolverFactory; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
/** | ||
* @author created by zealot.zt | ||
*/ | ||
public class KeyResolverFactoryTest { | ||
|
||
private final KeyResolverFactory factory = new DefaultKeyResolverFactory(); | ||
|
||
@Test | ||
public void testRegister() { | ||
String key = "testRegister"; | ||
KeyResolver resolver = event -> key; | ||
Assert.assertNull(factory.get(resolver.getClass())); | ||
factory.register(resolver); | ||
Assert.assertNotNull(factory.get(resolver.getClass())); | ||
} | ||
|
||
@Test | ||
public void testRegisterNone() { | ||
String key = "testRegister"; | ||
KeyResolver resolver = event -> key; | ||
Assert.assertNull(factory.get(KeyResolver.class)); | ||
factory.register(resolver); | ||
Assert.assertNull(factory.get(KeyResolver.class)); | ||
} | ||
|
||
} |