test/jdk/java/net/Socket/Streams.java
author chegar
Sun, 05 Nov 2017 17:32:13 +0000
branchhttp-client-branch
changeset 55763 634d8e14c172
parent 47216 71c04702a3d5
child 54787 7748aa47b4e2
permissions -rw-r--r--
http-client-branch: intial load from jdk10/sandbox
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14671
22fad096737b 8003833: Spurious NPE from Socket.getIn/OutputStream
chegar
parents:
diff changeset
     1
/*
22fad096737b 8003833: Spurious NPE from Socket.getIn/OutputStream
chegar
parents:
diff changeset
     2
 * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
22fad096737b 8003833: Spurious NPE from Socket.getIn/OutputStream
chegar
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
22fad096737b 8003833: Spurious NPE from Socket.getIn/OutputStream
chegar
parents:
diff changeset
     4
 *
22fad096737b 8003833: Spurious NPE from Socket.getIn/OutputStream
chegar
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
22fad096737b 8003833: Spurious NPE from Socket.getIn/OutputStream
chegar
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
22fad096737b 8003833: Spurious NPE from Socket.getIn/OutputStream
chegar
parents:
diff changeset
     7
 * published by the Free Software Foundation.
22fad096737b 8003833: Spurious NPE from Socket.getIn/OutputStream
chegar
parents:
diff changeset
     8
 *
22fad096737b 8003833: Spurious NPE from Socket.getIn/OutputStream
chegar
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
22fad096737b 8003833: Spurious NPE from Socket.getIn/OutputStream
chegar
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
22fad096737b 8003833: Spurious NPE from Socket.getIn/OutputStream
chegar
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
22fad096737b 8003833: Spurious NPE from Socket.getIn/OutputStream
chegar
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
22fad096737b 8003833: Spurious NPE from Socket.getIn/OutputStream
chegar
parents:
diff changeset
    13
 * accompanied this code).
22fad096737b 8003833: Spurious NPE from Socket.getIn/OutputStream
chegar
parents:
diff changeset
    14
 *
22fad096737b 8003833: Spurious NPE from Socket.getIn/OutputStream
chegar
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
22fad096737b 8003833: Spurious NPE from Socket.getIn/OutputStream
chegar
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
22fad096737b 8003833: Spurious NPE from Socket.getIn/OutputStream
chegar
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
22fad096737b 8003833: Spurious NPE from Socket.getIn/OutputStream
chegar
parents:
diff changeset
    18
 *
22fad096737b 8003833: Spurious NPE from Socket.getIn/OutputStream
chegar
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22fad096737b 8003833: Spurious NPE from Socket.getIn/OutputStream
chegar
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
22fad096737b 8003833: Spurious NPE from Socket.getIn/OutputStream
chegar
parents:
diff changeset
    21
 * questions.
22fad096737b 8003833: Spurious NPE from Socket.getIn/OutputStream
chegar
parents:
diff changeset
    22
 */
22fad096737b 8003833: Spurious NPE from Socket.getIn/OutputStream
chegar
parents:
diff changeset
    23
22fad096737b 8003833: Spurious NPE from Socket.getIn/OutputStream
chegar
parents:
diff changeset
    24
/*
22fad096737b 8003833: Spurious NPE from Socket.getIn/OutputStream
chegar
parents:
diff changeset
    25
 * @test
22fad096737b 8003833: Spurious NPE from Socket.getIn/OutputStream
chegar
parents:
diff changeset
    26
 * @bug 8003833
22fad096737b 8003833: Spurious NPE from Socket.getIn/OutputStream
chegar
parents:
diff changeset
    27
 * @summary Spurious NPE from Socket.getIn/OutputStream
22fad096737b 8003833: Spurious NPE from Socket.getIn/OutputStream
chegar
parents:
diff changeset
    28
 */
22fad096737b 8003833: Spurious NPE from Socket.getIn/OutputStream
chegar
parents:
diff changeset
    29
22fad096737b 8003833: Spurious NPE from Socket.getIn/OutputStream
chegar
parents:
diff changeset
    30
import java.io.IOException;
22fad096737b 8003833: Spurious NPE from Socket.getIn/OutputStream
chegar
parents:
diff changeset
    31
import java.lang.reflect.Constructor;
22fad096737b 8003833: Spurious NPE from Socket.getIn/OutputStream
chegar
parents:
diff changeset
    32
import java.net.ServerSocket;
22fad096737b 8003833: Spurious NPE from Socket.getIn/OutputStream
chegar
parents:
diff changeset
    33
import java.net.Socket;
22fad096737b 8003833: Spurious NPE from Socket.getIn/OutputStream
chegar
parents:
diff changeset
    34
import java.util.concurrent.Phaser;
22fad096737b 8003833: Spurious NPE from Socket.getIn/OutputStream
chegar
parents:
diff changeset
    35
22fad096737b 8003833: Spurious NPE from Socket.getIn/OutputStream
chegar
parents:
diff changeset
    36
// Racey test, will not always fail, but if it does then there is a problem.
22fad096737b 8003833: Spurious NPE from Socket.getIn/OutputStream
chegar
parents:
diff changeset
    37
22fad096737b 8003833: Spurious NPE from Socket.getIn/OutputStream
chegar
parents:
diff changeset
    38
public class Streams {
22fad096737b 8003833: Spurious NPE from Socket.getIn/OutputStream
chegar
parents:
diff changeset
    39
    static final int NUM_THREADS = 100;
22fad096737b 8003833: Spurious NPE from Socket.getIn/OutputStream
chegar
parents:
diff changeset
    40
    static volatile boolean failed;
22fad096737b 8003833: Spurious NPE from Socket.getIn/OutputStream
chegar
parents:
diff changeset
    41
    static final Phaser startingGate = new Phaser(NUM_THREADS + 1);
22fad096737b 8003833: Spurious NPE from Socket.getIn/OutputStream
chegar
parents:
diff changeset
    42
22fad096737b 8003833: Spurious NPE from Socket.getIn/OutputStream
chegar
parents:
diff changeset
    43
    public static void main(String[] args) throws Exception {
22fad096737b 8003833: Spurious NPE from Socket.getIn/OutputStream
chegar
parents:
diff changeset
    44
22fad096737b 8003833: Spurious NPE from Socket.getIn/OutputStream
chegar
parents:
diff changeset
    45
        try (ServerSocket ss = new ServerSocket(0)) {
22fad096737b 8003833: Spurious NPE from Socket.getIn/OutputStream
chegar
parents:
diff changeset
    46
            runTest(OutputStreamGetter.class, ss);
22fad096737b 8003833: Spurious NPE from Socket.getIn/OutputStream
chegar
parents:
diff changeset
    47
            runTest(InputStreamGetter.class, ss);
22fad096737b 8003833: Spurious NPE from Socket.getIn/OutputStream
chegar
parents:
diff changeset
    48
        }
22fad096737b 8003833: Spurious NPE from Socket.getIn/OutputStream
chegar
parents:
diff changeset
    49
22fad096737b 8003833: Spurious NPE from Socket.getIn/OutputStream
chegar
parents:
diff changeset
    50
        if (failed)
22fad096737b 8003833: Spurious NPE from Socket.getIn/OutputStream
chegar
parents:
diff changeset
    51
            throw new RuntimeException("Failed, check output");
22fad096737b 8003833: Spurious NPE from Socket.getIn/OutputStream
chegar
parents:
diff changeset
    52
    }
22fad096737b 8003833: Spurious NPE from Socket.getIn/OutputStream
chegar
parents:
diff changeset
    53
22fad096737b 8003833: Spurious NPE from Socket.getIn/OutputStream
chegar
parents:
diff changeset
    54
    static void runTest(Class<? extends StreamGetter> klass, ServerSocket ss)
22fad096737b 8003833: Spurious NPE from Socket.getIn/OutputStream
chegar
parents:
diff changeset
    55
        throws Exception
22fad096737b 8003833: Spurious NPE from Socket.getIn/OutputStream
chegar
parents:
diff changeset
    56
    {
22fad096737b 8003833: Spurious NPE from Socket.getIn/OutputStream
chegar
parents:
diff changeset
    57
        final int port = ss.getLocalPort();
22fad096737b 8003833: Spurious NPE from Socket.getIn/OutputStream
chegar
parents:
diff changeset
    58
        Socket[] sockets = new Socket[NUM_THREADS];
22fad096737b 8003833: Spurious NPE from Socket.getIn/OutputStream
chegar
parents:
diff changeset
    59
        for (int i=0; i<NUM_THREADS; i++) {
22fad096737b 8003833: Spurious NPE from Socket.getIn/OutputStream
chegar
parents:
diff changeset
    60
            sockets[i] = new Socket("localhost", port);
22fad096737b 8003833: Spurious NPE from Socket.getIn/OutputStream
chegar
parents:
diff changeset
    61
            try (Socket socket = ss.accept()) {}
22fad096737b 8003833: Spurious NPE from Socket.getIn/OutputStream
chegar
parents:
diff changeset
    62
        }
22fad096737b 8003833: Spurious NPE from Socket.getIn/OutputStream
chegar
parents:
diff changeset
    63
22fad096737b 8003833: Spurious NPE from Socket.getIn/OutputStream
chegar
parents:
diff changeset
    64
        Constructor<? extends StreamGetter> ctr = klass.getConstructor(Socket.class);
22fad096737b 8003833: Spurious NPE from Socket.getIn/OutputStream
chegar
parents:
diff changeset
    65
22fad096737b 8003833: Spurious NPE from Socket.getIn/OutputStream
chegar
parents:
diff changeset
    66
        Thread[] threads = new Thread[NUM_THREADS];
22fad096737b 8003833: Spurious NPE from Socket.getIn/OutputStream
chegar
parents:
diff changeset
    67
        for (int i=0; i<NUM_THREADS; i++)
22fad096737b 8003833: Spurious NPE from Socket.getIn/OutputStream
chegar
parents:
diff changeset
    68
            threads[i] = ctr.newInstance(sockets[i]);
22fad096737b 8003833: Spurious NPE from Socket.getIn/OutputStream
chegar
parents:
diff changeset
    69
        for (int i=0; i<NUM_THREADS; i++)
22fad096737b 8003833: Spurious NPE from Socket.getIn/OutputStream
chegar
parents:
diff changeset
    70
            threads[i].start();
22fad096737b 8003833: Spurious NPE from Socket.getIn/OutputStream
chegar
parents:
diff changeset
    71
22fad096737b 8003833: Spurious NPE from Socket.getIn/OutputStream
chegar
parents:
diff changeset
    72
        startingGate.arriveAndAwaitAdvance();
22fad096737b 8003833: Spurious NPE from Socket.getIn/OutputStream
chegar
parents:
diff changeset
    73
        for (int i=0; i<NUM_THREADS; i++)
22fad096737b 8003833: Spurious NPE from Socket.getIn/OutputStream
chegar
parents:
diff changeset
    74
            sockets[i].close();
22fad096737b 8003833: Spurious NPE from Socket.getIn/OutputStream
chegar
parents:
diff changeset
    75
22fad096737b 8003833: Spurious NPE from Socket.getIn/OutputStream
chegar
parents:
diff changeset
    76
        for (int i=0; i<NUM_THREADS; i++)
22fad096737b 8003833: Spurious NPE from Socket.getIn/OutputStream
chegar
parents:
diff changeset
    77
            threads[i].join();
22fad096737b 8003833: Spurious NPE from Socket.getIn/OutputStream
chegar
parents:
diff changeset
    78
    }
22fad096737b 8003833: Spurious NPE from Socket.getIn/OutputStream
chegar
parents:
diff changeset
    79
22fad096737b 8003833: Spurious NPE from Socket.getIn/OutputStream
chegar
parents:
diff changeset
    80
    static abstract class StreamGetter extends Thread {
22fad096737b 8003833: Spurious NPE from Socket.getIn/OutputStream
chegar
parents:
diff changeset
    81
        final Socket socket;
22fad096737b 8003833: Spurious NPE from Socket.getIn/OutputStream
chegar
parents:
diff changeset
    82
        StreamGetter(Socket s) { socket = s; }
22fad096737b 8003833: Spurious NPE from Socket.getIn/OutputStream
chegar
parents:
diff changeset
    83
22fad096737b 8003833: Spurious NPE from Socket.getIn/OutputStream
chegar
parents:
diff changeset
    84
        @Override
22fad096737b 8003833: Spurious NPE from Socket.getIn/OutputStream
chegar
parents:
diff changeset
    85
        public void run() {
22fad096737b 8003833: Spurious NPE from Socket.getIn/OutputStream
chegar
parents:
diff changeset
    86
            try {
22fad096737b 8003833: Spurious NPE from Socket.getIn/OutputStream
chegar
parents:
diff changeset
    87
                startingGate.arriveAndAwaitAdvance();
22fad096737b 8003833: Spurious NPE from Socket.getIn/OutputStream
chegar
parents:
diff changeset
    88
                getStream();
22fad096737b 8003833: Spurious NPE from Socket.getIn/OutputStream
chegar
parents:
diff changeset
    89
            } catch (IOException x) {
22fad096737b 8003833: Spurious NPE from Socket.getIn/OutputStream
chegar
parents:
diff changeset
    90
                // OK, socket may be closed
22fad096737b 8003833: Spurious NPE from Socket.getIn/OutputStream
chegar
parents:
diff changeset
    91
            } catch (NullPointerException x) {
22fad096737b 8003833: Spurious NPE from Socket.getIn/OutputStream
chegar
parents:
diff changeset
    92
                x.printStackTrace();
22fad096737b 8003833: Spurious NPE from Socket.getIn/OutputStream
chegar
parents:
diff changeset
    93
                failed = true;
22fad096737b 8003833: Spurious NPE from Socket.getIn/OutputStream
chegar
parents:
diff changeset
    94
            }
22fad096737b 8003833: Spurious NPE from Socket.getIn/OutputStream
chegar
parents:
diff changeset
    95
        }
22fad096737b 8003833: Spurious NPE from Socket.getIn/OutputStream
chegar
parents:
diff changeset
    96
22fad096737b 8003833: Spurious NPE from Socket.getIn/OutputStream
chegar
parents:
diff changeset
    97
        abstract void getStream() throws IOException;
22fad096737b 8003833: Spurious NPE from Socket.getIn/OutputStream
chegar
parents:
diff changeset
    98
    }
22fad096737b 8003833: Spurious NPE from Socket.getIn/OutputStream
chegar
parents:
diff changeset
    99
22fad096737b 8003833: Spurious NPE from Socket.getIn/OutputStream
chegar
parents:
diff changeset
   100
    static class InputStreamGetter extends StreamGetter {
22fad096737b 8003833: Spurious NPE from Socket.getIn/OutputStream
chegar
parents:
diff changeset
   101
        public InputStreamGetter(Socket s) { super(s); }
22fad096737b 8003833: Spurious NPE from Socket.getIn/OutputStream
chegar
parents:
diff changeset
   102
        void getStream() throws IOException {
22fad096737b 8003833: Spurious NPE from Socket.getIn/OutputStream
chegar
parents:
diff changeset
   103
            socket.getInputStream();
22fad096737b 8003833: Spurious NPE from Socket.getIn/OutputStream
chegar
parents:
diff changeset
   104
        }
22fad096737b 8003833: Spurious NPE from Socket.getIn/OutputStream
chegar
parents:
diff changeset
   105
    }
22fad096737b 8003833: Spurious NPE from Socket.getIn/OutputStream
chegar
parents:
diff changeset
   106
22fad096737b 8003833: Spurious NPE from Socket.getIn/OutputStream
chegar
parents:
diff changeset
   107
    static class OutputStreamGetter extends StreamGetter {
22fad096737b 8003833: Spurious NPE from Socket.getIn/OutputStream
chegar
parents:
diff changeset
   108
        public OutputStreamGetter(Socket s) { super(s); }
22fad096737b 8003833: Spurious NPE from Socket.getIn/OutputStream
chegar
parents:
diff changeset
   109
        void getStream() throws IOException {
22fad096737b 8003833: Spurious NPE from Socket.getIn/OutputStream
chegar
parents:
diff changeset
   110
            socket.getOutputStream();
22fad096737b 8003833: Spurious NPE from Socket.getIn/OutputStream
chegar
parents:
diff changeset
   111
        }
22fad096737b 8003833: Spurious NPE from Socket.getIn/OutputStream
chegar
parents:
diff changeset
   112
    }
22fad096737b 8003833: Spurious NPE from Socket.getIn/OutputStream
chegar
parents:
diff changeset
   113
}