Refactored Event Queue (#806)

* 1) Test cases were not stopping AudioService before ending test case 2) Changed Audio to be a good singleton, previously because of Audio being bad singleton, test cases which were using static methods could have caused intermittent failures. 3) Made some other refactorings as well

* Removed sonar issue and converted Audio to eager singleton for simplicity

* Updated class diagram PNG
This commit is contained in:
Narendra Pathai
2018-10-21 23:45:33 +05:30
committed by Ilkka Seppälä
parent 922fd62da6
commit 25ed7c09c5
4 changed files with 64 additions and 43 deletions

View File

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