# HG changeset patch # User Omair Majid # Date 1222722881 14400 # Node ID 59f5e34743f6f0375ce5f0f47ddd016774114a34 # Parent 5ab54b0ca4ead63e025c3027eebd176e69a53fe1 2008-09-29 Omair Majid * unittests/org/classpath/icedtea/pulseaudio/PulseAudioSourceDataLineTest.java (testWriteAndClose): New test. Tries to close() a SourceDataLine during a write(). (testWriteAndStop): New test. Tries to stop() a SourceDataLine during a write(). (testWriteAndFlush): New test. Tries to flush() a SourceDataLine during a write(). diff -r 5ab54b0ca4ea -r 59f5e34743f6 unittests/org/classpath/icedtea/pulseaudio/PulseAudioSourceDataLineTest.java --- a/unittests/org/classpath/icedtea/pulseaudio/PulseAudioSourceDataLineTest.java Mon Sep 29 15:57:43 2008 -0400 +++ b/unittests/org/classpath/icedtea/pulseaudio/PulseAudioSourceDataLineTest.java Mon Sep 29 17:14:41 2008 -0400 @@ -41,6 +41,7 @@ import java.io.File; import java.io.IOException; +import java.net.UnknownHostException; import javax.sound.sampled.AudioFormat; import javax.sound.sampled.AudioInputStream; @@ -229,6 +230,166 @@ } @Test + public void testWriteAndClose() throws UnsupportedAudioFileException, + IOException, LineUnavailableException, InterruptedException { + System.out.println("This test tires to close the line during a write"); + + File soundFile = new File("testsounds/startup.wav"); + final AudioInputStream audioInputStream = AudioSystem + .getAudioInputStream(soundFile); + AudioFormat audioFormat = audioInputStream.getFormat(); + + sourceDataLine = (SourceDataLine) mixer.getLine(new DataLine.Info( + SourceDataLine.class, audioFormat)); + Assert.assertNotNull(sourceDataLine); + + sourceDataLine.open(audioFormat); + sourceDataLine.start(); + + Thread writer = new Thread() { + + @Override + public void run() { + try { + final byte[] abData = new byte[10000000]; + + int bytesRead = 0; + while (bytesRead >= 0) { + bytesRead = audioInputStream.read(abData, 0, + abData.length); + if (bytesRead > 0) { + sourceDataLine.write(abData, 0, bytesRead); + } + } + } catch (UnknownHostException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + }; + + writer.start(); + Thread.sleep(100); + + sourceDataLine.close(); + + writer.join(500); + Assert.assertFalse(writer.isAlive()); + + } + + @Test + public void testWriteAndStop() throws UnsupportedAudioFileException, + IOException, LineUnavailableException, InterruptedException { + System.out.println("This test tires to stop the line during a write"); + + File soundFile = new File("testsounds/startup.wav"); + final AudioInputStream audioInputStream = AudioSystem + .getAudioInputStream(soundFile); + AudioFormat audioFormat = audioInputStream.getFormat(); + + sourceDataLine = (SourceDataLine) mixer.getLine(new DataLine.Info( + SourceDataLine.class, audioFormat)); + Assert.assertNotNull(sourceDataLine); + + sourceDataLine.open(audioFormat); + sourceDataLine.start(); + + Thread writer = new Thread() { + + @Override + public void run() { + try { + final byte[] abData = new byte[10000000]; + + int bytesRead = 0; + while (bytesRead >= 0) { + bytesRead = audioInputStream.read(abData, 0, + abData.length); + if (bytesRead > 0) { + sourceDataLine.write(abData, 0, bytesRead); + } + } + } catch (UnknownHostException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + }; + + writer.start(); + + Thread.sleep(500); + + sourceDataLine.stop(); + + writer.join(500); + Assert.assertFalse(writer.isAlive()); + + sourceDataLine.close(); + + } + + @Test + public void testWriteAndFlush() throws UnsupportedAudioFileException, + IOException, LineUnavailableException, InterruptedException { + + System.out.println("This test tries to flush a line during a write"); + + File soundFile = new File("testsounds/startup.wav"); + final AudioInputStream audioInputStream = AudioSystem + .getAudioInputStream(soundFile); + AudioFormat audioFormat = audioInputStream.getFormat(); + + sourceDataLine = (SourceDataLine) mixer.getLine(new DataLine.Info( + SourceDataLine.class, audioFormat)); + Assert.assertNotNull(sourceDataLine); + + sourceDataLine.open(audioFormat); + sourceDataLine.start(); + + Thread writer = new Thread() { + + @Override + public void run() { + try { + final byte[] abData = new byte[10000000]; + + int bytesRead = 0; + while (bytesRead >= 0) { + bytesRead = audioInputStream.read(abData, 0, + abData.length); + if (bytesRead > 0) { + sourceDataLine.write(abData, 0, bytesRead); + } + } + } catch (UnknownHostException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + }; + + writer.start(); + + Thread.sleep(100); + + sourceDataLine.flush(); + + writer.join(500); + Assert.assertFalse(writer.isAlive()); + + sourceDataLine.stop(); + sourceDataLine.close(); + } + + @Test public void testStartedStopped() throws LineUnavailableException, UnsupportedAudioFileException, IOException {