jdk/test/java/nio/channels/etc/AdaptorCloseAndInterrupt.java
author bpb
Fri, 07 Apr 2017 11:31:57 -0700
changeset 44548 632ed240ed22
parent 36678 4a684dceb848
permissions -rw-r--r--
8178074: (ch) java/nio/channels/etc/AdaptorCloseAndInterrupt.java: add instrumentation Summary: Add some print statements to indicate state at strategic points Reviewed-by: chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14025
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
     1
/*
44548
632ed240ed22 8178074: (ch) java/nio/channels/etc/AdaptorCloseAndInterrupt.java: add instrumentation
bpb
parents: 36678
diff changeset
     2
 * Copyright (c) 2012, 2017, Oracle and/or its affiliates. All rights reserved.
14025
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
     4
 *
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
     7
 * published by the Free Software Foundation.
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
     8
 *
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
    13
 * accompanied this code).
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
    14
 *
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
    18
 *
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
    21
 * questions.
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
    22
 */
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
    23
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
    24
/* @test
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
    25
 * @bug 7184932
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
    26
 * @summary Test asynchronous close and interrupt of timed socket adapter methods
36678
4a684dceb848 8152749: Mark AdaptorCloseAndInterrupt.java as intermittently failing
amlu
parents: 30046
diff changeset
    27
 * @key randomness intermittent
14025
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
    28
 */
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
    29
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
    30
import java.io.*;
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
    31
import java.nio.*;
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
    32
import java.nio.channels.*;
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
    33
import java.nio.channels.spi.AbstractSelectableChannel;
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
    34
import java.net.*;
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
    35
import java.util.concurrent.Callable;
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
    36
import java.util.concurrent.Executors;
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
    37
import java.util.concurrent.ScheduledExecutorService;
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
    38
import java.util.concurrent.TimeUnit;
44548
632ed240ed22 8178074: (ch) java/nio/channels/etc/AdaptorCloseAndInterrupt.java: add instrumentation
bpb
parents: 36678
diff changeset
    39
import java.util.concurrent.atomic.AtomicBoolean;
14025
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
    40
import java.util.Random;
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
    41
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
    42
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
    43
public class AdaptorCloseAndInterrupt {
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
    44
    private static final ScheduledExecutorService pool =
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
    45
        Executors.newScheduledThreadPool(1);
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
    46
    final ServerSocketChannel listener;
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
    47
    final DatagramChannel peer;
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
    48
    final int port;
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
    49
44548
632ed240ed22 8178074: (ch) java/nio/channels/etc/AdaptorCloseAndInterrupt.java: add instrumentation
bpb
parents: 36678
diff changeset
    50
    final AtomicBoolean isClosed = new AtomicBoolean();
632ed240ed22 8178074: (ch) java/nio/channels/etc/AdaptorCloseAndInterrupt.java: add instrumentation
bpb
parents: 36678
diff changeset
    51
    final AtomicBoolean isInterrupted = new AtomicBoolean();
632ed240ed22 8178074: (ch) java/nio/channels/etc/AdaptorCloseAndInterrupt.java: add instrumentation
bpb
parents: 36678
diff changeset
    52
14025
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
    53
    public AdaptorCloseAndInterrupt() {
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
    54
        listener = null;
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
    55
        peer = null;
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
    56
        port = -1;
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
    57
    }
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
    58
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
    59
    public AdaptorCloseAndInterrupt(ServerSocketChannel listener) {
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
    60
        this.listener = listener;
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
    61
        this.port = listener.socket().getLocalPort();
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
    62
        this.peer = null;
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
    63
    }
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
    64
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
    65
    public AdaptorCloseAndInterrupt(DatagramChannel listener) {
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
    66
        this.peer = listener;
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
    67
        this.port = peer.socket().getLocalPort();
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
    68
        this.listener = null;
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
    69
    }
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
    70
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
    71
    public static void main(String args[]) throws Exception {
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
    72
        try {
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
    73
            try (ServerSocketChannel listener = ServerSocketChannel.open()) {
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
    74
                listener.socket().bind(null);
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
    75
                new AdaptorCloseAndInterrupt(listener).scReadAsyncClose();
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
    76
                new AdaptorCloseAndInterrupt(listener).scReadAsyncInterrupt();
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
    77
            }
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
    78
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
    79
            try (DatagramChannel peer = DatagramChannel.open()) {
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
    80
                peer.socket().bind(null);
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
    81
                new AdaptorCloseAndInterrupt(peer).dcReceiveAsyncClose();
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
    82
                new AdaptorCloseAndInterrupt(peer).dcReceiveAsyncInterrupt();
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
    83
            }
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
    84
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
    85
            new AdaptorCloseAndInterrupt().ssAcceptAsyncClose();
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
    86
            new AdaptorCloseAndInterrupt().ssAcceptAsyncInterrupt();
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
    87
        } finally {
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
    88
            pool.shutdown();
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
    89
        }
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
    90
        System.out.println("Test Passed");
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
    91
    }
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
    92
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
    93
    void scReadAsyncClose() throws IOException {
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
    94
        try {
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
    95
            SocketChannel sc = SocketChannel.open(new InetSocketAddress(
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
    96
                "127.0.0.1", port));
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
    97
            sc.socket().setSoTimeout(30*1000);
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
    98
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
    99
            doAsyncClose(sc);
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
   100
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
   101
            try {
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
   102
                sc.socket().getInputStream().read(new byte[100]);
44548
632ed240ed22 8178074: (ch) java/nio/channels/etc/AdaptorCloseAndInterrupt.java: add instrumentation
bpb
parents: 36678
diff changeset
   103
                System.err.format("close() was invoked: %s%n", isClosed.get());
14025
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
   104
                throw new RuntimeException("read should not have completed");
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
   105
            } catch (ClosedChannelException expected) {}
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
   106
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
   107
            if (!sc.socket().isClosed())
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
   108
                throw new RuntimeException("socket is not closed");
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
   109
        } finally {
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
   110
            // accept connection and close it.
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
   111
            listener.accept().close();
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
   112
        }
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
   113
    }
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
   114
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
   115
    void scReadAsyncInterrupt() throws IOException {
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
   116
        try {
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
   117
            final SocketChannel sc = SocketChannel.open(new InetSocketAddress(
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
   118
                "127.0.0.1", port));
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
   119
            sc.socket().setSoTimeout(30*1000);
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
   120
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
   121
            doAsyncInterrupt();
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
   122
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
   123
            try {
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
   124
                sc.socket().getInputStream().read(new byte[100]);
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
   125
                throw new RuntimeException("read should not have completed");
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
   126
            } catch (ClosedByInterruptException expected) {
44548
632ed240ed22 8178074: (ch) java/nio/channels/etc/AdaptorCloseAndInterrupt.java: add instrumentation
bpb
parents: 36678
diff changeset
   127
                System.out.format("interrupt() was invoked: %s%n",
632ed240ed22 8178074: (ch) java/nio/channels/etc/AdaptorCloseAndInterrupt.java: add instrumentation
bpb
parents: 36678
diff changeset
   128
                    isInterrupted.get());
632ed240ed22 8178074: (ch) java/nio/channels/etc/AdaptorCloseAndInterrupt.java: add instrumentation
bpb
parents: 36678
diff changeset
   129
                System.out.format("scReadAsyncInterrupt was interrupted: %s%n",
632ed240ed22 8178074: (ch) java/nio/channels/etc/AdaptorCloseAndInterrupt.java: add instrumentation
bpb
parents: 36678
diff changeset
   130
                    Thread.currentThread().interrupted());
14025
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
   131
            }
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
   132
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
   133
            if (!sc.socket().isClosed())
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
   134
                throw new RuntimeException("socket is not closed");
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
   135
        } finally {
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
   136
            // accept connection and close it.
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
   137
            listener.accept().close();
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
   138
        }
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
   139
    }
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
   140
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
   141
    void dcReceiveAsyncClose() throws IOException {
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
   142
        DatagramChannel dc = DatagramChannel.open();
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
   143
        dc.connect(new InetSocketAddress(
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
   144
            "127.0.0.1", port));
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
   145
        dc.socket().setSoTimeout(30*1000);
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
   146
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
   147
        doAsyncClose(dc);
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
   148
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
   149
        try {
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
   150
            dc.socket().receive(new DatagramPacket(new byte[100], 100));
44548
632ed240ed22 8178074: (ch) java/nio/channels/etc/AdaptorCloseAndInterrupt.java: add instrumentation
bpb
parents: 36678
diff changeset
   151
            System.err.format("close() was invoked: %s%n", isClosed.get());
14025
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
   152
            throw new RuntimeException("receive should not have completed");
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
   153
        } catch (ClosedChannelException expected) {}
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
   154
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
   155
        if (!dc.socket().isClosed())
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
   156
            throw new RuntimeException("socket is not closed");
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
   157
    }
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
   158
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
   159
    void dcReceiveAsyncInterrupt() throws IOException {
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
   160
        DatagramChannel dc = DatagramChannel.open();
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
   161
        dc.connect(new InetSocketAddress(
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
   162
            "127.0.0.1", port));
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
   163
        dc.socket().setSoTimeout(30*1000);
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
   164
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
   165
        doAsyncInterrupt();
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
   166
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
   167
        try {
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
   168
            dc.socket().receive(new DatagramPacket(new byte[100], 100));
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
   169
            throw new RuntimeException("receive should not have completed");
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
   170
        } catch (ClosedByInterruptException expected) {
44548
632ed240ed22 8178074: (ch) java/nio/channels/etc/AdaptorCloseAndInterrupt.java: add instrumentation
bpb
parents: 36678
diff changeset
   171
            System.out.format("interrupt() was invoked: %s%n",
632ed240ed22 8178074: (ch) java/nio/channels/etc/AdaptorCloseAndInterrupt.java: add instrumentation
bpb
parents: 36678
diff changeset
   172
                isInterrupted.get());
632ed240ed22 8178074: (ch) java/nio/channels/etc/AdaptorCloseAndInterrupt.java: add instrumentation
bpb
parents: 36678
diff changeset
   173
            System.out.format("dcReceiveAsyncInterrupt was interrupted: %s%n",
632ed240ed22 8178074: (ch) java/nio/channels/etc/AdaptorCloseAndInterrupt.java: add instrumentation
bpb
parents: 36678
diff changeset
   174
                Thread.currentThread().interrupted());
632ed240ed22 8178074: (ch) java/nio/channels/etc/AdaptorCloseAndInterrupt.java: add instrumentation
bpb
parents: 36678
diff changeset
   175
        } catch (SocketTimeoutException unexpected) {
632ed240ed22 8178074: (ch) java/nio/channels/etc/AdaptorCloseAndInterrupt.java: add instrumentation
bpb
parents: 36678
diff changeset
   176
            System.err.format("Receive thread interrupt invoked: %s%n",
632ed240ed22 8178074: (ch) java/nio/channels/etc/AdaptorCloseAndInterrupt.java: add instrumentation
bpb
parents: 36678
diff changeset
   177
                isInterrupted.get());
632ed240ed22 8178074: (ch) java/nio/channels/etc/AdaptorCloseAndInterrupt.java: add instrumentation
bpb
parents: 36678
diff changeset
   178
            System.err.format("Receive thread was interrupted: %s%n",
632ed240ed22 8178074: (ch) java/nio/channels/etc/AdaptorCloseAndInterrupt.java: add instrumentation
bpb
parents: 36678
diff changeset
   179
                Thread.currentThread().isInterrupted());
632ed240ed22 8178074: (ch) java/nio/channels/etc/AdaptorCloseAndInterrupt.java: add instrumentation
bpb
parents: 36678
diff changeset
   180
            throw unexpected;
14025
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
   181
        }
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
   182
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
   183
        if (!dc.socket().isClosed())
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
   184
            throw new RuntimeException("socket is not closed");
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
   185
    }
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
   186
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
   187
    void ssAcceptAsyncClose() throws IOException {
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
   188
        ServerSocketChannel ssc = ServerSocketChannel.open();
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
   189
        ssc.socket().bind(null);
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
   190
        ssc.socket().setSoTimeout(30*1000);
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
   191
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
   192
        doAsyncClose(ssc);
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
   193
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
   194
        try {
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
   195
            ssc.socket().accept();
44548
632ed240ed22 8178074: (ch) java/nio/channels/etc/AdaptorCloseAndInterrupt.java: add instrumentation
bpb
parents: 36678
diff changeset
   196
            System.err.format("close() was invoked: %s%n", isClosed.get());
14025
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
   197
            throw new RuntimeException("accept should not have completed");
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
   198
        } catch (ClosedChannelException expected) {}
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
   199
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
   200
        if (!ssc.socket().isClosed())
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
   201
            throw new RuntimeException("socket is not closed");
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
   202
    }
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
   203
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
   204
    void ssAcceptAsyncInterrupt() throws IOException {
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
   205
        ServerSocketChannel ssc = ServerSocketChannel.open();
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
   206
        ssc.socket().bind(null);
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
   207
        ssc.socket().setSoTimeout(30*1000);
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
   208
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
   209
        doAsyncInterrupt();
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
   210
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
   211
        try {
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
   212
            ssc.socket().accept();
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
   213
            throw new RuntimeException("accept should not have completed");
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
   214
        } catch (ClosedByInterruptException expected) {
44548
632ed240ed22 8178074: (ch) java/nio/channels/etc/AdaptorCloseAndInterrupt.java: add instrumentation
bpb
parents: 36678
diff changeset
   215
            System.out.format("interrupt() was invoked: %s%n",
632ed240ed22 8178074: (ch) java/nio/channels/etc/AdaptorCloseAndInterrupt.java: add instrumentation
bpb
parents: 36678
diff changeset
   216
                isInterrupted.get());
632ed240ed22 8178074: (ch) java/nio/channels/etc/AdaptorCloseAndInterrupt.java: add instrumentation
bpb
parents: 36678
diff changeset
   217
            System.out.format("ssAcceptAsyncInterrupt was interrupted: %s%n",
632ed240ed22 8178074: (ch) java/nio/channels/etc/AdaptorCloseAndInterrupt.java: add instrumentation
bpb
parents: 36678
diff changeset
   218
                Thread.currentThread().interrupted());
14025
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
   219
        }
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
   220
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
   221
        if (!ssc.socket().isClosed())
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
   222
            throw new RuntimeException("socket is not closed");
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
   223
    }
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
   224
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
   225
    void doAsyncClose(final AbstractSelectableChannel sc) {
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
   226
        AdaptorCloseAndInterrupt.pool.schedule(new Callable<Void>() {
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
   227
            public Void call() throws Exception {
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
   228
                sc.close();
44548
632ed240ed22 8178074: (ch) java/nio/channels/etc/AdaptorCloseAndInterrupt.java: add instrumentation
bpb
parents: 36678
diff changeset
   229
                isClosed.set(true);
14025
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
   230
                return null;
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
   231
            }
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
   232
        }, new Random().nextInt(1000), TimeUnit.MILLISECONDS);
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
   233
    }
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
   234
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
   235
    void doAsyncInterrupt() {
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
   236
        final Thread current = Thread.currentThread();
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
   237
        AdaptorCloseAndInterrupt.pool.schedule(new Callable<Void>() {
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
   238
            public Void call() throws Exception {
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
   239
                current.interrupt();
44548
632ed240ed22 8178074: (ch) java/nio/channels/etc/AdaptorCloseAndInterrupt.java: add instrumentation
bpb
parents: 36678
diff changeset
   240
                isInterrupted.set(true);
14025
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
   241
                return null;
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
   242
            }
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
   243
        }, new Random().nextInt(1000), TimeUnit.MILLISECONDS);
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
   244
    }
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
   245
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
diff changeset
   246
}