Path: blob/master/test/jdk/javax/sound/sampled/Clip/AutoCloseTimeCheck.java
41153 views
/*1* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.applet.AudioClip;24import java.io.ByteArrayInputStream;25import java.io.File;26import java.io.IOException;27import java.io.InputStream;28import java.nio.file.Files;29import java.util.concurrent.TimeUnit;3031import javax.sound.sampled.AudioFileFormat.Type;32import javax.sound.sampled.AudioFormat;33import javax.sound.sampled.AudioInputStream;34import javax.sound.sampled.AudioSystem;3536import static javax.sound.sampled.AudioFormat.Encoding.PCM_SIGNED;37import static javax.sound.sampled.AudioSystem.NOT_SPECIFIED;3839/**40* @test41* @bug 820226442*/43public final class AutoCloseTimeCheck {4445public static void main(final String[] args) throws Exception {46// Prepare the audio file47File file = new File("audio.wav");48try {49AudioFormat format =50new AudioFormat(PCM_SIGNED, 44100, 8, 1, 1, 44100, false);51AudioSystem.write(getStream(format), Type.WAVE, file);52} catch (final Exception ignored) {53return; // the test is not applicable54}55try {56testSmallDelay(file);57testBigDelay(file);58} finally {59Files.delete(file.toPath());60}61}6263/**64* Checks that after a big period of non-activity the clip will be closed65* and the "Direct Clip" thread will stop.66*/67private static void testBigDelay(final File file) throws Exception {68AudioClip clip = (AudioClip) file.toURL().getContent();69clip.loop();70clip.stop();71sleep(20000); // 20 sec for slow systems72if (count() != 0) {73throw new RuntimeException("Thread was found");74}75}7677/**78* Checks that after small period of non-activity the clip will not be79* closed and the "Direct Clip" thread will alive.80*/81private static void testSmallDelay(final File file) throws IOException {82AudioClip clip = (AudioClip) file.toURL().getContent();83long threadID = 0;84// Will run the test no more than 15 seconds85long endtime = System.nanoTime() + TimeUnit.SECONDS.toNanos(15);86while (endtime - System.nanoTime() > 0) {87clip.loop();88sleep(500);8990long data = count();91if (data != threadID) {92System.out.println("Playing on new thread: " + data + " at "93+ new java.util.Date());94if (threadID == 0) {95threadID = data;96} else {97throw new RuntimeException("Thread was changed");98}99}100101clip.stop();102sleep(500);103}104}105106private static void sleep(int millis) {107try {108Thread.sleep(millis);109} catch (InterruptedException ignored) {110}111}112113private static long count() {114for (final Thread t : Thread.getAllStackTraces().keySet()) {115if (t.getName().equals("Direct Clip")) {116return t.getId();117}118}119return 0;120}121122private static AudioInputStream getStream(final AudioFormat format) {123final int dataSize = 5000 * format.getFrameSize();124final InputStream in = new ByteArrayInputStream(new byte[dataSize]);125return new AudioInputStream(in, format, NOT_SPECIFIED);126}127}128129130