jdk/test/java/nio/channels/Selector/CloseWhenKeyIdle.java
author alanb
Fri, 10 Sep 2010 18:48:49 +0100
changeset 6538 cb0da65ba680
parent 5970 d4e98bbfb0be
child 7668 d4a77089c587
permissions -rw-r--r--
6983794: TEST_BUG: test/java/nio/channels/Selector/ConnectWrite.java failing Reviewed-by: chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/* @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @bug 6403933
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @summary POLLHUP or POLLERR on "idle" key can cause Selector to spin
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.nio.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.nio.channels.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.nio.channels.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
public class CloseWhenKeyIdle {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    // indicates if the wakeup has happened
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    static volatile boolean wakeupDone = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    // Wakes up a Selector after a given delay
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    static class Waker implements Runnable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
        private Selector sel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
        private long delay;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        Waker(Selector sel, long delay) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
            this.sel = sel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
            this.delay = delay;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
                Thread.sleep(delay);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
                wakeupDone = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
                sel.wakeup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
            } catch (Exception x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
                x.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        // Skip test on pre-2.6 kernels until the poll SelectorProvider
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        // is updated
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        String osname = System.getProperty("os.name");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        if (osname.equals("Linux")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            String[] ver = System.getProperty("os.version").split("\\.", 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            if (ver.length >=2 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                int major = Integer.parseInt(ver[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                int minor = Integer.parseInt(ver[1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                if (major < 2 || (major == 2 && minor < 6)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                    System.out.println("Test passing on pre-2.6 kernel");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        // establish loopback connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        ServerSocketChannel ssc = ServerSocketChannel.open();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        ssc.socket().bind(new InetSocketAddress(0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        SocketAddress remote = new InetSocketAddress(InetAddress.getLocalHost(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            ssc.socket().getLocalPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        SocketChannel sc1 = SocketChannel.open(remote);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        SocketChannel sc2 = ssc.accept();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        // register channel for one end with a Selector, interest set = 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        Selector sel = Selector.open();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        sc1.configureBlocking(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        SelectionKey k = sc1.register(sel, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        sel.selectNow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        // hard close to provoke POLLHUP
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        sc2.socket().setSoLinger(true, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        sc2.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        // schedule wakeup after a few seconds
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        Thread t = new Thread(new Waker(sel, 5000));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        t.setDaemon(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        t.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        // select should block
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        int spinCount = 0;
5970
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
   114
        boolean failed = false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            int n = sel.select();
5970
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
   117
            if (n > 0) {
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
   118
                System.err.println("Channel should not be selected!!!");
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
   119
                failed = true;
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
   120
                break;
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
   121
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            // wakeup
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            if (wakeupDone)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            // wakeup for no reason - if it happens a few times then we have a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            // problem
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            spinCount++;
5970
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
   130
            if (spinCount >= 3) {
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
   131
                System.err.println("Selector appears to be spinning");
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
   132
                failed = true;
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
   133
                break;
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
   134
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
5970
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
   137
        sc1.close();
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
   138
        sel.close();
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
   139
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
   140
        if (failed)
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
   141
            throw new RuntimeException("Test failed");
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
   142
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        System.out.println("PASS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
}