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