Path: blob/master/test/jdk/javax/sound/midi/Transmitter/bug6415669.java
41152 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*/2223import javax.sound.midi.MidiSystem;24import javax.sound.midi.MidiUnavailableException;25import javax.sound.midi.Transmitter;2627/**28* @test29* @bug 641566930* @summary Tests that terminating thread which got transmitter doesn't cause31* JVM crash (windows)32* @run main bug641566933*/34public class bug6415669 {3536public static void main(String args[]) throws Exception {37String osStr = System.getProperty("os.name");38boolean isWin = osStr.toLowerCase().startsWith("windows");39log("OS: " + osStr);40log("Arch: " + System.getProperty("os.arch"));41if (!isWin) {42log("The test is for Windows only");43return;44}4546bug6415669 This = new bug6415669();47if (This.test()) {48log("Test sucessfully passed.");49} else {50log("Test FAILED!");51throw new RuntimeException("Test FAILED!");52}53}5455volatile Transmitter transmitter = null;56Thread openThread = null;57boolean test() {58openThread = new Thread(new Runnable() {59public void run() {60try {61log("openThread: getting transmitter...");62transmitter = MidiSystem.getTransmitter();63log("openThread: - OK: " + transmitter);64} catch (MidiUnavailableException ex) {65log("openThread: - Exception: ");66ex.printStackTrace(System.out);67log("openThread: skipping...");68}69log("openThread: exiting...");70}71});72log("starting openThread...");73openThread.start();7475while (openThread.isAlive())76delay(500);77// make additional delay78delay(500);7980if (transmitter == null) {81return true; // midi is not available, just ignore82}8384log("closing transmitter");85transmitter.close();86log(" - OK");8788return true;89}9091// helper routines92static long startTime = currentTimeMillis();93static long currentTimeMillis() {94//return System.nanoTime() / 1000000L;95return System.currentTimeMillis();96}97static void log(String s) {98long time = currentTimeMillis() - startTime;99long ms = time % 1000;100time /= 1000;101long sec = time % 60;102time /= 60;103long min = time % 60;104time /= 60;105System.out.println(""106+ (time < 10 ? "0" : "") + time107+ ":" + (min < 10 ? "0" : "") + min108+ ":" + (sec < 10 ? "0" : "") + sec109+ "." + (ms < 10 ? "00" : (ms < 100 ? "0" : "")) + ms110+ " (" + Thread.currentThread().getName() + ") " + s);111}112static void delay(int millis) {113try {114Thread.sleep(millis);115} catch (InterruptedException e) {}116}117}118119120