Path: blob/master/test/jdk/java/net/Socket/Streams.java
41152 views
/*1* Copyright (c) 2012, 2019, 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.InetAddress;32import java.net.InetSocketAddress;33import java.net.ServerSocket;34import java.net.Socket;35import java.util.concurrent.Phaser;3637// Racey test, will not always fail, but if it does then there is a problem.3839public class Streams {40static final int NUM_THREADS = 100;41static volatile boolean failed;42static final Phaser startingGate = new Phaser(NUM_THREADS + 1);4344public static void main(String[] args) throws Exception {4546try (ServerSocket ss = new ServerSocket()) {47InetAddress loopback = InetAddress.getLoopbackAddress();48ss.bind(new InetSocketAddress(loopback, 0));49runTest(OutputStreamGetter.class, ss);50runTest(InputStreamGetter.class, ss);51}5253if (failed)54throw new RuntimeException("Failed, check output");55}5657static void runTest(Class<? extends StreamGetter> klass, ServerSocket ss)58throws Exception59{60final int port = ss.getLocalPort();61final InetAddress address = ss.getInetAddress();62Socket[] sockets = new Socket[NUM_THREADS];63for (int i=0; i<NUM_THREADS; i++) {64sockets[i] = address.isAnyLocalAddress()65? new Socket("localhost", port)66: new Socket(address, port);67try (Socket socket = ss.accept()) {}68}6970Constructor<? extends StreamGetter> ctr = klass.getConstructor(Socket.class);7172Thread[] threads = new Thread[NUM_THREADS];73for (int i=0; i<NUM_THREADS; i++)74threads[i] = ctr.newInstance(sockets[i]);75for (int i=0; i<NUM_THREADS; i++)76threads[i].start();7778startingGate.arriveAndAwaitAdvance();79for (int i=0; i<NUM_THREADS; i++)80sockets[i].close();8182for (int i=0; i<NUM_THREADS; i++)83threads[i].join();84}8586static abstract class StreamGetter extends Thread {87final Socket socket;88StreamGetter(Socket s) { socket = s; }8990@Override91public void run() {92try {93startingGate.arriveAndAwaitAdvance();94getStream();95} catch (IOException x) {96// OK, socket may be closed97} catch (NullPointerException x) {98x.printStackTrace();99failed = true;100}101}102103abstract void getStream() throws IOException;104}105106static class InputStreamGetter extends StreamGetter {107public InputStreamGetter(Socket s) { super(s); }108void getStream() throws IOException {109socket.getInputStream();110}111}112113static class OutputStreamGetter extends StreamGetter {114public OutputStreamGetter(Socket s) { super(s); }115void getStream() throws IOException {116socket.getOutputStream();117}118}119}120121122