diff --git a/throttling/src/main/java/com/iluwatar/throttling/B2BService.java b/throttling/src/main/java/com/iluwatar/throttling/B2BService.java index c9acd4b73..51ed492eb 100644 --- a/throttling/src/main/java/com/iluwatar/throttling/B2BService.java +++ b/throttling/src/main/java/com/iluwatar/throttling/B2BService.java @@ -46,7 +46,7 @@ class B2BService { */ public int dummyCustomerApi(Tenant tenant) { String tenantName = tenant.getName(); - int count = CallsCount.getCount(tenantName); + long count = CallsCount.getCount(tenantName); LOGGER.debug("Counter for {} : {} ", tenant.getName(), count); if (count >= tenant.getAllowedCallsPerSecond()) { LOGGER.error("API access per second limit reached for: {}", tenantName); diff --git a/throttling/src/main/java/com/iluwatar/throttling/CallsCount.java b/throttling/src/main/java/com/iluwatar/throttling/CallsCount.java index 81195b074..25b3dedb3 100644 --- a/throttling/src/main/java/com/iluwatar/throttling/CallsCount.java +++ b/throttling/src/main/java/com/iluwatar/throttling/CallsCount.java @@ -22,9 +22,13 @@ */ package com.iluwatar.throttling; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import java.util.Map; import java.util.Map.Entry; import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.atomic.AtomicLong; /** * A class to keep track of the counter of different Tenants @@ -32,16 +36,16 @@ import java.util.concurrent.ConcurrentHashMap; * */ public final class CallsCount { - private static Map tenantCallsCount = new ConcurrentHashMap<>(); + + private static final Logger LOGGER = LoggerFactory.getLogger(CallsCount.class); + private static Map tenantCallsCount = new ConcurrentHashMap<>(); /** * Add a new tenant to the map. * @param tenantName name of the tenant. */ public static void addTenant(String tenantName) { - if (!tenantCallsCount.containsKey(tenantName)) { - tenantCallsCount.put(tenantName, 0); - } + tenantCallsCount.putIfAbsent(tenantName, new AtomicLong(0)); } /** @@ -49,7 +53,7 @@ public final class CallsCount { * @param tenantName name of the tenant. */ 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. * @return the count of the tenant. */ - public static int getCount(String tenantName) { - return tenantCallsCount.get(tenantName); + public static Long getCount(String tenantName) { + return tenantCallsCount.get(tenantName).get(); } /** * Resets the count of all the tenants in the map. */ public static void reset() { - for (Entry e : tenantCallsCount.entrySet()) { - tenantCallsCount.put(e.getKey(), 0); + LOGGER.debug("Resetting the map."); + for (Entry e : tenantCallsCount.entrySet()) { + tenantCallsCount.put(e.getKey(), new AtomicLong(0)); } } } diff --git a/throttling/src/test/java/com/iluwatar/throttling/B2BServiceTest.java b/throttling/src/test/java/com/iluwatar/throttling/B2BServiceTest.java index b9ca1a1d8..4ea540bcd 100644 --- a/throttling/src/test/java/com/iluwatar/throttling/B2BServiceTest.java +++ b/throttling/src/test/java/com/iluwatar/throttling/B2BServiceTest.java @@ -36,14 +36,15 @@ public class B2BServiceTest { @Test public void dummyCustomerApiTest() { Tenant tenant = new Tenant("testTenant", 2); - Throttler timer = new ThrottleTimerImpl(10); + Throttler timer = new ThrottleTimerImpl(100); B2BService service = new B2BService(timer); - + + long counter = 0; for (int i = 0; i < 5; i++) { service.dummyCustomerApi(tenant); + counter = CallsCount.getCount(tenant.getName()); } - int counter = CallsCount.getCount(tenant.getName()); Assert.assertTrue("Counter limit must be reached", counter == 2); } }