From 01b85b972160d6131207381708e39afa080aa70a Mon Sep 17 00:00:00 2001 From: Deepanshu Rastogi <deepanshu.rastogi@zalando-lounge.de> Date: Mon, 25 Sep 2017 12:59:39 +0200 Subject: [PATCH 1/5] Used AtomicLong for concurrenthashmap operations --- .../com/iluwatar/throttling/B2BService.java | 2 +- .../com/iluwatar/throttling/CallsCount.java | 23 +++++++++++-------- .../iluwatar/throttling/B2BServiceTest.java | 7 +++--- 3 files changed, 19 insertions(+), 13 deletions(-) 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<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. * @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<String, Integer> e : tenantCallsCount.entrySet()) { - tenantCallsCount.put(e.getKey(), 0); + LOGGER.debug("Resetting the map."); + for (Entry<String, AtomicLong> 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); } } From 4abef6da331a67bc9b6878bb5c55601de5995d07 Mon Sep 17 00:00:00 2001 From: Peter-Morawski <thefinesia@gmail.com> Date: Tue, 26 Sep 2017 20:51:32 +0200 Subject: [PATCH 2/5] added missing whitespace to the elements of the unordered list --- balking/README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/balking/README.md b/balking/README.md index f720c0d02..fb5c83d5b 100644 --- a/balking/README.md +++ b/balking/README.md @@ -18,10 +18,10 @@ incomplete or inappropriate state ## Applicability Use the Balking pattern when -*you want to invoke an action on an object only when it is in a particular state -*objects are generally only in a state that is prone to balking temporarily +* you want to invoke an action on an object only when it is in a particular state +* objects are generally only in a state that is prone to balking temporarily but for an unknown amount of time ## Related patterns * Guarded Suspension Pattern -* Double Checked Locking Pattern \ No newline at end of file +* Double Checked Locking Pattern From 4e236f6da8118844e8c2cd23687d974f79753396 Mon Sep 17 00:00:00 2001 From: Deepanshu Rastogi <deepanshu.rastogi@zalando-lounge.de> Date: Thu, 28 Sep 2017 15:04:32 +0200 Subject: [PATCH 3/5] Minor modification of Long to long --- .../src/main/java/com/iluwatar/throttling/CallsCount.java | 2 +- .../src/test/java/com/iluwatar/throttling/B2BServiceTest.java | 4 +--- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/throttling/src/main/java/com/iluwatar/throttling/CallsCount.java b/throttling/src/main/java/com/iluwatar/throttling/CallsCount.java index 25b3dedb3..9b274849a 100644 --- a/throttling/src/main/java/com/iluwatar/throttling/CallsCount.java +++ b/throttling/src/main/java/com/iluwatar/throttling/CallsCount.java @@ -61,7 +61,7 @@ public final class CallsCount { * @param tenantName name of the tenant. * @return the count of the tenant. */ - public static Long getCount(String tenantName) { + public static long getCount(String tenantName) { return tenantCallsCount.get(tenantName).get(); } diff --git a/throttling/src/test/java/com/iluwatar/throttling/B2BServiceTest.java b/throttling/src/test/java/com/iluwatar/throttling/B2BServiceTest.java index 4ea540bcd..aaab62b3a 100644 --- a/throttling/src/test/java/com/iluwatar/throttling/B2BServiceTest.java +++ b/throttling/src/test/java/com/iluwatar/throttling/B2BServiceTest.java @@ -39,12 +39,10 @@ public class B2BServiceTest { 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()); } - + long counter = CallsCount.getCount(tenant.getName()); Assert.assertTrue("Counter limit must be reached", counter == 2); } } From 56ba78a5e905bd6d049d3d17554e9f6e24d198a4 Mon Sep 17 00:00:00 2001 From: Yosfik Alqadri <yosfik@gmail.com> Date: Sat, 7 Oct 2017 20:33:24 +0700 Subject: [PATCH 4/5] fix test message --- .../java/com/iluwatar/factory/method/FactoryMethodTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/factory-method/src/test/java/com/iluwatar/factory/method/FactoryMethodTest.java b/factory-method/src/test/java/com/iluwatar/factory/method/FactoryMethodTest.java index 4f3a5930d..69736855c 100644 --- a/factory-method/src/test/java/com/iluwatar/factory/method/FactoryMethodTest.java +++ b/factory-method/src/test/java/com/iluwatar/factory/method/FactoryMethodTest.java @@ -95,7 +95,7 @@ public class FactoryMethodTest { */ private void verifyWeapon(Weapon weapon, WeaponType expectedWeaponType, Class<?> clazz) { assertTrue("Weapon must be an object of: " + clazz.getName(), clazz.isInstance(weapon)); - assertEquals("Weapon must be of weaponType: " + clazz.getName(), expectedWeaponType, + assertEquals("Weapon must be of weaponType: " + expectedWeaponType, expectedWeaponType, weapon.getWeaponType()); } } From ed1852a789daeef03ab260ef4f962bca9160d13e Mon Sep 17 00:00:00 2001 From: Yosfik Alqadri <yosfik@gmail.com> Date: Sun, 8 Oct 2017 20:41:28 +0700 Subject: [PATCH 5/5] Fix unused generic type in PrototypeTest --- .../src/test/java/com/iluwatar/prototype/PrototypeTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/prototype/src/test/java/com/iluwatar/prototype/PrototypeTest.java b/prototype/src/test/java/com/iluwatar/prototype/PrototypeTest.java index add5617b1..839f27bc7 100644 --- a/prototype/src/test/java/com/iluwatar/prototype/PrototypeTest.java +++ b/prototype/src/test/java/com/iluwatar/prototype/PrototypeTest.java @@ -56,7 +56,7 @@ public class PrototypeTest<P extends Prototype> { /** * The tested prototype instance */ - private final Prototype testedPrototype; + private final P testedPrototype; /** * The expected {@link Prototype#toString()} value @@ -69,7 +69,7 @@ public class PrototypeTest<P extends Prototype> { * @param testedPrototype The tested prototype instance * @param expectedToString The expected {@link Prototype#toString()} value */ - public PrototypeTest(final Prototype testedPrototype, final String expectedToString) { + public PrototypeTest(final P testedPrototype, final String expectedToString) { this.expectedToString = expectedToString; this.testedPrototype = testedPrototype; }