Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/sound/sampled/DirectAudio/bug6400879.java
38855 views
/*1* Copyright (c) 2006, 2016, 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*/2223/*24* @test25* @bug 6400879 710014026* @summary Tests that Start/Stop sequence doesn't hang27* @author Alexey Menkov28* @run main bug640087929* @key intermittent30*/3132import javax.sound.sampled.*;3334public class bug6400879 extends Thread {3536public static void main(String args[]) throws Exception {37bug6400879 pThis = new bug6400879();38//pThis.init();39pThis.setDaemon(true);40pThis.start();41monitor(pThis);42}4344static final long BLOCK_TIMEOUT = 5000; // 5 sec4546// monitors that pThis doesn't hang47public static void monitor(bug6400879 pThis) throws Exception {48long prevLoop = -1;49long prevTime = currentTimeMillis();50while (pThis.isAlive()) {51if (pThis.loopCounter == prevLoop) {52if (currentTimeMillis() - prevTime > BLOCK_TIMEOUT) {53// block!54log("Test FAILED.");55throw new RuntimeException("Test FAILED: thread has been blocked!");56}57} else {58prevLoop = pThis.loopCounter;59prevTime = currentTimeMillis();60}61delay(500); // sleep for 0.5 sec62}63log("Test sucessfully passed.");64}6566volatile long loopCounter = 0;67final long LOOPS_PER_LINE = 100;6869public void run() {70SourceDataLine line = null;7172DataLine.Info line_info = new DataLine.Info(SourceDataLine.class, null);73Line.Info infos[] = AudioSystem.getSourceLineInfo(line_info);7475log("total " + infos.length + " lines");7677for (int lineNum = 0; lineNum < infos.length; lineNum++) {78try {79line = (SourceDataLine)AudioSystem.getLine(infos[lineNum]);80log("testing line: " + line);81line.open(line.getFormat());82for (int i=0; i<LOOPS_PER_LINE; i++) {83log("start->stop (" + i + ")");84line.start();85line.stop();86log(" - OK");87loopCounter++;88}89line.close();90line = null;91} catch (LineUnavailableException e1) {92log("LineUnavailableException caught, test okay.");93log(e1.getMessage());94} catch (SecurityException e2) {95log("SecurityException caught, test okay.");96log(e2.getMessage());97} catch (IllegalArgumentException e3) {98log("IllegalArgumentException caught, test okay.");99log(e3.getMessage());100}101if (line != null) {102line.close();103line = null;104}105}106107}108109110// helper routines111static long startTime = currentTimeMillis();112static long currentTimeMillis() {113//return System.nanoTime() / 1000000L;114return System.currentTimeMillis();115}116static void log(String s) {117long time = currentTimeMillis() - startTime;118long ms = time % 1000;119time /= 1000;120long sec = time % 60;121time /= 60;122long min = time % 60;123time /= 60;124System.out.println(""125+ (time < 10 ? "0" : "") + time126+ ":" + (min < 10 ? "0" : "") + min127+ ":" + (sec < 10 ? "0" : "") + sec128+ "." + (ms < 10 ? "00" : (ms < 100 ? "0" : "")) + ms129+ " (" + Thread.currentThread().getName() + ") " + s);130}131static void delay(int millis) {132try {133Thread.sleep(millis);134} catch (InterruptedException e) {}135}136}137138139