2
|
1 |
/*
|
5506
|
2 |
* Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved.
|
2
|
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 |
*
|
5506
|
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.
|
2
|
22 |
*/
|
|
23 |
|
|
24 |
/* @test
|
|
25 |
* @bug 4499763
|
|
26 |
* @summary Check for race condition between close and available
|
|
27 |
*/
|
|
28 |
|
|
29 |
import java.io.*;
|
|
30 |
|
|
31 |
/*
|
|
32 |
* Note: this is a probabalistic test and will not always fail on
|
|
33 |
* a workspace where the race condition can occur. However it should
|
|
34 |
* never fail on a workspace where the bug has been fixed.
|
|
35 |
*/
|
|
36 |
public class CloseAndAvailableRC {
|
|
37 |
public static void main(final String[] args) throws Exception {
|
|
38 |
CloseAndAvailableRC rc = new CloseAndAvailableRC();
|
|
39 |
rc.go();
|
|
40 |
}
|
|
41 |
|
|
42 |
private PipedInputStream inPipe = null;
|
|
43 |
private PipedOutputStream outPipe = null;
|
|
44 |
private Thread sink = null;
|
|
45 |
private volatile boolean stop = false;
|
|
46 |
private volatile boolean stopTest = false;
|
|
47 |
|
|
48 |
private void go() throws Exception {
|
|
49 |
for (long i=0; i<2000; i++) {
|
|
50 |
if (stopTest) {
|
|
51 |
cleanup();
|
|
52 |
throw new RuntimeException("Test failed");
|
|
53 |
}
|
|
54 |
resetPipes();
|
|
55 |
System.err.println("Closing...");
|
|
56 |
inPipe.close();
|
|
57 |
}
|
|
58 |
cleanup();
|
|
59 |
}
|
|
60 |
|
|
61 |
// Cleanup old threads
|
|
62 |
private void cleanup() throws Exception {
|
|
63 |
if (sink != null) {
|
|
64 |
stop = true;
|
|
65 |
sink.interrupt();
|
|
66 |
try {
|
|
67 |
sink.join();
|
|
68 |
} catch (InterruptedException e) {
|
|
69 |
}
|
|
70 |
stop = false;
|
|
71 |
// Input Stream will have been closed already
|
|
72 |
outPipe.close();
|
|
73 |
}
|
|
74 |
}
|
|
75 |
|
|
76 |
private void resetPipes() throws Exception {
|
|
77 |
cleanup();
|
|
78 |
|
|
79 |
// Init pipe; Note: output never read
|
|
80 |
inPipe = new PipedInputStream();
|
|
81 |
outPipe = new PipedOutputStream(inPipe);
|
|
82 |
|
|
83 |
// Put stuff in pipe so that available() > 0
|
|
84 |
for (byte b = 0; b < 10; b++)
|
|
85 |
outPipe.write(b);
|
|
86 |
|
|
87 |
sink = new Sink();
|
|
88 |
sink.start();
|
|
89 |
}
|
|
90 |
|
|
91 |
private class Sink extends Thread {
|
|
92 |
public void run() {
|
|
93 |
while (!stop) {
|
|
94 |
try {
|
|
95 |
final int num = inPipe.available();
|
|
96 |
if (num < 0) {
|
|
97 |
// Bug detected; stop the test
|
|
98 |
stopTest = true;
|
|
99 |
}
|
|
100 |
} catch (final IOException e) {
|
|
101 |
System.err.println("Error on available:" + e.getMessage());
|
|
102 |
e.printStackTrace(System.err);
|
|
103 |
}
|
|
104 |
}
|
|
105 |
}
|
|
106 |
}
|
|
107 |
}
|