Compare commits

..

2 Commits

13 changed files with 163 additions and 91 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 23 KiB

After

Width:  |  Height:  |  Size: 17 KiB

View File

@ -47,15 +47,13 @@ public class App {
* @throws IOException when there is a problem with the audio file loading * @throws IOException when there is a problem with the audio file loading
* @throws UnsupportedAudioFileException when the loaded audio file is unsupported * @throws UnsupportedAudioFileException when the loaded audio file is unsupported
*/ */
public static void main(String[] args) throws UnsupportedAudioFileException, IOException, InterruptedException { public static void main(String[] args) throws UnsupportedAudioFileException, IOException {
Audio audio = Audio.getInstance(); Audio.playSound(Audio.getAudioStream("./etc/Bass-Drum-1.wav"), -10.0f);
audio.playSound(audio.getAudioStream("./etc/Bass-Drum-1.wav"), -10.0f); Audio.playSound(Audio.getAudioStream("./etc/Closed-Hi-Hat-1.wav"), -8.0f);
audio.playSound(audio.getAudioStream("./etc/Closed-Hi-Hat-1.wav"), -8.0f);
System.out.println("Press Enter key to stop the program..."); System.out.println("Press Enter key to stop the program...");
try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) { BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
br.read(); br.read();
} Audio.stopService();
audio.stopService();
} }
} }

View File

@ -23,9 +23,6 @@
package com.iluwatar.event.queue; package com.iluwatar.event.queue;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
@ -41,56 +38,49 @@ import javax.sound.sampled.UnsupportedAudioFileException;
* *
*/ */
public class Audio { public class Audio {
private static final Logger LOGGER = LoggerFactory.getLogger(Audio.class);
private static final Audio INSTANCE = new Audio();
private static final int MAX_PENDING = 16; private static final int MAX_PENDING = 16;
private int headIndex; private static int headIndex;
private int tailIndex; private static int tailIndex;
private volatile Thread updateThread = null; private static Thread updateThread = null;
private PlayMessage[] pendingAudio = new PlayMessage[MAX_PENDING]; private static PlayMessage[] pendingAudio = new PlayMessage[MAX_PENDING];
// Visible only for testing purposes
Audio() {
}
public static Audio getInstance() {
return INSTANCE;
}
/** /**
* This method stops the Update Method's thread and waits till service stops. * This method stops the Update Method's thread.
*/ */
public synchronized void stopService() throws InterruptedException { public static synchronized void stopService() {
if (updateThread != null) { if (updateThread != null) {
updateThread.interrupt(); updateThread.interrupt();
} }
updateThread.join();
updateThread = null;
} }
/** /**
* This method check the Update Method's thread is started. * This method check the Update Method's thread is started.
* @return boolean * @return boolean
*/ */
public synchronized boolean isServiceRunning() { public static synchronized boolean isServiceRunning() {
return updateThread != null && updateThread.isAlive(); if (updateThread != null && updateThread.isAlive() ) {
return true;
} else {
return false;
}
} }
/** /**
* Starts the thread for the Update Method pattern if it was not started previously. * Starts the thread for the Update Method pattern if it was not started previously.
* Also when the thread is is ready initializes the indexes of the queue * Also when the thread is is ready initializes the indexes of the queue
*/ */
public void init() { public static void init() {
if (updateThread == null) { if (updateThread == null) {
updateThread = new Thread(() -> { updateThread = new Thread(new Runnable() {
while (!Thread.currentThread().isInterrupted()) { public void run() {
update(); while (!Thread.currentThread().isInterrupted()) {
Audio.update();
}
} }
}); });
} }
@ -100,7 +90,7 @@ public class Audio {
/** /**
* This is a synchronized thread starter * This is a synchronized thread starter
*/ */
private synchronized void startThread() { public static synchronized void startThread() {
if (!updateThread.isAlive()) { if (!updateThread.isAlive()) {
updateThread.start(); updateThread.start();
headIndex = 0; headIndex = 0;
@ -113,7 +103,7 @@ public class Audio {
* @param stream is the AudioInputStream for the method * @param stream is the AudioInputStream for the method
* @param volume is the level of the audio's volume * @param volume is the level of the audio's volume
*/ */
public void playSound(AudioInputStream stream, float volume) { public static void playSound(AudioInputStream stream, float volume) {
init(); init();
// Walk the pending requests. // Walk the pending requests.
for (int i = headIndex; i != tailIndex; i = (i + 1) % MAX_PENDING) { for (int i = headIndex; i != tailIndex; i = (i + 1) % MAX_PENDING) {
@ -133,7 +123,7 @@ public class Audio {
* This method uses the Update Method pattern. * This method uses the Update Method pattern.
* It takes the audio from the queue and plays it * It takes the audio from the queue and plays it
*/ */
private void update() { public static void update() {
// If there are no pending requests, do nothing. // If there are no pending requests, do nothing.
if (headIndex == tailIndex) { if (headIndex == tailIndex) {
return; return;
@ -146,11 +136,13 @@ public class Audio {
clip.open(audioStream); clip.open(audioStream);
clip.start(); clip.start();
} catch (LineUnavailableException e) { } catch (LineUnavailableException e) {
LOGGER.trace("Error occoured while loading the audio: The line is unavailable", e); System.err.println("Error occoured while loading the audio: The line is unavailable");
e.printStackTrace();
} catch (IOException e) { } catch (IOException e) {
LOGGER.trace("Input/Output error while loading the audio", e); System.err.println("Input/Output error while loading the audio");
e.printStackTrace();
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
LOGGER.trace("The system doesn't support the sound: " + e.getMessage(), e); System.err.println("The system doesn't support the sound: " + e.getMessage());
} }
} }
@ -161,7 +153,7 @@ public class Audio {
* @throws UnsupportedAudioFileException when the audio file is not supported * @throws UnsupportedAudioFileException when the audio file is not supported
* @throws IOException when the file is not readable * @throws IOException when the file is not readable
*/ */
public AudioInputStream getAudioStream(String filePath) public static AudioInputStream getAudioStream(String filePath)
throws UnsupportedAudioFileException, IOException { throws UnsupportedAudioFileException, IOException {
return AudioSystem.getAudioInputStream(new File(filePath).getAbsoluteFile()); return AudioSystem.getAudioInputStream(new File(filePath).getAbsoluteFile());
} }
@ -170,7 +162,7 @@ public class Audio {
* Returns with the message array of the queue * Returns with the message array of the queue
* @return PlayMessage[] * @return PlayMessage[]
*/ */
public PlayMessage[] getPendingAudio() { public static PlayMessage[] getPendingAudio() {
return pendingAudio; return pendingAudio;
} }

View File

@ -23,7 +23,6 @@
package com.iluwatar.event.queue; package com.iluwatar.event.queue;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import javax.sound.sampled.UnsupportedAudioFileException; import javax.sound.sampled.UnsupportedAudioFileException;
@ -40,12 +39,6 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
*/ */
public class AudioTest { public class AudioTest {
private Audio audio;
@BeforeEach
void createAudioInstance() {
audio = new Audio();
}
/** /**
* Test here that the playSound method works correctly * Test here that the playSound method works correctly
* @throws UnsupportedAudioFileException when the audio file is not supported * @throws UnsupportedAudioFileException when the audio file is not supported
@ -54,15 +47,13 @@ public class AudioTest {
*/ */
@Test @Test
public void testPlaySound() throws UnsupportedAudioFileException, IOException, InterruptedException { public void testPlaySound() throws UnsupportedAudioFileException, IOException, InterruptedException {
audio.playSound(audio.getAudioStream("./etc/Bass-Drum-1.wav"), -10.0f); Audio.playSound(Audio.getAudioStream("./etc/Bass-Drum-1.wav"), -10.0f);
// test that service is started // test that service is started
assertTrue(audio.isServiceRunning()); assertTrue(Audio.isServiceRunning());
// adding a small pause to be sure that the sound is ended // adding a small pause to be sure that the sound is ended
Thread.sleep(5000); Thread.sleep(5000);
audio.stopService();
// test that service is finished // test that service is finished
assertFalse(audio.isServiceRunning()); assertFalse(!Audio.isServiceRunning());
} }
/** /**
@ -73,18 +64,16 @@ public class AudioTest {
*/ */
@Test @Test
public void testQueue() throws UnsupportedAudioFileException, IOException, InterruptedException { public void testQueue() throws UnsupportedAudioFileException, IOException, InterruptedException {
audio.playSound(audio.getAudioStream("./etc/Bass-Drum-1.aif"), -10.0f); Audio.playSound(Audio.getAudioStream("./etc/Bass-Drum-1.aif"), -10.0f);
audio.playSound(audio.getAudioStream("./etc/Bass-Drum-1.aif"), -10.0f); Audio.playSound(Audio.getAudioStream("./etc/Bass-Drum-1.aif"), -10.0f);
audio.playSound(audio.getAudioStream("./etc/Bass-Drum-1.aif"), -10.0f); Audio.playSound(Audio.getAudioStream("./etc/Bass-Drum-1.aif"), -10.0f);
assertTrue(audio.getPendingAudio().length > 0); assertTrue(Audio.getPendingAudio().length > 0);
// test that service is started // test that service is started
assertTrue(audio.isServiceRunning()); assertTrue(Audio.isServiceRunning());
// adding a small pause to be sure that the sound is ended // adding a small pause to be sure that the sound is ended
Thread.sleep(10000); Thread.sleep(10000);
audio.stopService();
// test that service is finished // test that service is finished
assertFalse(audio.isServiceRunning()); assertFalse(!Audio.isServiceRunning());
} }
} }

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 * @param args main arguments
*/ */
public static void main(String[] args) { public static void main(String[] args) {
CallsCount callsCount = new CallsCount();
Tenant adidas = new Tenant("Adidas", 5); Tenant adidas = new Tenant("Adidas", 5, callsCount);
Tenant nike = new Tenant("Nike", 6); Tenant nike = new Tenant("Nike", 6, callsCount);
ExecutorService executorService = Executors.newFixedThreadPool(2); ExecutorService executorService = Executors.newFixedThreadPool(2);
executorService.execute(() -> makeServiceCalls(adidas)); executorService.execute(() -> makeServiceCalls(adidas, callsCount));
executorService.execute(() -> makeServiceCalls(nike)); executorService.execute(() -> makeServiceCalls(nike, callsCount));
executorService.shutdown(); executorService.shutdown();
try { try {
@ -73,9 +73,9 @@ public class App {
/** /**
* Make calls to the B2BService dummy API * Make calls to the B2BService dummy API
*/ */
private static void makeServiceCalls(Tenant tenant) { private static void makeServiceCalls(Tenant tenant, CallsCount callsCount) {
Throttler timer = new ThrottleTimerImpl(10); Throttler timer = new ThrottleTimerImpl(10, callsCount);
B2BService service = new B2BService(timer); B2BService service = new B2BService(timer, callsCount);
for (int i = 0; i < 20; i++) { for (int i = 0; i < 20; i++) {
service.dummyCustomerApi(tenant); service.dummyCustomerApi(tenant);
// Sleep is introduced to keep the output in check and easy to view and analyze the results. // 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 { class B2BService {
private static final Logger LOGGER = LoggerFactory.getLogger(B2BService.class); 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(); timer.start();
} }
@ -46,13 +48,13 @@ class B2BService {
*/ */
public int dummyCustomerApi(Tenant tenant) { public int dummyCustomerApi(Tenant tenant) {
String tenantName = tenant.getName(); String tenantName = tenant.getName();
long 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);
return -1; return -1;
} }
CallsCount.incrementCount(tenantName); callsCount.incrementCount(tenantName);
return getRandomCustomerId(); return getRandomCustomerId();
} }

View File

@ -38,13 +38,13 @@ import java.util.concurrent.atomic.AtomicLong;
public final class CallsCount { public final class CallsCount {
private static final Logger LOGGER = LoggerFactory.getLogger(CallsCount.class); 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. * 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 void addTenant(String tenantName) {
tenantCallsCount.putIfAbsent(tenantName, new AtomicLong(0)); tenantCallsCount.putIfAbsent(tenantName, new AtomicLong(0));
} }
@ -52,7 +52,7 @@ public final class CallsCount {
* Increment the count of the specified tenant. * Increment the count of the specified tenant.
* @param tenantName name of the tenant. * @param tenantName name of the tenant.
*/ */
public static void incrementCount(String tenantName) { public void incrementCount(String tenantName) {
tenantCallsCount.get(tenantName).incrementAndGet(); tenantCallsCount.get(tenantName).incrementAndGet();
} }
@ -61,14 +61,14 @@ 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 long getCount(String tenantName) { public long getCount(String tenantName) {
return tenantCallsCount.get(tenantName).get(); 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 void reset() {
LOGGER.debug("Resetting the map."); LOGGER.debug("Resetting the map.");
for (Entry<String, AtomicLong> e : tenantCallsCount.entrySet()) { for (Entry<String, AtomicLong> e : tenantCallsCount.entrySet()) {
tenantCallsCount.put(e.getKey(), new AtomicLong(0)); 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. * @param allowedCallsPerSecond The number of calls allowed for a particular tenant.
* @throws InvalidParameterException If number of calls is less than 0, throws exception. * @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) { if (allowedCallsPerSecond < 0) {
throw new InvalidParameterException("Number of calls less than 0 not allowed"); throw new InvalidParameterException("Number of calls less than 0 not allowed");
} }
this.name = name; this.name = name;
this.allowedCallsPerSecond = allowedCallsPerSecond; this.allowedCallsPerSecond = allowedCallsPerSecond;
CallsCount.addTenant(name); callsCount.addTenant(name);
} }
public String getName() { public String getName() {

View File

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

View File

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

View File

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