Skip to content

Commit 31120a8

Browse files
authored
Merge pull request iluwatar#646 from rastdeepanshu/master
Modifications for intermittent test failure in Throttling pattern.
2 parents 0d4a8db + 4e236f6 commit 31120a8

File tree

3 files changed

+18
-14
lines changed

3 files changed

+18
-14
lines changed

throttling/src/main/java/com/iluwatar/throttling/B2BService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public B2BService(Throttler timer) {
4646
*/
4747
public int dummyCustomerApi(Tenant tenant) {
4848
String tenantName = tenant.getName();
49-
int count = CallsCount.getCount(tenantName);
49+
long count = CallsCount.getCount(tenantName);
5050
LOGGER.debug("Counter for {} : {} ", tenant.getName(), count);
5151
if (count >= tenant.getAllowedCallsPerSecond()) {
5252
LOGGER.error("API access per second limit reached for: {}", tenantName);

throttling/src/main/java/com/iluwatar/throttling/CallsCount.java

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,51 +22,56 @@
2222
*/
2323
package com.iluwatar.throttling;
2424

25+
import org.slf4j.Logger;
26+
import org.slf4j.LoggerFactory;
27+
2528
import java.util.Map;
2629
import java.util.Map.Entry;
2730
import java.util.concurrent.ConcurrentHashMap;
31+
import java.util.concurrent.atomic.AtomicLong;
2832

2933
/**
3034
* A class to keep track of the counter of different Tenants
3135
* @author drastogi
3236
*
3337
*/
3438
public final class CallsCount {
35-
private static Map<String, Integer> tenantCallsCount = new ConcurrentHashMap<>();
39+
40+
private static final Logger LOGGER = LoggerFactory.getLogger(CallsCount.class);
41+
private static Map<String, AtomicLong> tenantCallsCount = new ConcurrentHashMap<>();
3642

3743
/**
3844
* Add a new tenant to the map.
3945
* @param tenantName name of the tenant.
4046
*/
4147
public static void addTenant(String tenantName) {
42-
if (!tenantCallsCount.containsKey(tenantName)) {
43-
tenantCallsCount.put(tenantName, 0);
44-
}
48+
tenantCallsCount.putIfAbsent(tenantName, new AtomicLong(0));
4549
}
4650

4751
/**
4852
* Increment the count of the specified tenant.
4953
* @param tenantName name of the tenant.
5054
*/
5155
public static void incrementCount(String tenantName) {
52-
tenantCallsCount.put(tenantName, tenantCallsCount.get(tenantName) + 1);
56+
tenantCallsCount.get(tenantName).incrementAndGet();
5357
}
5458

5559
/**
5660
*
5761
* @param tenantName name of the tenant.
5862
* @return the count of the tenant.
5963
*/
60-
public static int getCount(String tenantName) {
61-
return tenantCallsCount.get(tenantName);
64+
public static long getCount(String tenantName) {
65+
return tenantCallsCount.get(tenantName).get();
6266
}
6367

6468
/**
6569
* Resets the count of all the tenants in the map.
6670
*/
6771
public static void reset() {
68-
for (Entry<String, Integer> e : tenantCallsCount.entrySet()) {
69-
tenantCallsCount.put(e.getKey(), 0);
72+
LOGGER.debug("Resetting the map.");
73+
for (Entry<String, AtomicLong> e : tenantCallsCount.entrySet()) {
74+
tenantCallsCount.put(e.getKey(), new AtomicLong(0));
7075
}
7176
}
7277
}

throttling/src/test/java/com/iluwatar/throttling/B2BServiceTest.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,13 @@ public class B2BServiceTest {
3636
@Test
3737
public void dummyCustomerApiTest() {
3838
Tenant tenant = new Tenant("testTenant", 2);
39-
Throttler timer = new ThrottleTimerImpl(10);
39+
Throttler timer = new ThrottleTimerImpl(100);
4040
B2BService service = new B2BService(timer);
41-
41+
4242
for (int i = 0; i < 5; i++) {
4343
service.dummyCustomerApi(tenant);
4444
}
45-
46-
int counter = CallsCount.getCount(tenant.getName());
45+
long counter = CallsCount.getCount(tenant.getName());
4746
Assert.assertTrue("Counter limit must be reached", counter == 2);
4847
}
4948
}

0 commit comments

Comments
 (0)