Path: blob/master/test/jdk/javax/sound/sampled/DirectAudio/bug6400879.java
41153 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/othervm bug640087929*/3031import java.util.concurrent.TimeUnit;3233import javax.sound.sampled.*;3435public class bug6400879 extends Thread {3637public static void main(String args[]) throws Exception {38bug6400879 pThis = new bug6400879();39//pThis.init();40pThis.setDaemon(true);41pThis.start();42monitor(pThis);43}4445static final long BLOCK_TIMEOUT = 5000; // 5 sec4647// monitors that pThis doesn't hang48public static void monitor(bug6400879 pThis) throws Exception {49long prevLoop = -1;50long prevTime = currentTimeMillis();51while (pThis.isAlive()) {52if (pThis.loopCounter == prevLoop) {53long delay = currentTimeMillis() - prevTime;54if (delay > BLOCK_TIMEOUT) {55// blocked?56log("The test is slow, delay = " + delay);57}58} else {59prevLoop = pThis.loopCounter;60prevTime = currentTimeMillis();61}62delay(1000); // sleep for 1 sec63}64log("Test sucessfully passed.");65}6667volatile long loopCounter = 0;68final long LOOPS_PER_LINE = 100;6970public void run() {71SourceDataLine line = null;7273DataLine.Info line_info = new DataLine.Info(SourceDataLine.class, null);74Line.Info infos[] = AudioSystem.getSourceLineInfo(line_info);7576log("total " + infos.length + " lines");7778for (int lineNum = 0; lineNum < infos.length; lineNum++) {79try {80line = (SourceDataLine)AudioSystem.getLine(infos[lineNum]);81log("testing line: " + line);82line.open(line.getFormat());83for (int i=0; i<LOOPS_PER_LINE; i++) {84log("start->stop (" + i + ")");85line.start();86line.stop();87log(" - OK");88loopCounter++;89}90line.close();91line = null;92} catch (LineUnavailableException e1) {93log("LineUnavailableException caught, test okay.");94log(e1.getMessage());95} catch (SecurityException e2) {96log("SecurityException caught, test okay.");97log(e2.getMessage());98} catch (IllegalArgumentException e3) {99log("IllegalArgumentException caught, test okay.");100log(e3.getMessage());101}102if (line != null) {103line.close();104line = null;105}106}107108}109110111// helper routines112static long startTime = currentTimeMillis();113static long currentTimeMillis() {114return TimeUnit.NANOSECONDS.toMillis(System.nanoTime());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