Skip to content

Commit

Permalink
feat:add junit test
Browse files Browse the repository at this point in the history
  • Loading branch information
itzealot committed Aug 21, 2022
1 parent 9df7774 commit c849e5d
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 1 deletion.
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)));
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class OneLimiterTest {

static {
for (int i = 0; i < KEY_SIZE; i++) {
factory.register(getKey(i), new GuavaRateLimiter(10));
factory.register(getKey(i), GuavaRateLimiter.create(10));
}
}

Expand Down
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));
}

}

0 comments on commit c849e5d

Please sign in to comment.