Compare commits

..

1 Commits

7 changed files with 102 additions and 92 deletions

View File

@ -0,0 +1,10 @@
package com.iluwatar.balking;
import java.util.concurrent.TimeUnit;
/**
* An interface to simulate delay while executing some work.
*/
public interface DelayProvider {
void executeAfterDelay(long interval, TimeUnit timeUnit, Runnable task);
}

View File

@ -25,17 +25,38 @@ package com.iluwatar.balking;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.util.concurrent.TimeUnit;
/** /**
* Washing machine class * Washing machine class
*/ */
public class WashingMachine { public class WashingMachine {
private static final Logger LOGGER = LoggerFactory.getLogger(WashingMachine.class); private static final Logger LOGGER = LoggerFactory.getLogger(WashingMachine.class);
private final DelayProvider delayProvider;
private WashingMachineState washingMachineState; private WashingMachineState washingMachineState;
/**
* Creates a new instance of WashingMachine
*/
public WashingMachine() { public WashingMachine() {
washingMachineState = WashingMachineState.ENABLED; this((interval, timeUnit, task) -> {
try {
Thread.sleep(timeUnit.toMillis(interval));
} catch (InterruptedException ie) {
ie.printStackTrace();
}
task.run();
});
}
/**
* Creates a new instance of WashingMachine using provided delayProvider. This constructor is used only for
* unit testing purposes.
*/
public WashingMachine(DelayProvider delayProvider) {
this.delayProvider = delayProvider;
this.washingMachineState = WashingMachineState.ENABLED;
} }
public WashingMachineState getWashingMachineState() { public WashingMachineState getWashingMachineState() {
@ -56,12 +77,8 @@ public class WashingMachine {
washingMachineState = WashingMachineState.WASHING; washingMachineState = WashingMachineState.WASHING;
} }
LOGGER.info("{}: Doing the washing", Thread.currentThread().getName()); LOGGER.info("{}: Doing the washing", Thread.currentThread().getName());
try {
Thread.sleep(50); this.delayProvider.executeAfterDelay(50, TimeUnit.MILLISECONDS, this::endOfWashing);
} catch (InterruptedException ie) {
ie.printStackTrace();
}
endOfWashing();
} }
/** /**

View File

@ -22,11 +22,8 @@
*/ */
package com.iluwatar.balking; package com.iluwatar.balking;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
@ -36,32 +33,39 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
*/ */
public class WashingMachineTest { public class WashingMachineTest {
private volatile WashingMachineState machineStateGlobal; private FakeDelayProvider fakeDelayProvider = new FakeDelayProvider();
@Disabled
@Test @Test
public void wash() throws Exception { public void wash() {
WashingMachine washingMachine = new WashingMachine(); WashingMachine washingMachine = new WashingMachine(fakeDelayProvider);
ExecutorService executorService = Executors.newFixedThreadPool(2);
executorService.execute(washingMachine::wash);
executorService.execute(() -> {
washingMachine.wash(); washingMachine.wash();
machineStateGlobal = washingMachine.getWashingMachineState(); washingMachine.wash();
});
executorService.shutdown(); WashingMachineState machineStateGlobal = washingMachine.getWashingMachineState();
try {
executorService.awaitTermination(10, TimeUnit.SECONDS); fakeDelayProvider.task.run();
} catch (InterruptedException ie) {
ie.printStackTrace(); // washing machine remains in washing state
}
assertEquals(WashingMachineState.WASHING, machineStateGlobal); assertEquals(WashingMachineState.WASHING, machineStateGlobal);
// washing machine goes back to enabled state
assertEquals(WashingMachineState.ENABLED, washingMachine.getWashingMachineState());
} }
@Test @Test
public void endOfWashing() throws Exception { public void endOfWashing() {
WashingMachine washingMachine = new WashingMachine(); WashingMachine washingMachine = new WashingMachine();
washingMachine.wash(); washingMachine.wash();
assertEquals(WashingMachineState.ENABLED, washingMachine.getWashingMachineState()); assertEquals(WashingMachineState.ENABLED, washingMachine.getWashingMachineState());
} }
private class FakeDelayProvider implements DelayProvider {
private Runnable task;
@Override
public void executeAfterDelay(long interval, TimeUnit timeUnit, Runnable task) {
this.task = task;
}
}
} }

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() {
public void run() {
while (!Thread.currentThread().isInterrupted()) { while (!Thread.currentThread().isInterrupted()) {
update(); 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());
} }
} }