Path: blob/master/test/jdk/java/io/PipedInputStream/CloseAndAvailableRC.java
41149 views
/*1* Copyright (c) 2003, 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/* @test24* @bug 449976325* @summary Check for race condition between close and available26*/2728import java.io.*;2930/*31* Note: this is a probabalistic test and will not always fail on32* a workspace where the race condition can occur. However it should33* never fail on a workspace where the bug has been fixed.34*/35public class CloseAndAvailableRC {36public static void main(final String[] args) throws Exception {37CloseAndAvailableRC rc = new CloseAndAvailableRC();38rc.go();39}4041private PipedInputStream inPipe = null;42private PipedOutputStream outPipe = null;43private Thread sink = null;44private volatile boolean stop = false;45private volatile boolean stopTest = false;4647private void go() throws Exception {48for (long i=0; i<2000; i++) {49if (stopTest) {50cleanup();51throw new RuntimeException("Test failed");52}53resetPipes();54System.err.println("Closing...");55inPipe.close();56}57cleanup();58}5960// Cleanup old threads61private void cleanup() throws Exception {62if (sink != null) {63stop = true;64sink.interrupt();65try {66sink.join();67} catch (InterruptedException e) {68}69stop = false;70// Input Stream will have been closed already71outPipe.close();72}73}7475private void resetPipes() throws Exception {76cleanup();7778// Init pipe; Note: output never read79inPipe = new PipedInputStream();80outPipe = new PipedOutputStream(inPipe);8182// Put stuff in pipe so that available() > 083for (byte b = 0; b < 10; b++)84outPipe.write(b);8586sink = new Sink();87sink.start();88}8990private class Sink extends Thread {91public void run() {92while (!stop) {93try {94final int num = inPipe.available();95if (num < 0) {96// Bug detected; stop the test97stopTest = true;98}99} catch (final IOException e) {100System.err.println("Error on available:" + e.getMessage());101e.printStackTrace(System.err);102}103}104}105}106}107108109