jdk/test/java/nio/channels/AsyncCloseAndInterrupt.java
author katleman
Thu, 17 Apr 2014 10:13:36 -0700
changeset 23942 7b722b4cdd2d
parent 13814 76b94c403066
child 36643 6e0a015cd8c8
permissions -rw-r--r--
Added tag jdk9-b09 for changeset 667dccd79b65
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
13814
76b94c403066 7142919: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing intermittently [sol11]
dxu
parents: 13028
diff changeset
     2
 * Copyright (c) 2002, 2012, 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: 2441
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2441
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2441
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
13814
76b94c403066 7142919: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing intermittently [sol11]
dxu
parents: 13028
diff changeset
    25
 * @bug 4460583 4470470 4840199 6419424 6710579 6596323 6824135 6395224 7142919
76b94c403066 7142919: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing intermittently [sol11]
dxu
parents: 13028
diff changeset
    26
 * @run main/othervm AsyncCloseAndInterrupt
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Comprehensive test of asynchronous closing and interruption
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @author Mark Reinhold
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.nio.channels.*;
13814
76b94c403066 7142919: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing intermittently [sol11]
dxu
parents: 13028
diff changeset
    34
import java.nio.ByteBuffer;
76b94c403066 7142919: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing intermittently [sol11]
dxu
parents: 13028
diff changeset
    35
import java.util.ArrayList;
76b94c403066 7142919: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing intermittently [sol11]
dxu
parents: 13028
diff changeset
    36
import java.util.List;
76b94c403066 7142919: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing intermittently [sol11]
dxu
parents: 13028
diff changeset
    37
import java.util.concurrent.ExecutorService;
76b94c403066 7142919: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing intermittently [sol11]
dxu
parents: 13028
diff changeset
    38
import java.util.concurrent.Executors;
76b94c403066 7142919: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing intermittently [sol11]
dxu
parents: 13028
diff changeset
    39
import java.util.concurrent.ThreadFactory;
76b94c403066 7142919: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing intermittently [sol11]
dxu
parents: 13028
diff changeset
    40
import java.util.concurrent.Callable;
76b94c403066 7142919: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing intermittently [sol11]
dxu
parents: 13028
diff changeset
    41
import java.util.concurrent.Future;
76b94c403066 7142919: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing intermittently [sol11]
dxu
parents: 13028
diff changeset
    42
import java.util.concurrent.TimeUnit;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
public class AsyncCloseAndInterrupt {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    static PrintStream log = System.err;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    static void sleep(int ms) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
            Thread.sleep(ms);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        } catch (InterruptedException x) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    // Wildcard address localized to this machine -- Windoze doesn't allow
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    // connecting to a server socket that was previously bound to a true
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    // wildcard, namely new InetSocketAddress((InetAddress)null, 0).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    private static InetSocketAddress wildcardAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    // Server socket that blindly accepts all connections
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    static ServerSocketChannel acceptor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    private static void initAcceptor() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        acceptor = ServerSocketChannel.open();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        acceptor.socket().bind(wildcardAddress);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        Thread th = new Thread("Acceptor") {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                        for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                            SocketChannel sc = acceptor.accept();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                    } catch (IOException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                        x.printStackTrace();
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
        th.setDaemon(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        th.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    // Server socket that refuses all connections
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    static ServerSocketChannel refuser;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    private static void initRefuser() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        refuser = ServerSocketChannel.open();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        refuser.socket().bind(wildcardAddress);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    // Dead pipe source and sink
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    static Pipe.SourceChannel deadSource;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    static Pipe.SinkChannel deadSink;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    private static void initPipes() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        if (deadSource != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            deadSource.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        deadSource = Pipe.open().source();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        if (deadSink != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            deadSink.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        deadSink = Pipe.open().sink();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    // Files
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    private static File fifoFile = null; // File that blocks on reads and writes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    private static File diskFile = null; // Disk file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    private static void initFile() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        diskFile = File.createTempFile("aci", ".tmp");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        diskFile.deleteOnExit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        FileChannel fc = new FileOutputStream(diskFile).getChannel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        buffer.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        if (fc.write(buffer) != buffer.capacity())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            throw new RuntimeException("Cannot create disk file");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        fc.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        if (TestUtil.onWindows()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            log.println("WARNING: Cannot completely test FileChannels on Windows");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        fifoFile = new File("x.fifo");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        if (fifoFile.exists()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            if (!fifoFile.delete())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                throw new IOException("Cannot delete existing fifo " + fifoFile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        Process p = Runtime.getRuntime().exec("mkfifo " + fifoFile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        if (p.waitFor() != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            throw new IOException("Error creating fifo");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        new RandomAccessFile(fifoFile, "rw").close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    // Channel factories
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    static abstract class ChannelFactory {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        private final String name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        ChannelFactory(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            this.name = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        abstract InterruptibleChannel create() throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    static ChannelFactory socketChannelFactory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        = new ChannelFactory("SocketChannel") {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                InterruptibleChannel create() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                    return SocketChannel.open();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    static ChannelFactory connectedSocketChannelFactory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        = new ChannelFactory("SocketChannel") {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                InterruptibleChannel create() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                    SocketAddress sa = acceptor.socket().getLocalSocketAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                    return SocketChannel.open(sa);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    static ChannelFactory serverSocketChannelFactory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        = new ChannelFactory("ServerSocketChannel") {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                InterruptibleChannel create() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                    ServerSocketChannel ssc = ServerSocketChannel.open();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                    ssc.socket().bind(wildcardAddress);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                    return ssc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    static ChannelFactory datagramChannelFactory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        = new ChannelFactory("DatagramChannel") {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                InterruptibleChannel create() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                    DatagramChannel dc = DatagramChannel.open();
5804
faf9bd47d6ce 6395224: (so) SocketChannel writer blocked on large buffer is not preempted by close method (vista)
alanb
parents: 5506
diff changeset
   183
                    InetAddress lb = InetAddress.getByName("127.0.0.1");
faf9bd47d6ce 6395224: (so) SocketChannel writer blocked on large buffer is not preempted by close method (vista)
alanb
parents: 5506
diff changeset
   184
                    dc.bind(new InetSocketAddress(lb, 0));
faf9bd47d6ce 6395224: (so) SocketChannel writer blocked on large buffer is not preempted by close method (vista)
alanb
parents: 5506
diff changeset
   185
                    dc.connect(new InetSocketAddress(lb, 80));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                    return dc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    static ChannelFactory pipeSourceChannelFactory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        = new ChannelFactory("Pipe.SourceChannel") {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                InterruptibleChannel create() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                    // ## arrange to close sink
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                    return Pipe.open().source();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    static ChannelFactory pipeSinkChannelFactory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        = new ChannelFactory("Pipe.SinkChannel") {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                InterruptibleChannel create() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                    // ## arrange to close source
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                    return Pipe.open().sink();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    static ChannelFactory fifoFileChannelFactory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        = new ChannelFactory("FileChannel") {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                InterruptibleChannel create() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                    return new RandomAccessFile(fifoFile, "rw").getChannel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    static ChannelFactory diskFileChannelFactory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        = new ChannelFactory("FileChannel") {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                InterruptibleChannel create() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                    return new RandomAccessFile(diskFile, "rw").getChannel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    // I/O operations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    static abstract class Op {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        private final String name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        protected Op(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            this.name = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        abstract void doIO(InterruptibleChannel ich) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        void setup() throws IOException { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        public String toString() { return name; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    static ByteBuffer buffer = ByteBuffer.allocateDirect(1 << 20);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    static ByteBuffer[] buffers = new ByteBuffer[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        ByteBuffer.allocateDirect(1 << 19),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        ByteBuffer.allocateDirect(1 << 19)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    static void clearBuffers() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        buffers[0].clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        buffers[1].clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    static void show(Channel ch) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        log.print("Channel " + (ch.isOpen() ? "open" : "closed"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        if (ch.isOpen() && (ch instanceof SocketChannel)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            SocketChannel sc = (SocketChannel)ch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            if (sc.socket().isInputShutdown())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                log.print(", input shutdown");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            if (sc.socket().isOutputShutdown())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                log.print(", output shutdown");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        log.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    static final Op READ = new Op("read") {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            void doIO(InterruptibleChannel ich) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                ReadableByteChannel rbc = (ReadableByteChannel)ich;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                buffer.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                int n = rbc.read(buffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                log.println("Read returned " + n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                show(rbc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                if     (rbc.isOpen()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                        && (n == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                        && (rbc instanceof SocketChannel)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                        && ((SocketChannel)rbc).socket().isInputShutdown()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                throw new RuntimeException("Read succeeded");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    static final Op READV = new Op("readv") {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            void doIO(InterruptibleChannel ich) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                ScatteringByteChannel sbc = (ScatteringByteChannel)ich;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                clearBuffers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                int n = (int)sbc.read(buffers);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                log.println("Read returned " + n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                show(sbc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                if     (sbc.isOpen()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                        && (n == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                        && (sbc instanceof SocketChannel)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                        && ((SocketChannel)sbc).socket().isInputShutdown()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                throw new RuntimeException("Read succeeded");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    static final Op RECEIVE = new Op("receive") {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            void doIO(InterruptibleChannel ich) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                DatagramChannel dc = (DatagramChannel)ich;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                buffer.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                dc.receive(buffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                show(dc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                throw new RuntimeException("Read succeeded");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    static final Op WRITE = new Op("write") {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            void doIO(InterruptibleChannel ich) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                WritableByteChannel wbc = (WritableByteChannel)ich;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                SocketChannel sc = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                if (wbc instanceof SocketChannel)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                    sc = (SocketChannel)wbc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                int n = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                    buffer.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                    int d = wbc.write(buffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                    n += d;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                    if (!wbc.isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                    if ((sc != null) && sc.socket().isOutputShutdown())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                log.println("Wrote " + n + " bytes");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                show(wbc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    static final Op WRITEV = new Op("writev") {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            void doIO(InterruptibleChannel ich) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                GatheringByteChannel gbc = (GatheringByteChannel)ich;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                SocketChannel sc = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                if (gbc instanceof SocketChannel)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                    sc = (SocketChannel)gbc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                int n = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                    clearBuffers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                    int d = (int)gbc.write(buffers);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                    n += d;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                    if (!gbc.isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                    if ((sc != null) && sc.socket().isOutputShutdown())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                log.println("Wrote " + n + " bytes");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                show(gbc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    static final Op CONNECT = new Op("connect") {
13814
76b94c403066 7142919: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing intermittently [sol11]
dxu
parents: 13028
diff changeset
   351
            void setup() {
76b94c403066 7142919: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing intermittently [sol11]
dxu
parents: 13028
diff changeset
   352
                waitPump("connect wait for pumping refuser ...");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            void doIO(InterruptibleChannel ich) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                SocketChannel sc = (SocketChannel)ich;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                if (sc.connect(refuser.socket().getLocalSocketAddress()))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                    throw new RuntimeException("Connection succeeded");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                throw new RuntimeException("Connection did not block");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    static final Op FINISH_CONNECT = new Op("finishConnect") {
13814
76b94c403066 7142919: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing intermittently [sol11]
dxu
parents: 13028
diff changeset
   363
            void setup() {
76b94c403066 7142919: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing intermittently [sol11]
dxu
parents: 13028
diff changeset
   364
                waitPump("finishConnect wait for pumping refuser ...");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            void doIO(InterruptibleChannel ich) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                SocketChannel sc = (SocketChannel)ich;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                sc.configureBlocking(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                SocketAddress sa = refuser.socket().getLocalSocketAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                if (sc.connect(sa))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                    throw new RuntimeException("Connection succeeded");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                sc.configureBlocking(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                if (sc.finishConnect())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                    throw new RuntimeException("Connection succeeded");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                throw new RuntimeException("Connection did not block");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
    static final Op ACCEPT = new Op("accept") {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            void doIO(InterruptibleChannel ich) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                ServerSocketChannel ssc = (ServerSocketChannel)ich;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                ssc.accept();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                throw new RuntimeException("Accept succeeded");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    // Use only with diskFileChannelFactory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
    static final Op TRANSFER_TO = new Op("transferTo") {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            void doIO(InterruptibleChannel ich) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                FileChannel fc = (FileChannel)ich;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                long n = fc.transferTo(0, fc.size(), deadSink);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                log.println("Transferred " + n + " bytes");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                show(fc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    // Use only with diskFileChannelFactory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
    static final Op TRANSFER_FROM = new Op("transferFrom") {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
            void doIO(InterruptibleChannel ich) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                FileChannel fc = (FileChannel)ich;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                long n = fc.transferFrom(deadSource, 0, 1 << 20);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                log.println("Transferred " + n + " bytes");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                show(fc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
    // Test modes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    static final int TEST_PREINTR = 0;  // Interrupt thread before I/O
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
    static final int TEST_INTR = 1;     // Interrupt thread during I/O
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
    static final int TEST_CLOSE = 2;    // Close channel during I/O
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
    static final int TEST_SHUTI = 3;    // Shutdown input during I/O
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
    static final int TEST_SHUTO = 4;    // Shutdown output during I/O
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
    static final String[] testName = new String[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        "pre-interrupt", "interrupt", "close",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        "shutdown-input", "shutdown-output"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
    static class Tester extends TestThread {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        private InterruptibleChannel ch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        private Op op;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        private int test;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        volatile boolean ready = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        protected Tester(ChannelFactory cf, InterruptibleChannel ch,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                         Op op, int test)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
            super(cf + "/" + op + "/" + testName[test]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            this.ch = ch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
            this.op = op;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
            this.test = test;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
13814
76b94c403066 7142919: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing intermittently [sol11]
dxu
parents: 13028
diff changeset
   439
        @SuppressWarnings("fallthrough")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        private void caught(Channel ch, IOException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
            String xn = x.getClass().getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
            switch (test) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
            case TEST_PREINTR:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
            case TEST_INTR:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                if (!xn.equals("java.nio.channels.ClosedByInterruptException"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                    throw new RuntimeException("Wrong exception thrown: " + x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
            case TEST_CLOSE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
            case TEST_SHUTO:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                if (!xn.equals("java.nio.channels.AsynchronousCloseException"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                    throw new RuntimeException("Wrong exception thrown: " + x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
            case TEST_SHUTI:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                if (TestUtil.onWindows())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                // FALL THROUGH
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                throw new Error(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
            if (ch.isOpen()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                if (test == TEST_SHUTO) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                    SocketChannel sc = (SocketChannel)ch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                    if (!sc.socket().isOutputShutdown())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                        throw new RuntimeException("Output not shutdown");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                } else if ((test == TEST_INTR) && (op == TRANSFER_FROM)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                    // Let this case pass -- CBIE applies to other channel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
                    throw new RuntimeException("Channel still open");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
            log.println("Thrown as expected: " + x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
        final void go() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
            if (test == TEST_PREINTR)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
                Thread.currentThread().interrupt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
            ready = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
                op.doIO(ch);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
            } catch (ClosedByInterruptException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
                caught(ch, x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
            } catch (AsynchronousCloseException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
                caught(ch, x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
                ch.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
13814
76b94c403066 7142919: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing intermittently [sol11]
dxu
parents: 13028
diff changeset
   497
    private static volatile boolean pumpDone = false;
76b94c403066 7142919: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing intermittently [sol11]
dxu
parents: 13028
diff changeset
   498
    private static volatile boolean pumpReady = false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
13814
76b94c403066 7142919: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing intermittently [sol11]
dxu
parents: 13028
diff changeset
   500
    private static void waitPump(String msg){
76b94c403066 7142919: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing intermittently [sol11]
dxu
parents: 13028
diff changeset
   501
        pumpReady = false;
76b94c403066 7142919: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing intermittently [sol11]
dxu
parents: 13028
diff changeset
   502
        log.println(msg);
76b94c403066 7142919: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing intermittently [sol11]
dxu
parents: 13028
diff changeset
   503
76b94c403066 7142919: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing intermittently [sol11]
dxu
parents: 13028
diff changeset
   504
        while (!pumpReady){
76b94c403066 7142919: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing intermittently [sol11]
dxu
parents: 13028
diff changeset
   505
            sleep(200);
76b94c403066 7142919: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing intermittently [sol11]
dxu
parents: 13028
diff changeset
   506
        }
76b94c403066 7142919: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing intermittently [sol11]
dxu
parents: 13028
diff changeset
   507
    }
76b94c403066 7142919: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing intermittently [sol11]
dxu
parents: 13028
diff changeset
   508
76b94c403066 7142919: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing intermittently [sol11]
dxu
parents: 13028
diff changeset
   509
    // Create a pump thread dedicated to saturate refuser's connection backlog
76b94c403066 7142919: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing intermittently [sol11]
dxu
parents: 13028
diff changeset
   510
    private static Future<Integer> pumpRefuser(ExecutorService pumperExecutor) {
76b94c403066 7142919: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing intermittently [sol11]
dxu
parents: 13028
diff changeset
   511
76b94c403066 7142919: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing intermittently [sol11]
dxu
parents: 13028
diff changeset
   512
        Callable<Integer> pumpTask = new Callable<Integer>() {
76b94c403066 7142919: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing intermittently [sol11]
dxu
parents: 13028
diff changeset
   513
76b94c403066 7142919: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing intermittently [sol11]
dxu
parents: 13028
diff changeset
   514
            @Override
76b94c403066 7142919: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing intermittently [sol11]
dxu
parents: 13028
diff changeset
   515
            public Integer call() throws IOException {
76b94c403066 7142919: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing intermittently [sol11]
dxu
parents: 13028
diff changeset
   516
                // Can't reliably saturate connection backlog on Windows Server editions
76b94c403066 7142919: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing intermittently [sol11]
dxu
parents: 13028
diff changeset
   517
                assert !TestUtil.onWindows();
76b94c403066 7142919: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing intermittently [sol11]
dxu
parents: 13028
diff changeset
   518
                log.println("Start pumping refuser ...");
76b94c403066 7142919: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing intermittently [sol11]
dxu
parents: 13028
diff changeset
   519
                List<SocketChannel> refuserClients = new ArrayList<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
13814
76b94c403066 7142919: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing intermittently [sol11]
dxu
parents: 13028
diff changeset
   521
                // Saturate the refuser's connection backlog so that further connection
76b94c403066 7142919: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing intermittently [sol11]
dxu
parents: 13028
diff changeset
   522
                // attempts will be blocked
76b94c403066 7142919: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing intermittently [sol11]
dxu
parents: 13028
diff changeset
   523
                while (!pumpDone) {
76b94c403066 7142919: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing intermittently [sol11]
dxu
parents: 13028
diff changeset
   524
                    SocketChannel sc = SocketChannel.open();
76b94c403066 7142919: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing intermittently [sol11]
dxu
parents: 13028
diff changeset
   525
                    sc.configureBlocking(false);
76b94c403066 7142919: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing intermittently [sol11]
dxu
parents: 13028
diff changeset
   526
                    boolean connected = sc.connect(refuser.socket().getLocalSocketAddress());
76b94c403066 7142919: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing intermittently [sol11]
dxu
parents: 13028
diff changeset
   527
76b94c403066 7142919: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing intermittently [sol11]
dxu
parents: 13028
diff changeset
   528
                    // Assume that the connection backlog is saturated if a
76b94c403066 7142919: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing intermittently [sol11]
dxu
parents: 13028
diff changeset
   529
                    // client cannot connect to the refuser within 50 miliseconds
76b94c403066 7142919: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing intermittently [sol11]
dxu
parents: 13028
diff changeset
   530
                    long start = System.currentTimeMillis();
76b94c403066 7142919: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing intermittently [sol11]
dxu
parents: 13028
diff changeset
   531
                    while (!connected && (System.currentTimeMillis() - start < 50)) {
76b94c403066 7142919: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing intermittently [sol11]
dxu
parents: 13028
diff changeset
   532
                        connected = sc.finishConnect();
76b94c403066 7142919: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing intermittently [sol11]
dxu
parents: 13028
diff changeset
   533
                    }
76b94c403066 7142919: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing intermittently [sol11]
dxu
parents: 13028
diff changeset
   534
76b94c403066 7142919: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing intermittently [sol11]
dxu
parents: 13028
diff changeset
   535
                    if (connected) {
76b94c403066 7142919: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing intermittently [sol11]
dxu
parents: 13028
diff changeset
   536
                        // Retain so that finalizer doesn't close
76b94c403066 7142919: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing intermittently [sol11]
dxu
parents: 13028
diff changeset
   537
                        refuserClients.add(sc);
76b94c403066 7142919: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing intermittently [sol11]
dxu
parents: 13028
diff changeset
   538
                        pumpReady = false;
76b94c403066 7142919: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing intermittently [sol11]
dxu
parents: 13028
diff changeset
   539
                    } else {
76b94c403066 7142919: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing intermittently [sol11]
dxu
parents: 13028
diff changeset
   540
                        sc.close();
76b94c403066 7142919: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing intermittently [sol11]
dxu
parents: 13028
diff changeset
   541
                        pumpReady = true;
76b94c403066 7142919: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing intermittently [sol11]
dxu
parents: 13028
diff changeset
   542
                    }
76b94c403066 7142919: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing intermittently [sol11]
dxu
parents: 13028
diff changeset
   543
                }
76b94c403066 7142919: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing intermittently [sol11]
dxu
parents: 13028
diff changeset
   544
76b94c403066 7142919: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing intermittently [sol11]
dxu
parents: 13028
diff changeset
   545
                log.println("Stop pumping refuser ...");
76b94c403066 7142919: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing intermittently [sol11]
dxu
parents: 13028
diff changeset
   546
                return refuserClients.size();
76b94c403066 7142919: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing intermittently [sol11]
dxu
parents: 13028
diff changeset
   547
            }
76b94c403066 7142919: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing intermittently [sol11]
dxu
parents: 13028
diff changeset
   548
        };
76b94c403066 7142919: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing intermittently [sol11]
dxu
parents: 13028
diff changeset
   549
76b94c403066 7142919: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing intermittently [sol11]
dxu
parents: 13028
diff changeset
   550
        return pumperExecutor.submit(pumpTask);
76b94c403066 7142919: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing intermittently [sol11]
dxu
parents: 13028
diff changeset
   551
    }
76b94c403066 7142919: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing intermittently [sol11]
dxu
parents: 13028
diff changeset
   552
76b94c403066 7142919: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing intermittently [sol11]
dxu
parents: 13028
diff changeset
   553
    // Test
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
    static void test(ChannelFactory cf, Op op, int test)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
        throws Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
        log.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
        initPipes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
        InterruptibleChannel ch = cf.create();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
        Tester t = new Tester(cf, ch, op, test);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
        log.println(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
        op.setup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
        t.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
            sleep(50);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
        } while (!t.ready);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
        sleep(100);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
        switch (test) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
        case TEST_INTR:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
            t.interrupt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
        case TEST_CLOSE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
            ch.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
        case TEST_SHUTI:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
            if (TestUtil.onWindows()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
                log.println("WARNING: Asynchronous shutdown not working on Windows");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
                ch.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
                ((SocketChannel)ch).socket().shutdownInput();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
        case TEST_SHUTO:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
            if (TestUtil.onWindows()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
                log.println("WARNING: Asynchronous shutdown not working on Windows");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
                ch.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
                ((SocketChannel)ch).socket().shutdownOutput();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
        t.finishAndThrow(500);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
    static void test(ChannelFactory cf, Op op) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
        // Test INTR cases before PREINTER cases since sometimes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
        // interrupted threads can't load classes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
        test(cf, op, TEST_INTR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
        test(cf, op, TEST_PREINTR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
        // Bugs, see FileChannelImpl for details
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
        if (op == TRANSFER_FROM) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
            log.println("WARNING: transferFrom/close not tested");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
        }
694
0da2a4a73721 6710579: (ch) test/java/nio/channels/AsyncCloseAndInterrupt fails (lnx)
alanb
parents: 693
diff changeset
   617
        if ((op == TRANSFER_TO) && !TestUtil.onWindows()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
            log.println("WARNING: transferTo/close not tested");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
        test(cf, op, TEST_CLOSE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
    static void test(ChannelFactory cf)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
        throws Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
        InterruptibleChannel ch = cf.create(); // Sample channel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
        ch.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
        if (ch instanceof ReadableByteChannel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
            test(cf, READ);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
            if (ch instanceof SocketChannel)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
                test(cf, READ, TEST_SHUTI);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
        if (ch instanceof ScatteringByteChannel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
            test(cf, READV);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
            if (ch instanceof SocketChannel)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
                test(cf, READV, TEST_SHUTI);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
        if (ch instanceof DatagramChannel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
            test(cf, RECEIVE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
            // Return here: We can't effectively test writes since, if they
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
            // block, they do so only for a fleeting moment unless the network
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
            // interface is overloaded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
        if (ch instanceof WritableByteChannel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
            test(cf, WRITE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
            if (ch instanceof SocketChannel)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
                test(cf, WRITE, TEST_SHUTO);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
        if (ch instanceof GatheringByteChannel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
            test(cf, WRITEV);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
            if (ch instanceof SocketChannel)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
                test(cf, WRITEV, TEST_SHUTO);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
        wildcardAddress = new InetSocketAddress(InetAddress.getLocalHost(), 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
        initAcceptor();
5804
faf9bd47d6ce 6395224: (so) SocketChannel writer blocked on large buffer is not preempted by close method (vista)
alanb
parents: 5506
diff changeset
   671
        if (!TestUtil.onWindows())
faf9bd47d6ce 6395224: (so) SocketChannel writer blocked on large buffer is not preempted by close method (vista)
alanb
parents: 5506
diff changeset
   672
            initRefuser();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
        initPipes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
        initFile();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
13028
f72c7e618ed0 7177617: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing (win)
alanb
parents: 7668
diff changeset
   676
        if (TestUtil.onWindows()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
            log.println("WARNING: Cannot test FileChannel transfer operations"
13028
f72c7e618ed0 7177617: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing (win)
alanb
parents: 7668
diff changeset
   678
                        + " on Windows");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
            test(diskFileChannelFactory, TRANSFER_TO);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
            test(diskFileChannelFactory, TRANSFER_FROM);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
        if (fifoFile != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
            test(fifoFileChannelFactory);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
        // Testing positional file reads and writes is impractical: It requires
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
        // access to a large file soft-mounted via NFS, and even then isn't
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
        // completely guaranteed to work.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
        // Testing map is impractical and arguably unnecessary: It's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
        // unclear under what conditions mmap(2) will actually block.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
        test(connectedSocketChannelFactory);
5804
faf9bd47d6ce 6395224: (so) SocketChannel writer blocked on large buffer is not preempted by close method (vista)
alanb
parents: 5506
diff changeset
   694
faf9bd47d6ce 6395224: (so) SocketChannel writer blocked on large buffer is not preempted by close method (vista)
alanb
parents: 5506
diff changeset
   695
        if (TestUtil.onWindows()) {
faf9bd47d6ce 6395224: (so) SocketChannel writer blocked on large buffer is not preempted by close method (vista)
alanb
parents: 5506
diff changeset
   696
            log.println("WARNING Cannot reliably test connect/finishConnect"
faf9bd47d6ce 6395224: (so) SocketChannel writer blocked on large buffer is not preempted by close method (vista)
alanb
parents: 5506
diff changeset
   697
                + " operations on Windows");
faf9bd47d6ce 6395224: (so) SocketChannel writer blocked on large buffer is not preempted by close method (vista)
alanb
parents: 5506
diff changeset
   698
        } else {
13814
76b94c403066 7142919: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing intermittently [sol11]
dxu
parents: 13028
diff changeset
   699
            // Only the following tests need refuser's connection backlog
76b94c403066 7142919: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing intermittently [sol11]
dxu
parents: 13028
diff changeset
   700
            // to be saturated
76b94c403066 7142919: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing intermittently [sol11]
dxu
parents: 13028
diff changeset
   701
            ExecutorService pumperExecutor =
76b94c403066 7142919: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing intermittently [sol11]
dxu
parents: 13028
diff changeset
   702
                    Executors.newSingleThreadExecutor(
76b94c403066 7142919: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing intermittently [sol11]
dxu
parents: 13028
diff changeset
   703
                    new ThreadFactory() {
76b94c403066 7142919: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing intermittently [sol11]
dxu
parents: 13028
diff changeset
   704
76b94c403066 7142919: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing intermittently [sol11]
dxu
parents: 13028
diff changeset
   705
                        @Override
76b94c403066 7142919: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing intermittently [sol11]
dxu
parents: 13028
diff changeset
   706
                        public Thread newThread(Runnable r) {
76b94c403066 7142919: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing intermittently [sol11]
dxu
parents: 13028
diff changeset
   707
                            Thread t = new Thread(r);
76b94c403066 7142919: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing intermittently [sol11]
dxu
parents: 13028
diff changeset
   708
                            t.setDaemon(true);
76b94c403066 7142919: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing intermittently [sol11]
dxu
parents: 13028
diff changeset
   709
                            t.setName("Pumper");
76b94c403066 7142919: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing intermittently [sol11]
dxu
parents: 13028
diff changeset
   710
                            return t;
76b94c403066 7142919: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing intermittently [sol11]
dxu
parents: 13028
diff changeset
   711
                        }
76b94c403066 7142919: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing intermittently [sol11]
dxu
parents: 13028
diff changeset
   712
                    });
76b94c403066 7142919: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing intermittently [sol11]
dxu
parents: 13028
diff changeset
   713
76b94c403066 7142919: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing intermittently [sol11]
dxu
parents: 13028
diff changeset
   714
            pumpDone = false;
76b94c403066 7142919: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing intermittently [sol11]
dxu
parents: 13028
diff changeset
   715
            try {
76b94c403066 7142919: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing intermittently [sol11]
dxu
parents: 13028
diff changeset
   716
                Future<Integer> pumpFuture = pumpRefuser(pumperExecutor);
76b94c403066 7142919: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing intermittently [sol11]
dxu
parents: 13028
diff changeset
   717
                waitPump("\nWait for initial Pump");
76b94c403066 7142919: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing intermittently [sol11]
dxu
parents: 13028
diff changeset
   718
76b94c403066 7142919: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing intermittently [sol11]
dxu
parents: 13028
diff changeset
   719
                test(socketChannelFactory, CONNECT);
76b94c403066 7142919: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing intermittently [sol11]
dxu
parents: 13028
diff changeset
   720
                test(socketChannelFactory, FINISH_CONNECT);
76b94c403066 7142919: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing intermittently [sol11]
dxu
parents: 13028
diff changeset
   721
76b94c403066 7142919: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing intermittently [sol11]
dxu
parents: 13028
diff changeset
   722
                pumpDone = true;
76b94c403066 7142919: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing intermittently [sol11]
dxu
parents: 13028
diff changeset
   723
                Integer newConn = pumpFuture.get(30, TimeUnit.SECONDS);
76b94c403066 7142919: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing intermittently [sol11]
dxu
parents: 13028
diff changeset
   724
                log.println("Pump " + newConn + " connections.");
76b94c403066 7142919: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing intermittently [sol11]
dxu
parents: 13028
diff changeset
   725
            } finally {
76b94c403066 7142919: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing intermittently [sol11]
dxu
parents: 13028
diff changeset
   726
                pumperExecutor.shutdown();
76b94c403066 7142919: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing intermittently [sol11]
dxu
parents: 13028
diff changeset
   727
            }
5804
faf9bd47d6ce 6395224: (so) SocketChannel writer blocked on large buffer is not preempted by close method (vista)
alanb
parents: 5506
diff changeset
   728
        }
faf9bd47d6ce 6395224: (so) SocketChannel writer blocked on large buffer is not preempted by close method (vista)
alanb
parents: 5506
diff changeset
   729
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
        test(serverSocketChannelFactory, ACCEPT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
        test(datagramChannelFactory);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
        test(pipeSourceChannelFactory);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
        test(pipeSinkChannelFactory);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
}