1 /*
   2  * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 import java.util.concurrent.TimeUnit;
  25 
  26 import javax.sound.sampled.AudioFormat;
  27 import javax.sound.sampled.AudioSystem;
  28 import javax.sound.sampled.Clip;
  29 import javax.sound.sampled.DataLine;
  30 import javax.sound.sampled.LineUnavailableException;
  31 
  32 import static javax.sound.sampled.AudioFormat.Encoding.PCM_SIGNED;
  33 
  34 /**
  35  * @test
  36  * @bug 8207150
  37  * @summary Clip.isRunning() may return true after Clip.stop() was called
  38  */
  39 public final class ClipIsRunningAfterStop {
  40 
  41     private static volatile Exception failed;
  42 
  43     public static void main(final String[] args) throws Exception {
  44         final Runnable r = () -> {
  45             try {
  46                 test();
  47             } catch (LineUnavailableException | IllegalArgumentException ignored) {
  48                 // the test is not applicable
  49             } catch (Exception ex) {
  50                 failed = ex;
  51             }
  52         };
  53         Thread t1 = new Thread(r);
  54         Thread t2 = new Thread(r);
  55         Thread t3 = new Thread(r);
  56         t1.start();
  57         t2.start();
  58         t3.start();
  59         t1.join();
  60         t2.join();
  61         t3.join();
  62         if (failed != null) {
  63             throw new RuntimeException(failed);
  64         }
  65     }
  66 
  67     private static void test() throws Exception {
  68         // Will run the test no more than 15 seconds
  69         long endtime = System.nanoTime() + TimeUnit.SECONDS.toNanos(15);
  70         while (failed == null && endtime - System.nanoTime() > 0) {
  71             Clip clip = createClip();
  72             clip.loop(Clip.LOOP_CONTINUOUSLY);
  73             clip.stop();
  74             if (clip.isRunning()) {
  75                 if (clip.isRunning()) {
  76                     throw new RuntimeException("Clip is running");
  77                 }
  78             }
  79             if (clip.isActive()) {
  80                 if (clip.isActive()) {
  81                     throw new RuntimeException("Clip is active");
  82                 }
  83             }
  84             clip.close();
  85         }
  86     }
  87 
  88     private static Clip createClip() throws LineUnavailableException {
  89         AudioFormat format =
  90                 new AudioFormat(PCM_SIGNED, 44100, 8, 1, 1, 44100, false);
  91         DataLine.Info info = new DataLine.Info(Clip.class, format);
  92         Clip clip = (Clip) AudioSystem.getLine(info);
  93         clip.open(format, new byte[2], 0, 2);
  94         return clip;
  95     }
  96 }