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