Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/net/Socket/Streams.java
38812 views
/*1* Copyright (c) 2012, 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 800383326* @summary Spurious NPE from Socket.getIn/OutputStream27*/2829import java.io.IOException;30import java.lang.reflect.Constructor;31import java.net.ServerSocket;32import java.net.Socket;33import java.util.concurrent.Phaser;3435// Racey test, will not always fail, but if it does then there is a problem.3637public class Streams {38static final int NUM_THREADS = 100;39static volatile boolean failed;40static final Phaser startingGate = new Phaser(NUM_THREADS + 1);4142public static void main(String[] args) throws Exception {4344try (ServerSocket ss = new ServerSocket(0)) {45runTest(OutputStreamGetter.class, ss);46runTest(InputStreamGetter.class, ss);47}4849if (failed)50throw new RuntimeException("Failed, check output");51}5253static void runTest(Class<? extends StreamGetter> klass, ServerSocket ss)54throws Exception55{56final int port = ss.getLocalPort();57Socket[] sockets = new Socket[NUM_THREADS];58for (int i=0; i<NUM_THREADS; i++) {59sockets[i] = new Socket("localhost", port);60try (Socket socket = ss.accept()) {}61}6263Constructor<? extends StreamGetter> ctr = klass.getConstructor(Socket.class);6465Thread[] threads = new Thread[NUM_THREADS];66for (int i=0; i<NUM_THREADS; i++)67threads[i] = ctr.newInstance(sockets[i]);68for (int i=0; i<NUM_THREADS; i++)69threads[i].start();7071startingGate.arriveAndAwaitAdvance();72for (int i=0; i<NUM_THREADS; i++)73sockets[i].close();7475for (int i=0; i<NUM_THREADS; i++)76threads[i].join();77}7879static abstract class StreamGetter extends Thread {80final Socket socket;81StreamGetter(Socket s) { socket = s; }8283@Override84public void run() {85try {86startingGate.arriveAndAwaitAdvance();87getStream();88} catch (IOException x) {89// OK, socket may be closed90} catch (NullPointerException x) {91x.printStackTrace();92failed = true;93}94}9596abstract void getStream() throws IOException;97}9899static class InputStreamGetter extends StreamGetter {100public InputStreamGetter(Socket s) { super(s); }101void getStream() throws IOException {102socket.getInputStream();103}104}105106static class OutputStreamGetter extends StreamGetter {107public OutputStreamGetter(Socket s) { super(s); }108void getStream() throws IOException {109socket.getOutputStream();110}111}112}113114115