1 /*
   2  * Copyright (c) 2006, 2016, 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 /*
  25  * @test
  26  * @bug 6400879 7100140
  27  * @summary Tests that Start/Stop sequence doesn't hang
  28  * @author Alexey Menkov
  29  * @run main bug6400879
  30  * @key intermittent
  31  */
  32 
  33 import javax.sound.sampled.*;
  34 
  35 public class bug6400879 extends Thread {
  36 
  37     public static void main(String args[]) throws Exception {
  38         bug6400879 pThis = new bug6400879();
  39         //pThis.init();
  40         pThis.setDaemon(true);
  41         pThis.start();
  42         monitor(pThis);
  43     }
  44 
  45     static final long BLOCK_TIMEOUT = 5000;    // 5 sec
  46 
  47     // monitors that pThis doesn't hang
  48     public static void monitor(bug6400879 pThis) throws Exception {
  49         long prevLoop = -1;
  50         long prevTime = currentTimeMillis();
  51         while (pThis.isAlive()) {
  52             if (pThis.loopCounter == prevLoop) {
  53                 if (currentTimeMillis() - prevTime > BLOCK_TIMEOUT) {
  54                     // block!
  55                     log("Test FAILED.");
  56                     throw new RuntimeException("Test FAILED: thread has been blocked!");
  57                 }
  58             } else {
  59                 prevLoop = pThis.loopCounter;
  60                 prevTime = currentTimeMillis();
  61             }
  62             delay(500);    // sleep for 0.5 sec
  63         }
  64         log("Test sucessfully passed.");
  65     }
  66 
  67     volatile long loopCounter = 0;
  68     final long LOOPS_PER_LINE = 100;
  69 
  70     public void run() {
  71         SourceDataLine line = null;
  72 
  73         DataLine.Info line_info = new DataLine.Info(SourceDataLine.class, null);
  74         Line.Info infos[] = AudioSystem.getSourceLineInfo(line_info);
  75 
  76         log("total " + infos.length + " lines");
  77 
  78         for (int lineNum = 0; lineNum < infos.length; lineNum++) {
  79             try {
  80                 line = (SourceDataLine)AudioSystem.getLine(infos[lineNum]);
  81                 log("testing line: " + line);
  82                 line.open(line.getFormat());
  83                 for (int i=0; i<LOOPS_PER_LINE; i++) {
  84                     log("start->stop (" + i + ")");
  85                     line.start();
  86                     line.stop();
  87                     log(" - OK");
  88                     loopCounter++;
  89                 }
  90                 line.close();
  91                 line = null;
  92             } catch (LineUnavailableException e1) {
  93                 log("LineUnavailableException caught, test okay.");
  94                 log(e1.getMessage());
  95             } catch (SecurityException e2) {
  96                 log("SecurityException caught, test okay.");
  97                 log(e2.getMessage());
  98             } catch (IllegalArgumentException e3) {
  99                 log("IllegalArgumentException caught, test okay.");
 100                 log(e3.getMessage());
 101             }
 102             if (line != null) {
 103                 line.close();
 104                 line = null;
 105             }
 106         }
 107 
 108     }
 109 
 110 
 111     // helper routines
 112     static long startTime = currentTimeMillis();
 113     static long currentTimeMillis() {
 114         //return System.nanoTime() / 1000000L;
 115         return System.currentTimeMillis();
 116     }
 117     static void log(String s) {
 118         long time = currentTimeMillis() - startTime;
 119         long ms = time % 1000;
 120         time /= 1000;
 121         long sec = time % 60;
 122         time /= 60;
 123         long min = time % 60;
 124         time /= 60;
 125         System.out.println(""
 126                 + (time < 10 ? "0" : "") + time
 127                 + ":" + (min < 10 ? "0" : "") + min
 128                 + ":" + (sec < 10 ? "0" : "") + sec
 129                 + "." + (ms < 10 ? "00" : (ms < 100 ? "0" : "")) + ms
 130                 + " (" + Thread.currentThread().getName() + ") " + s);
 131     }
 132     static void delay(int millis) {
 133         try {
 134             Thread.sleep(millis);
 135         } catch (InterruptedException e) {}
 136     }
 137 }