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