Compare commits

..

2 Commits

16 changed files with 134 additions and 92 deletions

View File

@ -22,7 +22,6 @@
*/
package com.iluwatar.ambassador;
import com.iluwatar.ambassador.util.RandomProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -32,10 +31,9 @@ import static java.lang.Thread.sleep;
* A remote legacy application represented by a Singleton implementation.
*/
public class RemoteService implements RemoteServiceInterface {
static final int THRESHOLD = 200;
private static final Logger LOGGER = LoggerFactory.getLogger(RemoteService.class);
private static RemoteService service = null;
private final RandomProvider randomProvider;
static synchronized RemoteService getRemoteService() {
if (service == null) {
@ -44,33 +42,24 @@ public class RemoteService implements RemoteServiceInterface {
return service;
}
private RemoteService() {
this(Math::random);
}
private RemoteService() {}
/**
* This constuctor is used for testing purposes only.
*/
RemoteService(RandomProvider randomProvider) {
this.randomProvider = randomProvider;
}
/**
* Remote function takes a value and multiplies it by 10 taking a random amount of time.
* Will sometimes return -1. This imitates connectivity issues a client might have to account for.
* @param value integer value to be multiplied.
* @return if waitTime is less than {@link RemoteService#THRESHOLD}, it returns value * 10,
* otherwise {@link RemoteServiceInterface#FAILURE}.
* @return if waitTime is more than 200ms, it returns value * 10, otherwise -1.
*/
@Override
public long doRemoteFunction(int value) {
long waitTime = (long) Math.floor(randomProvider.random() * 1000);
long waitTime = (long) Math.floor(Math.random() * 1000);
try {
sleep(waitTime);
} catch (InterruptedException e) {
LOGGER.error("Thread sleep state interrupted", e);
}
return waitTime <= THRESHOLD ? value * 10 : FAILURE;
return waitTime >= 200 ? value * 10 : -1;
}
}

View File

@ -26,7 +26,6 @@ package com.iluwatar.ambassador;
* Interface shared by ({@link RemoteService}) and ({@link ServiceAmbassador}).
*/
interface RemoteServiceInterface {
int FAILURE = -1;
long doRemoteFunction(int value) throws Exception;
}

View File

@ -59,15 +59,15 @@ public class ServiceAmbassador implements RemoteServiceInterface {
private long safeCall(int value) {
int retries = 0;
long result = FAILURE;
long result = -1;
for (int i = 0; i < RETRIES; i++) {
if (retries >= RETRIES) {
return FAILURE;
return -1;
}
if ((result = checkLatency(value)) == FAILURE) {
if ((result = checkLatency(value)) == -1) {
LOGGER.info("Failed to reach remote: (" + (i + 1) + ")");
retries++;
try {

View File

@ -1,8 +0,0 @@
package com.iluwatar.ambassador.util;
/**
* An interface for randomness. Useful for testing purposes.
*/
public interface RandomProvider {
double random();
}

View File

@ -24,8 +24,6 @@ package com.iluwatar.ambassador;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* Test for {@link Client}
*/
@ -37,6 +35,6 @@ public class ClientTest {
Client client = new Client();
long result = client.useService(10);
assertTrue(result == 100 || result == RemoteService.FAILURE);
assert result == 100 || result == -1;
}
}

View File

@ -22,43 +22,16 @@
*/
package com.iluwatar.ambassador;
import com.iluwatar.ambassador.util.RandomProvider;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* Test for {@link RemoteService}
*/
public class RemoteServiceTest {
@Test
public void testFailedCall() {
RemoteService remoteService = new RemoteService(
new StaticRandomProvider(0.21));
long result = remoteService.doRemoteFunction(10);
assertEquals(RemoteServiceInterface.FAILURE, result);
}
@Test
public void testSuccessfulCall() {
RemoteService remoteService = new RemoteService(
new StaticRandomProvider(0.2));
long result = remoteService.doRemoteFunction(10);
assertEquals(100, result);
}
private class StaticRandomProvider implements RandomProvider {
private double value;
StaticRandomProvider(double value) {
this.value = value;
}
@Override
public double random() {
return value;
}
public void test() {
long result = RemoteService.getRemoteService().doRemoteFunction(10);
assert result == 100 || result == -1;
}
}

View File

@ -24,8 +24,6 @@ package com.iluwatar.ambassador;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* Test for {@link ServiceAmbassador}
*/
@ -34,6 +32,6 @@ public class ServiceAmbassadorTest {
@Test
public void test() {
long result = new ServiceAmbassador().doRemoteFunction(10);
assertTrue(result == 100 || result == RemoteServiceInterface.FAILURE);
assert result == 100 || result == -1;
}
}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 59 KiB

After

Width:  |  Height:  |  Size: 49 KiB

View File

@ -0,0 +1,88 @@
<?xml version="1.0" encoding="UTF-8"?>
<class-diagram version="1.2.2" icons="true" always-add-relationships="false" generalizations="true" realizations="true"
associations="true" dependencies="false" nesting-relationships="true" router="FAN">
<class id="1" language="java" name="com.iluwatar.throttling.CallsCount" project="throttling"
file="/throttling/src/main/java/com/iluwatar/throttling/CallsCount.java" binary="false" corner="BOTTOM_RIGHT">
<position height="211" width="256" x="656" y="228"/>
<display autosize="false" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true">
<attributes public="true" package="true" protected="true" private="true" static="true"/>
<operations public="true" package="true" protected="true" private="true" static="true"/>
</display>
</class>
<class id="2" language="java" name="com.iluwatar.throttling.Tenant" project="throttling"
file="/throttling/src/main/java/com/iluwatar/throttling/Tenant.java" binary="false" corner="BOTTOM_RIGHT">
<position height="-1" width="-1" x="465" y="524"/>
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true">
<attributes public="true" package="true" protected="true" private="true" static="true"/>
<operations public="true" package="true" protected="true" private="true" static="true"/>
</display>
</class>
<class id="3" language="java" name="com.iluwatar.throttling.B2BService" project="throttling"
file="/throttling/src/main/java/com/iluwatar/throttling/B2BService.java" binary="false" corner="BOTTOM_RIGHT">
<position height="-1" width="-1" x="464" y="192"/>
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true">
<attributes public="true" package="true" protected="true" private="true" static="true"/>
<operations public="true" package="true" protected="true" private="true" static="true"/>
</display>
</class>
<interface id="4" language="java" name="com.iluwatar.throttling.timer.Throttler" project="throttling"
file="/throttling/src/main/java/com/iluwatar/throttling/timer/Throttler.java" binary="false" corner="BOTTOM_RIGHT">
<position height="-1" width="-1" x="167" y="174"/>
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true">
<attributes public="true" package="true" protected="true" private="true" static="true"/>
<operations public="true" package="true" protected="true" private="true" static="true"/>
</display>
</interface>
<class id="5" language="java" name="com.iluwatar.throttling.timer.ThrottleTimerImpl" project="throttling"
file="/throttling/src/main/java/com/iluwatar/throttling/timer/ThrottleTimerImpl.java" binary="false"
corner="BOTTOM_RIGHT">
<position height="-1" width="-1" x="166" y="396"/>
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true">
<attributes public="true" package="true" protected="true" private="true" static="true"/>
<operations public="true" package="true" protected="true" private="true" static="true"/>
</display>
</class>
<association id="6">
<end type="SOURCE" refId="3" navigable="false">
<attribute id="7" name="callsCount"/>
<multiplicity id="8" minimum="0" maximum="1"/>
</end>
<end type="TARGET" refId="1" navigable="true"/>
<display labels="true" multiplicity="true"/>
</association>
<dependency id="9">
<end type="SOURCE" refId="3"/>
<end type="TARGET" refId="4"/>
</dependency>
<dependency id="10">
<end type="SOURCE" refId="3"/>
<end type="TARGET" refId="2"/>
</dependency>
<association id="11">
<end type="SOURCE" refId="5" navigable="false">
<attribute id="12" name="callsCount"/>
<multiplicity id="13" minimum="0" maximum="1"/>
</end>
<end type="TARGET" refId="1" navigable="true"/>
<display labels="true" multiplicity="true"/>
</association>
<dependency id="14">
<end type="SOURCE" refId="2"/>
<end type="TARGET" refId="1"/>
</dependency>
<realization id="15">
<end type="SOURCE" refId="5"/>
<end type="TARGET" refId="4"/>
</realization>
<classifier-display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true">
<attributes public="true" package="true" protected="true" private="true" static="true"/>
<operations public="true" package="true" protected="true" private="true" static="true"/>
</classifier-display>
<association-display labels="true" multiplicity="true"/>
</class-diagram>

View File

@ -53,14 +53,14 @@ public class App {
* @param args main arguments
*/
public static void main(String[] args) {
Tenant adidas = new Tenant("Adidas", 5);
Tenant nike = new Tenant("Nike", 6);
CallsCount callsCount = new CallsCount();
Tenant adidas = new Tenant("Adidas", 5, callsCount);
Tenant nike = new Tenant("Nike", 6, callsCount);
ExecutorService executorService = Executors.newFixedThreadPool(2);
executorService.execute(() -> makeServiceCalls(adidas));
executorService.execute(() -> makeServiceCalls(nike));
executorService.execute(() -> makeServiceCalls(adidas, callsCount));
executorService.execute(() -> makeServiceCalls(nike, callsCount));
executorService.shutdown();
try {
@ -73,9 +73,9 @@ public class App {
/**
* Make calls to the B2BService dummy API
*/
private static void makeServiceCalls(Tenant tenant) {
Throttler timer = new ThrottleTimerImpl(10);
B2BService service = new B2BService(timer);
private static void makeServiceCalls(Tenant tenant, CallsCount callsCount) {
Throttler timer = new ThrottleTimerImpl(10, callsCount);
B2BService service = new B2BService(timer, callsCount);
for (int i = 0; i < 20; i++) {
service.dummyCustomerApi(tenant);
// Sleep is introduced to keep the output in check and easy to view and analyze the results.

View File

@ -35,8 +35,10 @@ import java.util.concurrent.ThreadLocalRandom;
class B2BService {
private static final Logger LOGGER = LoggerFactory.getLogger(B2BService.class);
private final CallsCount callsCount;
public B2BService(Throttler timer) {
public B2BService(Throttler timer, CallsCount callsCount) {
this.callsCount = callsCount;
timer.start();
}
@ -46,13 +48,13 @@ class B2BService {
*/
public int dummyCustomerApi(Tenant tenant) {
String tenantName = tenant.getName();
long 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);
return -1;
}
CallsCount.incrementCount(tenantName);
callsCount.incrementCount(tenantName);
return getRandomCustomerId();
}

View File

@ -38,13 +38,13 @@ import java.util.concurrent.atomic.AtomicLong;
public final class CallsCount {
private static final Logger LOGGER = LoggerFactory.getLogger(CallsCount.class);
private static Map<String, AtomicLong> tenantCallsCount = new ConcurrentHashMap<>();
private 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) {
public void addTenant(String tenantName) {
tenantCallsCount.putIfAbsent(tenantName, new AtomicLong(0));
}
@ -52,7 +52,7 @@ public final class CallsCount {
* Increment the count of the specified tenant.
* @param tenantName name of the tenant.
*/
public static void incrementCount(String tenantName) {
public void incrementCount(String tenantName) {
tenantCallsCount.get(tenantName).incrementAndGet();
}
@ -61,14 +61,14 @@ public final class CallsCount {
* @param tenantName name of the tenant.
* @return the count of the tenant.
*/
public static long getCount(String tenantName) {
public long getCount(String tenantName) {
return tenantCallsCount.get(tenantName).get();
}
/**
* Resets the count of all the tenants in the map.
*/
public static void reset() {
public void reset() {
LOGGER.debug("Resetting the map.");
for (Entry<String, AtomicLong> e : tenantCallsCount.entrySet()) {
tenantCallsCount.put(e.getKey(), new AtomicLong(0));

View File

@ -38,13 +38,13 @@ public class Tenant {
* @param allowedCallsPerSecond The number of calls allowed for a particular tenant.
* @throws InvalidParameterException If number of calls is less than 0, throws exception.
*/
public Tenant(String name, int allowedCallsPerSecond) {
public Tenant(String name, int allowedCallsPerSecond, CallsCount callsCount) {
if (allowedCallsPerSecond < 0) {
throw new InvalidParameterException("Number of calls less than 0 not allowed");
}
this.name = name;
this.allowedCallsPerSecond = allowedCallsPerSecond;
CallsCount.addTenant(name);
callsCount.addTenant(name);
}
public String getName() {

View File

@ -37,10 +37,12 @@ import com.iluwatar.throttling.CallsCount;
*/
public class ThrottleTimerImpl implements Throttler {
private int throttlePeriod;
public ThrottleTimerImpl(int throttlePeriod) {
private final int throttlePeriod;
private final CallsCount callsCount;
public ThrottleTimerImpl(int throttlePeriod, CallsCount callsCount) {
this.throttlePeriod = throttlePeriod;
this.callsCount = callsCount;
}
/**
@ -51,7 +53,7 @@ public class ThrottleTimerImpl implements Throttler {
new Timer(true).schedule(new TimerTask() {
@Override
public void run() {
CallsCount.reset();
callsCount.reset();
}
}, 0, throttlePeriod);
}

View File

@ -33,18 +33,19 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
*/
public class B2BServiceTest {
@Disabled
private CallsCount callsCount = new CallsCount();
@Test
public void dummyCustomerApiTest() {
Tenant tenant = new Tenant("testTenant", 2);
Tenant tenant = new Tenant("testTenant", 2, callsCount);
// In order to assure that throttling limits will not be reset, we use an empty throttling implementation
Throttler timer = () -> { };
B2BService service = new B2BService(timer);
B2BService service = new B2BService(timer, callsCount);
for (int i = 0; i < 5; i++) {
service.dummyCustomerApi(tenant);
}
long counter = CallsCount.getCount(tenant.getName());
long counter = callsCount.getCount(tenant.getName());
assertEquals(2, counter, "Counter limit must be reached");
}
}

View File

@ -36,7 +36,7 @@ public class TenantTest {
@Test
public void constructorTest() {
assertThrows(InvalidParameterException.class, () -> {
Tenant tenant = new Tenant("FailTenant", -1);
Tenant tenant = new Tenant("FailTenant", -1, new CallsCount());
});
}
}