jdk/test/java/nio/channels/Selector/TemporarySelector.java
author dfuchs
Wed, 07 Nov 2012 13:24:39 +0100
changeset 14415 7a31b0e0cfaf
parent 6907 932cbb0eed6b
child 42338 a60f280f803c
permissions -rw-r--r--
6720349: (ch) Channels tests depending on hosts inside Sun Summary: This changeset make the nio tests start small TCP or UDP servers from within the tests, instead of relying on external services. Reviewed-by: alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6907
932cbb0eed6b 6974104: TEST: sun/nio/ch/6645197.java should be fixed in 1.5.0u25b05 and jdk6 workspace
coffeys
parents:
diff changeset
     1
/*
932cbb0eed6b 6974104: TEST: sun/nio/ch/6645197.java should be fixed in 1.5.0u25b05 and jdk6 workspace
coffeys
parents:
diff changeset
     2
 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
932cbb0eed6b 6974104: TEST: sun/nio/ch/6645197.java should be fixed in 1.5.0u25b05 and jdk6 workspace
coffeys
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
932cbb0eed6b 6974104: TEST: sun/nio/ch/6645197.java should be fixed in 1.5.0u25b05 and jdk6 workspace
coffeys
parents:
diff changeset
     4
 *
932cbb0eed6b 6974104: TEST: sun/nio/ch/6645197.java should be fixed in 1.5.0u25b05 and jdk6 workspace
coffeys
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
932cbb0eed6b 6974104: TEST: sun/nio/ch/6645197.java should be fixed in 1.5.0u25b05 and jdk6 workspace
coffeys
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
932cbb0eed6b 6974104: TEST: sun/nio/ch/6645197.java should be fixed in 1.5.0u25b05 and jdk6 workspace
coffeys
parents:
diff changeset
     7
 * published by the Free Software Foundation.
932cbb0eed6b 6974104: TEST: sun/nio/ch/6645197.java should be fixed in 1.5.0u25b05 and jdk6 workspace
coffeys
parents:
diff changeset
     8
 *
932cbb0eed6b 6974104: TEST: sun/nio/ch/6645197.java should be fixed in 1.5.0u25b05 and jdk6 workspace
coffeys
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
932cbb0eed6b 6974104: TEST: sun/nio/ch/6645197.java should be fixed in 1.5.0u25b05 and jdk6 workspace
coffeys
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
932cbb0eed6b 6974104: TEST: sun/nio/ch/6645197.java should be fixed in 1.5.0u25b05 and jdk6 workspace
coffeys
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
932cbb0eed6b 6974104: TEST: sun/nio/ch/6645197.java should be fixed in 1.5.0u25b05 and jdk6 workspace
coffeys
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
932cbb0eed6b 6974104: TEST: sun/nio/ch/6645197.java should be fixed in 1.5.0u25b05 and jdk6 workspace
coffeys
parents:
diff changeset
    13
 * accompanied this code).
932cbb0eed6b 6974104: TEST: sun/nio/ch/6645197.java should be fixed in 1.5.0u25b05 and jdk6 workspace
coffeys
parents:
diff changeset
    14
 *
932cbb0eed6b 6974104: TEST: sun/nio/ch/6645197.java should be fixed in 1.5.0u25b05 and jdk6 workspace
coffeys
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
932cbb0eed6b 6974104: TEST: sun/nio/ch/6645197.java should be fixed in 1.5.0u25b05 and jdk6 workspace
coffeys
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
932cbb0eed6b 6974104: TEST: sun/nio/ch/6645197.java should be fixed in 1.5.0u25b05 and jdk6 workspace
coffeys
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
932cbb0eed6b 6974104: TEST: sun/nio/ch/6645197.java should be fixed in 1.5.0u25b05 and jdk6 workspace
coffeys
parents:
diff changeset
    18
 *
932cbb0eed6b 6974104: TEST: sun/nio/ch/6645197.java should be fixed in 1.5.0u25b05 and jdk6 workspace
coffeys
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
932cbb0eed6b 6974104: TEST: sun/nio/ch/6645197.java should be fixed in 1.5.0u25b05 and jdk6 workspace
coffeys
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
932cbb0eed6b 6974104: TEST: sun/nio/ch/6645197.java should be fixed in 1.5.0u25b05 and jdk6 workspace
coffeys
parents:
diff changeset
    21
 * questions.
932cbb0eed6b 6974104: TEST: sun/nio/ch/6645197.java should be fixed in 1.5.0u25b05 and jdk6 workspace
coffeys
parents:
diff changeset
    22
 */
932cbb0eed6b 6974104: TEST: sun/nio/ch/6645197.java should be fixed in 1.5.0u25b05 and jdk6 workspace
coffeys
parents:
diff changeset
    23
932cbb0eed6b 6974104: TEST: sun/nio/ch/6645197.java should be fixed in 1.5.0u25b05 and jdk6 workspace
coffeys
parents:
diff changeset
    24
/* @test
932cbb0eed6b 6974104: TEST: sun/nio/ch/6645197.java should be fixed in 1.5.0u25b05 and jdk6 workspace
coffeys
parents:
diff changeset
    25
 * @bug 6645197
932cbb0eed6b 6974104: TEST: sun/nio/ch/6645197.java should be fixed in 1.5.0u25b05 and jdk6 workspace
coffeys
parents:
diff changeset
    26
 * @run main/othervm -Xmx5m TemporarySelector
932cbb0eed6b 6974104: TEST: sun/nio/ch/6645197.java should be fixed in 1.5.0u25b05 and jdk6 workspace
coffeys
parents:
diff changeset
    27
 * @summary Timed read with socket adaptor throws ClosedSelectorException if temporary selector GC'ed.
932cbb0eed6b 6974104: TEST: sun/nio/ch/6645197.java should be fixed in 1.5.0u25b05 and jdk6 workspace
coffeys
parents:
diff changeset
    28
 */
932cbb0eed6b 6974104: TEST: sun/nio/ch/6645197.java should be fixed in 1.5.0u25b05 and jdk6 workspace
coffeys
parents:
diff changeset
    29
import java.io.IOException;
932cbb0eed6b 6974104: TEST: sun/nio/ch/6645197.java should be fixed in 1.5.0u25b05 and jdk6 workspace
coffeys
parents:
diff changeset
    30
import java.net.InetSocketAddress;
932cbb0eed6b 6974104: TEST: sun/nio/ch/6645197.java should be fixed in 1.5.0u25b05 and jdk6 workspace
coffeys
parents:
diff changeset
    31
import java.net.ServerSocket;
932cbb0eed6b 6974104: TEST: sun/nio/ch/6645197.java should be fixed in 1.5.0u25b05 and jdk6 workspace
coffeys
parents:
diff changeset
    32
import java.net.Socket;
932cbb0eed6b 6974104: TEST: sun/nio/ch/6645197.java should be fixed in 1.5.0u25b05 and jdk6 workspace
coffeys
parents:
diff changeset
    33
import java.nio.channels.ServerSocketChannel;
932cbb0eed6b 6974104: TEST: sun/nio/ch/6645197.java should be fixed in 1.5.0u25b05 and jdk6 workspace
coffeys
parents:
diff changeset
    34
import java.nio.channels.SocketChannel;
932cbb0eed6b 6974104: TEST: sun/nio/ch/6645197.java should be fixed in 1.5.0u25b05 and jdk6 workspace
coffeys
parents:
diff changeset
    35
932cbb0eed6b 6974104: TEST: sun/nio/ch/6645197.java should be fixed in 1.5.0u25b05 and jdk6 workspace
coffeys
parents:
diff changeset
    36
public class TemporarySelector {
932cbb0eed6b 6974104: TEST: sun/nio/ch/6645197.java should be fixed in 1.5.0u25b05 and jdk6 workspace
coffeys
parents:
diff changeset
    37
932cbb0eed6b 6974104: TEST: sun/nio/ch/6645197.java should be fixed in 1.5.0u25b05 and jdk6 workspace
coffeys
parents:
diff changeset
    38
    static volatile boolean done = false;
932cbb0eed6b 6974104: TEST: sun/nio/ch/6645197.java should be fixed in 1.5.0u25b05 and jdk6 workspace
coffeys
parents:
diff changeset
    39
932cbb0eed6b 6974104: TEST: sun/nio/ch/6645197.java should be fixed in 1.5.0u25b05 and jdk6 workspace
coffeys
parents:
diff changeset
    40
    public static void main(String[] args) throws Exception {
932cbb0eed6b 6974104: TEST: sun/nio/ch/6645197.java should be fixed in 1.5.0u25b05 and jdk6 workspace
coffeys
parents:
diff changeset
    41
932cbb0eed6b 6974104: TEST: sun/nio/ch/6645197.java should be fixed in 1.5.0u25b05 and jdk6 workspace
coffeys
parents:
diff changeset
    42
        Runnable r = new Runnable() {
932cbb0eed6b 6974104: TEST: sun/nio/ch/6645197.java should be fixed in 1.5.0u25b05 and jdk6 workspace
coffeys
parents:
diff changeset
    43
            public void run() {
932cbb0eed6b 6974104: TEST: sun/nio/ch/6645197.java should be fixed in 1.5.0u25b05 and jdk6 workspace
coffeys
parents:
diff changeset
    44
                while (!done) {
932cbb0eed6b 6974104: TEST: sun/nio/ch/6645197.java should be fixed in 1.5.0u25b05 and jdk6 workspace
coffeys
parents:
diff changeset
    45
                    System.gc();
932cbb0eed6b 6974104: TEST: sun/nio/ch/6645197.java should be fixed in 1.5.0u25b05 and jdk6 workspace
coffeys
parents:
diff changeset
    46
                    try {
932cbb0eed6b 6974104: TEST: sun/nio/ch/6645197.java should be fixed in 1.5.0u25b05 and jdk6 workspace
coffeys
parents:
diff changeset
    47
                        Thread.sleep(1000);
932cbb0eed6b 6974104: TEST: sun/nio/ch/6645197.java should be fixed in 1.5.0u25b05 and jdk6 workspace
coffeys
parents:
diff changeset
    48
                    } catch (Exception e) {
932cbb0eed6b 6974104: TEST: sun/nio/ch/6645197.java should be fixed in 1.5.0u25b05 and jdk6 workspace
coffeys
parents:
diff changeset
    49
                    }
932cbb0eed6b 6974104: TEST: sun/nio/ch/6645197.java should be fixed in 1.5.0u25b05 and jdk6 workspace
coffeys
parents:
diff changeset
    50
                }
932cbb0eed6b 6974104: TEST: sun/nio/ch/6645197.java should be fixed in 1.5.0u25b05 and jdk6 workspace
coffeys
parents:
diff changeset
    51
            }
932cbb0eed6b 6974104: TEST: sun/nio/ch/6645197.java should be fixed in 1.5.0u25b05 and jdk6 workspace
coffeys
parents:
diff changeset
    52
        };
932cbb0eed6b 6974104: TEST: sun/nio/ch/6645197.java should be fixed in 1.5.0u25b05 and jdk6 workspace
coffeys
parents:
diff changeset
    53
932cbb0eed6b 6974104: TEST: sun/nio/ch/6645197.java should be fixed in 1.5.0u25b05 and jdk6 workspace
coffeys
parents:
diff changeset
    54
        try {
932cbb0eed6b 6974104: TEST: sun/nio/ch/6645197.java should be fixed in 1.5.0u25b05 and jdk6 workspace
coffeys
parents:
diff changeset
    55
            // Create a server socket that will open and accept on loopback connection
932cbb0eed6b 6974104: TEST: sun/nio/ch/6645197.java should be fixed in 1.5.0u25b05 and jdk6 workspace
coffeys
parents:
diff changeset
    56
            ServerSocketChannel ssc =  ServerSocketChannel.open();
932cbb0eed6b 6974104: TEST: sun/nio/ch/6645197.java should be fixed in 1.5.0u25b05 and jdk6 workspace
coffeys
parents:
diff changeset
    57
            final ServerSocket ss =  ssc.socket();
932cbb0eed6b 6974104: TEST: sun/nio/ch/6645197.java should be fixed in 1.5.0u25b05 and jdk6 workspace
coffeys
parents:
diff changeset
    58
            ss.bind(new InetSocketAddress(0));
932cbb0eed6b 6974104: TEST: sun/nio/ch/6645197.java should be fixed in 1.5.0u25b05 and jdk6 workspace
coffeys
parents:
diff changeset
    59
            int localPort = ss.getLocalPort();
932cbb0eed6b 6974104: TEST: sun/nio/ch/6645197.java should be fixed in 1.5.0u25b05 and jdk6 workspace
coffeys
parents:
diff changeset
    60
932cbb0eed6b 6974104: TEST: sun/nio/ch/6645197.java should be fixed in 1.5.0u25b05 and jdk6 workspace
coffeys
parents:
diff changeset
    61
            // Create a client socket that will connect and read
932cbb0eed6b 6974104: TEST: sun/nio/ch/6645197.java should be fixed in 1.5.0u25b05 and jdk6 workspace
coffeys
parents:
diff changeset
    62
            System.out.println("Connecting to server socket");
932cbb0eed6b 6974104: TEST: sun/nio/ch/6645197.java should be fixed in 1.5.0u25b05 and jdk6 workspace
coffeys
parents:
diff changeset
    63
            System.out.flush();
932cbb0eed6b 6974104: TEST: sun/nio/ch/6645197.java should be fixed in 1.5.0u25b05 and jdk6 workspace
coffeys
parents:
diff changeset
    64
            SocketChannel channel = SocketChannel.open(new InetSocketAddress("localhost", localPort));
932cbb0eed6b 6974104: TEST: sun/nio/ch/6645197.java should be fixed in 1.5.0u25b05 and jdk6 workspace
coffeys
parents:
diff changeset
    65
            System.out.println("Connected to server socket");
932cbb0eed6b 6974104: TEST: sun/nio/ch/6645197.java should be fixed in 1.5.0u25b05 and jdk6 workspace
coffeys
parents:
diff changeset
    66
            System.out.flush();
932cbb0eed6b 6974104: TEST: sun/nio/ch/6645197.java should be fixed in 1.5.0u25b05 and jdk6 workspace
coffeys
parents:
diff changeset
    67
932cbb0eed6b 6974104: TEST: sun/nio/ch/6645197.java should be fixed in 1.5.0u25b05 and jdk6 workspace
coffeys
parents:
diff changeset
    68
            // Create a thread to try and cause the GC to run
932cbb0eed6b 6974104: TEST: sun/nio/ch/6645197.java should be fixed in 1.5.0u25b05 and jdk6 workspace
coffeys
parents:
diff changeset
    69
            Thread t = new Thread(r);
932cbb0eed6b 6974104: TEST: sun/nio/ch/6645197.java should be fixed in 1.5.0u25b05 and jdk6 workspace
coffeys
parents:
diff changeset
    70
            t.start();
932cbb0eed6b 6974104: TEST: sun/nio/ch/6645197.java should be fixed in 1.5.0u25b05 and jdk6 workspace
coffeys
parents:
diff changeset
    71
            byte[] buffer = new byte[500];
932cbb0eed6b 6974104: TEST: sun/nio/ch/6645197.java should be fixed in 1.5.0u25b05 and jdk6 workspace
coffeys
parents:
diff changeset
    72
            System.out.println("Reading from socket input stream");
932cbb0eed6b 6974104: TEST: sun/nio/ch/6645197.java should be fixed in 1.5.0u25b05 and jdk6 workspace
coffeys
parents:
diff changeset
    73
            System.out.flush();
932cbb0eed6b 6974104: TEST: sun/nio/ch/6645197.java should be fixed in 1.5.0u25b05 and jdk6 workspace
coffeys
parents:
diff changeset
    74
            Socket socket = channel.socket();
932cbb0eed6b 6974104: TEST: sun/nio/ch/6645197.java should be fixed in 1.5.0u25b05 and jdk6 workspace
coffeys
parents:
diff changeset
    75
            socket.setSoTimeout(10000);  // The timeout must be set
932cbb0eed6b 6974104: TEST: sun/nio/ch/6645197.java should be fixed in 1.5.0u25b05 and jdk6 workspace
coffeys
parents:
diff changeset
    76
            // to trigger this bug
932cbb0eed6b 6974104: TEST: sun/nio/ch/6645197.java should be fixed in 1.5.0u25b05 and jdk6 workspace
coffeys
parents:
diff changeset
    77
            try {
932cbb0eed6b 6974104: TEST: sun/nio/ch/6645197.java should be fixed in 1.5.0u25b05 and jdk6 workspace
coffeys
parents:
diff changeset
    78
                socket.getInputStream().read(buffer);
932cbb0eed6b 6974104: TEST: sun/nio/ch/6645197.java should be fixed in 1.5.0u25b05 and jdk6 workspace
coffeys
parents:
diff changeset
    79
            } catch (java.net.SocketTimeoutException ste) {
932cbb0eed6b 6974104: TEST: sun/nio/ch/6645197.java should be fixed in 1.5.0u25b05 and jdk6 workspace
coffeys
parents:
diff changeset
    80
                // no java.nio.channels.ClosedSelectorException
932cbb0eed6b 6974104: TEST: sun/nio/ch/6645197.java should be fixed in 1.5.0u25b05 and jdk6 workspace
coffeys
parents:
diff changeset
    81
            }
932cbb0eed6b 6974104: TEST: sun/nio/ch/6645197.java should be fixed in 1.5.0u25b05 and jdk6 workspace
coffeys
parents:
diff changeset
    82
        } finally {
932cbb0eed6b 6974104: TEST: sun/nio/ch/6645197.java should be fixed in 1.5.0u25b05 and jdk6 workspace
coffeys
parents:
diff changeset
    83
            done = true;
932cbb0eed6b 6974104: TEST: sun/nio/ch/6645197.java should be fixed in 1.5.0u25b05 and jdk6 workspace
coffeys
parents:
diff changeset
    84
        }
932cbb0eed6b 6974104: TEST: sun/nio/ch/6645197.java should be fixed in 1.5.0u25b05 and jdk6 workspace
coffeys
parents:
diff changeset
    85
    }
932cbb0eed6b 6974104: TEST: sun/nio/ch/6645197.java should be fixed in 1.5.0u25b05 and jdk6 workspace
coffeys
parents:
diff changeset
    86
}