Merge pull request #646 from rastdeepanshu/master
Modifications for intermittent test failure in Throttling pattern.
This commit is contained in:
commit
31120a8fa0
@ -46,7 +46,7 @@ class B2BService {
|
|||||||
*/
|
*/
|
||||||
public int dummyCustomerApi(Tenant tenant) {
|
public int dummyCustomerApi(Tenant tenant) {
|
||||||
String tenantName = tenant.getName();
|
String tenantName = tenant.getName();
|
||||||
int count = CallsCount.getCount(tenantName);
|
long count = CallsCount.getCount(tenantName);
|
||||||
LOGGER.debug("Counter for {} : {} ", tenant.getName(), count);
|
LOGGER.debug("Counter for {} : {} ", tenant.getName(), count);
|
||||||
if (count >= tenant.getAllowedCallsPerSecond()) {
|
if (count >= tenant.getAllowedCallsPerSecond()) {
|
||||||
LOGGER.error("API access per second limit reached for: {}", tenantName);
|
LOGGER.error("API access per second limit reached for: {}", tenantName);
|
||||||
|
@ -22,9 +22,13 @@
|
|||||||
*/
|
*/
|
||||||
package com.iluwatar.throttling;
|
package com.iluwatar.throttling;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Map.Entry;
|
import java.util.Map.Entry;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
import java.util.concurrent.atomic.AtomicLong;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A class to keep track of the counter of different Tenants
|
* A class to keep track of the counter of different Tenants
|
||||||
@ -32,16 +36,16 @@ import java.util.concurrent.ConcurrentHashMap;
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public final class CallsCount {
|
public final class CallsCount {
|
||||||
private static Map<String, Integer> tenantCallsCount = new ConcurrentHashMap<>();
|
|
||||||
|
private static final Logger LOGGER = LoggerFactory.getLogger(CallsCount.class);
|
||||||
|
private static Map<String, AtomicLong> tenantCallsCount = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add a new tenant to the map.
|
* Add a new tenant to the map.
|
||||||
* @param tenantName name of the tenant.
|
* @param tenantName name of the tenant.
|
||||||
*/
|
*/
|
||||||
public static void addTenant(String tenantName) {
|
public static void addTenant(String tenantName) {
|
||||||
if (!tenantCallsCount.containsKey(tenantName)) {
|
tenantCallsCount.putIfAbsent(tenantName, new AtomicLong(0));
|
||||||
tenantCallsCount.put(tenantName, 0);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -49,7 +53,7 @@ public final class CallsCount {
|
|||||||
* @param tenantName name of the tenant.
|
* @param tenantName name of the tenant.
|
||||||
*/
|
*/
|
||||||
public static void incrementCount(String tenantName) {
|
public static void incrementCount(String tenantName) {
|
||||||
tenantCallsCount.put(tenantName, tenantCallsCount.get(tenantName) + 1);
|
tenantCallsCount.get(tenantName).incrementAndGet();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -57,16 +61,17 @@ public final class CallsCount {
|
|||||||
* @param tenantName name of the tenant.
|
* @param tenantName name of the tenant.
|
||||||
* @return the count of the tenant.
|
* @return the count of the tenant.
|
||||||
*/
|
*/
|
||||||
public static int getCount(String tenantName) {
|
public static long getCount(String tenantName) {
|
||||||
return tenantCallsCount.get(tenantName);
|
return tenantCallsCount.get(tenantName).get();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Resets the count of all the tenants in the map.
|
* Resets the count of all the tenants in the map.
|
||||||
*/
|
*/
|
||||||
public static void reset() {
|
public static void reset() {
|
||||||
for (Entry<String, Integer> e : tenantCallsCount.entrySet()) {
|
LOGGER.debug("Resetting the map.");
|
||||||
tenantCallsCount.put(e.getKey(), 0);
|
for (Entry<String, AtomicLong> e : tenantCallsCount.entrySet()) {
|
||||||
|
tenantCallsCount.put(e.getKey(), new AtomicLong(0));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -36,14 +36,13 @@ public class B2BServiceTest {
|
|||||||
@Test
|
@Test
|
||||||
public void dummyCustomerApiTest() {
|
public void dummyCustomerApiTest() {
|
||||||
Tenant tenant = new Tenant("testTenant", 2);
|
Tenant tenant = new Tenant("testTenant", 2);
|
||||||
Throttler timer = new ThrottleTimerImpl(10);
|
Throttler timer = new ThrottleTimerImpl(100);
|
||||||
B2BService service = new B2BService(timer);
|
B2BService service = new B2BService(timer);
|
||||||
|
|
||||||
for (int i = 0; i < 5; i++) {
|
for (int i = 0; i < 5; i++) {
|
||||||
service.dummyCustomerApi(tenant);
|
service.dummyCustomerApi(tenant);
|
||||||
}
|
}
|
||||||
|
long counter = CallsCount.getCount(tenant.getName());
|
||||||
int counter = CallsCount.getCount(tenant.getName());
|
|
||||||
Assert.assertTrue("Counter limit must be reached", counter == 2);
|
Assert.assertTrue("Counter limit must be reached", counter == 2);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user