Compare commits

..

3 Commits

11 changed files with 78 additions and 108 deletions

View File

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

View File

@ -59,15 +59,15 @@ public class ServiceAmbassador implements RemoteServiceInterface {
private long safeCall(int value) { private long safeCall(int value) {
int retries = 0; int retries = 0;
long result = FAILURE; long result = -1;
for (int i = 0; i < RETRIES; i++) { for (int i = 0; i < RETRIES; i++) {
if (retries >= RETRIES) { 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) + ")"); LOGGER.info("Failed to reach remote: (" + (i + 1) + ")");
retries++; retries++;
try { 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 org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertTrue;
/** /**
* Test for {@link Client} * Test for {@link Client}
*/ */
@ -37,6 +35,6 @@ public class ClientTest {
Client client = new Client(); Client client = new Client();
long result = client.useService(10); 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; package com.iluwatar.ambassador;
import com.iluwatar.ambassador.util.RandomProvider;
import org.junit.jupiter.api.Test; 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} * Test for {@link RemoteService}
*/ */
public class RemoteServiceTest { public class RemoteServiceTest {
@Test @Test
public void testFailedCall() { public void test() {
RemoteService remoteService = new RemoteService( long result = RemoteService.getRemoteService().doRemoteFunction(10);
new StaticRandomProvider(0.21)); assert result == 100 || result == -1;
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;
}
} }
} }

View File

@ -24,8 +24,6 @@ package com.iluwatar.ambassador;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertTrue;
/** /**
* Test for {@link ServiceAmbassador} * Test for {@link ServiceAmbassador}
*/ */
@ -34,6 +32,6 @@ public class ServiceAmbassadorTest {
@Test @Test
public void test() { public void test() {
long result = new ServiceAmbassador().doRemoteFunction(10); 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: 17 KiB

After

Width:  |  Height:  |  Size: 23 KiB

View File

@ -47,13 +47,15 @@ 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 { public static void main(String[] args) throws UnsupportedAudioFileException, IOException, InterruptedException {
Audio.playSound(Audio.getAudioStream("./etc/Bass-Drum-1.wav"), -10.0f); Audio audio = Audio.getInstance();
Audio.playSound(Audio.getAudioStream("./etc/Closed-Hi-Hat-1.wav"), -8.0f); audio.playSound(audio.getAudioStream("./etc/Bass-Drum-1.wav"), -10.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...");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) {
br.read(); br.read();
Audio.stopService(); }
audio.stopService();
} }
} }

View File

@ -23,6 +23,9 @@
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;
@ -38,49 +41,56 @@ 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 static int headIndex; private int headIndex;
private static int tailIndex; private int tailIndex;
private static Thread updateThread = null; private volatile Thread updateThread = null;
private static PlayMessage[] pendingAudio = new PlayMessage[MAX_PENDING]; private 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. * This method stops the Update Method's thread and waits till service stops.
*/ */
public static synchronized void stopService() { public synchronized void stopService() throws InterruptedException {
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 static synchronized boolean isServiceRunning() { public synchronized boolean isServiceRunning() {
if (updateThread != null && updateThread.isAlive() ) { return 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 static void init() { public void init() {
if (updateThread == null) { if (updateThread == null) {
updateThread = new Thread(new Runnable() { updateThread = new Thread(() -> {
public void run() { while (!Thread.currentThread().isInterrupted()) {
while (!Thread.currentThread().isInterrupted()) { update();
Audio.update();
}
} }
}); });
} }
@ -90,7 +100,7 @@ public class Audio {
/** /**
* This is a synchronized thread starter * This is a synchronized thread starter
*/ */
public static synchronized void startThread() { private synchronized void startThread() {
if (!updateThread.isAlive()) { if (!updateThread.isAlive()) {
updateThread.start(); updateThread.start();
headIndex = 0; headIndex = 0;
@ -103,7 +113,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 static void playSound(AudioInputStream stream, float volume) { public 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) {
@ -123,7 +133,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
*/ */
public static void update() { private 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;
@ -136,13 +146,11 @@ public class Audio {
clip.open(audioStream); clip.open(audioStream);
clip.start(); clip.start();
} catch (LineUnavailableException e) { } catch (LineUnavailableException e) {
System.err.println("Error occoured while loading the audio: The line is unavailable"); LOGGER.trace("Error occoured while loading the audio: The line is unavailable", e);
e.printStackTrace();
} catch (IOException e) { } catch (IOException e) {
System.err.println("Input/Output error while loading the audio"); LOGGER.trace("Input/Output error while loading the audio", e);
e.printStackTrace();
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
System.err.println("The system doesn't support the sound: " + e.getMessage()); LOGGER.trace("The system doesn't support the sound: " + e.getMessage(), e);
} }
} }
@ -153,7 +161,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 static AudioInputStream getAudioStream(String filePath) public AudioInputStream getAudioStream(String filePath)
throws UnsupportedAudioFileException, IOException { throws UnsupportedAudioFileException, IOException {
return AudioSystem.getAudioInputStream(new File(filePath).getAbsoluteFile()); return AudioSystem.getAudioInputStream(new File(filePath).getAbsoluteFile());
} }
@ -162,7 +170,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 static PlayMessage[] getPendingAudio() { public PlayMessage[] getPendingAudio() {
return pendingAudio; return pendingAudio;
} }

View File

@ -23,6 +23,7 @@
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;
@ -39,6 +40,12 @@ 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
@ -47,13 +54,15 @@ 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());
} }
/** /**
@ -64,16 +73,18 @@ 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());
} }
} }