Path: blob/master/test/jdk/javax/sound/sampled/Clip/ClipIsRunningAfterStop.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.util.concurrent.TimeUnit;2425import javax.sound.sampled.AudioFormat;26import javax.sound.sampled.AudioSystem;27import javax.sound.sampled.Clip;28import javax.sound.sampled.DataLine;29import javax.sound.sampled.LineUnavailableException;3031import static javax.sound.sampled.AudioFormat.Encoding.PCM_SIGNED;3233/**34* @test35* @bug 820715036* @summary Clip.isRunning() may return true after Clip.stop() was called37*/38public final class ClipIsRunningAfterStop {3940private static volatile Exception failed;4142public static void main(final String[] args) throws Exception {43final Runnable r = () -> {44try {45test();46} catch (LineUnavailableException | IllegalArgumentException ignored) {47// the test is not applicable48} catch (Exception ex) {49failed = ex;50}51};52Thread t1 = new Thread(r);53Thread t2 = new Thread(r);54Thread t3 = new Thread(r);55t1.start();56t2.start();57t3.start();58t1.join();59t2.join();60t3.join();61if (failed != null) {62throw new RuntimeException(failed);63}64}6566private static void test() throws Exception {67// Will run the test no more than 15 seconds68long endtime = System.nanoTime() + TimeUnit.SECONDS.toNanos(15);69while (failed == null && endtime - System.nanoTime() > 0) {70Clip clip = createClip();71clip.loop(Clip.LOOP_CONTINUOUSLY);72clip.stop();73if (clip.isRunning()) {74if (clip.isRunning()) {75throw new RuntimeException("Clip is running");76}77}78if (clip.isActive()) {79if (clip.isActive()) {80throw new RuntimeException("Clip is active");81}82}83clip.close();84}85}8687private static Clip createClip() throws LineUnavailableException {88AudioFormat format =89new AudioFormat(PCM_SIGNED, 44100, 8, 1, 1, 44100, false);90DataLine.Info info = new DataLine.Info(Clip.class, format);91Clip clip = (Clip) AudioSystem.getLine(info);92clip.open(format, new byte[2], 0, 2);93return clip;94}95}969798